@@ -132,3 +132,86 @@ func TestEncode_Base64URLDecode_Ugly(t *T) {
132132 _ , ok := r .Value .(error )
133133 AssertTrue (t , ok )
134134}
135+
136+ // --- BigEndianUint64 ---
137+
138+ func TestEncode_BigEndianUint64_Good (t * T ) {
139+ AssertEqual (t , uint64 (1 ), BigEndianUint64 ([]byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }))
140+ }
141+
142+ // Bad: byte order is the whole point — the same bytes read big-endian and
143+ // little-endian must not agree, or a caller has picked the wrong one.
144+ func TestEncode_BigEndianUint64_Bad (t * T ) {
145+ b := []byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
146+ AssertEqual (t , uint64 (1 )<< 56 , BigEndianUint64 (b ))
147+ AssertFalse (t , BigEndianUint64 (b ) == LittleEndianUint64 (b ))
148+ }
149+
150+ // Ugly: only the first eight bytes are read, so a longer slice is not an error
151+ // and the tail is ignored.
152+ func TestEncode_BigEndianUint64_Ugly (t * T ) {
153+ AssertEqual (t , ^ uint64 (0 ), BigEndianUint64 ([]byte {255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 9 , 9 }))
154+ }
155+
156+ // --- LittleEndianUint64 ---
157+
158+ func TestEncode_LittleEndianUint64_Good (t * T ) {
159+ AssertEqual (t , uint64 (1 ), LittleEndianUint64 ([]byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }))
160+ }
161+
162+ func TestEncode_LittleEndianUint64_Bad (t * T ) {
163+ AssertEqual (t , uint64 (1 )<< 56 , LittleEndianUint64 ([]byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }))
164+ }
165+
166+ // Ugly: round-trips with the write side, which is how Keccak absorbs and
167+ // squeezes its state.
168+ func TestEncode_LittleEndianUint64_Ugly (t * T ) {
169+ buf := make ([]byte , 8 )
170+ PutLittleEndianUint64 (buf , 0xDEADBEEFCAFEBABE )
171+ AssertEqual (t , uint64 (0xDEADBEEFCAFEBABE ), LittleEndianUint64 (buf ))
172+ }
173+
174+ // --- PutLittleEndianUint64 ---
175+
176+ func TestEncode_PutLittleEndianUint64_Good (t * T ) {
177+ buf := make ([]byte , 8 )
178+ PutLittleEndianUint64 (buf , 1 )
179+ AssertEqual (t , byte (1 ), buf [0 ])
180+ AssertEqual (t , byte (0 ), buf [7 ])
181+ }
182+
183+ // Bad: writes exactly eight bytes and nothing beyond them, so a caller can
184+ // write into the middle of a larger buffer without clobbering its neighbours.
185+ func TestEncode_PutLittleEndianUint64_Bad (t * T ) {
186+ buf := make ([]byte , 10 )
187+ buf [8 ], buf [9 ] = 7 , 7
188+ PutLittleEndianUint64 (buf [:8 ], ^ uint64 (0 ))
189+ AssertEqual (t , byte (7 ), buf [8 ])
190+ AssertEqual (t , byte (7 ), buf [9 ])
191+ }
192+
193+ func TestEncode_PutLittleEndianUint64_Ugly (t * T ) {
194+ buf := make ([]byte , 16 )
195+ PutLittleEndianUint64 (buf [8 :], 0x0102030405060708 )
196+ AssertEqual (t , uint64 (0 ), LittleEndianUint64 (buf [:8 ]))
197+ AssertEqual (t , uint64 (0x0102030405060708 ), LittleEndianUint64 (buf [8 :]))
198+ }
199+
200+ // --- HexAppendEncode ---
201+
202+ func TestEncode_HexAppendEncode_Good (t * T ) {
203+ AssertEqual (t , "0aff" , AsString (HexAppendEncode (nil , []byte {0x0a , 0xff })))
204+ }
205+
206+ // Bad: an empty src appends nothing at all — dst comes back untouched rather
207+ // than gaining a separator or a padding byte.
208+ func TestEncode_HexAppendEncode_Bad (t * T ) {
209+ AssertEqual (t , "id-" , AsString (HexAppendEncode ([]byte ("id-" ), nil )))
210+ }
211+
212+ // Ugly: the reason this exists over HexEncode — it extends dst in place, so a
213+ // caller building an identifier stays at one allocation.
214+ func TestEncode_HexAppendEncode_Ugly (t * T ) {
215+ AssertEqual (t , "id-42-0aff" , AsString (HexAppendEncode ([]byte ("id-42-" ), []byte {0x0a , 0xff })))
216+ AssertEqual (t , HexEncode ([]byte {0x0a , 0xff }), AsString (HexAppendEncode (nil , []byte {0x0a , 0xff })))
217+ }
0 commit comments