LinkState_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "memory/Allocator.h"
  17. #include "subnode/LinkState.h"
  18. #include "util/Assert.h"
  19. #include "wire/Message.h"
  20. #include "util/Hex.h"
  21. #define CYCLES 10
  22. static uint64_t randVarInt(struct Random* rand)
  23. {
  24. uint64_t val = Random_uint64(rand);
  25. if ((val & 0xff) < 192) {
  26. return (val >> 8) & 0xff;
  27. }
  28. if ((val & 0xff) < 224) {
  29. return (val >> 8) & 0xffff;
  30. }
  31. if ((val & 0xff) < 240) {
  32. return (val >> 8) & 0xffffffff;
  33. }
  34. return Random_uint64(rand);
  35. }
  36. static void randomLs(struct Random* rand, struct LinkState* ls, bool total)
  37. {
  38. if (total) {
  39. ls->samples = Random_uint32(rand);
  40. ls->nodeId = Random_uint16(rand);
  41. }
  42. for (int i = 0; i < LinkState_SLOTS; i++) {
  43. ls->lagSlots[i] = Random_uint16(rand); //randVarInt(rand);
  44. ls->dropSlots[i] = Random_uint16(rand); // randVarInt(rand);
  45. ls->kbRecvSlots[i] = Random_uint32(rand); //randVarInt(rand);
  46. }
  47. }
  48. static void randomLsUpdate(struct Random* rand, struct LinkState* ls)
  49. {
  50. uint8_t toUpdate = Random_uint8(rand) % 20;
  51. if (!toUpdate) { return; }
  52. //printf(">>%d\n", toUpdate);
  53. int samplesNext = ls->samples;
  54. if (toUpdate >= LinkState_SLOTS) {
  55. samplesNext += LinkState_SLOTS;
  56. randomLs(rand, ls, false);
  57. } else {
  58. samplesNext += toUpdate;
  59. int i = ls->samples % LinkState_SLOTS;
  60. do {
  61. ls->lagSlots[i] = randVarInt(rand);
  62. ls->dropSlots[i] = randVarInt(rand);
  63. ls->kbRecvSlots[i] = randVarInt(rand);
  64. i = (i + 1) % LinkState_SLOTS;
  65. } while (i != samplesNext % LinkState_SLOTS);
  66. }
  67. Assert_true(samplesNext - ls->samples <= LinkState_SLOTS);
  68. ls->samples = samplesNext;
  69. }
  70. static void applyStateUpdates(struct LinkState* local, struct Message* msg)
  71. {
  72. if (!Message_getLength(msg)) { return; }
  73. uint8_t length = msg->msgbytes[0];
  74. struct VarInt_Iter it;
  75. Assert_true(!LinkState_mkDecoder(msg, &it));
  76. uint32_t id = 0;
  77. Assert_true(!LinkState_getNodeId(&it, &id));
  78. Assert_true(id == local->nodeId);
  79. Er_assert(Message_eshift(msg, -length));
  80. applyStateUpdates(local, msg);
  81. Assert_true(!LinkState_decode(&it, local));
  82. }
  83. static void assertSame(struct LinkState* beforeState,
  84. struct LinkState* updated,
  85. struct Message* update)
  86. {
  87. struct LinkState local;
  88. Bits_memcpy(&local, beforeState, sizeof(struct LinkState));
  89. //printf("%02x %02x\n", local.nodeId, local.samples);
  90. applyStateUpdates(&local, update);
  91. #if 0
  92. printf("%02x %02x\n", updated->nodeId, updated->samples);
  93. printf("%02x %02x\n", local.nodeId, local.samples);
  94. for (int i = 0; i < LinkState_SLOTS; i++) {
  95. printf("%02x %02x %02x\n",
  96. updated->lagSlots[i], updated->dropSlots[i], updated->kbRecvSlots[i]);
  97. printf("%02x %02x %02x\n\n",
  98. local.lagSlots[i], local.dropSlots[i], local.kbRecvSlots[i]);
  99. }
  100. printf("\n");
  101. #endif
  102. Assert_true(!Bits_memcmp(&local, updated, sizeof(struct LinkState)));
  103. }
  104. static void testStatic()
  105. {
  106. uint8_t* hex =
  107. "\x20\x03\x06\x00\x00\x00\x00\x00\x00"
  108. "\x04" "\x10"
  109. "\x13\x00\x01"
  110. "\x12\x00\x02"
  111. "\x13\x00\x02"
  112. "\x13\x00\x00"
  113. "\x14\x00\x03"
  114. "\x12\x00\x01"
  115. "\x13\x00\x01";
  116. struct LinkState ls = { .nodeId = 0 };
  117. struct VarInt_Iter it = { .start = 0 };
  118. struct Message msg = Message_foreign(32, hex);
  119. Assert_true(!LinkState_mkDecoder(&msg, &it));
  120. Assert_true(!LinkState_decode(&it, &ls));
  121. // printf("%u %u\n", ls.nodeId, ls.samples);
  122. // for (int i = 0; i < LinkState_SLOTS; i++) {
  123. // printf("%u %u %u\n", ls.lagSlots[i], ls.dropSlots[i], ls.kbRecvSlots[i]);
  124. // }
  125. struct LinkState gold = {
  126. .nodeId = 4,
  127. .samples = 7,
  128. .lagSlots = { 19, 19, 20, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 18 },
  129. .dropSlots = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  130. .kbRecvSlots = { 2, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2 },
  131. };
  132. Assert_true(!Bits_memcmp(&ls, &gold, sizeof(struct LinkState)));
  133. }
  134. int main(int argc, char* argv[])
  135. {
  136. testStatic();
  137. struct Allocator* mainAlloc = Allocator_new(1<<18);
  138. struct Random* rand = Random_new(mainAlloc, NULL, NULL);
  139. int cycles = CYCLES;
  140. struct Message* msg = Message_new(0, 2048, mainAlloc);
  141. for (int cycle = 0; cycle < cycles; cycle++) {
  142. uint16_t nodeId = Random_uint16(rand);
  143. struct LinkState ls0 = { .nodeId = nodeId };
  144. struct LinkState ls = { .nodeId = nodeId, .samples = LinkState_SLOTS };
  145. randomLs(rand, &ls, false);
  146. Assert_true(!Message_getLength(msg));
  147. Assert_true(!LinkState_encode(msg, &ls, 0));
  148. assertSame(&ls0, &ls, msg);
  149. for (int cc = 0; cc < 100; cc++) {
  150. Bits_memcpy(&ls0, &ls, sizeof(struct LinkState));
  151. randomLsUpdate(rand, &ls);
  152. Assert_true(!LinkState_encode(msg, &ls, ls0.samples));
  153. //printf("L>%d\n", Message_getLength(msg));
  154. //for (int i = 0; i < Message_getLength(msg); i++) { printf("%02x", msg->bytes[i]); }
  155. //printf("\n");
  156. assertSame(&ls0, &ls, msg);
  157. }
  158. //printf("Test %d\n", Message_getLength(msg));
  159. //for (int i = 0; i < Message_getLength(msg); i++) { printf("%02x", msg->bytes[i]); }
  160. //printf("\n");
  161. }
  162. Allocator_free(mainAlloc);
  163. return 0;
  164. }