| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <string.h> |
| 35 | #include <boost/filesystem.hpp> |
| 36 | |
| 37 | #include "gtest/gtest.h" |
| 38 | |
| 39 | #include "string_tools.h" |
| 40 | #include "crypto/crypto.h" |
| 41 | #include "crypto/random.h" |
| 42 | #include "crypto/chacha.h" |
| 43 | #include "ringct/rctOps.h" |
| 44 | #include "cryptonote_basic/cryptonote_basic.h" |
| 45 | #include "wallet/ringdb.h" |
| 46 | |
| 47 | static crypto::chacha_key generate_chacha_key() |
| 48 | { |
| 49 | crypto::chacha_key chacha_key; |
| 50 | uint64_t password = crypto::rand<uint64_t>(); |
| 51 | crypto::generate_chacha_key(std::string((const char*)&password, sizeof(password)), chacha_key, 1); |
| 52 | return chacha_key; |
| 53 | } |
| 54 | |
| 55 | static crypto::key_image generate_key_image() |
| 56 | { |
| 57 | crypto::key_image key_image; |
| 58 | cryptonote::keypair keypair = cryptonote::keypair::generate(hw::get_device("default")); |
| 59 | crypto::generate_key_image(keypair.pub, keypair.sec, key_image); |
| 60 | return key_image; |
| 61 | } |
| 62 | |
| 63 | static std::pair<uint64_t, uint64_t> generate_output() |
| 64 | { |
| 65 | return std::make_pair(rand(), rand()); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static crypto::chacha_key get_KEY_1() { static const crypto::chacha_key KEY_1 = generate_chacha_key(); return KEY_1; } |
| 70 | static crypto::chacha_key get_KEY_2() { static const crypto::chacha_key KEY_2 = generate_chacha_key(); return KEY_2; } |
| 71 | static crypto::key_image get_KEY_IMAGE_1() { static const crypto::key_image KEY_IMAGE_1 = generate_key_image(); return KEY_IMAGE_1; } |
| 72 | static std::pair<uint64_t, uint64_t> get_OUTPUT_1() { static const std::pair<uint64_t, uint64_t> OUTPUT_1 = generate_output(); return OUTPUT_1; } |
| 73 | static std::pair<uint64_t, uint64_t> get_OUTPUT_2() { static const std::pair<uint64_t, uint64_t> OUTPUT_2 = generate_output(); return OUTPUT_2; } |
| 74 | |
| 75 | #define KEY_1 get_KEY_1() |
| 76 | #define KEY_2 get_KEY_2() |
| 77 | #define KEY_IMAGE_1 get_KEY_IMAGE_1() |
| 78 | #define OUTPUT_1 get_OUTPUT_1() |
| 79 | #define OUTPUT_2 get_OUTPUT_2() |
| 80 | |
| 81 | class RingDB: public tools::ringdb |
| 82 | { |
| 83 | public: |
| 84 | RingDB(const char *genesis = ""): tools::ringdb(make_filename(), genesis) { } |
| 85 | ~RingDB() { close(); boost::filesystem::remove_all(filename); free(filename); } |
| 86 | |
| 87 | private: |
| 88 | std::string make_filename() |
| 89 | { |
| 90 | boost::filesystem::path path = |
| 91 | boost::filesystem::temp_directory_path(); |
| 92 | #if defined(__MINGW32__) || defined(__MINGW__) |
| 93 | filename = tempnam(path.string().c_str(), "monero-ringdb-test-"); |
| 94 | EXPECT_TRUE(filename != NULL); |
| 95 | #else |
| 96 | path /= "monero-ringdb-test-XXXXXX"; |
| 97 | filename = strdup(path.string().c_str()); |
| 98 | EXPECT_TRUE(mkdtemp(filename) != NULL); |
| 99 | #endif |
| 100 | return filename; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | char *filename; |
| 105 | }; |
| 106 | |
| 107 | TEST(ringdb, not_found) |
| 108 | { |
| 109 | RingDB ringdb; |
| 110 | std::vector<uint64_t> outs; |
| 111 | ASSERT_FALSE(ringdb.get_ring(KEY_1, KEY_IMAGE_1, outs)); |
| 112 | } |
| 113 | |
| 114 | TEST(ringdb, found) |
| 115 | { |
| 116 | RingDB ringdb; |
| 117 | std::vector<uint64_t> outs, outs2; |
| 118 | outs.push_back(43); outs.push_back(7320); outs.push_back(8429); |
| 119 | ASSERT_TRUE(ringdb.set_ring(KEY_1, KEY_IMAGE_1, outs, false)); |
| 120 | ASSERT_TRUE(ringdb.get_ring(KEY_1, KEY_IMAGE_1, outs2)); |
| 121 | ASSERT_EQ(outs, outs2); |
| 122 | } |
| 123 | |
| 124 | TEST(ringdb, convert) |
| 125 | { |
| 126 | RingDB ringdb; |
| 127 | std::vector<uint64_t> outs, outs2; |
| 128 | outs.push_back(43); outs.push_back(7320); outs.push_back(8429); |
| 129 | ASSERT_TRUE(ringdb.set_ring(KEY_1, KEY_IMAGE_1, outs, true)); |
| 130 | ASSERT_TRUE(ringdb.get_ring(KEY_1, KEY_IMAGE_1, outs2)); |
| 131 | ASSERT_EQ(outs2.size(), 3); |
| 132 | ASSERT_EQ(outs2[0], 43); |
| 133 | ASSERT_EQ(outs2[1], 43+7320); |
| 134 | ASSERT_EQ(outs2[2], 43+7320+8429); |
| 135 | } |
| 136 | |
| 137 | TEST(ringdb, different_genesis) |
| 138 | { |
| 139 | RingDB ringdb; |
| 140 | std::vector<uint64_t> outs, outs2; |
| 141 | outs.push_back(43); outs.push_back(7320); outs.push_back(8429); |
| 142 | ASSERT_TRUE(ringdb.set_ring(KEY_1, KEY_IMAGE_1, outs, false)); |
| 143 | ASSERT_FALSE(ringdb.get_ring(KEY_2, KEY_IMAGE_1, outs2)); |
| 144 | } |
| 145 | |
| 146 | TEST(spent_outputs, not_found) |
| 147 | { |
| 148 | RingDB ringdb; |
| 149 | ASSERT_TRUE(ringdb.blackball(OUTPUT_1)); |
| 150 | ASSERT_FALSE(ringdb.blackballed(OUTPUT_2)); |
| 151 | } |
| 152 | |
| 153 | TEST(spent_outputs, found) |
| 154 | { |
| 155 | RingDB ringdb; |
| 156 | ASSERT_TRUE(ringdb.blackball(OUTPUT_1)); |
| 157 | ASSERT_TRUE(ringdb.blackballed(OUTPUT_1)); |
| 158 | } |
| 159 | |
| 160 | TEST(spent_outputs, vector) |
| 161 | { |
| 162 | RingDB ringdb; |
| 163 | std::vector<std::pair<uint64_t, uint64_t>> outputs; |
| 164 | outputs.push_back(std::make_pair(0, 1)); |
| 165 | outputs.push_back(std::make_pair(10, 3)); |
| 166 | outputs.push_back(std::make_pair(10, 4)); |
| 167 | outputs.push_back(std::make_pair(10, 8)); |
| 168 | outputs.push_back(std::make_pair(20, 0)); |
| 169 | outputs.push_back(std::make_pair(20, 1)); |
| 170 | outputs.push_back(std::make_pair(30, 5)); |
| 171 | ASSERT_TRUE(ringdb.blackball(outputs)); |
| 172 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(0, 1))); |
| 173 | ASSERT_FALSE(ringdb.blackballed(std::make_pair(10, 2))); |
| 174 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(10, 3))); |
| 175 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(10, 4))); |
| 176 | ASSERT_FALSE(ringdb.blackballed(std::make_pair(10, 5))); |
| 177 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(10, 8))); |
| 178 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(20, 0))); |
| 179 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(20, 1))); |
| 180 | ASSERT_FALSE(ringdb.blackballed(std::make_pair(20, 2))); |
| 181 | ASSERT_TRUE(ringdb.blackballed(std::make_pair(30, 5))); |
| 182 | } |
| 183 | |
| 184 | TEST(spent_outputs, mark_as_unspent) |
| 185 | { |
| 186 | RingDB ringdb; |
| 187 | ASSERT_TRUE(ringdb.blackball(OUTPUT_1)); |
| 188 | ASSERT_TRUE(ringdb.unblackball(OUTPUT_1)); |
| 189 | ASSERT_FALSE(ringdb.blackballed(OUTPUT_1)); |
| 190 | } |
| 191 | |
| 192 | TEST(spent_outputs, clear) |
| 193 | { |
| 194 | RingDB ringdb; |
| 195 | ASSERT_TRUE(ringdb.blackball(OUTPUT_1)); |
| 196 | ASSERT_TRUE(ringdb.clear_blackballs()); |
| 197 | ASSERT_FALSE(ringdb.blackballed(OUTPUT_1)); |
| 198 | } |
| 199 |