]>
Commit | Line | Data |
---|---|---|
7c929cf5 JG |
1 | #include <gtest/gtest.h> |
2 | ||
3 | #include "keystore.h" | |
16d140f4 JG |
4 | #include "random.h" |
5 | #include "wallet/crypter.h" | |
7c929cf5 JG |
6 | #include "zcash/Address.hpp" |
7 | ||
8 | TEST(keystore_tests, store_and_retrieve_spending_key) { | |
9 | CBasicKeyStore keyStore; | |
04dfc3c5 | 10 | libzcash::SpendingKey skOut; |
7c929cf5 JG |
11 | |
12 | std::set<libzcash::PaymentAddress> addrs; | |
13 | keyStore.GetPaymentAddresses(addrs); | |
a4f4fa8f | 14 | EXPECT_EQ(0, addrs.size()); |
7c929cf5 JG |
15 | |
16 | auto sk = libzcash::SpendingKey::random(); | |
7c929cf5 | 17 | auto addr = sk.address(); |
7c929cf5 | 18 | |
04dfc3c5 | 19 | // Sanity-check: we can't get a key we haven't added |
a4f4fa8f JG |
20 | EXPECT_FALSE(keyStore.HaveSpendingKey(addr)); |
21 | EXPECT_FALSE(keyStore.GetSpendingKey(addr, skOut)); | |
04dfc3c5 JG |
22 | |
23 | keyStore.AddSpendingKey(sk); | |
a4f4fa8f JG |
24 | EXPECT_TRUE(keyStore.HaveSpendingKey(addr)); |
25 | EXPECT_TRUE(keyStore.GetSpendingKey(addr, skOut)); | |
26 | EXPECT_EQ(sk, skOut); | |
7c929cf5 JG |
27 | |
28 | keyStore.GetPaymentAddresses(addrs); | |
a4f4fa8f JG |
29 | EXPECT_EQ(1, addrs.size()); |
30 | EXPECT_EQ(1, addrs.count(addr)); | |
7c929cf5 | 31 | } |
be86b6c3 JG |
32 | |
33 | TEST(keystore_tests, store_and_retrieve_note_decryptor) { | |
34 | CBasicKeyStore keyStore; | |
35 | ZCNoteDecryption decOut; | |
36 | ||
37 | auto sk = libzcash::SpendingKey::random(); | |
38 | auto addr = sk.address(); | |
be86b6c3 | 39 | |
73db0c12 JG |
40 | EXPECT_FALSE(keyStore.GetNoteDecryptor(addr, decOut)); |
41 | ||
42 | keyStore.AddSpendingKey(sk); | |
43 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); | |
be86b6c3 JG |
44 | EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); |
45 | } | |
16d140f4 JG |
46 | |
47 | class TestCCryptoKeyStore : public CCryptoKeyStore | |
48 | { | |
49 | public: | |
50 | bool EncryptKeys(CKeyingMaterial& vMasterKeyIn) { return CCryptoKeyStore::EncryptKeys(vMasterKeyIn); } | |
51 | bool Unlock(const CKeyingMaterial& vMasterKeyIn) { return CCryptoKeyStore::Unlock(vMasterKeyIn); } | |
52 | }; | |
53 | ||
54 | TEST(keystore_tests, store_and_retrieve_spending_key_in_encrypted_store) { | |
55 | TestCCryptoKeyStore keyStore; | |
56 | uint256 r {GetRandHash()}; | |
57 | CKeyingMaterial vMasterKey (r.begin(), r.end()); | |
58 | libzcash::SpendingKey keyOut; | |
ad041fce | 59 | ZCNoteDecryption decOut; |
16d140f4 JG |
60 | std::set<libzcash::PaymentAddress> addrs; |
61 | ||
62 | // 1) Test adding a key to an unencrypted key store, then encrypting it | |
63 | auto sk = libzcash::SpendingKey::random(); | |
64 | auto addr = sk.address(); | |
ad041fce JG |
65 | EXPECT_FALSE(keyStore.GetNoteDecryptor(addr, decOut)); |
66 | ||
16d140f4 JG |
67 | keyStore.AddSpendingKey(sk); |
68 | ASSERT_TRUE(keyStore.HaveSpendingKey(addr)); | |
69 | ASSERT_TRUE(keyStore.GetSpendingKey(addr, keyOut)); | |
70 | ASSERT_EQ(sk, keyOut); | |
ad041fce JG |
71 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); |
72 | EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); | |
16d140f4 JG |
73 | |
74 | ASSERT_TRUE(keyStore.EncryptKeys(vMasterKey)); | |
75 | ASSERT_TRUE(keyStore.HaveSpendingKey(addr)); | |
76 | ASSERT_FALSE(keyStore.GetSpendingKey(addr, keyOut)); | |
ad041fce JG |
77 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); |
78 | EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); | |
16d140f4 | 79 | |
3bbf2c14 JG |
80 | // Unlocking with a random key should fail |
81 | uint256 r2 {GetRandHash()}; | |
82 | CKeyingMaterial vRandomKey (r2.begin(), r2.end()); | |
83 | EXPECT_FALSE(keyStore.Unlock(vRandomKey)); | |
84 | ||
85 | // Unlocking with a slightly-modified vMasterKey should fail | |
86 | CKeyingMaterial vModifiedKey (r.begin(), r.end()); | |
87 | vModifiedKey[0] += 1; | |
88 | EXPECT_FALSE(keyStore.Unlock(vModifiedKey)); | |
89 | ||
90 | // Unlocking with vMasterKey should succeed | |
16d140f4 JG |
91 | ASSERT_TRUE(keyStore.Unlock(vMasterKey)); |
92 | ASSERT_TRUE(keyStore.GetSpendingKey(addr, keyOut)); | |
93 | ASSERT_EQ(sk, keyOut); | |
94 | ||
95 | keyStore.GetPaymentAddresses(addrs); | |
96 | ASSERT_EQ(1, addrs.size()); | |
97 | ASSERT_EQ(1, addrs.count(addr)); | |
98 | ||
99 | // 2) Test adding a spending key to an already-encrypted key store | |
100 | auto sk2 = libzcash::SpendingKey::random(); | |
101 | auto addr2 = sk2.address(); | |
ad041fce JG |
102 | EXPECT_FALSE(keyStore.GetNoteDecryptor(addr2, decOut)); |
103 | ||
16d140f4 JG |
104 | keyStore.AddSpendingKey(sk2); |
105 | ASSERT_TRUE(keyStore.HaveSpendingKey(addr2)); | |
106 | ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut)); | |
107 | ASSERT_EQ(sk2, keyOut); | |
ad041fce JG |
108 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); |
109 | EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); | |
16d140f4 JG |
110 | |
111 | ASSERT_TRUE(keyStore.Lock()); | |
112 | ASSERT_TRUE(keyStore.HaveSpendingKey(addr2)); | |
113 | ASSERT_FALSE(keyStore.GetSpendingKey(addr2, keyOut)); | |
ad041fce JG |
114 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); |
115 | EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); | |
16d140f4 JG |
116 | |
117 | ASSERT_TRUE(keyStore.Unlock(vMasterKey)); | |
118 | ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut)); | |
119 | ASSERT_EQ(sk2, keyOut); | |
ad041fce JG |
120 | EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); |
121 | EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); | |
16d140f4 JG |
122 | |
123 | keyStore.GetPaymentAddresses(addrs); | |
124 | ASSERT_EQ(2, addrs.size()); | |
6ae516f1 | 125 | ASSERT_EQ(1, addrs.count(addr)); |
16d140f4 JG |
126 | ASSERT_EQ(1, addrs.count(addr2)); |
127 | } |