UDPInterface_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "admin/testframework/AdminTestFramework.h"
  16. #include "admin/Admin.h"
  17. #include "admin/AdminClient.h"
  18. #include "benc/Dict.h"
  19. #include "benc/String.h"
  20. #include "benc/Int.h"
  21. #include "interface/UDPInterface_admin.h"
  22. #include "memory/Allocator.h"
  23. #include "memory/MallocAllocator.h"
  24. #include "interface/InterfaceController.h"
  25. #include "io/FileWriter.h"
  26. #include "io/Writer.h"
  27. #include "util/Assert.h"
  28. #include "util/log/Log.h"
  29. #include "util/CString.h"
  30. static int registerPeer(struct InterfaceController* ic,
  31. uint8_t herPublicKey[32],
  32. String* password,
  33. bool requireAuth,
  34. bool transient,
  35. struct Interface* iface)
  36. {
  37. return 0;
  38. }
  39. static void ifNewCallback(struct AdminClient_Promise* p, struct AdminClient_Result* res)
  40. {
  41. struct AdminTestFramework* fw = p->userData;
  42. Assert_true(!res->err);
  43. //printf("result content: >>%s<<", res->messageBytes);
  44. // d11:bindAddress13:0.0.0.0:362615:error4:none15:interfaceNumberi0e4:txid8:01000000e
  45. Assert_true(CString_strstr(res->messageBytes, "5:error4:none15:interfaceNumberi0e"));
  46. EventBase_endLoop(fw->eventBase);
  47. }
  48. static void badKeyCallback(struct AdminClient_Promise* p, struct AdminClient_Result* res)
  49. {
  50. struct AdminTestFramework* fw = p->userData;
  51. Assert_true(CString_strstr(res->messageBytes, "5:error30:key must be 52 characters long"));
  52. EventBase_endLoop(fw->eventBase);
  53. //printf("result content: >>%s<<", res->messageBytes);
  54. }
  55. static void goodCallback(struct AdminClient_Promise* p, struct AdminClient_Result* res)
  56. {
  57. struct AdminTestFramework* fw = p->userData;
  58. Assert_true(CString_strstr(res->messageBytes, "5:error4:none"));
  59. EventBase_endLoop(fw->eventBase);
  60. //printf("result content: >>%s<<", res->messageBytes);
  61. }
  62. int main(int argc, char** argv)
  63. {
  64. struct AdminTestFramework* fw = AdminTestFramework_setUp(argc, argv, "UDPInterface_test");
  65. // mock interface controller.
  66. struct InterfaceController ifController = {
  67. .registerPeer = registerPeer
  68. };
  69. UDPInterface_admin_register(fw->eventBase,
  70. fw->alloc,
  71. fw->logger,
  72. fw->admin,
  73. &ifController);
  74. Dict* dict = Dict_new(fw->alloc);
  75. struct AdminClient_Promise* promise =
  76. AdminClient_rpcCall(String_CONST("UDPInterface_new"), dict, fw->client, fw->alloc);
  77. promise->callback = ifNewCallback;
  78. promise->userData = fw;
  79. EventBase_beginLoop(fw->eventBase);
  80. // bad key
  81. dict = Dict_new(fw->alloc);
  82. Dict_putString(dict, String_CONST("publicKey"), String_CONST("notAValidKey"), fw->alloc);
  83. Dict_putString(dict, String_CONST("address"), String_CONST("127.0.0.1:12345"), fw->alloc);
  84. promise = AdminClient_rpcCall(
  85. String_CONST("UDPInterface_beginConnection"), dict, fw->client, fw->alloc);
  86. promise->callback = badKeyCallback;
  87. promise->userData = fw;
  88. EventBase_beginLoop(fw->eventBase);
  89. dict = Dict_new(fw->alloc);
  90. Dict_putString(dict,
  91. String_CONST("publicKey"),
  92. String_CONST("c86pf0ngj3wlb569juqm10yzv29n9t4w5tmsyhx6xd3fbqjlcu50.k"),
  93. fw->alloc);
  94. Dict_putString(dict, String_CONST("address"), String_CONST("127.0.0.1:12345"), fw->alloc);
  95. promise = AdminClient_rpcCall(
  96. String_CONST("UDPInterface_beginConnection"), dict, fw->client, fw->alloc);
  97. promise->callback = goodCallback;
  98. promise->userData = fw;
  99. EventBase_beginLoop(fw->eventBase);
  100. AdminTestFramework_tearDown(fw);
  101. return 0;
  102. }