github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/data/node_obfuscator_test.go (about) 1 // Copyright 2019 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 data 6 7 import ( 8 "strings" 9 "testing" 10 11 "github.com/keybase/client/go/libkb" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestNodeObfuscatorBasic(t *testing.T) { 16 secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4}) 17 no := NewNodeObfuscator(secret) 18 19 t.Log("Obfuscate with the default hasher") 20 ob := no.Obfuscate("test") 21 require.NotEqual(t, "", ob) 22 23 t.Log("Make sure it has two valid sec words") 24 words := strings.Split(ob, separator) 25 require.Len(t, words, 2) 26 require.True(t, libkb.ValidSecWord(words[0]), words[0]) 27 require.True(t, libkb.ValidSecWord(words[1]), words[1]) 28 29 t.Log("Make sure caching works") 30 ob2 := no.obfuscateWithHasher("test", func(_ string) []byte { 31 panic("Hash called") 32 }) 33 require.Equal(t, ob, ob2) 34 } 35 36 func TestNodeObfuscatorCollisions(t *testing.T) { 37 secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4}) 38 no := NewNodeObfuscator(secret) 39 40 t.Log("Obfuscate with a test hash to get a known string") 41 hasher1 := func(_ string) []byte { 42 return []byte{0, 0, 4} // first = 0, second = 1 43 } 44 ob := no.obfuscateWithHasher("test", hasher1) 45 first := libkb.SecWord(0) 46 second := libkb.SecWord(1) 47 expectedOb := strings.Join([]string{first, second}, separator) 48 require.Equal(t, expectedOb, ob) 49 50 t.Log("Obfuscate a new plaintext string with the same hash") 51 ob2 := no.obfuscateWithHasher("test2", hasher1) 52 expectedOb2 := strings.Join([]string{first, second, "2"}, separator) 53 require.Equal(t, expectedOb2, ob2) 54 55 t.Log("And one more, with a different hash but the same first 22 bits") 56 hasher2 := func(_ string) []byte { 57 return []byte{0, 0, 7} // first = 0, second = 1 58 } 59 ob3 := no.obfuscateWithHasher("test3", hasher2) 60 expectedOb3 := strings.Join([]string{first, second, "3"}, separator) 61 require.Equal(t, expectedOb3, ob3) 62 63 t.Log("Make sure we get the original result again for the first string") 64 ob4 := no.obfuscateWithHasher("test", hasher1) 65 require.Equal(t, ob, ob4) 66 }