@@ -18,7 +18,7 @@ func TestInterpretUnsignedInteger(t *testing.T) {
1818 {"uint32 big endian" , []byte {0x12 , 0x34 , 0x56 , 0x78 }, "big" , uint32 (0x12345678 )},
1919 {"uint64 little endian" , []byte {0x12 , 0x34 , 0x56 , 0x78 , 0x9A , 0xBC , 0xDE , 0xF0 }, "little" , uint64 (0xF0DEBC9A78563412 )},
2020 {"uint64 big endian" , []byte {0x12 , 0x34 , 0x56 , 0x78 , 0x9A , 0xBC , 0xDE , 0xF0 }, "big" , uint64 (0x123456789ABCDEF0 )},
21- {"unsupported length" , []byte {0x12 , 0x34 , 0x56 }, "" , nil },
21+ {"unsupported length" , []byte {0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xAB , 0xCD , 0xEF , 0xCA , 0xFE , 0xBA , 0xBE , 0xDE , 0xAD , 0xBE , 0xE5 }, "" , nil },
2222 }
2323
2424 for _ , tt := range tests {
@@ -45,7 +45,7 @@ func TestInterpretSignedInteger(t *testing.T) {
4545 {"int32 big endian" , []byte {0xFF , 0xFF , 0xFF , 0x82 }, "big" , int32 (- 126 )},
4646 {"int64 little endian" , []byte {0x82 , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF }, "little" , int64 (- 126 )},
4747 {"int64 big endian" , []byte {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0x82 }, "big" , int64 (- 126 )},
48- {"unsupported length" , []byte {0x12 , 0x34 , 0x56 }, "" , nil },
48+ {"unsupported length" , []byte {0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xAB , 0xCD , 0xEF , 0xCA , 0xFE , 0xBA , 0xBE , 0xDE , 0xAD , 0xBE , 0xE5 }, "" , nil },
4949 }
5050
5151 for _ , tt := range tests {
@@ -69,7 +69,7 @@ func TestInterpretFloat(t *testing.T) {
6969 {"float32 big endian" , []byte {0x3F , 0x80 , 0x00 , 0x00 }, "big" , float32 (1.0 )},
7070 {"float64 little endian" , []byte {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xF0 , 0x3F }, "little" , 1.0 },
7171 {"float64 big endian" , []byte {0x3F , 0xF0 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 }, "big" , 1.0 },
72- {"unsupported length" , []byte {0x12 , 0x34 , 0x56 }, "" , nil },
72+ {"unsupported length" , []byte {0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xAB , 0xCD , 0xEF , 0xCA , 0xFE , 0xBA , 0xBE , 0xDE , 0xAD , 0xBE , 0xE5 }, "" , nil },
7373 }
7474
7575 for _ , tt := range tests {
@@ -104,3 +104,34 @@ func TestInterpretMeasurementValue(t *testing.T) {
104104 })
105105 }
106106}
107+
108+ func TestInterpretUnalignedMeasurementValue (t * testing.T ) {
109+ tests := []struct {
110+ name string
111+ measurement Measurement
112+ data []byte
113+ expected interface {}
114+ }{
115+ {"unsigned 3 byte" , Measurement {Type : "int" , Unsigned : true , Endianness : "big" , ScalingFactor : 1.0 }, []byte {0x01 , 0x02 , 0x03 }, uint32 (0x010203 )},
116+ // Note the hard-coded decimal value is validated. int32ing the 3-byte value 0xDEADFF results in 14593535 and doing 0xFFDEADFF will overflow the int32 :P
117+ {"signed 3 byte" , Measurement {Type : "int" , Unsigned : false , Endianness : "big" , ScalingFactor : 1.0 }, []byte {0xDE , 0xAD , 0xFF }, int32 (- 2183681 )},
118+
119+ {"unsigned 5 byte" , Measurement {Type : "int" , Unsigned : false , Endianness : "little" , ScalingFactor : 1.0 }, []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 }, int64 (0x0504030201 )},
120+ {"signed 5 byte" , Measurement {Type : "int" , Unsigned : true , Endianness : "little" , ScalingFactor : 1.0 }, []byte {0xDE , 0xAD , 0xBE , 0xEF , 0xFF }, uint64 (0xFFEFBEADDE )},
121+
122+ {"unsigned 6 byte" , Measurement {Type : "int" , Unsigned : false , Endianness : "big" , ScalingFactor : 1.0 }, []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 }, int64 (0x010203040506 )},
123+ {"signed 6 byte" , Measurement {Type : "int" , Unsigned : true , Endianness : "big" , ScalingFactor : 1.0 }, []byte {0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED }, uint64 (0xDEADBEEFFEED )},
124+
125+ {"unsigned 7 byte" , Measurement {Type : "int" , Unsigned : false , Endianness : "little" , ScalingFactor : 1.0 }, []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 }, int64 (0x07060504030201 )},
126+ {"signed 7 byte" , Measurement {Type : "int" , Unsigned : true , Endianness : "little" , ScalingFactor : 1.0 }, []byte {0xCA , 0xFE , 0xBA , 0xBE , 0xBB , 0xBA , 0xD0 }, uint64 (0xD0BABBBEBAFECA )},
127+ }
128+
129+ for _ , tt := range tests {
130+ t .Run (tt .name , func (t * testing.T ) {
131+ result , _ := InterpretMeasurementValue (tt .measurement , tt .data )
132+ if result != tt .expected {
133+ t .Errorf ("expected %v, got %v" , tt .expected , result )
134+ }
135+ })
136+ }
137+ }
0 commit comments