github.com/ethersphere/bee/v2@v2.2.0/pkg/keystore/test/test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package test 6 7 import ( 8 "bytes" 9 "errors" 10 "testing" 11 12 "github.com/ethersphere/bee/v2/pkg/crypto" 13 "github.com/ethersphere/bee/v2/pkg/keystore" 14 ) 15 16 // Service is a utility testing function that can be used to test 17 // implementations of the keystore.Service interface. 18 func Service(t *testing.T, s keystore.Service) { 19 t.Helper() 20 21 exists, err := s.Exists("swarm") 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 if exists { 27 t.Fatal("should not exist") 28 } 29 30 edg := crypto.EDGSecp256_K1 31 // create a new swarm key 32 k1, created, err := s.Key("swarm", "pass123456", edg) 33 if err != nil { 34 t.Fatal(err) 35 } 36 if !created { 37 t.Fatal("key is not created") 38 } 39 40 exists, err = s.Exists("swarm") 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 if !exists { 46 t.Fatal("should exist") 47 } 48 49 // get swarm key 50 k2, created, err := s.Key("swarm", "pass123456", edg) 51 if err != nil { 52 t.Fatal(err) 53 } 54 if created { 55 t.Fatal("key is created, but should not be") 56 } 57 if !bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) { 58 t.Fatal("two keys are not equal") 59 } 60 61 // invalid password 62 _, _, err = s.Key("swarm", "invalid password", edg) 63 if !errors.Is(err, keystore.ErrInvalidPassword) { 64 t.Fatal(err) 65 } 66 67 // create a new libp2p key 68 k3, created, err := s.Key("libp2p", "p2p pass", edg) 69 if err != nil { 70 t.Fatal(err) 71 } 72 if !created { 73 t.Fatal("key is not created") 74 } 75 if bytes.Equal(k1.D.Bytes(), k3.D.Bytes()) { 76 t.Fatal("two keys are equal, but should not be") 77 } 78 79 // get libp2p key 80 k4, created, err := s.Key("libp2p", "p2p pass", edg) 81 if err != nil { 82 t.Fatal(err) 83 } 84 if created { 85 t.Fatal("key is created, but should not be") 86 } 87 if !bytes.Equal(k3.D.Bytes(), k4.D.Bytes()) { 88 t.Fatal("two keys are not equal") 89 } 90 }