aes_gcm_storage_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. package storage
  17. import (
  18. "bytes"
  19. "encoding/json"
  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, nil)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. t.Logf("Ciphertext %x, nonce %x\n", c, iv)
  37. p, err := decrypt(key, iv, c, nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. t.Logf("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, err := s2.Get("bazonk"); v != "foobar" || err != nil {
  103. t.Errorf("Expected bazonk->foobar (nil error), got '%v' (%v error)", v, err)
  104. }
  105. }
  106. func TestSwappedKeys(t *testing.T) {
  107. // It should not be possible to swap the keys/values, so that
  108. // K1:V1, K2:V2 can be swapped into K1:V2, K2:V1
  109. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
  110. d, err := ioutil.TempDir("", "eth-encrypted-storage-test")
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. s1 := &AESEncryptedStorage{
  115. filename: fmt.Sprintf("%v/vault.json", d),
  116. key: []byte("AES256Key-32Characters1234567890"),
  117. }
  118. s1.Put("k1", "v1")
  119. s1.Put("k2", "v2")
  120. // Now make a modified copy
  121. creds := make(map[string]storedCredential)
  122. raw, err := ioutil.ReadFile(s1.filename)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if err = json.Unmarshal(raw, &creds); err != nil {
  127. t.Fatal(err)
  128. }
  129. swap := func() {
  130. // Turn it into K1:V2, K2:V2
  131. v1, v2 := creds["k1"], creds["k2"]
  132. creds["k2"], creds["k1"] = v1, v2
  133. raw, err = json.Marshal(creds)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if err = ioutil.WriteFile(s1.filename, raw, 0600); err != nil {
  138. t.Fatal(err)
  139. }
  140. }
  141. swap()
  142. if v, _ := s1.Get("k1"); v != "" {
  143. t.Errorf("swapped value should return empty")
  144. }
  145. swap()
  146. if v, _ := s1.Get("k1"); v != "v1" {
  147. t.Errorf("double-swapped value should work fine")
  148. }
  149. }