gitee.com/liu-zhao234568/cntest@v1.0.0/signer/storage/aes_gcm_storage_test.go (about)

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