github.com/luckypickle/go-ethereum-vet@v1.14.2/signer/storage/aes_gcm_storage_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 package storage 17 18 import ( 19 "bytes" 20 "fmt" 21 "io/ioutil" 22 "testing" 23 24 "github.com/luckypickle/go-ethereum-vet/common" 25 "github.com/luckypickle/go-ethereum-vet/log" 26 "github.com/mattn/go-colorable" 27 ) 28 29 func TestEncryption(t *testing.T) { 30 // key := []byte("AES256Key-32Characters1234567890") 31 // plaintext := []byte(value) 32 key := []byte("AES256Key-32Characters1234567890") 33 plaintext := []byte("exampleplaintext") 34 35 c, iv, err := encrypt(key, plaintext) 36 if err != nil { 37 t.Fatal(err) 38 } 39 fmt.Printf("Ciphertext %x, nonce %x\n", c, iv) 40 41 p, err := decrypt(key, iv, c) 42 if err != nil { 43 t.Fatal(err) 44 } 45 fmt.Printf("Plaintext %v\n", string(p)) 46 if !bytes.Equal(plaintext, p) { 47 t.Errorf("Failed: expected plaintext recovery, got %v expected %v", string(plaintext), string(p)) 48 } 49 } 50 51 func TestFileStorage(t *testing.T) { 52 53 a := map[string]storedCredential{ 54 "secret": { 55 Iv: common.Hex2Bytes("cdb30036279601aeee60f16b"), 56 CipherText: common.Hex2Bytes("f311ac49859d7260c2c464c28ffac122daf6be801d3cfd3edcbde7e00c9ff74f"), 57 }, 58 "secret2": { 59 Iv: common.Hex2Bytes("afb8a7579bf971db9f8ceeed"), 60 CipherText: common.Hex2Bytes("2df87baf86b5073ef1f03e3cc738de75b511400f5465bb0ddeacf47ae4dc267d"), 61 }, 62 } 63 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 64 if err != nil { 65 t.Fatal(err) 66 } 67 stored := &AESEncryptedStorage{ 68 filename: fmt.Sprintf("%v/vault.json", d), 69 key: []byte("AES256Key-32Characters1234567890"), 70 } 71 stored.writeEncryptedStorage(a) 72 read := &AESEncryptedStorage{ 73 filename: fmt.Sprintf("%v/vault.json", d), 74 key: []byte("AES256Key-32Characters1234567890"), 75 } 76 creds, err := read.readEncryptedStorage() 77 if err != nil { 78 t.Fatal(err) 79 } 80 for k, v := range a { 81 if v2, exist := creds[k]; !exist { 82 t.Errorf("Missing entry %v", k) 83 } else { 84 if !bytes.Equal(v.CipherText, v2.CipherText) { 85 t.Errorf("Wrong ciphertext, expected %x got %x", v.CipherText, v2.CipherText) 86 } 87 if !bytes.Equal(v.Iv, v2.Iv) { 88 t.Errorf("Wrong iv") 89 } 90 } 91 } 92 } 93 func TestEnd2End(t *testing.T) { 94 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 95 96 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 97 if err != nil { 98 t.Fatal(err) 99 } 100 101 s1 := &AESEncryptedStorage{ 102 filename: fmt.Sprintf("%v/vault.json", d), 103 key: []byte("AES256Key-32Characters1234567890"), 104 } 105 s2 := &AESEncryptedStorage{ 106 filename: fmt.Sprintf("%v/vault.json", d), 107 key: []byte("AES256Key-32Characters1234567890"), 108 } 109 110 s1.Put("bazonk", "foobar") 111 if v := s2.Get("bazonk"); v != "foobar" { 112 t.Errorf("Expected bazonk->foobar, got '%v'", v) 113 } 114 }