aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt2
-rw-r--r--tests/unit_tests/mnemonics.cpp154
-rw-r--r--tests/unit_tests/slow_memmem.cpp130
3 files changed, 285 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 3fda229b6..533bf8c58 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -70,7 +70,7 @@ target_link_libraries(functional_tests cryptonote_core wallet common crypto ${UN
target_link_libraries(hash-tests crypto)
target_link_libraries(hash-target-tests crypto cryptonote_core)
target_link_libraries(performance_tests cryptonote_core common crypto ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES})
-target_link_libraries(unit_tests gtest_main cryptonote_core wallet crypto common ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES})
+target_link_libraries(unit_tests gtest_main rpc cryptonote_core wallet crypto common ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES})
target_link_libraries(net_load_tests_clt cryptonote_core common crypto gtest_main ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES})
target_link_libraries(net_load_tests_srv cryptonote_core common crypto gtest_main ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES})
diff --git a/tests/unit_tests/mnemonics.cpp b/tests/unit_tests/mnemonics.cpp
new file mode 100644
index 000000000..d17bb7f85
--- /dev/null
+++ b/tests/unit_tests/mnemonics.cpp
@@ -0,0 +1,154 @@
+// Copyright (c) 2014, 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 "mnemonics/electrum-words.h"
+#include "crypto/crypto.h"
+#include <stdlib.h>
+#include <vector>
+#include <time.h>
+#include <iostream>
+#include <boost/algorithm/string.hpp>
+#include "mnemonics/english.h"
+#include "mnemonics/spanish.h"
+#include "mnemonics/portuguese.h"
+#include "mnemonics/japanese.h"
+#include "mnemonics/old_english.h"
+#include "mnemonics/language_base.h"
+#include "mnemonics/singleton.h"
+
+namespace
+{
+ /*!
+ * \brief Returns random index from 0 to max-1
+ * \param max Range maximum
+ * \return required random index
+ */
+ uint32_t get_random_index(int max)
+ {
+ return rand() % max;
+ }
+
+ /*!
+ * \brief Compares vectors for equality
+ * \param expected expected vector
+ * \param present current vector
+ */
+ void compare_vectors(const std::vector<std::string> &expected, const std::vector<std::string> &present)
+ {
+ std::vector<std::string>::const_iterator it1, it2;
+ for (it1 = expected.begin(), it2 = present.begin(); it1 != expected.end() && it2 != present.end();
+ it1++, it2++)
+ {
+ ASSERT_STREQ(it1->c_str(), it2->c_str());
+ }
+ }
+
+ /*!
+ * \brief Tests the given language mnemonics.
+ * \param language A Language instance to test
+ */
+ void test_language(const Language::Base &language)
+ {
+ const std::vector<std::string> &word_list = language.get_word_list();
+ std::string seed = "", return_seed = "";
+ // Generate a random seed without checksum
+ for (int ii = 0; ii < crypto::ElectrumWords::seed_length; ii++)
+ {
+ seed += (word_list[get_random_index(word_list.size())] + ' ');
+ }
+ seed.pop_back();
+ std::cout << "Test seed without checksum:\n";
+ std::cout << seed << std::endl;
+
+ crypto::secret_key key;
+ std::string language_name;
+ bool res;
+ std::vector<std::string> seed_vector, return_seed_vector;
+ std::string checksum_word;
+
+ // Convert it to secret key
+ res = crypto::ElectrumWords::words_to_bytes(seed, key, language_name);
+ ASSERT_EQ(true, res);
+ std::cout << "Detected language: " << language_name << std::endl;
+ ASSERT_STREQ(language.get_language_name().c_str(), language_name.c_str());
+
+ // Convert the secret key back to seed
+ crypto::ElectrumWords::bytes_to_words(key, return_seed, language.get_language_name());
+ ASSERT_EQ(true, res);
+ std::cout << "Returned seed:\n";
+ std::cout << return_seed << std::endl;
+ boost::split(seed_vector, seed, boost::is_any_of(" "));
+ boost::split(return_seed_vector, return_seed, boost::is_any_of(" "));
+
+ // Extract the checksum word
+ checksum_word = return_seed_vector.back();
+ return_seed_vector.pop_back();
+ ASSERT_EQ(seed_vector.size(), return_seed_vector.size());
+ // Ensure that the rest of it is same
+ compare_vectors(seed_vector, return_seed_vector);
+
+ // Append the checksum word to repeat the entire process with a seed with checksum
+ seed += (" " + checksum_word);
+ std::cout << "Test seed with checksum:\n";
+ std::cout << seed << std::endl;
+ res = crypto::ElectrumWords::words_to_bytes(seed, key, language_name);
+ ASSERT_EQ(true, res);
+ std::cout << "Detected language: " << language_name << std::endl;
+ ASSERT_STREQ(language.get_language_name().c_str(), language_name.c_str());
+
+ return_seed = "";
+ crypto::ElectrumWords::bytes_to_words(key, return_seed, language.get_language_name());
+ ASSERT_EQ(true, res);
+ std::cout << "Returned seed:\n";
+ std::cout << return_seed << std::endl;
+
+ seed_vector.clear();
+ return_seed_vector.clear();
+ boost::split(seed_vector, seed, boost::is_any_of(" "));
+ boost::split(return_seed_vector, return_seed, boost::is_any_of(" "));
+ ASSERT_EQ(seed_vector.size(), return_seed_vector.size());
+ compare_vectors(seed_vector, return_seed_vector);
+ }
+}
+
+TEST(mnemonics, all_languages)
+{
+ srand(time(NULL));
+ std::vector<Language::Base*> languages({
+ Language::Singleton<Language::English>::instance(),
+ Language::Singleton<Language::Spanish>::instance(),
+ Language::Singleton<Language::Portuguese>::instance(),
+ Language::Singleton<Language::Japanese>::instance(),
+ });
+
+ for (std::vector<Language::Base*>::iterator it = languages.begin(); it != languages.end(); it++)
+ {
+ test_language(*(*it));
+ }
+}
diff --git a/tests/unit_tests/slow_memmem.cpp b/tests/unit_tests/slow_memmem.cpp
new file mode 100644
index 000000000..2613209f8
--- /dev/null
+++ b/tests/unit_tests/slow_memmem.cpp
@@ -0,0 +1,130 @@
+// Copyright (c) 2014, 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.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdint.h>
+#include "gtest/gtest.h"
+
+// Both OS X and FreeBSD don't need malloc.h
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+ #include <malloc.h>
+#endif
+
+//#define TEST_ORIGINAL
+//#define VERBOSE
+
+#ifdef TEST_ORIGINAL
+uint64_t slow_memmem_original(void* start_buff, size_t buflen,void* pat,size_t patlen)
+{
+ void* buf = start_buff;
+ void* end=(char*)buf+buflen-patlen;
+ while((buf=memchr(buf,((char*)pat)[0],buflen)))
+ {
+ if(buf>end)
+ return 0;
+ if(memcmp(buf,pat,patlen)==0)
+ return (char*)buf - (char*)start_buff;
+ buf=(char*)buf+1;
+ }
+ return 0;
+}
+
+#define slow_memmem slow_memmem_original
+#else
+namespace cryptonote {
+ uint64_t slow_memmem(const void* start_buff, size_t buflen,const void* pat,size_t patlen);
+}
+using namespace cryptonote;
+#endif
+
+static const struct {
+ size_t buflen;
+ const char *buf;
+ size_t patlen;
+ const char *pat;
+ uint64_t res;
+} T[]={
+ {0,"",0,"",0},
+ {1,"",0,"",0},
+ {0,"",1,"",0},
+ {1,"x",1,"x",0},
+ {2,"x",1,"",1},
+ {1,"x",1,"",0},
+ {1,"x",2,"",0},
+ {1,"x",2,"x",0},
+ {2,"ax",2,"x",0},
+ {1,"xx",2,"xx",0},
+ {4,"abcd",3,"abc",0},
+ {4,"abcd",3,"bcd",1},
+ {4,"abcd",4,"abcd",0},
+ {4,"abcd",1,"d",3},
+ {4,"abcd",1,"c",2},
+ {4,"abcd",1,"bc",1},
+ {4,"abcd",1,"",0},
+ {3,"abcd",1,"d",0},
+ {5,"aaaab",2,"ab",3},
+ {7,"aaaabab",2,"ab",3},
+ {7,"aaaabab",3,"abc",0},
+ {4,"abcd",2,"cd",2},
+ {3,"abcd",2,"cd",0},
+ {3,"a\0b",1,"",1},
+ {3,"a\0b",2,"\0b",1},
+ {8,"xxxxxxab",3,"xyz",0},
+ {8,"xxxxxxab",6,"abcdef",0},
+ {9,"\0xxxxxab",3,"ab",6},
+ {4,"\0\0a",3,"\0a",1},
+};
+
+TEST(slowmem,Success)
+{
+ size_t n;
+ for (n=0;n<sizeof(T)/sizeof(T[0]);++n) {
+#ifdef VERBOSE
+ printf("%3zu: ",n);
+ fflush(stdout);
+#endif
+ void *buf=malloc(T[n].buflen);
+ memcpy(buf,T[n].buf,T[n].buflen);
+ void *pat=malloc(T[n].patlen);
+ memcpy(pat,T[n].pat,T[n].patlen);
+ uint64_t res=slow_memmem(buf,T[n].buflen,pat,T[n].patlen);
+ free(pat);
+ free(buf);
+ ASSERT_EQ(res,T[n].res);
+ASSERT_EQ(1,1);
+#ifdef VERBOSE
+ if (res!=T[n].res) printf("failed (got %zu, expected %zu)",res,T[n].res); else printf("ok");
+ printf("\n");
+#endif
+ }
+}