NodeStore_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "memory/Allocator.h"
  16. #include "crypto/Key.h"
  17. #include "crypto/random/Random.h"
  18. #include "dht/Address.h"
  19. #include "dht/dhtcore/Node.h"
  20. #include "dht/dhtcore/NodeList.h"
  21. #include "dht/dhtcore/NodeStore.h"
  22. #include "switch/NumberCompress.h"
  23. #include "util/Assert.h"
  24. #include "util/Base32.h"
  25. #include "util/log/FileWriterLog.h"
  26. static uint8_t* ADDRS[] = {
  27. "v13.0000.0000.0000.001f.usclqxtgkksmgwv10h8h3pltm3zy27bddb20mpsbrvjlcw4d9gl0.k",
  28. "v13.0000.0000.0000.001b.03ws4vngbq56ymd14vbpd92zdfr0783t7g6u3k4dtb1kuw5m62v0.k",
  29. "v13.0000.0000.0000.0019.8u2pvwuf1wmf5hwxytckbk4sbyrg3rdnqdwulbgsbmw408grm500.k",
  30. "v13.0000.0000.0000.0017.bf39dq2mubq17x2lmz8cwgr839s95b6gk7dmcty22uw3dj7v5zy0.k",
  31. "v13.0000.0000.0000.0015.q402jm870c215kdvf4wy2qvpt4kdrx0b4zyx2vnv2fdfprf41fk0.k",
  32. "v13.0000.0000.0000.0013.6npk9pfdw09t0ldp0c9usrp8pkhttg0104849ng6j5gsz3w8q3x0.k",
  33. "v13.0000.0000.0000.00b6.t9lpkc69nwpxpnusc7nlgrrjmzdcjhgf52zhhr9k69t9x6hrz5c0.k",
  34. "v13.0000.0000.0000.00b2.05t007gun13qnhm5czlkjlp14lpr2v2j6f4g6bmzgbwv5mj9uy60.k",
  35. "v13.0000.0000.0000.00ae.f5d1l67lb3dl7z41l1lwmh0jsptq382vsyvr999brjdjqutj5m90.k",
  36. "v13.0000.0000.0000.00aa.61jw1hdru3tnwv3vfpt9vmmbvyhfxc8chd9msf1jhumq2y3h5pn0.k",
  37. "v13.0000.0000.0000.00a2.684v75l5czfvgmr5qkb60xd7d9l79zxg5nyj5wmbhr8nxm7wzn20.k",
  38. "v13.0000.0000.0000.009e.th3p5791z6xr24plc3487xfb9tfy4n7n51y8pbhnr9771kluhr10.k",
  39. "v13.0000.0000.0000.00ba.d40x5rkb8jj5v1521j5l6wd1pu7svzrmyb2kvf1rj7ll0kuydt40.k",
  40. "v13.0000.0000.0000.001d.rujhjmq178wtfxccuwp3h17uq7u7phfr1t1m1zn80855h2wngl50.k",
  41. "v13.0000.0000.0000.00a6.0czm5qrryjrhc4dv9zcl148pnbur1869zufrcfw8f9b7vw132yu0.k",
  42. NULL
  43. };
  44. static void addNode(struct NodeStore* ns, uint8_t* address, struct Allocator* alloc)
  45. {
  46. struct EncodingScheme* scheme = NumberCompress_v3x5x8_defineScheme(alloc);
  47. struct Address* addr = Address_fromString(String_new(address, alloc), alloc);
  48. Assert_true(NodeStore_discoverNode(ns, addr, scheme, 0, 100));
  49. }
  50. static void checkList(struct NodeList* list,
  51. uint64_t* expectedOutputs,
  52. struct Log* logger,
  53. struct Allocator* alloc)
  54. {
  55. int j = 0;
  56. for (int i = (int)list->size - 1; i >= 0; i--) {
  57. String* str = Address_toStringKey(&list->nodes[i]->address, alloc);
  58. Log_info(logger, "output: [%s]", str->bytes);
  59. Assert_true(expectedOutputs[j]);
  60. Assert_true(expectedOutputs[j] == list->nodes[i]->address.path);
  61. j++;
  62. }
  63. Assert_true(!expectedOutputs[j]);
  64. }
  65. static void genAddress(uint8_t* addr, struct Random* rand)
  66. {
  67. uint8_t publicKey[32];
  68. uint8_t ip[16];
  69. uint8_t privateKey[32];
  70. Key_gen(ip, publicKey, privateKey, rand);
  71. uint8_t* publicKeyBase32 = &addr[24];
  72. Assert_true(publicKeyBase32);
  73. Base32_encode(publicKeyBase32, 53, publicKey, 32);
  74. publicKeyBase32[52] = '.';
  75. publicKeyBase32[53] = 'k';
  76. }
  77. static void getPeersTest(uint8_t* addrs[],
  78. struct EventBase* base,
  79. struct Log* logger,
  80. struct Allocator* alloc,
  81. struct Random* rand)
  82. {
  83. uint8_t my_addr[] =
  84. "v13.0000.0000.0000.0001.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.k";
  85. genAddress(my_addr, rand);
  86. struct Address* myAddr = Address_fromString(String_new(my_addr, alloc), alloc);
  87. Assert_true(myAddr);
  88. struct NodeStore* ns = NodeStore_new(myAddr, alloc, base, logger, NULL);
  89. NodeStore_setFullVerify(ns, true);
  90. for (int i = 0; addrs[i]; i++) {
  91. addNode(ns, addrs[i], alloc);
  92. }
  93. struct NodeList* list = NodeStore_getPeers(0, 8, alloc, ns);
  94. checkList(list, (uint64_t[]){ 0x01,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0 }, logger, alloc);
  95. list = NodeStore_getPeers(0x1f, 8, alloc, ns);
  96. checkList(list, (uint64_t[]){ 0x1f,0x9e,0xa2,0xa6,0xaa,0xae,0xb2,0xb6,0 }, logger, alloc);
  97. list = NodeStore_getPeers(0xb6, 8, alloc, ns);
  98. checkList(list, (uint64_t[]){ 0xb6,0xba,0 }, logger, alloc);
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. struct Allocator* alloc = Allocator_new(1<<20);
  103. struct Log* logger = FileWriterLog_new(stdout, alloc);
  104. struct EventBase* base = EventBase_new(alloc);
  105. struct Random* rand = Random_new(alloc, NULL, NULL);
  106. #if defined(ADDRESS_PREFIX) || defined(ADDRESS_PREFIX_BITS)
  107. // Make all addresses writeable
  108. for (uint8_t** addr = ADDRS; *addr; addr++) {
  109. char *addr_rw = Allocator_malloc(alloc, 79);
  110. Bits_memcpy(addr_rw, *addr, 79);
  111. *addr = addr_rw;
  112. }
  113. for (uint8_t** addr = ADDRS; *addr; addr++) {
  114. genAddress(*addr, rand);
  115. }
  116. #endif
  117. getPeersTest(ADDRS, base, logger, alloc, rand);
  118. Allocator_free(alloc);
  119. return 0;
  120. }