github.com/hamba/avro@v1.8.0/reader_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/hamba/avro"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestNewReader(t *testing.T) {
    13  	r := avro.NewReader(bytes.NewBuffer([]byte{}), 10)
    14  
    15  	assert.IsType(t, &avro.Reader{}, r)
    16  }
    17  
    18  func TestReader_Reset(t *testing.T) {
    19  	r := &avro.Reader{}
    20  
    21  	r.Reset([]byte{0x01})
    22  
    23  	assert.True(t, r.ReadBool())
    24  }
    25  
    26  func TestReader_ReportError(t *testing.T) {
    27  	r := &avro.Reader{}
    28  
    29  	r.ReportError("test", "bar")
    30  
    31  	assert.EqualError(t, r.Error, "avro: test: bar")
    32  }
    33  
    34  func TestReader_ReportErrorExistingError(t *testing.T) {
    35  	err := errors.New("test")
    36  
    37  	r := &avro.Reader{}
    38  	r.Error = err
    39  
    40  	r.ReportError("test", "bar")
    41  
    42  	assert.Equal(t, err, r.Error)
    43  }
    44  
    45  func TestReader_ReadPastBuffer(t *testing.T) {
    46  	r := (&avro.Reader{}).Reset([]byte{0xE2})
    47  
    48  	r.ReadInt()
    49  
    50  	assert.Error(t, r.Error)
    51  }
    52  
    53  func TestReader_ReadDelayedReader(t *testing.T) {
    54  	rdr := &delayedReader{b: []byte{0x36}}
    55  	r := avro.NewReader(rdr, 10)
    56  
    57  	i := r.ReadInt()
    58  
    59  	assert.NoError(t, r.Error)
    60  	assert.Equal(t, int32(27), i)
    61  }
    62  
    63  func TestReader_Read(t *testing.T) {
    64  	tests := []struct {
    65  		data    []byte
    66  		want    []byte
    67  		wantErr bool
    68  	}{
    69  		{
    70  			data:    []byte{0xAC, 0xDC, 0x01, 0x00, 0x10, 0x0F},
    71  			want:    make([]byte, 6),
    72  			wantErr: false,
    73  		},
    74  		{
    75  			data:    []byte{0xAC}, // io.EOF
    76  			want:    make([]byte, 6),
    77  			wantErr: true,
    78  		},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		r := avro.NewReader(bytes.NewReader(tt.data), 2)
    83  
    84  		r.Read(tt.want)
    85  
    86  		if tt.wantErr {
    87  			assert.Error(t, r.Error)
    88  			continue
    89  		}
    90  
    91  		assert.NoError(t, r.Error)
    92  		assert.Equal(t, tt.want, tt.data)
    93  	}
    94  }
    95  
    96  func TestReader_ReadBool(t *testing.T) {
    97  	tests := []struct {
    98  		data    []byte
    99  		want    bool
   100  		wantErr bool
   101  	}{
   102  		{
   103  			data:    []byte{0x00},
   104  			want:    false,
   105  			wantErr: false,
   106  		},
   107  		{
   108  			data:    []byte{0x01},
   109  			want:    true,
   110  			wantErr: false,
   111  		},
   112  		{
   113  			data:    []byte{0x02}, // Invalid Bool
   114  			want:    false,
   115  			wantErr: true,
   116  		},
   117  		{
   118  			data:    []byte(nil), // io.EOF
   119  			want:    false,
   120  			wantErr: true,
   121  		},
   122  	}
   123  
   124  	for _, tt := range tests {
   125  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   126  
   127  		got := r.ReadBool()
   128  
   129  		if tt.wantErr {
   130  			assert.Error(t, r.Error)
   131  			continue
   132  		}
   133  
   134  		assert.NoError(t, r.Error)
   135  		assert.Equal(t, tt.want, got)
   136  	}
   137  }
   138  
   139  func TestReader_ReadInt(t *testing.T) {
   140  	tests := []struct {
   141  		data    []byte
   142  		want    int32
   143  		wantErr bool
   144  	}{
   145  		{
   146  			data:    []byte{0x36},
   147  			want:    27,
   148  			wantErr: false,
   149  		},
   150  		{
   151  			data:    []byte{0x0F},
   152  			want:    -8,
   153  			wantErr: false,
   154  		},
   155  		{
   156  			data:    []byte{0x01},
   157  			want:    -1,
   158  			wantErr: false,
   159  		},
   160  		{
   161  			data:    []byte{0x00},
   162  			want:    0,
   163  			wantErr: false,
   164  		},
   165  		{
   166  			data:    []byte{0x02},
   167  			want:    1,
   168  			wantErr: false,
   169  		},
   170  		{
   171  			data:    []byte{0x7F},
   172  			want:    -64,
   173  			wantErr: false,
   174  		},
   175  		{
   176  			data:    []byte{0x80, 0x01},
   177  			want:    64,
   178  			wantErr: false,
   179  		},
   180  		{
   181  			data:    []byte{0xAA, 0xB4, 0xDE, 0x75},
   182  			want:    123456789,
   183  			wantErr: false,
   184  		},
   185  		{
   186  			data:    []byte{0xE2, 0xA2, 0xF3, 0xAD, 0x07},
   187  			want:    987654321,
   188  			wantErr: false,
   189  		},
   190  		{
   191  			data:    []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD}, // Overflow
   192  			want:    0,
   193  			wantErr: true,
   194  		},
   195  		{
   196  			data:    []byte{0xE2}, // io.EOF
   197  			want:    0,
   198  			wantErr: true,
   199  		},
   200  	}
   201  
   202  	for _, tt := range tests {
   203  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   204  
   205  		got := r.ReadInt()
   206  
   207  		if tt.wantErr {
   208  			assert.Error(t, r.Error)
   209  			continue
   210  		}
   211  
   212  		assert.NoError(t, r.Error)
   213  		assert.Equal(t, tt.want, got)
   214  	}
   215  }
   216  
   217  func TestReader_ReadLong(t *testing.T) {
   218  	tests := []struct {
   219  		data    []byte
   220  		want    int64
   221  		wantErr bool
   222  	}{
   223  		{
   224  			data:    []byte{0x36},
   225  			want:    27,
   226  			wantErr: false,
   227  		},
   228  		{
   229  			data:    []byte{0x0F},
   230  			want:    -8,
   231  			wantErr: false,
   232  		},
   233  		{
   234  			data:    []byte{0x01},
   235  			want:    -1,
   236  			wantErr: false,
   237  		},
   238  		{
   239  			data:    []byte{0x00},
   240  			want:    0,
   241  			wantErr: false,
   242  		},
   243  		{
   244  			data:    []byte{0x02},
   245  			want:    1,
   246  			wantErr: false,
   247  		},
   248  		{
   249  			data:    []byte{0x7F},
   250  			want:    -64,
   251  			wantErr: false,
   252  		},
   253  		{
   254  			data:    []byte{0x80, 0x01},
   255  			want:    64,
   256  			wantErr: false,
   257  		},
   258  		{
   259  			data:    []byte{0xAA, 0xB4, 0xDE, 0x75},
   260  			want:    123456789,
   261  			wantErr: false,
   262  		},
   263  		{
   264  			data:    []byte{0xE2, 0xA2, 0xF3, 0xAD, 0x07},
   265  			want:    987654321,
   266  			wantErr: false,
   267  		},
   268  		{
   269  			data:    []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
   270  			want:    9223372036854775807,
   271  			wantErr: false,
   272  		},
   273  		{
   274  			data:    []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
   275  			want:    -9223372036854775808,
   276  			wantErr: false,
   277  		},
   278  		{
   279  			data:    []byte{0xBD, 0xB1, 0xAE, 0xD4, 0xD2, 0xCD, 0xBD, 0xE4, 0x97, 0x01},
   280  			want:    -5468631321897454687,
   281  			wantErr: false,
   282  		},
   283  		{
   284  			data:    []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD, 0xE2, 0xA2, 0xF3, 0xAD, 0xAD}, // Overflow
   285  			want:    0,
   286  			wantErr: true,
   287  		},
   288  		{
   289  			data:    []byte{0xE2}, // io.EOF
   290  			want:    0,
   291  			wantErr: true,
   292  		},
   293  	}
   294  
   295  	for _, tt := range tests {
   296  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   297  
   298  		got := r.ReadLong()
   299  
   300  		if tt.wantErr {
   301  			assert.Error(t, r.Error)
   302  			continue
   303  		}
   304  
   305  		assert.NoError(t, r.Error)
   306  		assert.Equal(t, tt.want, got)
   307  	}
   308  }
   309  
   310  func TestReader_ReadFloat(t *testing.T) {
   311  	tests := []struct {
   312  		data    []byte
   313  		want    float32
   314  		wantErr bool
   315  	}{
   316  		{
   317  			data:    []byte{0x00, 0x00, 0x00, 0x00},
   318  			want:    0.0,
   319  			wantErr: false,
   320  		},
   321  		{
   322  			data:    []byte{0x00, 0x00, 0x80, 0x3F},
   323  			want:    1.0,
   324  			wantErr: false,
   325  		},
   326  		{
   327  			data:    []byte{0x33, 0x33, 0x93, 0x3F},
   328  			want:    1.15,
   329  			wantErr: false,
   330  		},
   331  		{
   332  			data:    []byte{0x23, 0xDB, 0x57, 0xC2},
   333  			want:    -53.964,
   334  			wantErr: false,
   335  		},
   336  		{
   337  			data:    []byte{0xA3, 0x79, 0xEB, 0xCC},
   338  			want:    -123456789.123,
   339  			wantErr: false,
   340  		},
   341  		{
   342  			data:    []byte{0x62, 0x20, 0x71, 0x49},
   343  			want:    987654.111115,
   344  			wantErr: false,
   345  		},
   346  		{
   347  			data:    []byte(nil), // io.EOF
   348  			want:    0,
   349  			wantErr: true,
   350  		},
   351  	}
   352  
   353  	for _, tt := range tests {
   354  		r := avro.NewReader(bytes.NewReader(tt.data), 2)
   355  
   356  		got := r.ReadFloat()
   357  
   358  		if tt.wantErr {
   359  			assert.Error(t, r.Error)
   360  			continue
   361  		}
   362  
   363  		assert.NoError(t, r.Error)
   364  		assert.Equal(t, tt.want, got)
   365  	}
   366  }
   367  
   368  func TestReader_ReadDouble(t *testing.T) {
   369  	tests := []struct {
   370  		data    []byte
   371  		want    float64
   372  		wantErr bool
   373  	}{
   374  		{
   375  			data:    []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
   376  			want:    0.0,
   377  			wantErr: false,
   378  		},
   379  		{
   380  			data:    []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F},
   381  			want:    1.0,
   382  			wantErr: false,
   383  		},
   384  		{
   385  			data:    []byte{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xF2, 0x3F},
   386  			want:    1.15,
   387  			wantErr: false,
   388  		},
   389  		{
   390  			data:    []byte{0x08, 0xAC, 0x1C, 0x5A, 0x64, 0xFB, 0x4A, 0xC0},
   391  			want:    -53.964,
   392  			wantErr: false,
   393  		},
   394  		{
   395  			data:    []byte{0xB6, 0xF3, 0x7D, 0x54, 0x34, 0x6F, 0x9D, 0xC1},
   396  			want:    -123456789.123,
   397  			wantErr: false,
   398  		},
   399  		{
   400  			data:    []byte{0xB6, 0x10, 0xE4, 0x38, 0x0C, 0x24, 0x2E, 0x41},
   401  			want:    987654.111115,
   402  			wantErr: false,
   403  		},
   404  		{
   405  			data:    []byte{0x75, 0x6B, 0x7E, 0x54, 0x34, 0x6F, 0x9D, 0x41},
   406  			want:    123456789.123456789,
   407  			wantErr: false,
   408  		},
   409  		{
   410  			data:    []byte{0x00, 0x00, 0x00, 0x00, 0xD0, 0x12, 0x63, 0x41},
   411  			want:    9999999.99999999999999999999999,
   412  			wantErr: false,
   413  		},
   414  		{
   415  			data:    []byte{0x18, 0xFC, 0x1A, 0xDD, 0x1F, 0x0E, 0x0A, 0x43},
   416  			want:    916734926348163.01973408746523,
   417  			wantErr: false,
   418  		},
   419  		{
   420  			data:    []byte{0x0A, 0x8F, 0xA6, 0x40, 0xAC, 0xAD, 0x8D, 0xC3},
   421  			want:    -267319348967891263.1928357138913857,
   422  			wantErr: false,
   423  		},
   424  		{
   425  			data:    []byte(nil), // io.EOF
   426  			want:    0,
   427  			wantErr: true,
   428  		},
   429  	}
   430  
   431  	for _, tt := range tests {
   432  		r := avro.NewReader(bytes.NewReader(tt.data), 4)
   433  
   434  		got := r.ReadDouble()
   435  
   436  		if tt.wantErr {
   437  			assert.Error(t, r.Error)
   438  			continue
   439  		}
   440  
   441  		assert.NoError(t, r.Error)
   442  		assert.Equal(t, tt.want, got)
   443  	}
   444  }
   445  
   446  func TestReader_ReadBytes(t *testing.T) {
   447  	tests := []struct {
   448  		data    []byte
   449  		want    []byte
   450  		wantErr bool
   451  	}{
   452  		{
   453  			data:    []byte{0x02, 0x02},
   454  			want:    []byte{0x02},
   455  			wantErr: false,
   456  		},
   457  		{
   458  			data:    []byte{0x04, 0x03, 0xFF},
   459  			want:    []byte{0x03, 0xFF},
   460  			wantErr: false,
   461  		},
   462  		{
   463  			data:    []byte{0x08, 0xEC, 0xAB, 0x44, 0x00},
   464  			want:    []byte{0xEC, 0xAB, 0x44, 0x00},
   465  			wantErr: false,
   466  		},
   467  		{
   468  			data:    []byte{0x0C, 0xAC, 0xDC, 0x01, 0x00, 0x10, 0x0F},
   469  			want:    []byte{0xAC, 0xDC, 0x01, 0x00, 0x10, 0x0F},
   470  			wantErr: false,
   471  		},
   472  		{
   473  			data:    []byte(nil), // io.EOF no length
   474  			want:    nil,
   475  			wantErr: true,
   476  		},
   477  		{
   478  			data:    []byte{0x05, 0x03, 0xFF, 0x0A}, // Invalid bytes length
   479  			want:    nil,
   480  			wantErr: true,
   481  		},
   482  		{
   483  			data:    []byte{0x08, 0xFF}, // io.EOF length greater then data
   484  			want:    nil,
   485  			wantErr: true,
   486  		},
   487  	}
   488  
   489  	for _, tt := range tests {
   490  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   491  
   492  		got := r.ReadBytes()
   493  
   494  		if tt.wantErr {
   495  			assert.Error(t, r.Error)
   496  			continue
   497  		}
   498  
   499  		assert.NoError(t, r.Error)
   500  		assert.Equal(t, tt.want, got)
   501  	}
   502  }
   503  
   504  func TestReader_ReadString(t *testing.T) {
   505  	tests := []struct {
   506  		data    []byte
   507  		want    string
   508  		wantErr bool
   509  	}{
   510  		{
   511  			data:    []byte{0x00},
   512  			want:    "",
   513  			wantErr: false,
   514  		},
   515  		{
   516  			data:    []byte{0x06, 0x66, 0x6F, 0x6F},
   517  			want:    "foo",
   518  			wantErr: false,
   519  		},
   520  		{
   521  			data:    []byte{0x08, 0x61, 0x76, 0x72, 0x6F},
   522  			want:    "avro",
   523  			wantErr: false,
   524  		},
   525  		{
   526  			data:    []byte{0x0C, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65},
   527  			want:    "apache",
   528  			wantErr: false,
   529  		},
   530  		{
   531  			data:    []byte{0x28, 0x6F, 0x70, 0x70, 0x61, 0x6E, 0x20, 0x67, 0x61, 0x6E, 0x67, 0x6E, 0x61, 0x6D, 0x20, 0x73, 0x74, 0x79, 0x6C, 0x65, 0x21},
   532  			want:    "oppan gangnam style!",
   533  			wantErr: false,
   534  		},
   535  		{
   536  			data:    []byte{0x36, 0xD1, 0x87, 0xD0, 0xB5, 0x2D, 0xD1, 0x82, 0xD0, 0xBE, 0x20, 0xD0, 0xBF, 0xD0, 0xBE, 0x20, 0xD1, 0x80, 0xD1, 0x83, 0xD1, 0x81, 0xD1, 0x81, 0xD0, 0xBA, 0xD0, 0xB8},
   537  			want:    "че-то по русски",
   538  			wantErr: false,
   539  		},
   540  		{
   541  			data:    []byte{0x0C, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C},
   542  			want:    "世界",
   543  			wantErr: false,
   544  		},
   545  		{
   546  			data:    []byte{0x22, 0x21, 0xE2, 0x84, 0x96, 0x3B, 0x25, 0x3A, 0x3F, 0x2A, 0x22, 0x28, 0x29, 0x40, 0x23, 0x24, 0x5E, 0x26},
   547  			want:    "!№;%:?*\"()@#$^&",
   548  			wantErr: false,
   549  		},
   550  		{
   551  			data:    []byte(nil), // io.EOF no length
   552  			want:    "",
   553  			wantErr: true,
   554  		},
   555  		{
   556  			data:    []byte{0x05, 0x66, 0x6F, 0x6F, 0x6F}, // Invalid string length
   557  			want:    "",
   558  			wantErr: true,
   559  		},
   560  		{
   561  			data:    []byte{0x08, 0x66}, // io.EOF length greater then data
   562  			want:    "",
   563  			wantErr: true,
   564  		},
   565  	}
   566  
   567  	for _, tt := range tests {
   568  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   569  
   570  		got := r.ReadString()
   571  
   572  		if tt.wantErr {
   573  			assert.Error(t, r.Error)
   574  			continue
   575  		}
   576  
   577  		assert.NoError(t, r.Error)
   578  		assert.Equal(t, tt.want, got)
   579  	}
   580  }
   581  
   582  func TestReader_ReadStringFastPathIsntBoundToBuffer(t *testing.T) {
   583  	data := []byte{0x06, 0x66, 0x6F, 0x6F, 0x08, 0x61, 0x76, 0x72, 0x6F}
   584  	r := avro.NewReader(bytes.NewReader(data), 4)
   585  
   586  	got1 := r.ReadString()
   587  	got2 := r.ReadString()
   588  
   589  	assert.NoError(t, r.Error)
   590  	assert.Equal(t, "foo", got1)
   591  	assert.Equal(t, "avro", got2)
   592  }
   593  
   594  func TestReader_ReadBlockHeader(t *testing.T) {
   595  	tests := []struct {
   596  		data []byte
   597  		len  int64
   598  		size int64
   599  	}{
   600  		{
   601  			data: []byte{0x80, 0x01},
   602  			len:  64,
   603  			size: 0,
   604  		},
   605  		{
   606  			data: []byte{0x7F, 0x80, 0x01},
   607  			len:  64,
   608  			size: 64,
   609  		},
   610  	}
   611  
   612  	for _, tt := range tests {
   613  		r := avro.NewReader(bytes.NewReader(tt.data), 10)
   614  
   615  		gotLen, gotSize := r.ReadBlockHeader()
   616  
   617  		assert.NoError(t, r.Error)
   618  		assert.Equal(t, tt.len, gotLen)
   619  		assert.Equal(t, tt.size, gotSize)
   620  	}
   621  }
   622  
   623  type delayedReader struct {
   624  	count int
   625  	b     []byte
   626  }
   627  
   628  func (r *delayedReader) Read(p []byte) (n int, err error) {
   629  	if r.count == 0 {
   630  		r.count++
   631  		return 0, nil
   632  	}
   633  
   634  	return copy(p, r.b), nil
   635  }