MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CheckEncryption

Function CheckEncryption

src/tests/test_network_crypto.cpp:217–246  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

215
216
217static void CheckEncryption(MockNetworkSocketHandler *sending_socket_handler, MockNetworkSocketHandler *receiving_socket_handler)
218{
219 PacketType sent_packet_type{ 1 };
220 uint64_t sent_value = 0x1234567890ABCDEF;
221 std::set<PacketType> encrypted_packet_types;
222
223 for (int i = 0; i < 10; i++) {
224 Packet request(sending_socket_handler, sent_packet_type);
225 request.Send_uint64(sent_value);
226
227 auto [response, valid] = CreatePacketForReading(request, receiving_socket_handler);
228 CHECK(valid);
229 CHECK(response.Recv_uint64() == sent_value);
230
231 encrypted_packet_types.insert(request.GetPacketType());
232 }
233 /*
234 * Check whether it looks like encryption has happened. This is done by checking the value
235 * of the packet type after encryption. If after a few iterations more than one encrypted
236 * value has been seen, then we know that some type of encryption/scrambling is happening.
237 *
238 * Technically this check could fail erroneously when 16 subsequent encryptions yield the
239 * same encrypted packet type. However, with encryption that byte should have random value
240 * value, so the chance of this happening are tiny given enough iterations.
241 * Roughly in the order of 2**((iterations - 1) * 8), which with 10 iterations is in the
242 * one-in-sextillion (10**21) order of magnitude.
243 */
244 CHECK(encrypted_packet_types.size() != 1);
245
246}
247
248TEST_CASE("Encryption handling")
249{

Callers 1

Calls 6

CreatePacketForReadingFunction · 0.85
Send_uint64Method · 0.80
Recv_uint64Method · 0.80
GetPacketTypeMethod · 0.80
insertMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected