github.com/truechain/truechain-fpow@v1.8.11/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  //
    17  package storage
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/log"
    27  	"github.com/mattn/go-colorable"
    28  )
    29  
    30  func TestEncryption(t *testing.T) {
    31  	//	key := []byte("AES256Key-32Characters1234567890")
    32  	//	plaintext := []byte(value)
    33  	key := []byte("AES256Key-32Characters1234567890")
    34  	plaintext := []byte("exampleplaintext")
    35  
    36  	c, iv, err := encrypt(key, plaintext)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	fmt.Printf("Ciphertext %x, nonce %x\n", c, iv)
    41  
    42  	p, err := decrypt(key, iv, c)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	fmt.Printf("Plaintext %v\n", string(p))
    47  	if !bytes.Equal(plaintext, p) {
    48  		t.Errorf("Failed: expected plaintext recovery, got %v expected %v", string(plaintext), string(p))
    49  	}
    50  }
    51  
    52  func TestFileStorage(t *testing.T) {
    53  
    54  	a := map[string]storedCredential{
    55  		"secret": {
    56  			Iv:         common.Hex2Bytes("cdb30036279601aeee60f16b"),
    57  			CipherText: common.Hex2Bytes("f311ac49859d7260c2c464c28ffac122daf6be801d3cfd3edcbde7e00c9ff74f"),
    58  		},
    59  		"secret2": {
    60  			Iv:         common.Hex2Bytes("afb8a7579bf971db9f8ceeed"),
    61  			CipherText: common.Hex2Bytes("2df87baf86b5073ef1f03e3cc738de75b511400f5465bb0ddeacf47ae4dc267d"),
    62  		},
    63  	}
    64  	d, err := ioutil.TempDir("", "eth-encrypted-storage-test")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	stored := &AESEncryptedStorage{
    69  		filename: fmt.Sprintf("%v/vault.json", d),
    70  		key:      []byte("AES256Key-32Characters1234567890"),
    71  	}
    72  	stored.writeEncryptedStorage(a)
    73  	read := &AESEncryptedStorage{
    74  		filename: fmt.Sprintf("%v/vault.json", d),
    75  		key:      []byte("AES256Key-32Characters1234567890"),
    76  	}
    77  	creds, err := read.readEncryptedStorage()
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	for k, v := range a {
    82  		if v2, exist := creds[k]; !exist {
    83  			t.Errorf("Missing entry %v", k)
    84  		} else {
    85  			if !bytes.Equal(v.CipherText, v2.CipherText) {
    86  				t.Errorf("Wrong ciphertext, expected %x got %x", v.CipherText, v2.CipherText)
    87  			}
    88  			if !bytes.Equal(v.Iv, v2.Iv) {
    89  				t.Errorf("Wrong iv")
    90  			}
    91  		}
    92  	}
    93  }
    94  func TestEnd2End(t *testing.T) {
    95  	log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
    96  
    97  	d, err := ioutil.TempDir("", "eth-encrypted-storage-test")
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	s1 := &AESEncryptedStorage{
   103  		filename: fmt.Sprintf("%v/vault.json", d),
   104  		key:      []byte("AES256Key-32Characters1234567890"),
   105  	}
   106  	s2 := &AESEncryptedStorage{
   107  		filename: fmt.Sprintf("%v/vault.json", d),
   108  		key:      []byte("AES256Key-32Characters1234567890"),
   109  	}
   110  
   111  	s1.Put("bazonk", "foobar")
   112  	if v := s2.Get("bazonk"); v != "foobar" {
   113  		t.Errorf("Expected bazonk->foobar, got '%v'", v)
   114  	}
   115  }