package emojihash import ( "reflect" "testing" "github.com/stretchr/testify/require" "github.com/status-im/status-go/protocol/identity" ) func TestGenerateFor(t *testing.T) { checker := func(pubkey string, expected *[](string)) { emojihash, err := GenerateFor(pubkey) require.NoError(t, err) if !reflect.DeepEqual(emojihash, *expected) { t.Fatalf("invalid emojihash %v != %v", emojihash, *expected) } } checker("0x04e25da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8", &[](string){"πŸ‘¦πŸ½", "🦹🏻", "πŸ‘ΆπŸΏ", "πŸ›", "🌁", "πŸ™ŒπŸ»", "πŸ™‡πŸ½β€β™‚οΈ", "πŸ™ŒπŸΎ", "πŸ€₯", "πŸ›", "πŸ‘©πŸ½β€πŸ”§", "πŸ”§", "βš™οΈ", "πŸ§’πŸ½"}) checker("0x0400000000000000000000000000000000000000000000000000000000000000014218F20AE6C646B363DB68605822FB14264CA8D2587FDD6FBC750D587E76A7EE", &[](string){"πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€"}) checker("0x04000000000000000000000000000000000000000010000000000000000000000033600332D373318ECC2F212A30A5750D2EAC827B6A32B33D326CCF369B12B1BE", &[](string){"πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", (emojisAlphabet)[1]}) checker("0x040000000000000000000000000000000000000000200000000000000000000000353050BFE33B724E60A0C600FBA565A9B62217B1BD35BF9848F2AB847C598B30", &[](string){"πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", "πŸ˜€", (emojisAlphabet)[2]}) } func TestEmojiHashOfInvalidKey(t *testing.T) { checker := func(pubkey string) { _, err := GenerateFor(pubkey) require.Error(t, err) } checker("abc") checker("0x01") checker("0x01e25da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8") checker("0x04425da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8") } func TestToEmojiHash(t *testing.T) { alphabet := [](string){"πŸ˜‡", "🀐", "πŸ₯΅", "πŸ™Š", "🀌"} checker := func(valueStr string, hashLen int, expected *[](string)) { value := identity.ToBigInt(t, valueStr) res, err := toEmojiHash(value, hashLen, &alphabet) require.NoError(t, err) if !reflect.DeepEqual(res, *expected) { t.Fatalf("invalid emojihash conversion %v != %v", res, *expected) } } checker("777", 5, &[](string){"🀐", "🀐", "🀐", "πŸ˜‡", "πŸ₯΅"}) checker("777", 0, &[](string){"🀐", "🀐", "🀐", "πŸ˜‡", "πŸ₯΅"}) checker("777", 10, &[](string){"πŸ˜‡", "πŸ˜‡", "πŸ˜‡", "πŸ˜‡", "πŸ˜‡", "🀐", "🀐", "🀐", "πŸ˜‡", "πŸ₯΅"}) // 20bytes of data described by 14 emojis requires at least 2757 length alphabet alphabet = make([](string), 2757) val := identity.ToBigInt(t, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") // 20 bytes _, err := toEmojiHash(val, 14, &alphabet) require.NoError(t, err) alphabet = make([](string), 2757-1) _, err = toEmojiHash(val, 14, &alphabet) require.Error(t, err) }