119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package varn_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.intruders.space/public/opentimestamps/varn"
|
|
)
|
|
|
|
// Helper functions for decoding (to verify encoding)
|
|
func readVarUint(t *testing.T, data []byte) (uint64, int) {
|
|
t.Helper()
|
|
require.NotZero(t, data, "empty buffer")
|
|
|
|
if data[0] == 0 {
|
|
return 0, 1
|
|
}
|
|
|
|
var value uint64
|
|
var bytesRead int
|
|
|
|
for i, b := range data {
|
|
bytesRead = i + 1
|
|
value |= uint64(b&0x7F) << uint(7*i)
|
|
if b&0x80 == 0 {
|
|
break
|
|
}
|
|
require.Less(t, i, 9, "varint too long") // 9 is max bytes needed for uint64
|
|
}
|
|
|
|
return value, bytesRead
|
|
}
|
|
|
|
func readVarBytes(t *testing.T, data []byte) ([]byte, int) {
|
|
t.Helper()
|
|
length, bytesRead := readVarUint(t, data)
|
|
|
|
require.GreaterOrEqual(t, uint64(len(data)-bytesRead), length, "buffer too short")
|
|
|
|
end := bytesRead + int(length)
|
|
return data[bytesRead:end], end
|
|
}
|
|
|
|
func TestAppendVarUint(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value uint64
|
|
expected string // hex representation of expected bytes
|
|
}{
|
|
{"zero", 0, "00"},
|
|
{"small value", 42, "2a"},
|
|
{"value below 128", 127, "7f"},
|
|
{"value requiring 2 bytes", 128, "8001"},
|
|
{"medium value", 300, "ac02"},
|
|
{"large value", 1234567890, "d285d8cc04"}, // Updated from "d2a6e58e07" to correct encoding
|
|
{"max uint64", 18446744073709551615, "ffffffffffffffffff01"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var buf []byte
|
|
result := varn.AppendVarUint(buf, tt.value)
|
|
|
|
// Check encoding
|
|
expected, err := hex.DecodeString(tt.expected)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Verify by decoding
|
|
decoded, _ := readVarUint(t, result)
|
|
assert.Equal(t, tt.value, decoded)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAppendVarBytes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value []byte
|
|
}{
|
|
{"empty", []byte{}},
|
|
{"small", []byte{0x01, 0x02, 0x03}},
|
|
{"medium", bytes.Repeat([]byte{0xAA}, 127)},
|
|
{"large", bytes.Repeat([]byte{0xBB}, 256)},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var buf []byte
|
|
result := varn.AppendVarBytes(buf, tt.value)
|
|
|
|
// Verify by decoding
|
|
decoded, _ := readVarBytes(t, result)
|
|
assert.Equal(t, tt.value, decoded)
|
|
|
|
// Check that length is properly encoded
|
|
lenEncoded, _ := readVarUint(t, result)
|
|
assert.Equal(t, uint64(len(tt.value)), lenEncoded)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAppendingToExistingBuffer(t *testing.T) {
|
|
existing := []byte{0xFF, 0xFF}
|
|
|
|
// Test AppendVarUint
|
|
result := varn.AppendVarUint(existing, 42)
|
|
assert.Equal(t, []byte{0xFF, 0xFF, 0x2A}, result)
|
|
|
|
// Test AppendVarBytes
|
|
data := []byte{0x01, 0x02, 0x03}
|
|
result = varn.AppendVarBytes(existing, data)
|
|
assert.Equal(t, []byte{0xFF, 0xFF, 0x03, 0x01, 0x02, 0x03}, result)
|
|
}
|