github.com/aergoio/aergo@v1.3.1/account/key/store_test.go (about) 1 package key 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "sync" 7 "testing" 8 9 "github.com/aergoio/aergo/types" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 var ( 14 testDir string 15 ks *Store 16 ) 17 18 func initTest() { 19 testDir, _ = ioutil.TempDir("", "test") 20 ks = NewStore(testDir, 0) 21 } 22 23 func deinitTest() { 24 ks.CloseStore() 25 } 26 func TestCreateKey(t *testing.T) { 27 initTest() 28 defer deinitTest() 29 const testSize = 10 30 for i := 0; i < testSize; i++ { 31 pass := fmt.Sprintf("%d", i) 32 addr, err := ks.CreateKey(pass) 33 if err != nil { 34 t.Errorf("could not create key : %s", err.Error()) 35 } 36 if len(addr) != types.AddressLength { 37 t.Errorf("invalid address created : length = %d", len(addr)) 38 } 39 } 40 } 41 42 func TestCreateKeyLongPass(t *testing.T) { 43 initTest() 44 defer deinitTest() 45 const testSize = 10 46 for i := 0; i < testSize; i++ { 47 pass := fmt.Sprintf("%1024d", i) 48 addr, err := ks.CreateKey(pass) 49 if err != nil { 50 t.Errorf("could not create key : %s", err.Error()) 51 } 52 if len(addr) != types.AddressLength { 53 t.Errorf("invalid address created : length = %d", len(addr)) 54 } 55 } 56 } 57 58 func TestExportImportKey(t *testing.T) { 59 initTest() 60 defer deinitTest() 61 const testSize = 10 62 for i := 0; i < testSize; i++ { 63 pass := fmt.Sprintf("%d", i) 64 addr, err := ks.CreateKey(pass) 65 if err != nil { 66 t.Errorf("could not create key : %s", err.Error()) 67 } 68 if len(addr) != types.AddressLength { 69 t.Errorf("invalid address created : length = %d", len(addr)) 70 } 71 exported, err := ks.ExportKey(addr, pass) 72 if err != nil { 73 t.Errorf("could not export key : %s", err.Error()) 74 } 75 if len(exported) != 48 { 76 t.Errorf("invalid exported address : length = %d", len(exported)) 77 } 78 imported, err := ks.ImportKey(exported, pass, pass) 79 assert.NoError(t, err, "import") 80 assert.Equal(t, imported, addr, "import result") 81 } 82 } 83 84 func TestSignTx(t *testing.T) { 85 initTest() 86 defer deinitTest() 87 const testSize = 10 88 for i := 0; i < testSize; i++ { 89 pass := fmt.Sprintf("%32d", i) 90 addr, err := ks.CreateKey(pass) 91 if err != nil { 92 t.Errorf("could not create key : %s", err.Error()) 93 } 94 if len(addr) != types.AddressLength { 95 t.Errorf("invalid address created : length = %d", len(addr)) 96 } 97 unlocked, err := ks.Unlock(addr, pass) 98 if err != nil { 99 t.Errorf("could not unlock address: %s", err.Error()) 100 } 101 if len(unlocked) != types.AddressLength { 102 t.Errorf("invalid unlock address : length = %d", len(unlocked)) 103 } 104 tx := &types.Tx{Body: &types.TxBody{Account: addr}} 105 err = ks.SignTx(tx, nil) //TODO : improve 106 if err != nil { 107 t.Errorf("could not sign : %s", err.Error()) 108 } 109 if tx.Body.Sign == nil { 110 t.Errorf("sign is nil : %s", tx.String()) 111 } 112 } 113 } 114 115 func TestSign(t *testing.T) { 116 initTest() 117 defer deinitTest() 118 const testSize = 10 119 for i := 0; i < testSize; i++ { 120 pass := fmt.Sprintf("%32d", i) 121 addr, err := ks.CreateKey(pass) 122 if err != nil { 123 t.Errorf("could not create key : %s", err.Error()) 124 } 125 if len(addr) != types.AddressLength { 126 t.Errorf("invalid address created : length = %d", len(addr)) 127 } 128 hash := []byte(pass) 129 _, err = ks.Sign(addr, pass, hash) //TODO : improve 130 if err != nil { 131 t.Errorf("could not sign : %s", err.Error()) 132 } 133 } 134 } 135 136 func TestConcurrentUnlockAndLock(t *testing.T) { 137 initTest() 138 defer deinitTest() 139 140 pass := "password" 141 addr, err := ks.CreateKey(pass) 142 if err != nil { 143 t.Errorf("could not create key : %s", err.Error()) 144 } 145 146 const testSize = 50 147 var wg sync.WaitGroup 148 for i := 0; i < testSize; i++ { 149 wg.Add(1) 150 go func(wg *sync.WaitGroup, id int) { 151 defer wg.Done() 152 ks.Unlock(addr, pass) 153 ks.Lock(addr, pass) 154 }(&wg, i) 155 } 156 wg.Wait() 157 }