NodeStore_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/MallocAllocator.h"
  16. #include "dht/Address.h"
  17. #include "dht/dhtcore/Node.h"
  18. #include "dht/dhtcore/NodeList.h"
  19. #include "dht/dhtcore/NodeStore.h"
  20. #include "switch/NumberCompress.h"
  21. #include "util/Assert.h"
  22. #include "util/log/FileWriterLog.h"
  23. static uint8_t* MY_ADDR =
  24. "v13.0000.0000.0000.0001.3c95l67xxd0juy9zjn7scz5lnjsrcum33y9ghbnqf64d6z0t7sw0.k";
  25. static uint8_t* ADDRS[] = {
  26. "v13.0000.0000.0000.001f.usclqxtgkksmgwv10h8h3pltm3zy27bddb20mpsbrvjlcw4d9gl0.k",
  27. "v13.0000.0000.0000.001b.03ws4vngbq56ymd14vbpd92zdfr0783t7g6u3k4dtb1kuw5m62v0.k",
  28. "v13.0000.0000.0000.0019.8u2pvwuf1wmf5hwxytckbk4sbyrg3rdnqdwulbgsbmw408grm500.k",
  29. "v13.0000.0000.0000.0017.bf39dq2mubq17x2lmz8cwgr839s95b6gk7dmcty22uw3dj7v5zy0.k",
  30. "v13.0000.0000.0000.0015.q402jm870c215kdvf4wy2qvpt4kdrx0b4zyx2vnv2fdfprf41fk0.k",
  31. "v13.0000.0000.0000.0013.6npk9pfdw09t0ldp0c9usrp8pkhttg0104849ng6j5gsz3w8q3x0.k",
  32. "v13.0000.0000.0000.00b6.t9lpkc69nwpxpnusc7nlgrrjmzdcjhgf52zhhr9k69t9x6hrz5c0.k",
  33. "v13.0000.0000.0000.00b2.05t007gun13qnhm5czlkjlp14lpr2v2j6f4g6bmzgbwv5mj9uy60.k",
  34. "v13.0000.0000.0000.00ae.f5d1l67lb3dl7z41l1lwmh0jsptq382vsyvr999brjdjqutj5m90.k",
  35. "v13.0000.0000.0000.00aa.61jw1hdru3tnwv3vfpt9vmmbvyhfxc8chd9msf1jhumq2y3h5pn0.k",
  36. "v13.0000.0000.0000.00a2.684v75l5czfvgmr5qkb60xd7d9l79zxg5nyj5wmbhr8nxm7wzn20.k",
  37. "v13.0000.0000.0000.009e.th3p5791z6xr24plc3487xfb9tfy4n7n51y8pbhnr9771kluhr10.k",
  38. "v13.0000.0000.0000.00ba.d40x5rkb8jj5v1521j5l6wd1pu7svzrmyb2kvf1rj7ll0kuydt40.k",
  39. "v13.0000.0000.0000.001d.rujhjmq178wtfxccuwp3h17uq7u7phfr1t1m1zn80855h2wngl50.k",
  40. "v13.0000.0000.0000.00a6.0czm5qrryjrhc4dv9zcl148pnbur1869zufrcfw8f9b7vw132yu0.k",
  41. NULL
  42. };
  43. static void addNode(struct NodeStore* ns, uint8_t* address, struct Allocator* alloc)
  44. {
  45. struct EncodingScheme* scheme = NumberCompress_v3x5x8_defineScheme(alloc);
  46. struct Address* addr = Address_fromString(String_new(address, alloc), alloc);
  47. Assert_true(NodeStore_discoverNode(ns, addr, scheme, 0, 100));
  48. }
  49. static void checkList(struct NodeList* list,
  50. uint64_t* expectedOutputs,
  51. struct Log* logger,
  52. struct Allocator* alloc)
  53. {
  54. int j = 0;
  55. for (int i = (int)list->size - 1; i >= 0; i--) {
  56. String* str = Address_toString(&list->nodes[i]->address, alloc);
  57. Log_debug(logger, "output: [%s]", str->bytes);
  58. Assert_true(expectedOutputs[j]);
  59. Assert_true(expectedOutputs[j] == list->nodes[i]->address.path);
  60. j++;
  61. }
  62. Assert_true(!expectedOutputs[j]);
  63. }
  64. static void getPeersTest(struct EventBase* base, struct Log* logger, struct Allocator* alloc)
  65. {
  66. struct Address* myAddr = Address_fromString(String_new(MY_ADDR, alloc), alloc);
  67. struct NodeStore* ns = NodeStore_new(myAddr, alloc, base, logger, NULL);
  68. for (int i = 0; ADDRS[i]; i++) {
  69. addNode(ns, ADDRS[i], alloc);
  70. }
  71. struct NodeList* list = NodeStore_getPeers(0, 8, alloc, ns);
  72. checkList(list, (uint64_t[]){ 0x01,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0 }, logger, alloc);
  73. list = NodeStore_getPeers(0x1f, 8, alloc, ns);
  74. checkList(list, (uint64_t[]){ 0x1f,0x9e,0xa2,0xa6,0xaa,0xae,0xb2,0xb6,0 }, logger, alloc);
  75. list = NodeStore_getPeers(0xb6, 8, alloc, ns);
  76. checkList(list, (uint64_t[]){ 0xb6,0xba,0 }, logger, alloc);
  77. }
  78. int main(int argc, char** argv)
  79. {
  80. struct Allocator* alloc = MallocAllocator_new(1<<20);
  81. struct Log* logger = FileWriterLog_new(stdout, alloc);
  82. struct EventBase* base = EventBase_new(alloc);
  83. getPeersTest(base, logger, alloc);
  84. Allocator_free(alloc);
  85. return 0;
  86. }