github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/signer/storage/aes_gcm_storage_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package storage 19 20 import ( 21 "bytes" 22 "encoding/json" 23 "fmt" 24 "io/ioutil" 25 "testing" 26 27 "github.com/AigarNetwork/aigar/common" 28 "github.com/AigarNetwork/aigar/log" 29 "github.com/mattn/go-colorable" 30 ) 31 32 func TestEncryption(t *testing.T) { 33 // key := []byte("AES256Key-32Characters1234567890") 34 // plaintext := []byte(value) 35 key := []byte("AES256Key-32Characters1234567890") 36 plaintext := []byte("exampleplaintext") 37 38 c, iv, err := encrypt(key, plaintext, nil) 39 if err != nil { 40 t.Fatal(err) 41 } 42 t.Logf("Ciphertext %x, nonce %x\n", c, iv) 43 44 p, err := decrypt(key, iv, c, nil) 45 if err != nil { 46 t.Fatal(err) 47 } 48 t.Logf("Plaintext %v\n", string(p)) 49 if !bytes.Equal(plaintext, p) { 50 t.Errorf("Failed: expected plaintext recovery, got %v expected %v", string(plaintext), string(p)) 51 } 52 } 53 54 func TestFileStorage(t *testing.T) { 55 56 a := map[string]storedCredential{ 57 "secret": { 58 Iv: common.Hex2Bytes("cdb30036279601aeee60f16b"), 59 CipherText: common.Hex2Bytes("f311ac49859d7260c2c464c28ffac122daf6be801d3cfd3edcbde7e00c9ff74f"), 60 }, 61 "secret2": { 62 Iv: common.Hex2Bytes("afb8a7579bf971db9f8ceeed"), 63 CipherText: common.Hex2Bytes("2df87baf86b5073ef1f03e3cc738de75b511400f5465bb0ddeacf47ae4dc267d"), 64 }, 65 } 66 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 67 if err != nil { 68 t.Fatal(err) 69 } 70 stored := &AESEncryptedStorage{ 71 filename: fmt.Sprintf("%v/vault.json", d), 72 key: []byte("AES256Key-32Characters1234567890"), 73 } 74 stored.writeEncryptedStorage(a) 75 read := &AESEncryptedStorage{ 76 filename: fmt.Sprintf("%v/vault.json", d), 77 key: []byte("AES256Key-32Characters1234567890"), 78 } 79 creds, err := read.readEncryptedStorage() 80 if err != nil { 81 t.Fatal(err) 82 } 83 for k, v := range a { 84 if v2, exist := creds[k]; !exist { 85 t.Errorf("Missing entry %v", k) 86 } else { 87 if !bytes.Equal(v.CipherText, v2.CipherText) { 88 t.Errorf("Wrong ciphertext, expected %x got %x", v.CipherText, v2.CipherText) 89 } 90 if !bytes.Equal(v.Iv, v2.Iv) { 91 t.Errorf("Wrong iv") 92 } 93 } 94 } 95 } 96 func TestEnd2End(t *testing.T) { 97 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 98 99 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 s1 := &AESEncryptedStorage{ 105 filename: fmt.Sprintf("%v/vault.json", d), 106 key: []byte("AES256Key-32Characters1234567890"), 107 } 108 s2 := &AESEncryptedStorage{ 109 filename: fmt.Sprintf("%v/vault.json", d), 110 key: []byte("AES256Key-32Characters1234567890"), 111 } 112 113 s1.Put("bazonk", "foobar") 114 if v, err := s2.Get("bazonk"); v != "foobar" || err != nil { 115 t.Errorf("Expected bazonk->foobar (nil error), got '%v' (%v error)", v, err) 116 } 117 } 118 119 func TestSwappedKeys(t *testing.T) { 120 // It should not be possible to swap the keys/values, so that 121 // K1:V1, K2:V2 can be swapped into K1:V2, K2:V1 122 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 123 124 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 s1 := &AESEncryptedStorage{ 130 filename: fmt.Sprintf("%v/vault.json", d), 131 key: []byte("AES256Key-32Characters1234567890"), 132 } 133 s1.Put("k1", "v1") 134 s1.Put("k2", "v2") 135 // Now make a modified copy 136 137 creds := make(map[string]storedCredential) 138 raw, err := ioutil.ReadFile(s1.filename) 139 if err != nil { 140 t.Fatal(err) 141 } 142 if err = json.Unmarshal(raw, &creds); err != nil { 143 t.Fatal(err) 144 } 145 swap := func() { 146 // Turn it into K1:V2, K2:V2 147 v1, v2 := creds["k1"], creds["k2"] 148 creds["k2"], creds["k1"] = v1, v2 149 raw, err = json.Marshal(creds) 150 if err != nil { 151 t.Fatal(err) 152 } 153 if err = ioutil.WriteFile(s1.filename, raw, 0600); err != nil { 154 t.Fatal(err) 155 } 156 } 157 swap() 158 if v, _ := s1.Get("k1"); v != "" { 159 t.Errorf("swapped value should return empty") 160 } 161 swap() 162 if v, _ := s1.Get("k1"); v != "v1" { 163 t.Errorf("double-swapped value should work fine") 164 } 165 }