Browse Source

Split up mocks.h

pull/2459/head
Michael Lynch 1 month ago
parent
commit
9bb9d31f43
  1. 70
      test/mocks/mesh.h
  2. 36
      test/mocks/radio.h
  3. 25
      test/mocks/random.h
  4. 42
      test/mocks/time.h
  5. 2
      test/test_helpers/test_advert_data.cpp

70
test/test_helpers/mocks.h → test/mocks/mesh.h

@ -1,72 +1,16 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <optional>
#include "helpers/BaseChatMesh.h"
#include "helpers/SimpleMeshTables.h"
#include "radio.h"
#include "random.h"
#include "time.h"
class FakeMillis final : public mesh::MillisecondClock {
public:
unsigned long getMillis() override {
return 0;
}
};
class FakeRtc final : public mesh::RTCClock {
public:
explicit FakeRtc(uint32_t initial_time) : _time(initial_time) {}
uint32_t getCurrentTime() override {
return _time;
}
void setCurrentTime(uint32_t time) override {
_time = time;
}
private:
uint32_t _time;
};
class FakeRng final : public mesh::RNG {
public:
void random(uint8_t* dest, size_t sz) override {
memset(dest, 0x5A, sz);
}
};
class FakeRadio final : public mesh::Radio {
public:
int recvRaw(uint8_t*, int) override {
return 0;
}
uint32_t getEstAirtimeFor(int) override {
return 1;
}
float packetScore(float, int) override {
return 1.0f;
}
bool startSendRaw(const uint8_t*, int) override {
return true;
}
bool isSendComplete() override {
return true;
}
void onSendFinished() override {}
bool isInRecvMode() const override {
return true;
}
};
// No-op packet manager for native tests.
// Satisfies Mesh dependencies while preventing packet allocation or queuing.
class NoopPacketManager final : public mesh::PacketManager {
public:
mesh::Packet* allocNew() override {
@ -108,6 +52,8 @@ public:
}
};
// Test chat mesh for native tests.
// Exposes packet receive handling and captures discovered contacts for assertions.
class TestChatMesh final : public BaseChatMesh {
public:
TestChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc,
@ -156,6 +102,8 @@ protected:
void onContactResponse(const ContactInfo&, const uint8_t*, uint8_t) override {}
};
// Test mesh context for native tests.
// Owns mock dependencies in construction order and exposes the mesh via operator->.
struct TestMeshContext {
explicit TestMeshContext(uint32_t current_timestamp)
: rtc(current_timestamp), mesh(radio, ms, rng, rtc, packet_manager, tables) {}

36
test/mocks/radio.h

@ -0,0 +1,36 @@
#pragma once
#include <cstdint>
#include "Dispatcher.h"
// Fake radio for native tests.
// Provides successful no-op send/receive behavior without hardware access.
class FakeRadio final : public mesh::Radio {
public:
int recvRaw(uint8_t*, int) override {
return 0;
}
uint32_t getEstAirtimeFor(int) override {
return 1;
}
float packetScore(float, int) override {
return 1.0f;
}
bool startSendRaw(const uint8_t*, int) override {
return true;
}
bool isSendComplete() override {
return true;
}
void onSendFinished() override {}
bool isInRecvMode() const override {
return true;
}
};

25
test/mocks/random.h

@ -0,0 +1,25 @@
#pragma once
#if defined(__has_include_next)
#if __has_include_next(<random.h>)
#include_next <random.h>
#endif
#endif
#ifdef __cplusplus
#include <cstddef>
#include <cstring>
#include "Utils.h"
// Fake random generator for native tests.
// Fills buffers with deterministic bytes so generated packets are repeatable.
class FakeRng final : public mesh::RNG {
public:
void random(uint8_t* dest, size_t sz) override {
memset(dest, 0x5A, sz);
}
};
#endif

42
test/mocks/time.h

@ -0,0 +1,42 @@
#pragma once
#if defined(__has_include_next)
#if __has_include_next(<time.h>)
#include_next <time.h>
#endif
#endif
#ifdef __cplusplus
#include <cstdint>
#include "Dispatcher.h"
// Fake millisecond clock for native tests.
// Returns a stable timestamp so timer-dependent code stays deterministic.
class FakeMillis final : public mesh::MillisecondClock {
public:
unsigned long getMillis() override {
return 0;
}
};
// Fake RTC clock for native tests.
// Stores caller-controlled Unix time without depending on hardware RTC APIs.
class FakeRtc final : public mesh::RTCClock {
public:
explicit FakeRtc(uint32_t initial_time) : _time(initial_time) {}
uint32_t getCurrentTime() override {
return _time;
}
void setCurrentTime(uint32_t time) override {
_time = time;
}
private:
uint32_t _time;
};
#endif

2
test/test_helpers/test_advert_data.cpp

@ -4,7 +4,7 @@
#include <gtest/gtest.h>
#include "mocks.h"
#include "../mocks/mesh.h"
namespace {

Loading…
Cancel
Save