aes_gcm_storage_test.go 3.3 KB

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