github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/pseudonym_test.go (about)

     1  // Copyright 2017 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 libkb
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func testPseudonymName(name, idStr string, keyGen KeyGen,
    16  	keyStr, expectedPseudonymStr string) string {
    17  	return fmt.Sprintf("%s,%s,%d,%s,%s", name, idStr, keyGen, keyStr, expectedPseudonymStr)
    18  }
    19  
    20  func testMakePseudonym(t *testing.T, name, idStr string, keyGen KeyGen,
    21  	keyStr, expectedPseudonymStr string) {
    22  	idBytes, err := hex.DecodeString(idStr)
    23  	require.NoError(t, err)
    24  	var id tlfID
    25  	require.Equal(t, 16, copy(id[:], idBytes))
    26  
    27  	keyBytes, err := hex.DecodeString(keyStr)
    28  	require.NoError(t, err)
    29  	require.Equal(t, 32, len(keyBytes))
    30  	var key [32]byte
    31  	copy(key[:], keyBytes)
    32  
    33  	pseudonym, err := MakePseudonym(TlfPseudonymInfo{
    34  		Name:    name,
    35  		ID:      id,
    36  		KeyGen:  keyGen,
    37  		HmacKey: key,
    38  	})
    39  	require.NoError(t, err)
    40  	pseudonymStr := hex.EncodeToString(pseudonym[:])
    41  	require.Equal(t, expectedPseudonymStr, pseudonymStr)
    42  }
    43  
    44  func TestMakePseudonym(t *testing.T) {
    45  	names := []string{"/keybase/private/a1", "/keybase/private/zz"}
    46  	idStrs := []string{
    47  		"b070f968fdc9d1c8827d7e4953659416",
    48  		"0d399cb03bd1b59a07f92fffaaffa516",
    49  	}
    50  	keyGens := []KeyGen{1, 50}
    51  	keyStrs := []string{
    52  		"9d13584d962bf1acebd1ccee109c8bb5d4a014e77f125302455477c53307cc14",
    53  		"ca7014befa7470d87129841c0f41c5b6ed548b9a6431d205b4ff44556bd51a42",
    54  	}
    55  
    56  	expectedPseudonymStrs := []string{
    57  		"dcec9bccde1b859cb355a9264bca56b91f07e7c831710ca945a0f7fe7b7e1191",
    58  		"66b38986a207c00b5f6b835af0ae8403f8bd07f7ef520dbbd4addf53d2b58edb",
    59  		"3d3cbb61788f87bfbc9d1d6c528600eccf4bbde4c3925b3f05a7692e54fd74ff",
    60  		"94c8152fb780041bbbb62df29a47f2579392927af73fa2dcfbccaac8718cce75",
    61  		"085075dc9ef14821e61c8e669286ad5031faa2ebf1b6e2b63d4f2d74fc7bb861",
    62  		"eeca52e9fe0b4699fe2f2baa834603ed5badc769bf99dd1f33d2863796d50142",
    63  		"42dcd4ce7eb97d6bea874bb2af2c7b3f5414c131decfd7e47213524dfd0c90a3",
    64  		"ebd2ff9eba99a09a5723f1dfab563596eef05213ab1af5f10d40ff102c4f0d69",
    65  		"d0e0031b1dc21fbb03312f26e6e8b4c1e5b5bed0a7b8902bffdbe467af7ee81c",
    66  		"2ddf845eebd66816f8f07aaf23901aa7a3ebb60b3be340573f2b05e8246d096d",
    67  		"4240789d970b301d3ede262f6f81f97efe42296ac83145d5c6de340188f73b2d",
    68  		"51786cb8605a4be7373363ed1a7a98b54df8c49e31397294996bf37de57f2f40",
    69  		"9046fac921597b06332a2655647f22eb7955b34a21a6d97e8e3a74e92ad9f149",
    70  		"529085af3111d1a64186e01f3a8b2607586060116b3db092e79b943a133ebb32",
    71  		"700cebb5f5e4360f12d97028e7ac96582a0f733aa54db23f9a19740efbf8d626",
    72  		"7531de864cbc313817c0107cfadaaddf01350f1b1cd8f1e497358166048b1a64",
    73  	}
    74  
    75  	i := 0
    76  	for _, name := range names {
    77  		for _, idStr := range idStrs {
    78  			for _, keyGen := range keyGens {
    79  				for _, keyStr := range keyStrs {
    80  					epStr := expectedPseudonymStrs[i]
    81  					t.Run(testPseudonymName(name, idStr, keyGen, keyStr, epStr),
    82  						func(t *testing.T) {
    83  							testMakePseudonym(t, name, idStr, keyGen, keyStr, epStr)
    84  						})
    85  					i++
    86  				}
    87  			}
    88  		}
    89  	}
    90  }