github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/tlf/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 tlf
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/keybase/client/go/kbfs/kbfscodec"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestIDEncodeDecode(t *testing.T) {
    16  	codec := kbfscodec.NewMsgpack()
    17  	id := FakeID(1, Public)
    18  
    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, idByteLen+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  func TestMakeIDFromTeam(t *testing.T) {
    36  	privateTID := keybase1.MakeTestTeamID(1, false)
    37  	publicTID := keybase1.MakeTestTeamID(2, true)
    38  
    39  	epochIndex := idByteLen - 2
    40  	check := func(ty Type, tid keybase1.TeamID, epoch byte) {
    41  		id, err := MakeIDFromTeam(ty, tid, epoch)
    42  		require.NoError(t, err)
    43  		require.Equal(t, id.Type(), ty)
    44  		require.Equal(t, tid.ToBytes()[:epochIndex], id.Bytes()[:epochIndex])
    45  		require.Equal(t, epoch, id.Bytes()[epochIndex])
    46  	}
    47  	check(Private, privateTID, 0)
    48  	check(Public, publicTID, 0)
    49  	check(SingleTeam, privateTID, 0)
    50  	check(Private, privateTID, 15)
    51  
    52  	_, err := MakeIDFromTeam(Public, privateTID, 0)
    53  	require.NotNil(t, err)
    54  	_, err = MakeIDFromTeam(Private, publicTID, 0)
    55  	require.NotNil(t, err)
    56  	_, err = MakeIDFromTeam(SingleTeam, publicTID, 0)
    57  	require.NotNil(t, err)
    58  	_, err = MakeIDFromTeam(
    59  		Private, keybase1.TeamID("extra"+privateTID.String()), 0)
    60  	require.NotNil(t, err)
    61  }