2022-03-17 16:58:35 +00:00
|
|
|
package emojihash
|
2022-03-11 21:17:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-03-17 16:58:35 +00:00
|
|
|
"github.com/status-im/status-go/protocol/identity"
|
|
|
|
)
|
2022-03-11 21:17:48 +00:00
|
|
|
|
2022-03-17 16:58:35 +00:00
|
|
|
func TestGenerateFor(t *testing.T) {
|
2022-03-11 21:17:48 +00:00
|
|
|
checker := func(pubkey string, expected *[](string)) {
|
2022-03-17 16:58:35 +00:00
|
|
|
emojihash, err := GenerateFor(pubkey)
|
2022-03-11 21:17:48 +00:00
|
|
|
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",
|
2022-03-17 16:58:35 +00:00
|
|
|
&[](string){"😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", (emojisAlphabet)[1]})
|
2022-03-11 21:17:48 +00:00
|
|
|
|
|
|
|
checker("0x040000000000000000000000000000000000000000200000000000000000000000353050BFE33B724E60A0C600FBA565A9B62217B1BD35BF9848F2AB847C598B30",
|
2022-03-17 16:58:35 +00:00
|
|
|
&[](string){"😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", "😀", (emojisAlphabet)[2]})
|
2022-03-11 21:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 16:58:35 +00:00
|
|
|
func TestEmojiHashOfInvalidKey(t *testing.T) {
|
2022-03-11 21:17:48 +00:00
|
|
|
checker := func(pubkey string) {
|
2022-03-17 16:58:35 +00:00
|
|
|
_, err := GenerateFor(pubkey)
|
2022-03-11 21:17:48 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
checker("abc")
|
|
|
|
checker("0x01")
|
|
|
|
checker("0x01e25da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8")
|
|
|
|
checker("0x04425da6994ea2dc4ac70727e07eca153ae92bf7609db7befb7ebdceaad348f4fc55bbe90abf9501176301db5aa103fc0eb3bc3750272a26c424a10887db2a7ea8")
|
|
|
|
}
|
2022-03-17 16:58:35 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|