aes_gcm_storage_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "os"
  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 := t.TempDir()
  58. stored := &AESEncryptedStorage{
  59. filename: fmt.Sprintf("%v/vault.json", d),
  60. key: []byte("AES256Key-32Characters1234567890"),
  61. }
  62. stored.writeEncryptedStorage(a)
  63. read := &AESEncryptedStorage{
  64. filename: fmt.Sprintf("%v/vault.json", d),
  65. key: []byte("AES256Key-32Characters1234567890"),
  66. }
  67. creds, err := read.readEncryptedStorage()
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. for k, v := range a {
  72. if v2, exist := creds[k]; !exist {
  73. t.Errorf("Missing entry %v", k)
  74. } else {
  75. if !bytes.Equal(v.CipherText, v2.CipherText) {
  76. t.Errorf("Wrong ciphertext, expected %x got %x", v.CipherText, v2.CipherText)
  77. }
  78. if !bytes.Equal(v.Iv, v2.Iv) {
  79. t.Errorf("Wrong iv")
  80. }
  81. }
  82. }
  83. }
  84. func TestEnd2End(t *testing.T) {
  85. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
  86. d := t.TempDir()
  87. s1 := &AESEncryptedStorage{
  88. filename: fmt.Sprintf("%v/vault.json", d),
  89. key: []byte("AES256Key-32Characters1234567890"),
  90. }
  91. s2 := &AESEncryptedStorage{
  92. filename: fmt.Sprintf("%v/vault.json", d),
  93. key: []byte("AES256Key-32Characters1234567890"),
  94. }
  95. s1.Put("bazonk", "foobar")
  96. if v, err := s2.Get("bazonk"); v != "foobar" || err != nil {
  97. t.Errorf("Expected bazonk->foobar (nil error), got '%v' (%v error)", v, err)
  98. }
  99. }
  100. func TestSwappedKeys(t *testing.T) {
  101. // It should not be possible to swap the keys/values, so that
  102. // K1:V1, K2:V2 can be swapped into K1:V2, K2:V1
  103. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
  104. d := t.TempDir()
  105. s1 := &AESEncryptedStorage{
  106. filename: fmt.Sprintf("%v/vault.json", d),
  107. key: []byte("AES256Key-32Characters1234567890"),
  108. }
  109. s1.Put("k1", "v1")
  110. s1.Put("k2", "v2")
  111. // Now make a modified copy
  112. creds := make(map[string]storedCredential)
  113. raw, err := os.ReadFile(s1.filename)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if err = json.Unmarshal(raw, &creds); err != nil {
  118. t.Fatal(err)
  119. }
  120. swap := func() {
  121. // Turn it into K1:V2, K2:V2
  122. v1, v2 := creds["k1"], creds["k2"]
  123. creds["k2"], creds["k1"] = v1, v2
  124. raw, err = json.Marshal(creds)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. if err = os.WriteFile(s1.filename, raw, 0600); err != nil {
  129. t.Fatal(err)
  130. }
  131. }
  132. swap()
  133. if v, _ := s1.Get("k1"); v != "" {
  134. t.Errorf("swapped value should return empty")
  135. }
  136. swap()
  137. if v, _ := s1.Get("k1"); v != "v1" {
  138. t.Errorf("double-swapped value should work fine")
  139. }
  140. }