10 #include "../stdafx.h"
12 #include "../3rdparty/catch2/catch.hpp"
14 #include "../core/format.hpp"
15 #include "../network/network_crypto_internal.h"
16 #include "../network/core/packet.h"
17 #include "../string_func.h"
25 MockNetworkSocketHandler(std::unique_ptr<NetworkEncryptionHandler> &&receive = {}, std::unique_ptr<NetworkEncryptionHandler> &&send = {})
40 auto transfer_in = [](
Packet &source,
char *dest_data,
size_t length) {
41 auto transfer_out = [](
char *dest_data,
const char *source_data,
size_t length) {
42 std::copy(source_data, source_data + length, dest_data);
47 dest.TransferIn(transfer_in, source);
49 bool valid = dest.PrepareToRead();
51 return { dest,
valid };
60 void AskUserForPassword(std::shared_ptr<NetworkAuthenticationPasswordRequest> request)
override { request->Reply(this->password); }
71 std::tie(request,
valid) = CreatePacketForReading(request, &mock_socket_handler);
78 std::tie(response,
valid) = CreatePacketForReading(response, &mock_socket_handler);
84 TEST_CASE(
"Authentication_KeyExchangeOnly")
93 static void TestAuthenticationPAKE(std::string server_password, std::string client_password,
103 TEST_CASE(
"Authentication_PAKE")
105 SECTION(
"Correct password") {
109 SECTION(
"Empty password") {
113 SECTION(
"Wrong password") {
132 TEST_CASE(
"Authentication_AuthorizedKey")
138 SECTION(
"Correct public key") {
142 SECTION(
"Incorrect public key") {
148 TEST_CASE(
"Authentication_Combined")
156 valid_authorized_keys.
Add(client_public_key_str);
160 invalid_authorized_keys.
Add(
"not-a-valid-authorized-key");
166 std::string no_password =
"";
168 std::string valid_password =
"sikrit";
170 std::string invalid_password =
"secret";
175 SECTION(
"Invalid authorized keys, invalid password") {
182 SECTION(
"Invalid authorized keys, valid password") {
189 SECTION(
"Valid authorized keys, valid password") {
195 SECTION(
"No authorized keys, invalid password") {
201 SECTION(
"No authorized keys, valid password") {
207 SECTION(
"No authorized keys, no password") {
218 uint64_t sent_value = 0x1234567890ABCDEF;
219 std::set<PacketType> encrypted_packet_types;
221 for (
int i = 0; i < 10; i++) {
222 Packet request(sending_socket_handler, sent_packet_type);
223 request.Send_uint64(sent_value);
225 auto [response,
valid] = CreatePacketForReading(request, receiving_socket_handler);
227 CHECK(response.Recv_uint64() == sent_value);
229 encrypted_packet_types.insert(request.GetPacketType());
242 CHECK(encrypted_packet_types.size() != 1);
246 TEST_CASE(
"Encryption handling")
257 std::tie(packet,
valid) = CreatePacketForReading(packet, &mock_socket_handler);
264 SECTION(
"Encyption happening client -> server") {
265 CheckEncryption(&client_socket_handler, &server_socket_handler);
268 SECTION(
"Encyption happening server -> client") {
269 CheckEncryption(&server_socket_handler, &client_socket_handler);
272 SECTION(
"Unencrypted packet sent causes invalid read packet") {
276 auto [response,
valid] = CreatePacketForReading(request, &client_socket_handler);