aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/CMakeLists.txt7
-rw-r--r--tests/unit_tests/multisig.cpp179
-rw-r--r--tests/unit_tests/notify.cpp57
-rw-r--r--tests/unit_tests/test_notifier.cpp54
4 files changed, 192 insertions, 105 deletions
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt
index d6bcbde46..e248ed965 100644
--- a/tests/unit_tests/CMakeLists.txt
+++ b/tests/unit_tests/CMakeLists.txt
@@ -61,6 +61,7 @@ set(unit_tests_sources
mul_div.cpp
multiexp.cpp
multisig.cpp
+ notify.cpp
parse_amount.cpp
random.cpp
serialization.cpp
@@ -122,4 +123,8 @@ SET_PROPERTY(SOURCE memwipe.cpp PROPERTY COMPILE_FLAGS -Ofast)
add_test(
NAME unit_tests
- COMMAND unit_tests --data-dir "${TEST_DATA_DIR}")
+ COMMAND unit_tests --data-dir "${TEST_DATA_DIR} --binary-dir ${CMAKE_BINARY_DIR}")
+
+add_executable(test_notifier test_notifier.cpp)
+target_link_libraries(test_notifier ${EXTRA_LIBRARIES})
+set_property(TARGET test_notifier PROPERTY FOLDER "tests")
diff --git a/tests/unit_tests/multisig.cpp b/tests/unit_tests/multisig.cpp
index 83924c7af..7268f2690 100644
--- a/tests/unit_tests/multisig.cpp
+++ b/tests/unit_tests/multisig.cpp
@@ -49,9 +49,19 @@ static const struct
{
"9t6Hn946u3eah5cuncH1hB5hGzsTUoevtf4SY7MHN5NgJZh2SFWsyVt3vUhuHyRKyrCQvr71Lfc1AevG3BXE11PQFoXDtD8",
"bbd3175ef9fd9f5eefdc43035f882f74ad14c4cf1799d8b6f9001bc197175d02"
+ },
+ {
+ "9zmAWoNyNPbgnYSm3nJNpAKHm6fCcs3MR94gBWxp9MCDUiMUhyYFfyQETUDLPF7DP6ZsmNo6LRxwPP9VmhHNxKrER9oGigT",
+ "f2efae45bef1917a7430cda8fcffc4ee010e3178761aa41d4628e23b1fe2d501"
+ },
+ {
+ "9ue8NJMg3WzKxTtmjeXzWYF5KmU6dC7LHEt9wvYdPn2qMmoFUa8hJJHhSHvJ46UEwpDyy5jSboNMRaDBKwU54NT42YcNUp5",
+ "a4cef54ed3fd61cd78a2ceb82ecf85a903ad2db9a86fb77ff56c35c56016280a"
}
};
+static const size_t KEYS_COUNT = 5;
+
static void make_wallet(unsigned int idx, tools::wallet2 &wallet)
{
ASSERT_TRUE(idx < sizeof(test_addresses) / sizeof(test_addresses[0]));
@@ -76,126 +86,87 @@ static void make_wallet(unsigned int idx, tools::wallet2 &wallet)
}
}
-static void make_M_2_wallet(tools::wallet2 &wallet0, tools::wallet2 &wallet1, unsigned int M)
+static std::vector<std::string> exchange_round(std::vector<tools::wallet2>& wallets, const std::vector<std::string>& mis)
{
- ASSERT_TRUE(M <= 2);
-
- make_wallet(0, wallet0);
- make_wallet(1, wallet1);
-
- std::vector<crypto::secret_key> sk0(1), sk1(1);
- std::vector<crypto::public_key> pk0(1), pk1(1);
-
- wallet0.decrypt_keys("");
- std::string mi0 = wallet0.get_multisig_info();
- wallet0.encrypt_keys("");
- wallet1.decrypt_keys("");
- std::string mi1 = wallet1.get_multisig_info();
- wallet1.encrypt_keys("");
-
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk0[0], pk0[0]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk1[0], pk1[0]));
-
- ASSERT_FALSE(wallet0.multisig() || wallet1.multisig());
- wallet0.make_multisig("", sk0, pk0, M);
- wallet1.make_multisig("", sk1, pk1, M);
-
- ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet1.get_account().get_public_address_str(cryptonote::TESTNET));
-
- bool ready;
- uint32_t threshold, total;
- ASSERT_TRUE(wallet0.multisig(&ready, &threshold, &total));
- ASSERT_TRUE(ready);
- ASSERT_TRUE(threshold == M);
- ASSERT_TRUE(total == 2);
- ASSERT_TRUE(wallet1.multisig(&ready, &threshold, &total));
- ASSERT_TRUE(ready);
- ASSERT_TRUE(threshold == M);
- ASSERT_TRUE(total == 2);
+ std::vector<std::string> new_infos;
+ for (size_t i = 0; i < wallets.size(); ++i) {
+ new_infos.push_back(wallets[i].exchange_multisig_keys("", mis));
+ }
+
+ return new_infos;
}
-static void make_M_3_wallet(tools::wallet2 &wallet0, tools::wallet2 &wallet1, tools::wallet2 &wallet2, unsigned int M)
+static void make_wallets(std::vector<tools::wallet2>& wallets, unsigned int M)
{
- ASSERT_TRUE(M <= 3);
-
- make_wallet(0, wallet0);
- make_wallet(1, wallet1);
- make_wallet(2, wallet2);
-
- std::vector<crypto::secret_key> sk0(2), sk1(2), sk2(2);
- std::vector<crypto::public_key> pk0(2), pk1(2), pk2(2);
-
- wallet0.decrypt_keys("");
- std::string mi0 = wallet0.get_multisig_info();
- wallet0.encrypt_keys("");
- wallet1.decrypt_keys("");
- std::string mi1 = wallet1.get_multisig_info();
- wallet1.encrypt_keys("");
- wallet2.decrypt_keys("");
- std::string mi2 = wallet2.get_multisig_info();
- wallet2.encrypt_keys("");
-
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk0[0], pk0[0]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi2, sk0[1], pk0[1]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk1[0], pk1[0]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi2, sk1[1], pk1[1]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk2[0], pk2[0]));
- ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk2[1], pk2[1]));
-
- ASSERT_FALSE(wallet0.multisig() || wallet1.multisig() || wallet2.multisig());
- std::string mxi0 = wallet0.make_multisig("", sk0, pk0, M);
- std::string mxi1 = wallet1.make_multisig("", sk1, pk1, M);
- std::string mxi2 = wallet2.make_multisig("", sk2, pk2, M);
-
- const size_t nset = !mxi0.empty() + !mxi1.empty() + !mxi2.empty();
- ASSERT_TRUE((M < 3 && nset == 3) || (M == 3 && nset == 0));
-
- if (nset > 0)
- {
- std::unordered_set<crypto::public_key> pkeys;
- std::vector<crypto::public_key> signers(3, crypto::null_pkey);
- ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi0, pkeys, signers[0]));
- ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi1, pkeys, signers[1]));
- ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi2, pkeys, signers[2]));
- ASSERT_TRUE(pkeys.size() == 3);
- ASSERT_TRUE(wallet0.finalize_multisig("", pkeys, signers));
- ASSERT_TRUE(wallet1.finalize_multisig("", pkeys, signers));
- ASSERT_TRUE(wallet2.finalize_multisig("", pkeys, signers));
+ ASSERT_TRUE(wallets.size() > 1 && wallets.size() <= KEYS_COUNT);
+ ASSERT_TRUE(M <= wallets.size());
+
+ std::vector<std::string> mis(wallets.size());
+
+ for (size_t i = 0; i < wallets.size(); ++i) {
+ make_wallet(i, wallets[i]);
+
+ wallets[i].decrypt_keys("");
+ mis[i] = wallets[i].get_multisig_info();
+ wallets[i].encrypt_keys("");
+ }
+
+ for (auto& wallet: wallets) {
+ ASSERT_FALSE(wallet.multisig() || wallet.multisig() || wallet.multisig());
+ }
+
+ std::vector<std::string> mxis;
+ for (size_t i = 0; i < wallets.size(); ++i) {
+ // it's ok to put all of multisig keys in this function. it throws in case of error
+ mxis.push_back(wallets[i].make_multisig("", mis, M));
}
- ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet1.get_account().get_public_address_str(cryptonote::TESTNET));
- ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet2.get_account().get_public_address_str(cryptonote::TESTNET));
-
- bool ready;
- uint32_t threshold, total;
- ASSERT_TRUE(wallet0.multisig(&ready, &threshold, &total));
- ASSERT_TRUE(ready);
- ASSERT_TRUE(threshold == M);
- ASSERT_TRUE(total == 3);
- ASSERT_TRUE(wallet1.multisig(&ready, &threshold, &total));
- ASSERT_TRUE(ready);
- ASSERT_TRUE(threshold == M);
- ASSERT_TRUE(total == 3);
- ASSERT_TRUE(wallet2.multisig(&ready, &threshold, &total));
- ASSERT_TRUE(ready);
- ASSERT_TRUE(threshold == M);
- ASSERT_TRUE(total == 3);
+ while (!mxis[0].empty()) {
+ mxis = exchange_round(wallets, mxis);
+ }
+
+ for (size_t i = 0; i < wallets.size(); ++i) {
+ ASSERT_TRUE(mxis[i].empty());
+ bool ready;
+ uint32_t threshold, total;
+ ASSERT_TRUE(wallets[i].multisig(&ready, &threshold, &total));
+ ASSERT_TRUE(ready);
+ ASSERT_TRUE(threshold == M);
+ ASSERT_TRUE(total == wallets.size());
+
+ if (i != 0) {
+ // "equals" is transitive relation so we need only to compare first wallet's address to each others' addresses. no need to compare 0's address with itself.
+ ASSERT_TRUE(wallets[0].get_account().get_public_address_str(cryptonote::TESTNET) == wallets[i].get_account().get_public_address_str(cryptonote::TESTNET));
+ }
+ }
}
TEST(multisig, make_2_2)
{
- tools::wallet2 wallet0, wallet1;
- make_M_2_wallet(wallet0, wallet1, 2);
+ std::vector<tools::wallet2> wallets(2);
+ make_wallets(wallets, 2);
}
TEST(multisig, make_3_3)
{
- tools::wallet2 wallet0, wallet1, wallet2;
- make_M_3_wallet(wallet0, wallet1, wallet2, 3);
+ std::vector<tools::wallet2> wallets(3);
+ make_wallets(wallets, 3);
}
TEST(multisig, make_2_3)
{
- tools::wallet2 wallet0, wallet1, wallet2;
- make_M_3_wallet(wallet0, wallet1, wallet2, 2);
+ std::vector<tools::wallet2> wallets(3);
+ make_wallets(wallets, 2);
+}
+
+TEST(multisig, make_2_4)
+{
+ std::vector<tools::wallet2> wallets(4);
+ make_wallets(wallets, 2);
+}
+
+TEST(multisig, make_2_5)
+{
+ std::vector<tools::wallet2> wallets(5);
+ make_wallets(wallets, 2);
}
diff --git a/tests/unit_tests/notify.cpp b/tests/unit_tests/notify.cpp
new file mode 100644
index 000000000..d6811c6bd
--- /dev/null
+++ b/tests/unit_tests/notify.cpp
@@ -0,0 +1,57 @@
+// Copyright (c) 2018, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "gtest/gtest.h"
+
+#include <boost/filesystem.hpp>
+
+#include "misc_language.h"
+#include "string_tools.h"
+#include "file_io_utils.h"
+#include "common/notify.h"
+
+TEST(notify, works)
+{
+ char name_template[] = "/tmp/monero-notify-unit-test-XXXXXX";
+ int fd = mkstemp(name_template);
+ ASSERT_TRUE(fd >= 0);
+ close(fd);
+
+ const std::string spec = epee::string_tools::get_current_module_folder() + "/test_notifier " + name_template + " %s";
+
+ tools::Notify notify(spec.c_str());
+ notify.notify("1111111111111111111111111111111111111111111111111111111111111111");
+
+ epee::misc_utils::sleep_no_w(100);
+
+ std::string s;
+ ASSERT_TRUE(epee::file_io_utils::load_file_to_string(name_template, s));
+ ASSERT_TRUE(s == "1111111111111111111111111111111111111111111111111111111111111111");
+
+ boost::filesystem::remove(name_template);
+}
diff --git a/tests/unit_tests/test_notifier.cpp b/tests/unit_tests/test_notifier.cpp
new file mode 100644
index 000000000..7fd9809c5
--- /dev/null
+++ b/tests/unit_tests/test_notifier.cpp
@@ -0,0 +1,54 @@
+// Copyright (c) 2018, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+int main(int argc, char **argv)
+{
+ if (argc < 3)
+ {
+ fprintf(stderr, "usage: %s <filename> <hash>\n", argv[0]);
+ return 1;
+ }
+ const char *filename = argv[1];
+ const char *hash = argv[2];
+
+ FILE *f = fopen(filename, "a+");
+ if (!f)
+ {
+ fprintf(stderr, "error opening file %s: %s\n", filename, strerror(errno));
+ return 1;
+ }
+ fprintf(f, "%s", hash);
+ fclose(f);
+
+ return 0;
+}