-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathvdf_client_session_test.cpp
More file actions
85 lines (70 loc) · 3 KB
/
vdf_client_session_test.cpp
File metadata and controls
85 lines (70 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "vdf_client_session.h"
#include <boost/asio.hpp>
#include <gtest/gtest.h>
#include <cstdint>
#include <exception>
#include <string>
#include <thread>
#include <vector>
namespace {
std::string run_init_session_with_payload(const std::vector<uint8_t>& payload) {
boost::asio::io_context io;
using boost::asio::ip::tcp;
tcp::acceptor acceptor(io, tcp::endpoint(tcp::v4(), 0));
const uint16_t port = acceptor.local_endpoint().port();
std::thread server([&]() {
boost::system::error_code server_error;
tcp::socket peer(io);
acceptor.accept(peer, server_error);
if (server_error) {
return;
}
if (!payload.empty()) {
boost::asio::write(peer, boost::asio::buffer(payload), server_error);
}
peer.shutdown(tcp::socket::shutdown_send, server_error);
peer.close(server_error);
});
std::string error_message;
try {
tcp::socket client(io);
client.connect(tcp::endpoint(boost::asio::ip::address_v4::loopback(), port));
InitSession(client);
} catch (const std::exception& e) {
error_message = e.what();
}
server.join();
return error_message;
}
} // namespace
TEST(VdfClientSessionRegressionTest, TruncatedDiscriminantSizeFailsBeforeParse) {
const std::vector<uint8_t> payload = {'0', '3'};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Connection closed while reading discriminant size"), std::string::npos);
}
TEST(VdfClientSessionRegressionTest, ZeroDiscriminantSizeIsRejected) {
const std::vector<uint8_t> payload = {'0', '0', '0'};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Invalid discriminant size"), std::string::npos);
}
TEST(VdfClientSessionRegressionTest, OversizedDiscriminantSizeIsRejected) {
const std::vector<uint8_t> payload = {'3', '5', '0'};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Invalid discriminant size"), std::string::npos);
}
TEST(VdfClientSessionRegressionTest, TruncatedFormSizeFailsBeforeValidation) {
const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c'};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Connection closed while reading form size"), std::string::npos);
}
TEST(VdfClientSessionRegressionTest, ZeroFormSizeIsRejected) {
const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c', 0x00};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Invalid form size"), std::string::npos);
}
TEST(VdfClientSessionRegressionTest, WrappedNegativeFormSizeIsRejected) {
// 0xFF arrives as -1 in signed char and must be rejected by the <= 0 check.
const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c', 0xFF};
const std::string error = run_init_session_with_payload(payload);
EXPECT_NE(error.find("Invalid form size"), std::string::npos);
}