github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/keycache_test.go (about) 1 // Copyright 2016 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 libkbfs 6 7 import ( 8 "errors" 9 "testing" 10 11 "github.com/keybase/client/go/kbfs/kbfscrypto" 12 "github.com/keybase/client/go/kbfs/kbfsmd" 13 "github.com/keybase/client/go/kbfs/tlf" 14 ) 15 16 func TestKeyCacheBasic(t *testing.T) { 17 cache := NewKeyCacheStandard(10) 18 id := tlf.FakeID(100, tlf.Public) 19 key := kbfscrypto.MakeTLFCryptKey([32]byte{0xf}) 20 keyGen := kbfsmd.FirstValidKeyGen 21 _, err := cache.GetTLFCryptKey(id, keyGen) 22 if _, ok := err.(KeyCacheMissError); !ok { 23 t.Fatal(errors.New("expected KeyCacheMissError")) 24 } 25 err = cache.PutTLFCryptKey(id, keyGen, key) 26 if err != nil { 27 t.Fatal(err) 28 } 29 // add the same key twice 30 err = cache.PutTLFCryptKey(id, keyGen, key) 31 if err != nil { 32 t.Fatal(err) 33 } 34 key2, err := cache.GetTLFCryptKey(id, keyGen) 35 if err != nil { 36 t.Fatal(err) 37 } 38 if key != key2 { 39 t.Fatal("keys are unequal") 40 } 41 for i := 0; i < 11; i++ { 42 id = tlf.FakeID(byte(i), tlf.Public) 43 key = kbfscrypto.MakeTLFCryptKey([32]byte{byte(i)}) 44 err = cache.PutTLFCryptKey(id, keyGen, key) 45 if err != nil { 46 t.Fatal(err) 47 } 48 } 49 for i := 0; i < 11; i++ { 50 id = tlf.FakeID(byte(i), tlf.Public) 51 _, err = cache.GetTLFCryptKey(id, keyGen) 52 if i > 0 && err != nil { 53 t.Fatal(err) 54 } 55 if i == 0 && err == nil { 56 t.Fatal(errors.New("key not expected")) 57 } 58 } 59 }