github.com/status-im/status-go@v1.1.0/protocol/identity/utils_test.go (about) 1 package identity 2 3 import ( 4 "math" 5 "math/big" 6 "reflect" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestToBigBase(t *testing.T) { 13 checker := func(value *big.Int, base uint64, expected *[](uint64)) { 14 res := ToBigBase(value, base) 15 if !reflect.DeepEqual(res, *expected) { 16 t.Fatalf("invalid big base conversion %v != %v", res, *expected) 17 } 18 } 19 20 lengthChecker := func(value *big.Int, base, expectedLength uint64) { 21 res := ToBigBase(value, base) 22 if len(res) != int(expectedLength) { 23 t.Fatalf("invalid big base conversion %d != %d", len(res), expectedLength) 24 } 25 } 26 27 checker(new(big.Int).SetUint64(15), 16, &[](uint64){15}) 28 checker(new(big.Int).SetUint64(495), 16, &[](uint64){1, 14, 15}) 29 checker(new(big.Int).SetUint64(495), 30, &[](uint64){16, 15}) 30 checker(new(big.Int).SetUint64(495), 1024, &[](uint64){495}) 31 checker(new(big.Int).SetUint64(2048), 1024, &[](uint64){2, 0}) 32 33 base := uint64(math.Pow(2, 7*4)) 34 checker(ToBigInt(t, "0xFFFFFFFFFFFFFF"), base, &[](uint64){base - 1, base - 1}) 35 36 val := ToBigInt(t, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") 37 lengthChecker(val, 2757, 14) 38 lengthChecker(val, 2756, 15) 39 } 40 41 func TestSlices(t *testing.T) { 42 checker := func(compressedKey, charsCutoffA, emojiHash, colorHash, charsCutoffB string) { 43 slices, err := Slices(ToBigInt(t, compressedKey).Bytes()) 44 require.NoError(t, err) 45 46 sliceChecker := func(idx int, value *big.Int) { 47 if !reflect.DeepEqual(slices[idx], value.Bytes()) { 48 t.Fatalf("invalid slice (%d) %v != %v", idx, slices[idx], value.Bytes()) 49 } 50 } 51 52 sliceChecker(0, ToBigInt(t, charsCutoffA)) 53 sliceChecker(1, ToBigInt(t, emojiHash)) 54 sliceChecker(2, ToBigInt(t, colorHash)) 55 sliceChecker(3, ToBigInt(t, charsCutoffB)) 56 } 57 58 checker("0x03086138b210f21d41c757ae8a5d2a4cb29c1350f7389517608378ebd9efcf4a55", "0x030", "0x86138b210f21d41c757ae8a5d2a4cb29c1350f73", "0x89517608378ebd9efcf4", "0xa55") 59 checker("0x020000000000000000000000000000000000000000100000000000000000000000", "0x020", "0x0000000000000000000000000000000000000001", "0x00000000000000000000", "0x000") 60 } 61 62 func TestSlicesInvalid(t *testing.T) { 63 _, err := Slices(ToBigInt(t, "0x01").Bytes()) 64 require.Error(t, err) 65 }