|
5 | 5 | // ====================================================================== |
6 | 6 |
|
7 | 7 | #include "ApidManagerTester.hpp" |
8 | | -#include "STest/Random/Random.hpp" |
9 | | -#include "Svc/Ccsds/Types/FppConstantsAc.hpp" |
10 | 8 |
|
11 | 9 | namespace Svc { |
12 | 10 |
|
13 | 11 | namespace Ccsds { |
14 | 12 |
|
15 | | -static constexpr ComCfg::Apid::T TEST_REGISTERED_APIDS[] = {ComCfg::Apid::FW_PACKET_COMMAND, |
16 | | - ComCfg::Apid::FW_PACKET_TELEM, ComCfg::Apid::FW_PACKET_LOG, |
17 | | - ComCfg::Apid::FW_PACKET_FILE}; |
18 | | - |
19 | 13 | // ---------------------------------------------------------------------- |
20 | 14 | // Construction and destruction |
21 | 15 | // ---------------------------------------------------------------------- |
22 | 16 |
|
23 | | -ApidManagerTester ::ApidManagerTester() |
| 17 | +ApidManagerTester::ApidManagerTester() |
24 | 18 | : ApidManagerGTestBase("ApidManagerTester", ApidManagerTester::MAX_HISTORY_SIZE), component("ApidManager") { |
25 | 19 | this->initComponents(); |
26 | 20 | this->connectPorts(); |
27 | | - // Initialize existing sequence counts for common APIDs |
28 | | - for (FwIndexType i = 0; i < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(TEST_REGISTERED_APIDS)); i++) { |
29 | | - this->component.m_apidSequences.insert(TEST_REGISTERED_APIDS[i], static_cast<U16>(0)); |
30 | | - this->shadow_seqCounts[TEST_REGISTERED_APIDS[i]] = 0; // Initialize shadow sequence counts to 0 |
31 | | - } |
32 | | -} |
33 | | - |
34 | | -ApidManagerTester ::~ApidManagerTester() {} |
35 | | - |
36 | | -// ---------------------------------------------------------------------- |
37 | | -// Tests |
38 | | -// ---------------------------------------------------------------------- |
39 | | - |
40 | | -bool ApidManagerTester::GetExistingSeqCount::precondition(const ApidManagerTester& testerState) { |
41 | | - return true; // Can always get existing sequence count |
42 | | -} |
43 | | - |
44 | | -void ApidManagerTester::GetExistingSeqCount::action(ApidManagerTester& testerState) { |
45 | | - testerState.clearHistory(); |
46 | | - ComCfg::Apid::T apid = testerState.shadow_getRandomTrackedApid(); |
47 | | - U16 seqCount = testerState.invoke_to_getApidSeqCountIn(0, apid, 0); |
48 | | - U16 shadowSeqCount = testerState.shadow_getAndIncrementSeqCount(apid); |
49 | | - ASSERT_EQ(seqCount, shadowSeqCount) << "Sequence count for APID " << static_cast<U16>(apid) |
50 | | - << " does not match shadow value." |
51 | | - << " Shadow: " << shadowSeqCount << ", Actual: " << seqCount; |
52 | | -} |
53 | | - |
54 | | -bool ApidManagerTester::GetNewSeqCountOk::precondition(const ApidManagerTester& testerState) { |
55 | | - return testerState.shadow_isTableFull == false; |
56 | | -} |
57 | | - |
58 | | -void ApidManagerTester::GetNewSeqCountOk::action(ApidManagerTester& testerState) { |
59 | | - testerState.clearHistory(); |
60 | | - // Use local constexpr to potentially avoid ODR-use of ApidManager::MAX_TRACKED_APIDS |
61 | | - constexpr U8 maxTrackedApidsVal = ApidManager::MAX_TRACKED_APIDS; |
62 | | - bool isTableFull = !(testerState.shadow_seqCounts.size() < maxTrackedApidsVal); |
63 | | - if (isTableFull) { |
64 | | - testerState.shadow_isTableFull = true; |
65 | | - return; // Cannot get new sequence count if table is full - skip action |
66 | | - } |
67 | | - |
68 | | - ComCfg::Apid::T apid = testerState.shadow_getRandomUntrackedApid(); |
69 | | - U16 seqCount = testerState.invoke_to_getApidSeqCountIn(0, apid, 0); |
70 | | - U16 shadowSeqCount = testerState.shadow_getAndIncrementSeqCount(apid); |
71 | | - ASSERT_EQ(seqCount, shadowSeqCount) << "Sequence count for APID " << static_cast<U16>(apid) |
72 | | - << " does not match shadow value." |
73 | | - << " Shadow: " << shadowSeqCount << ", Actual: " << seqCount; |
74 | | -} |
75 | | - |
76 | | -bool ApidManagerTester::GetNewSeqCountTableFull::precondition(const ApidManagerTester& testerState) { |
77 | | - return testerState.shadow_isTableFull == true; |
78 | | -} |
79 | | - |
80 | | -void ApidManagerTester::GetNewSeqCountTableFull::action(ApidManagerTester& testerState) { |
81 | | - testerState.clearHistory(); |
82 | | - ComCfg::Apid::T apid = testerState.shadow_getRandomUntrackedApid(); |
83 | | - U16 seqCount = testerState.invoke_to_getApidSeqCountIn(0, apid, 0); |
84 | | - // Use local constexpr to potentially avoid ODR-use of ApidManager::SEQUENCE_COUNT_ERROR |
85 | | - constexpr U16 sequenceCountErrorVal = ApidManager::SEQUENCE_COUNT_ERROR; |
86 | | - ASSERT_EQ(seqCount, sequenceCountErrorVal) |
87 | | - << "Expected SEQUENCE_COUNT_ERROR for untracked APID " << static_cast<U16>(apid) << ", but got " << seqCount; |
88 | | - testerState.assertEvents_ApidTableFull_size(__FILE__, __LINE__, 1); |
89 | | - testerState.assertEvents_ApidTableFull(__FILE__, __LINE__, 0, static_cast<U16>(apid)); |
90 | | -} |
91 | | - |
92 | | -bool ApidManagerTester::ValidateSeqCountOk::precondition(const ApidManagerTester& testerState) { |
93 | | - return true; |
94 | 21 | } |
95 | 22 |
|
96 | | -void ApidManagerTester::ValidateSeqCountOk::action(ApidManagerTester& testerState) { |
97 | | - testerState.clearHistory(); |
98 | | - ComCfg::Apid::T apid = testerState.shadow_getRandomTrackedApid(); |
99 | | - U16 shadow_expectedSeqCount = testerState.shadow_seqCounts[apid]; |
100 | | - testerState.invoke_to_validateApidSeqCountIn(0, apid, shadow_expectedSeqCount); |
101 | | - testerState.shadow_validateApidSeqCount(apid, shadow_expectedSeqCount); // keep shadow state in sync |
102 | | - |
103 | | - testerState.assertEvents_UnexpectedSequenceCount_size(__FILE__, __LINE__, 0); |
104 | | -} |
105 | | - |
106 | | -bool ApidManagerTester::ValidateSeqCountFailure::precondition(const ApidManagerTester& testerState) { |
107 | | - return true; |
108 | | -} |
109 | | - |
110 | | -void ApidManagerTester::ValidateSeqCountFailure::action(ApidManagerTester& testerState) { |
111 | | - testerState.clearHistory(); |
112 | | - ComCfg::Apid::T apid = testerState.shadow_getRandomTrackedApid(); |
113 | | - U16 shadow_expectedSeqCount = testerState.shadow_seqCounts.at(apid); |
114 | | - U16 invalidSeqCount = static_cast<U16>( |
115 | | - (shadow_expectedSeqCount + 1) % |
116 | | - (1 << SpacePacketSubfields::SeqCountWidth)); // Or any other value that's different, ensure wrap around |
117 | | - |
118 | | - // Invoke the port with the deliberately incorrect sequence count |
119 | | - testerState.invoke_to_validateApidSeqCountIn(0, apid, invalidSeqCount); |
120 | | - testerState.shadow_validateApidSeqCount(apid, invalidSeqCount); // keep shadow state in sync |
121 | | - |
122 | | - // Now, the event should be logged |
123 | | - testerState.assertEvents_UnexpectedSequenceCount_size(__FILE__, __LINE__, 1); |
124 | | - testerState.assertEvents_UnexpectedSequenceCount(__FILE__, __LINE__, 0, invalidSeqCount, shadow_expectedSeqCount); |
125 | | -} |
126 | | - |
127 | | -// ---------------------------------------------------------------------- |
128 | | -// Helpers |
129 | | -// ---------------------------------------------------------------------- |
130 | | - |
131 | | -U16 ApidManagerTester::shadow_getAndIncrementSeqCount(ComCfg::Apid::T apid) { |
132 | | - // This is a shadow function to simulate the getAndIncrementSeqCount behavior |
133 | | - // without modifying the actual component state. |
134 | | - auto found = this->shadow_seqCounts.find(apid); |
135 | | - if (found != this->shadow_seqCounts.end()) { |
136 | | - U16 seqCount = found->second; |
137 | | - found->second = |
138 | | - static_cast<U16>((seqCount + 1) % (1 << SpacePacketSubfields::SeqCountWidth)); // Increment for next call |
139 | | - return seqCount; // Return the current sequence count |
140 | | - } |
141 | | - // If APID not found, initialize a new entry |
142 | | - if (this->shadow_seqCounts.size() < this->component.MAX_TRACKED_APIDS) { |
143 | | - U16 seqCount = 0; |
144 | | - this->shadow_seqCounts[apid] = static_cast<U16>(seqCount + 1); // increment for next call |
145 | | - return seqCount; // Return the initialized sequence count |
146 | | - } |
147 | | - return this->component.SEQUENCE_COUNT_ERROR; // Return error if APID not found |
148 | | -} |
149 | | - |
150 | | -void ApidManagerTester::shadow_validateApidSeqCount(ComCfg::Apid::T apid, U16 expectedSeqCount) { |
151 | | - // This simply updates the shadow state to the next expected sequence count |
152 | | - auto found = this->shadow_seqCounts.find(apid); |
153 | | - if (found != this->shadow_seqCounts.end()) { |
154 | | - found->second = static_cast<U16>((expectedSeqCount + 1) % (1 << SpacePacketSubfields::SeqCountWidth)); |
155 | | - } |
156 | | -} |
157 | | - |
158 | | -ComCfg::Apid::T ApidManagerTester::shadow_getRandomTrackedApid() { |
159 | | - // Select a random APID from the sequence counts map |
160 | | - U32 mapSize = static_cast<U32>(this->shadow_seqCounts.size()); |
161 | | - U32 randomIndex = STest::Random::lowerUpper(0, mapSize - 1); |
162 | | - ComCfg::Apid apid = std::next(this->shadow_seqCounts.begin(), randomIndex)->first; |
163 | | - return apid; |
164 | | -} |
165 | | - |
166 | | -ComCfg::Apid::T ApidManagerTester::shadow_getRandomUntrackedApid() { |
167 | | - // Select a random APID that is not currently tracked |
168 | | - ComCfg::Apid::T apid; |
169 | | - do { |
170 | | - apid = static_cast<ComCfg::Apid::T>(STest::Random::lowerUpper(10, ComCfg::Apid::SPP_IDLE_PACKET)); |
171 | | - } while (this->shadow_seqCounts.find(apid) != this->shadow_seqCounts.end()); |
172 | | - return apid; |
173 | | -} |
| 23 | +ApidManagerTester::~ApidManagerTester() {} |
174 | 24 |
|
175 | 25 | } // namespace Ccsds |
| 26 | + |
176 | 27 | } // namespace Svc |
0 commit comments