Skip to content

Commit 23430e9

Browse files
committed
replayBundle tests
1 parent e5e9d3d commit 23430e9

23 files changed

+404
-18
lines changed

include/iota/api/core.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ namespace API {
5050
*/
5151
class Core {
5252
public:
53-
explicit Core(const std::string& host, const uint16_t& port, bool localPow = true);
53+
explicit Core(const std::string& host, const uint16_t& port, bool localPow = true,
54+
int timeout = 60);
5455
virtual ~Core();
5556

5657
public:

include/iota/api/extended.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace API {
5252
*/
5353
class Extended : public Core {
5454
public:
55-
Extended(const std::string& host, const uint16_t& port, bool localPow = true,
55+
Extended(const std::string& host, const uint16_t& port, bool localPow = true, int timeout = 60,
5656
Crypto::SpongeType cryptoType = Crypto::SpongeType::KERL);
5757
virtual ~Extended();
5858

include/iota/api/service.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ namespace IOTA {
4444
namespace API {
4545

4646
class Service {
47-
private:
48-
static const int timeout = 60;
49-
5047
public:
51-
Service(const std::string& host, const uint16_t& port);
48+
Service(const std::string& host, const uint16_t& port, int timeout = 60);
5249
virtual ~Service();
5350

5451
public:
@@ -64,7 +61,7 @@ class Service {
6461
auto headers = cpr::Header{ { "Content-Type", "application/json" },
6562
{ "Content-Length", std::to_string(body.size()) },
6663
{ "X-IOTA-API-Version", APIVersion } };
67-
auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout * 1000 });
64+
auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout_ * 1000 });
6865

6966
if (res.error.code != cpr::ErrorCode::OK)
7067
throw Errors::Network(res.error.message);
@@ -79,8 +76,8 @@ class Service {
7976
error = resJson["error"].get<std::string>();
8077
}
8178
} catch (const std::runtime_error&) {
82-
if (res.elapsed >= timeout) {
83-
throw Errors::Network("Time out after " + std::to_string(timeout) + "s");
79+
if (res.elapsed >= timeout_) {
80+
throw Errors::Network("Time out after " + std::to_string(timeout_) + "s");
8481
}
8582

8683
throw Errors::Unrecognized("Invalid reply from node (unrecognized format): " + res.text);
@@ -97,8 +94,8 @@ class Service {
9794
case 500:
9895
throw Errors::InternalServerError(error);
9996
default:
100-
if (res.elapsed >= timeout) {
101-
throw Errors::Network("Time out after " + std::to_string(timeout) + "s");
97+
if (res.elapsed >= timeout_) {
98+
throw Errors::Network("Time out after " + std::to_string(timeout_) + "s");
10299
}
103100

104101
throw Errors::Unrecognized(error);
@@ -110,6 +107,7 @@ class Service {
110107
private:
111108
std::string host_;
112109
unsigned int port_;
110+
const int timeout_;
113111
};
114112

115113
} // namespace API

source/api/core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ namespace IOTA {
4747

4848
namespace API {
4949

50-
Core::Core(const std::string& host, const uint16_t& port, bool localPow)
51-
: service_(host, port), localPow_(localPow) {
50+
Core::Core(const std::string& host, const uint16_t& port, bool localPow, int timeout)
51+
: service_(host, port, timeout), localPow_(localPow) {
5252
}
5353

5454
Core::~Core() {

source/api/extended.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace IOTA {
3838

3939
namespace API {
4040

41-
Extended::Extended(const std::string& host, const uint16_t& port, bool localPow,
41+
Extended::Extended(const std::string& host, const uint16_t& port, bool localPow, int timeout,
4242
Crypto::SpongeType t)
43-
: Core(host, port, localPow), cryptoType_(t) {
43+
: Core(host, port, localPow, timeout), cryptoType_(t) {
4444
}
4545

4646
Extended::~Extended() {

source/api/service.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace IOTA {
2929

3030
namespace API {
3131

32-
Service::Service(const std::string& host, const uint16_t& port) : host_(host), port_(port) {
32+
Service::Service(const std::string& host, const uint16_t& port, int timeout)
33+
: host_(host), port_(port), timeout_(timeout) {
3334
}
3435

3536
Service::~Service() {

test/include/test/utils/constants.hpp

Lines changed: 305 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2017-2018 Thibault Martinez and Simon Ninon
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
//
25+
26+
#include <gtest/gtest.h>
27+
28+
#include <iota/api/extended.hpp>
29+
#include <iota/crypto/checksum.hpp>
30+
#include <iota/errors/bad_request.hpp>
31+
#include <iota/errors/illegal_state.hpp>
32+
#include <test/utils/configuration.hpp>
33+
#include <test/utils/constants.hpp>
34+
#include <test/utils/expect_exception.hpp>
35+
36+
TEST(Extended, ReplayBundle) {
37+
IOTA::API::Extended api(get_proxy_host(), get_proxy_port(), true, 380);
38+
39+
auto res = api.replayBundle(BUNDLE_3_TRX_1_HASH, 27, 14);
40+
41+
ASSERT_EQ(res.getSuccessfully().size(), 4UL);
42+
ASSERT_EQ(res.getSuccessfully(), std::vector<bool>({ true, true, true, true }));
43+
}
44+
45+
TEST(Extended, ReplayBundleNonTailTransaction) {
46+
IOTA::API::Extended api(get_proxy_host(), get_proxy_port());
47+
48+
EXPECT_EXCEPTION(api.replayBundle(BUNDLE_3_TRX_4_HASH, 27, 14), IOTA::Errors::IllegalState,
49+
"Invalid tail transaction supplied.");
50+
}
51+
52+
TEST(Extended, ReplayBundleBundleHash) {
53+
IOTA::API::Extended api(get_proxy_host(), get_proxy_port());
54+
55+
EXPECT_EXCEPTION(api.replayBundle(BUNDLE_3_HASH, 27, 14), IOTA::Errors::IllegalState,
56+
"Invalid transaction supplied.");
57+
}
58+
59+
TEST(Extended, ReplayBundleInvalidHash) {
60+
IOTA::API::Extended api(get_proxy_host(), get_proxy_port());
61+
62+
EXPECT_EXCEPTION(api.replayBundle("hello", 27, 14), IOTA::Errors::IllegalState,
63+
"Invalid transaction supplied.");
64+
}

test/testnet/create_testing_data.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,21 @@
2121

2222
# Propagate
2323
api.send_transfer(1, [output_trx])
24+
25+
#
26+
# Create a new bundle transaction
27+
# Send from 42 iota account 5 to account 5
28+
#
29+
30+
# Connect to the node
31+
api = Iota('http://localhost:14265', "UYXMZDTITKOAPKPFCAZYB9DTPJ9VARFKCWVSYIOZROLBSUJECBLBZA99TTBFJTPUXEYN9PETLVENOHRKJ")
32+
33+
# Trx setup
34+
output_trx = ProposedTransaction(
35+
address = Address("BUDGFXLIN9FYRZGHQHQKEVIROEKCOVZUQKIFGRSOWGVBQEGVMPWDEAKGDPFPXUODDYVDLCEUL9VPPMLKA"),
36+
message = TryteString.from_string("Sending some money"),
37+
tag = Tag("PACIFICSOUND"),
38+
value = 42)
39+
40+
# Propagate
41+
api.send_transfer(1, [output_trx])

test/testnet/testnetdb/000057.log

-13.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)