github.com/status-im/status-go@v1.1.0/protocol/identity/colorhash/colorhash_test.go (about) 1 package colorhash 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/status-im/status-go/multiaccounts" 10 "github.com/status-im/status-go/protocol/identity" 11 ) 12 13 func TestGenerateFor(t *testing.T) { 14 checker := func(pubkey string, expected *multiaccounts.ColorHash) { 15 colorhash, err := GenerateFor(pubkey) 16 require.NoError(t, err) 17 if !reflect.DeepEqual(colorhash, *expected) { 18 t.Fatalf("invalid emojihash %v != %v", colorhash, *expected) 19 } 20 } 21 22 checker("0x04e25da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8", 23 &multiaccounts.ColorHash{{3, 30}, {2, 10}, {5, 5}, {3, 14}, {5, 4}, {4, 19}, {3, 16}, {4, 0}, {5, 28}, {4, 13}, {4, 15}}) 24 } 25 26 func TestColorHashOfInvalidKey(t *testing.T) { 27 checker := func(pubkey string) { 28 _, err := GenerateFor(pubkey) 29 require.Error(t, err) 30 } 31 checker("abc") 32 checker("0x01") 33 checker("0x01e25da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8") 34 checker("0x04425da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8") 35 } 36 37 func TestColorHash(t *testing.T) { 38 alphabet := makeColorHashAlphabet(4, 4) 39 40 checker := func(valueStr string, expected *multiaccounts.ColorHash) { 41 value := identity.ToBigInt(t, valueStr) 42 res := toColorHash(value, &alphabet, 4) 43 if !reflect.DeepEqual(res, *expected) { 44 t.Fatalf("invalid colorhash conversion %v != %v", res, *expected) 45 } 46 } 47 48 checker("0x0", &multiaccounts.ColorHash{{1, 0}}) 49 checker("0x1", &multiaccounts.ColorHash{{1, 1}}) 50 checker("0x4", &multiaccounts.ColorHash{{2, 0}}) 51 checker("0xF", &multiaccounts.ColorHash{{4, 3}}) 52 53 // oops, collision 54 checker("0xFF", &multiaccounts.ColorHash{{4, 3}, {4, 0}}) 55 checker("0xFC", &multiaccounts.ColorHash{{4, 3}, {4, 0}}) 56 57 checker("0xFFFF", &multiaccounts.ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}) 58 }