github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsmd/id_test.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package kbfsmd 6 7 import ( 8 "testing" 9 10 "github.com/keybase/client/go/kbfs/kbfscodec" 11 "github.com/keybase/client/go/kbfs/kbfshash" 12 "github.com/stretchr/testify/require" 13 ) 14 15 // Make sure ID encodes and decodes properly with minimal overhead. 16 func TestIDEncodeDecode(t *testing.T) { 17 id := FakeID(1) 18 codec := kbfscodec.NewMsgpack() 19 encodedID, err := codec.Encode(id) 20 require.NoError(t, err) 21 22 // See 23 // https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bin 24 // for why there are two bytes of overhead. 25 const overhead = 2 26 require.Equal(t, kbfshash.DefaultHashByteLength+overhead, len(encodedID)) 27 28 var id2 ID 29 err = codec.Decode(encodedID, &id2) 30 require.NoError(t, err) 31 32 require.Equal(t, id, id2) 33 } 34 35 // Make sure the zero ID value encodes and decodes properly. 36 func TestIDEncodeDecodeZero(t *testing.T) { 37 codec := kbfscodec.NewMsgpack() 38 encodedID, err := codec.Encode(ID{}) 39 require.NoError(t, err) 40 41 require.Equal(t, []byte{0xc0}, encodedID) 42 43 var id ID 44 err = codec.Decode(encodedID, &id) 45 require.NoError(t, err) 46 47 require.Equal(t, ID{}, id) 48 }