Skip to content

Commit e034cee

Browse files
fix: resolve clang-tidy issues in ProcessPacket tests
Replace using-directive with declarations, use std::array, by-value callback parameters, and const-correctness fixes. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6aa6c73 commit e034cee

1 file changed

Lines changed: 40 additions & 30 deletions

File tree

tests/tests/nuclearnet/ProcessPacket.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <cstring>
2929
#include <memory>
3030
#include <string>
31+
#include <utility>
3132
#include <vector>
3233

3334
#include "nuclearnet/Discovery.hpp"
@@ -42,7 +43,16 @@ using NUClear::network::NetworkConfig;
4243
using NUClear::network::PeerInfo;
4344
using NUClear::util::network::sock_t;
4445

45-
using namespace NUClear::network;
46+
using NUClear::network::ACK;
47+
using NUClear::network::ANNOUNCE;
48+
using NUClear::network::CON_ACK;
49+
using NUClear::network::CONNECT;
50+
using NUClear::network::DATA;
51+
using NUClear::network::DataPacket;
52+
using NUClear::network::PacketHeader;
53+
using NUClear::network::PROTOCOL_VERSION;
54+
using NUClear::network::SYN;
55+
using NUClear::network::validate_header;
4656

4757
namespace {
4858

@@ -162,13 +172,13 @@ SCENARIO("process_packet discards packets with invalid headers", "[nuclearnet][p
162172
const sock_t peer = make_addr(0x0A000001, 5000);
163173

164174
bool received = false;
165-
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>&&) {
175+
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>) {
166176
received = true;
167177
});
168178

169179
// Garbage data
170-
uint8_t garbage[10] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
171-
net->process_packet(peer, garbage, sizeof(garbage));
180+
const std::array<uint8_t, 10> garbage = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
181+
net->process_packet(peer, garbage.data(), garbage.size());
172182

173183
REQUIRE_FALSE(received);
174184
}
@@ -238,7 +248,7 @@ SCENARIO("process_data_packet rejects data from unconnected peers", "[nuclearnet
238248
const sock_t peer = make_addr(0x0A000001, 5000);
239249

240250
bool received = false;
241-
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>&&) {
251+
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>) {
242252
received = true;
243253
});
244254

@@ -267,7 +277,7 @@ SCENARIO("process_data_packet rejects data for unsubscribed hashes", "[nuclearne
267277
establish_peer(*net, peer, "Peer1");
268278

269279
bool received = false;
270-
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>&&) {
280+
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>) {
271281
received = true;
272282
});
273283

@@ -324,11 +334,11 @@ SCENARIO("process_data_packet detects and rejects duplicate packets", "[nuclearn
324334
establish_peer(*net, peer, "Sender");
325335

326336
int delivery_count = 0;
327-
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>&&) {
337+
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>) {
328338
++delivery_count;
329339
});
330340

331-
std::vector<uint8_t> payload = {0xDE, 0xAD};
341+
const std::vector<uint8_t> payload = {0xDE, 0xAD};
332342
auto data_pkt = build_data_packet(100, 0, 1, HASH, 0, payload);
333343

334344
// First delivery
@@ -352,20 +362,20 @@ SCENARIO("process_data_packet rejects packets that are too short", "[nuclearnet]
352362
establish_peer(*net, peer, "Sender");
353363

354364
bool received = false;
355-
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>&&) {
365+
net->set_packet_callback([&](const sock_t&, const std::string&, uint64_t, bool, std::vector<uint8_t>) {
356366
received = true;
357367
});
358368

359369
// Too short to be a DataPacket
360-
uint8_t short_data[10] = {};
361-
auto* hdr = reinterpret_cast<PacketHeader*>(short_data); // NOLINT
362-
hdr->header[0] = 0xE2;
363-
hdr->header[1] = 0x98;
364-
hdr->header[2] = 0xA2;
365-
hdr->version = PROTOCOL_VERSION;
366-
hdr->type = DATA;
367-
368-
net->process_data_packet(peer, short_data, sizeof(short_data));
370+
std::array<uint8_t, 10> short_data{};
371+
auto* hdr = reinterpret_cast<PacketHeader*>(short_data.data()); // NOLINT
372+
hdr->header[0] = 0xE2;
373+
hdr->header[1] = 0x98;
374+
hdr->header[2] = 0xA2;
375+
hdr->version = PROTOCOL_VERSION;
376+
hdr->type = DATA;
377+
378+
net->process_data_packet(peer, short_data.data(), short_data.size());
369379
REQUIRE_FALSE(received);
370380
}
371381

@@ -388,9 +398,9 @@ SCENARIO("process_data_packet reassembles multi-fragment messages", "[nuclearnet
388398
});
389399

390400
// Send 3 fragments of a 15-byte message (5 bytes each)
391-
std::vector<uint8_t> frag0 = {0, 1, 2, 3, 4};
392-
std::vector<uint8_t> frag1 = {5, 6, 7, 8, 9};
393-
std::vector<uint8_t> frag2 = {10, 11, 12, 13, 14};
401+
const std::vector<uint8_t> frag0 = {0, 1, 2, 3, 4};
402+
const std::vector<uint8_t> frag1 = {5, 6, 7, 8, 9};
403+
const std::vector<uint8_t> frag2 = {10, 11, 12, 13, 14};
394404

395405
auto pkt0 = build_data_packet(200, 0, 3, HASH, 0, frag0);
396406
auto pkt1 = build_data_packet(200, 1, 3, HASH, 0, frag1);
@@ -422,7 +432,7 @@ SCENARIO("process_ack_packet rejects ACKs from unconnected peers", "[nuclearnet]
422432
const sock_t peer = make_addr(0x0A000001, 5000);
423433

424434
// Build an ACK but don't connect the peer
425-
std::vector<bool> received_bits(1, true);
435+
const std::vector<bool> received_bits(1, true);
426436
auto ack_pkt = build_ack_packet(1, 1, received_bits);
427437

428438
// Should not crash — just silently discard
@@ -439,16 +449,16 @@ SCENARIO("process_ack_packet rejects packets that are too short", "[nuclearnet][
439449
establish_peer(*net, peer, "Peer1");
440450

441451
// Too short for ACKPacket
442-
uint8_t short_data[5] = {};
443-
auto* hdr = reinterpret_cast<PacketHeader*>(short_data); // NOLINT
444-
hdr->header[0] = 0xE2;
445-
hdr->header[1] = 0x98;
446-
hdr->header[2] = 0xA2;
447-
hdr->version = PROTOCOL_VERSION;
448-
hdr->type = ACK;
452+
std::array<uint8_t, 5> short_data{};
453+
auto* hdr = reinterpret_cast<PacketHeader*>(short_data.data()); // NOLINT
454+
hdr->header[0] = 0xE2;
455+
hdr->header[1] = 0x98;
456+
hdr->header[2] = 0xA2;
457+
hdr->version = PROTOCOL_VERSION;
458+
hdr->type = ACK;
449459

450460
// Should not crash
451-
net->process_ack_packet(peer, short_data, sizeof(short_data));
461+
net->process_ack_packet(peer, short_data.data(), short_data.size());
452462
}
453463

454464

0 commit comments

Comments
 (0)