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