diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/README.md | 14 | ||||
-rw-r--r-- | tests/data/fuzz/parse-url/URL2 | 2 | ||||
-rwxr-xr-x | tests/functional_tests/blockchain.py | 103 | ||||
-rwxr-xr-x | tests/functional_tests/speed.py | 87 | ||||
-rw-r--r-- | tests/functional_tests/test_framework/__init__.py | 0 | ||||
-rw-r--r-- | tests/functional_tests/test_framework/daemon.py | 105 | ||||
-rw-r--r-- | tests/functional_tests/test_framework/rpc.py | 49 | ||||
-rw-r--r-- | tests/functional_tests/test_framework/wallet.py | 120 | ||||
-rw-r--r-- | tests/gtest/cmake/internal_utils.cmake | 4 | ||||
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/unit_tests/device.cpp | 131 | ||||
-rw-r--r-- | tests/unit_tests/hardfork.cpp | 40 | ||||
-rw-r--r-- | tests/unit_tests/ringct.cpp | 22 |
13 files changed, 655 insertions, 23 deletions
diff --git a/tests/README.md b/tests/README.md index 48a6c41a7..0bf097254 100644 --- a/tests/README.md +++ b/tests/README.md @@ -50,6 +50,20 @@ To run the same tests on a release build, replace `debug` with `release`. # Functional tests [TODO] +Functional tests are located under the `tests/functional` directory. + +First, run a regtest daemon in the offline mode and with a fixed difficulty: +``` +monerod --regtest --offline --fixed-difficulty 1 +``` +Alternatively, you can run multiple daemons and let them connect with each other by using `--add-exclusive-node`. In this case, make sure that the same fixed difficulty is given to all the daemons. + +Next, restore a mainnet wallet with the following seed and restore height 0 (the file path doesn't matter): +``` +velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted +``` + +Open the wallet file with `monero-wallet-rpc` with RPC port 18083. Finally, start tests by invoking ./blockchain.py or ./speed.py # Fuzz tests diff --git a/tests/data/fuzz/parse-url/URL2 b/tests/data/fuzz/parse-url/URL2 index b66e7de9a..6eeadc01a 100644 --- a/tests/data/fuzz/parse-url/URL2 +++ b/tests/data/fuzz/parse-url/URL2 @@ -1 +1 @@ -iframe_test.html?api_url=http://api.vk.com/api.php&api_id=3289090&api_settings=1&viewer_id=562964060&viewer_type=0&sid=0aad8d1c5713130f9ca0076f2b7b47e532877424961367d81e7fa92455f069be7e21bc3193cbd0be11895&secret=368ebbc0ef&access_token=668bc03f43981d883f73876ffff4aa8564254b359cc745dfa1b3cde7bdab2e94105d8f6d8250717569c0a7&user_id=0&group_id=0&is_app_user=1&auth_key=d2f7a895ca5ff3fdb2a2a8ae23fe679a&language=0&parent_language=0&ad_info=ElsdCQBaQlxiAQRdFUVUXiN2AVBzBx5pU1BXIgZUJlIEAWcgAUoLQg==&referrer=unknown&lc_name=9834b6a3&hash=
\ No newline at end of file +iframe_test.html?api_url=https://api.vk.com/api.php&api_id=3289090&api_settings=1&viewer_id=562964060&viewer_type=0&sid=0aad8d1c5713130f9ca0076f2b7b47e532877424961367d81e7fa92455f069be7e21bc3193cbd0be11895&secret=368ebbc0ef&access_token=668bc03f43981d883f73876ffff4aa8564254b359cc745dfa1b3cde7bdab2e94105d8f6d8250717569c0a7&user_id=0&group_id=0&is_app_user=1&auth_key=d2f7a895ca5ff3fdb2a2a8ae23fe679a&language=0&parent_language=0&ad_info=ElsdCQBaQlxiAQRdFUVUXiN2AVBzBx5pU1BXIgZUJlIEAWcgAUoLQg==&referrer=unknown&lc_name=9834b6a3&hash=
\ No newline at end of file diff --git a/tests/functional_tests/blockchain.py b/tests/functional_tests/blockchain.py new file mode 100755 index 000000000..983658a7c --- /dev/null +++ b/tests/functional_tests/blockchain.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +# 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. + +"""Test blockchain RPC calls + +Test the following RPCs: + - get_info + - generateblocks + - [TODO: many tests still need to be written] + +""" + +from test_framework.daemon import Daemon +from test_framework.wallet import Wallet + +class BlockchainTest(): + def run_test(self): + self._test_get_info() + self._test_hardfork_info() + self._test_generateblocks(5) + + def _test_get_info(self): + print('Test get_info') + + daemon = Daemon() + res = daemon.get_info() + + # difficulty should be set to 1 for this test + assert 'difficulty' in res.keys() + assert res['difficulty'] == 1; + + # nettype should not be TESTNET + assert 'testnet' in res.keys() + assert res['testnet'] == False; + + # nettype should not be STAGENET + assert 'stagenet' in res.keys() + assert res['stagenet'] == False; + + # nettype should be FAKECHAIN + assert 'nettype' in res.keys() + assert res['nettype'] == "fakechain"; + + # free_space should be > 0 + assert 'free_space' in res.keys() + assert res['free_space'] > 0 + + # height should be greater or equal to 1 + assert 'height' in res.keys() + assert res['height'] >= 1 + + + def _test_hardfork_info(self): + print('Test hard_fork_info') + + daemon = Daemon() + res = daemon.hard_fork_info() + + # hard_fork version should be set at height 1 + assert 'earliest_height' in res.keys() + assert res['earliest_height'] == 1; + + + def _test_generateblocks(self, blocks): + print("Test generating", blocks, 'blocks') + + daemon = Daemon() + res = daemon.get_info() + height = res['height'] + res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks) + + assert res['height'] == height + blocks - 1 + + +if __name__ == '__main__': + BlockchainTest().run_test() diff --git a/tests/functional_tests/speed.py b/tests/functional_tests/speed.py new file mode 100755 index 000000000..3d2af9a10 --- /dev/null +++ b/tests/functional_tests/speed.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 + +# 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. + +"""Test speed of various procedures + +Test the following RPCs: + - generateblocks + - transfer + - [TODO: many tests still need to be written] + +""" + + +import time +from time import sleep +from decimal import Decimal + +from test_framework.daemon import Daemon +from test_framework.wallet import Wallet + + +class SpeedTest(): + def set_test_params(self): + self.num_nodes = 1 + + def run_test(self): + daemon = Daemon() + wallet = Wallet() + + destinations = wallet.make_uniform_destinations('44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A',1,3) + + self._test_speed_generateblocks(daemon=daemon, blocks=70) + for i in range(1, 10): + while wallet.get_balance()['unlocked_balance'] == 0: + print('Waiting for wallet to refresh...') + sleep(1) + self._test_speed_transfer_split(wallet=wallet) + self._test_speed_generateblocks(daemon=daemon, blocks=10) + + def _test_speed_generateblocks(self, daemon, blocks): + print('Test speed of block generation') + start = time.time() + + res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks) + # wallet seed: velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted + + print('generating ', blocks, 'blocks took: ', time.time() - start, 'seconds') + + def _test_speed_transfer_split(self, wallet): + print('Test speed of transfer') + start = time.time() + + destinations = wallet.make_uniform_destinations('44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A',1) + res = wallet.transfer_split(destinations) + + print('generating tx took: ', time.time() - start, 'seconds') + + +if __name__ == '__main__': + SpeedTest().run_test() diff --git a/tests/functional_tests/test_framework/__init__.py b/tests/functional_tests/test_framework/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/functional_tests/test_framework/__init__.py diff --git a/tests/functional_tests/test_framework/daemon.py b/tests/functional_tests/test_framework/daemon.py new file mode 100644 index 000000000..f3490b232 --- /dev/null +++ b/tests/functional_tests/test_framework/daemon.py @@ -0,0 +1,105 @@ +# 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. + +"""Daemon class to make rpc calls and store state.""" + +from .rpc import JSONRPC + +class Daemon(object): + + def __init__(self, protocol='http', host='127.0.0.1', port=18081, path='/json_rpc'): + self.rpc = JSONRPC('{protocol}://{host}:{port}{path}'.format(protocol=protocol, host=host, port=port, path=path)) + + def getblocktemplate(self, address): + getblocktemplate = { + 'method': 'getblocktemplate', + 'params': { + 'wallet_address': address, + 'reserve_size' : 1 + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(getblocktemplate) + + def submitblock(self, block): + submitblock = { + 'method': 'submitblock', + 'params': [ block ], + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(submitblock) + + def getblock(self, height=0): + getblock = { + 'method': 'getblock', + 'params': { + 'height': height + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(getblock) + + def get_connections(self): + get_connections = { + 'method': 'get_connections', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(get_connections) + + def get_info(self): + get_info = { + 'method': 'get_info', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(get_info) + + def hard_fork_info(self): + hard_fork_info = { + 'method': 'hard_fork_info', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(hard_fork_info) + + def generateblocks(self, address, blocks=1): + generateblocks = { + 'method': 'generateblocks', + 'params': { + 'amount_of_blocks' : blocks, + 'reserve_size' : 20, + 'wallet_address': address + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(generateblocks) diff --git a/tests/functional_tests/test_framework/rpc.py b/tests/functional_tests/test_framework/rpc.py new file mode 100644 index 000000000..b21df7b93 --- /dev/null +++ b/tests/functional_tests/test_framework/rpc.py @@ -0,0 +1,49 @@ +# 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. + +import requests +import json + +class JSONRPC(object): + def __init__(self, url): + self.url = url + + def send_request(self, inputs): + res = requests.post( + self.url, + data=json.dumps(inputs), + headers={'content-type': 'application/json'}) + res = res.json() + + assert 'error' not in res, res + + return res['result'] + + + + diff --git a/tests/functional_tests/test_framework/wallet.py b/tests/functional_tests/test_framework/wallet.py new file mode 100644 index 000000000..357eab5b2 --- /dev/null +++ b/tests/functional_tests/test_framework/wallet.py @@ -0,0 +1,120 @@ +# 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. + +"""Daemon class to make rpc calls and store state.""" + +from .rpc import JSONRPC + +class Wallet(object): + + def __init__(self, protocol='http', host='127.0.0.1', port=18083, path='/json_rpc'): + self.rpc = JSONRPC('{protocol}://{host}:{port}{path}'.format(protocol=protocol, host=host, port=port, path=path)) + + def make_uniform_destinations(self, address, transfer_amount, transfer_number_of_destinations=1): + destinations = [] + for i in range(transfer_number_of_destinations): + destinations.append({"amount":transfer_amount,"address":address}) + return destinations + + def make_destinations(self, addresses, transfer_amounts): + destinations = [] + for i in range(len(addresses)): + destinations.append({'amount':transfer_amounts[i],'address':addresses[i]}) + return destinations + + def transfer(self, destinations, ringsize=7, payment_id=''): + transfer = { + 'method': 'transfer', + 'params': { + 'destinations': destinations, + 'mixin' : ringsize - 1, + 'get_tx_key' : True + }, + 'jsonrpc': '2.0', + 'id': '0' + } + if(len(payment_id) > 0): + transfer['params'].update({'payment_id' : payment_id}) + return self.rpc.send_request(transfer) + + def transfer_split(self, destinations, ringsize=7, payment_id=''): + print(destinations) + transfer = { + "method": "transfer_split", + "params": { + "destinations": destinations, + "mixin" : ringsize - 1, + "get_tx_key" : True, + "new_algorithm" : True + }, + "jsonrpc": "2.0", + "id": "0" + } + if(len(payment_id) > 0): + transfer['params'].update({'payment_id' : payment_id}) + return self.rpc.send_request(transfer) + + def create_wallet(self, index=''): + create_wallet = { + 'method': 'create_wallet', + 'params': { + 'filename': 'testWallet' + index, + 'password' : '', + 'language' : 'English' + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(create_wallet) + + def get_balance(self): + get_balance = { + 'method': 'get_balance', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(get_balance) + + def sweep_dust(self): + sweep_dust = { + 'method': 'sweep_dust', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(sweep_dust) + + def sweep_all(self, address): + sweep_all = { + 'method': 'sweep_all', + 'params' : { + 'address' : '' + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_request(sweep_all) diff --git a/tests/gtest/cmake/internal_utils.cmake b/tests/gtest/cmake/internal_utils.cmake index 567edaa30..ae45d7d7a 100644 --- a/tests/gtest/cmake/internal_utils.cmake +++ b/tests/gtest/cmake/internal_utils.cmake @@ -96,6 +96,10 @@ macro(config_compiler_and_linker) set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") set(cxx_strict_flags "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers") + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + set(cxx_base_flags "-Wall -Wshadow -fPIC") + set(cxx_exception_flags "-fexceptions") + set(cxx_no_exception_flags "-fno-exceptions") elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") set(cxx_exception_flags "-features=except") # Sun Pro doesn't provide macros to indicate whether exceptions and diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index ee496d1cb..ffa8065ab 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -41,6 +41,7 @@ set(unit_tests_sources command_line.cpp crypto.cpp decompose_amount_into_digits.cpp + device.cpp dns_resolver.cpp epee_boosted_tcp_server.cpp epee_levin_protocol_handler_async.cpp diff --git a/tests/unit_tests/device.cpp b/tests/unit_tests/device.cpp new file mode 100644 index 000000000..50ccec9fa --- /dev/null +++ b/tests/unit_tests/device.cpp @@ -0,0 +1,131 @@ +// 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 "ringct/rctOps.h" +#include "device/device_default.hpp" + +TEST(device, name) +{ + hw::core::device_default dev; + ASSERT_TRUE(dev.set_name("test")); + ASSERT_EQ(dev.get_name(), "test"); +} + +/* +TEST(device, locking) +{ + hw::core::device_default dev; + ASSERT_TRUE(dev.try_lock()); + ASSERT_FALSE(dev.try_lock()); + dev.unlock(); + ASSERT_TRUE(dev.try_lock()); + dev.unlock(); + dev.lock(); + ASSERT_FALSE(dev.try_lock()); + dev.unlock(); + ASSERT_TRUE(dev.try_lock()); + dev.unlock(); +} +*/ + +TEST(device, open_close) +{ + hw::core::device_default dev; + crypto::secret_key key; + ASSERT_TRUE(dev.open_tx(key)); + ASSERT_TRUE(dev.close_tx()); +} + +TEST(device, ops) +{ + hw::core::device_default dev; + rct::key resd, res; + crypto::key_derivation derd, der; + rct::key sk, pk; + crypto::secret_key sk0, sk1; + crypto::public_key pk0, pk1; + crypto::ec_scalar ressc0, ressc1; + crypto::key_image ki0, ki1; + + rct::skpkGen(sk, pk); + rct::scalarmultBase((rct::key&)pk0, (rct::key&)sk0); + rct::scalarmultBase((rct::key&)pk1, (rct::key&)sk1); + + dev.scalarmultKey(resd, pk, sk); + rct::scalarmultKey(res, pk, sk); + ASSERT_EQ(resd, res); + + dev.scalarmultBase(resd, sk); + rct::scalarmultBase(res, sk); + ASSERT_EQ(resd, res); + + dev.sc_secret_add((crypto::secret_key&)resd, sk0, sk1); + sc_add((unsigned char*)&res, (unsigned char*)&sk0, (unsigned char*)&sk1); + ASSERT_EQ(resd, res); + + dev.generate_key_derivation(pk0, sk0, derd); + crypto::generate_key_derivation(pk0, sk0, der); + ASSERT_FALSE(memcmp(&derd, &der, sizeof(der))); + + dev.derivation_to_scalar(der, 0, ressc0); + crypto::derivation_to_scalar(der, 0, ressc1); + ASSERT_FALSE(memcmp(&ressc0, &ressc1, sizeof(ressc1))); + + dev.derive_secret_key(der, 0, rct::rct2sk(sk), sk0); + crypto::derive_secret_key(der, 0, rct::rct2sk(sk), sk1); + ASSERT_EQ(sk0, sk1); + + dev.derive_public_key(der, 0, rct::rct2pk(pk), pk0); + crypto::derive_public_key(der, 0, rct::rct2pk(pk), pk1); + ASSERT_EQ(pk0, pk1); + + dev.secret_key_to_public_key(rct::rct2sk(sk), pk0); + crypto::secret_key_to_public_key(rct::rct2sk(sk), pk1); + ASSERT_EQ(pk0, pk1); + + dev.generate_key_image(pk0, sk0, ki0); + crypto::generate_key_image(pk0, sk0, ki1); + ASSERT_EQ(ki0, ki1); +} + +TEST(device, ecdh) +{ + hw::core::device_default dev; + rct::ecdhTuple tuple, tuple2; + rct::key key = rct::skGen(); + tuple.mask = rct::skGen(); + tuple.amount = rct::skGen(); + tuple.senderPk = rct::pkGen(); + tuple2 = tuple; + dev.ecdhEncode(tuple, key); + dev.ecdhDecode(tuple, key); + ASSERT_EQ(tuple2.mask, tuple.mask); + ASSERT_EQ(tuple2.amount, tuple.amount); + ASSERT_EQ(tuple2.senderPk, tuple.senderPk); +} diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index 913ebe84a..930aeb782 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -50,6 +50,7 @@ public: virtual void safesyncmode(const bool onoff) {} virtual void reset() {} virtual std::vector<std::string> get_filenames() const { return std::vector<std::string>(); } + virtual bool remove_data_file(const std::string& folder) const { return true; } virtual std::string get_db_name() const { return std::string(); } virtual bool lock() { return true; } virtual void unlock() { } @@ -69,6 +70,7 @@ public: virtual uint64_t get_block_height(const crypto::hash& h) const { return 0; } virtual block_header get_block_header(const crypto::hash& h) const { return block_header(); } virtual uint64_t get_block_timestamp(const uint64_t& height) const { return 0; } + virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const { return {}; } virtual uint64_t get_top_block_timestamp() const { return 0; } virtual size_t get_block_size(const uint64_t& height) const { return 128; } virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const { return 10; } @@ -124,6 +126,7 @@ public: virtual void remove_txpool_tx(const crypto::hash& txid) {} virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const { return false; } virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const { return false; } + virtual uint64_t get_database_size() const { return 0; } virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const { return ""; } virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = false) const { return false; } @@ -131,6 +134,7 @@ public: , const size_t& block_size , const difficulty_type& cumulative_difficulty , const uint64_t& coins_generated + , uint64_t num_rct_outs , const crypto::hash& blk_hash ) { blocks.push_back(blk); @@ -183,20 +187,20 @@ TEST(major, Only) ASSERT_FALSE(hf.add(mkblock(0, 2), 0)); ASSERT_FALSE(hf.add(mkblock(2, 2), 0)); ASSERT_TRUE(hf.add(mkblock(1, 2), 0)); - db.add_block(mkblock(1, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(1, 1), 0, 0, 0, 0, crypto::hash()); // block height 1, only version 1 is accepted ASSERT_FALSE(hf.add(mkblock(0, 2), 1)); ASSERT_FALSE(hf.add(mkblock(2, 2), 1)); ASSERT_TRUE(hf.add(mkblock(1, 2), 1)); - db.add_block(mkblock(1, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(1, 1), 0, 0, 0, 0, crypto::hash()); // block height 2, only version 2 is accepted ASSERT_FALSE(hf.add(mkblock(0, 2), 2)); ASSERT_FALSE(hf.add(mkblock(1, 2), 2)); ASSERT_FALSE(hf.add(mkblock(3, 2), 2)); ASSERT_TRUE(hf.add(mkblock(2, 2), 2)); - db.add_block(mkblock(2, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(2, 1), 0, 0, 0, 0, crypto::hash()); } TEST(empty_hardforks, Success) @@ -210,7 +214,7 @@ TEST(empty_hardforks, Success) ASSERT_TRUE(hf.get_state(time(NULL) + 3600*24*400) == HardFork::Ready); for (uint64_t h = 0; h <= 10; ++h) { - db.add_block(mkblock(hf, h, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } ASSERT_EQ(hf.get(0), 1); @@ -244,14 +248,14 @@ TEST(check_for_height, Success) for (uint64_t h = 0; h <= 4; ++h) { ASSERT_TRUE(hf.check_for_height(mkblock(1, 1), h)); ASSERT_FALSE(hf.check_for_height(mkblock(2, 2), h)); // block version is too high - db.add_block(mkblock(hf, h, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } for (uint64_t h = 5; h <= 10; ++h) { ASSERT_FALSE(hf.check_for_height(mkblock(1, 1), h)); // block version is too low ASSERT_TRUE(hf.check_for_height(mkblock(2, 2), h)); - db.add_block(mkblock(hf, h, 2), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } } @@ -268,19 +272,19 @@ TEST(get, next_version) for (uint64_t h = 0; h <= 4; ++h) { ASSERT_EQ(2, hf.get_next_version()); - db.add_block(mkblock(hf, h, 1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } for (uint64_t h = 5; h <= 9; ++h) { ASSERT_EQ(4, hf.get_next_version()); - db.add_block(mkblock(hf, h, 2), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } for (uint64_t h = 10; h <= 15; ++h) { ASSERT_EQ(4, hf.get_next_version()); - db.add_block(mkblock(hf, h, 4), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 4), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } } @@ -321,7 +325,7 @@ TEST(steps_asap, Success) hf.init(); for (uint64_t h = 0; h < 10; ++h) { - db.add_block(mkblock(hf, h, 9), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, 9), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } @@ -348,7 +352,7 @@ TEST(steps_1, Success) hf.init(); for (uint64_t h = 0 ; h < 10; ++h) { - db.add_block(mkblock(hf, h, h+1), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, h+1), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } @@ -373,7 +377,7 @@ TEST(reorganize, Same) // index 0 1 2 3 4 5 6 7 8 9 static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; for (uint64_t h = 0; h < 20; ++h) { - db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } @@ -404,7 +408,7 @@ TEST(reorganize, Changed) static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; static const uint8_t expected_versions[] = { 1, 1, 1, 1, 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9 }; for (uint64_t h = 0; h < 16; ++h) { - db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE (hf.add(db.get_block_from_height(h), h)); } @@ -424,7 +428,7 @@ TEST(reorganize, Changed) ASSERT_EQ(db.height(), 3); hf.reorganize_from_block_height(2); for (uint64_t h = 3; h < 16; ++h) { - db.add_block(mkblock(hf, h, block_versions_new[h]), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, block_versions_new[h]), 0, 0, 0, 0, crypto::hash()); bool ret = hf.add(db.get_block_from_height(h), h); ASSERT_EQ (ret, h < 15); } @@ -448,7 +452,7 @@ TEST(voting, threshold) for (uint64_t h = 0; h <= 8; ++h) { uint8_t v = 1 + !!(h % 8); - db.add_block(mkblock(hf, h, v), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, v), 0, 0, 0, 0, crypto::hash()); bool ret = hf.add(db.get_block_from_height(h), h); if (h >= 8 && threshold == 87) { // for threshold 87, we reach the treshold at height 7, so from height 8, hard fork to version 2, but 8 tries to add 1 @@ -482,7 +486,7 @@ TEST(voting, different_thresholds) static const uint8_t expected_versions[] = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) { - db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, crypto::hash()); bool ret = hf.add(db.get_block_from_height(h), h); ASSERT_EQ(ret, true); } @@ -536,7 +540,7 @@ TEST(voting, info) ASSERT_EQ(expected_thresholds[h], threshold); ASSERT_EQ(4, voting); - db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, crypto::hash()); + db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, crypto::hash()); ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } } @@ -599,7 +603,7 @@ TEST(reorganize, changed) #define ADD(v, h, a) \ do { \ cryptonote::block b = mkblock(hf, h, v); \ - db.add_block(b, 0, 0, 0, crypto::hash()); \ + db.add_block(b, 0, 0, 0, 0, crypto::hash()); \ ASSERT_##a(hf.add(b, h)); \ } while(0) #define ADD_TRUE(v, h) ADD(v, h, TRUE) diff --git a/tests/unit_tests/ringct.cpp b/tests/unit_tests/ringct.cpp index 0f4bd3edf..6e3958f8a 100644 --- a/tests/unit_tests/ringct.cpp +++ b/tests/unit_tests/ringct.cpp @@ -957,12 +957,20 @@ TEST(ringct, fee_burn_valid_zero_out_simple) EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); } +static rctSig make_sig() +{ + static const uint64_t inputs[] = {1000, 1000}; + static const uint64_t outputs[] = {1000, 1000}; + static rct::rctSig sig = make_sample_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, true); + return sig; +} + #define TEST_rctSig_elements(name, op) \ TEST(ringct, rctSig_##name) \ { \ const uint64_t inputs[] = {1000, 1000}; \ const uint64_t outputs[] = {1000, 1000}; \ - rct::rctSig sig = make_sample_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, true); \ + rct::rctSig sig = make_sig(); \ ASSERT_TRUE(rct::verRct(sig)); \ op; \ ASSERT_FALSE(rct::verRct(sig)); \ @@ -994,12 +1002,18 @@ TEST_rctSig_elements(outPk_empty, sig.outPk.resize(0)); TEST_rctSig_elements(outPk_too_many, sig.outPk.push_back(sig.outPk.back())); TEST_rctSig_elements(outPk_too_few, sig.outPk.pop_back()); +static rct::rctSig make_sig_simple() +{ + static const uint64_t inputs[] = {1000, 1000}; + static const uint64_t outputs[] = {1000}; + static rct::rctSig sig = make_sample_simple_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, 1000); + return sig; +} + #define TEST_rctSig_elements_simple(name, op) \ TEST(ringct, rctSig_##name##_simple) \ { \ - const uint64_t inputs[] = {1000, 1000}; \ - const uint64_t outputs[] = {1000}; \ - rct::rctSig sig = make_sample_simple_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, 1000); \ + rct::rctSig sig = make_sig_simple(); \ ASSERT_TRUE(rct::verRctSimple(sig)); \ op; \ ASSERT_FALSE(rct::verRctSimple(sig)); \ |