AdminTestFramework.c 6.7 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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "admin/AdminClient.h"
  17. #include "admin/angel/AngelInit.h"
  18. #include "admin/angel/InterfaceWaiter.h"
  19. #include "admin/testframework/AdminTestFramework.h"
  20. #include "benc/String.h"
  21. #include "benc/Int.h"
  22. #include "benc/serialization/standard/BencMessageWriter.h"
  23. #include "crypto/random/Random.h"
  24. #include "memory/Allocator.h"
  25. #include "memory/MallocAllocator.h"
  26. #include "interface/FramingInterface.h"
  27. #include "interface/addressable/UDPAddrInterface.h"
  28. #include "io/FileWriter.h"
  29. #include "io/Writer.h"
  30. #include "util/events/EventBase.h"
  31. #include "util/events/Pipe.h"
  32. #include "util/events/Process.h"
  33. #include "util/Assert.h"
  34. #include "util/log/Log.h"
  35. #include "util/log/WriterLog.h"
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. static void spawnAngel(char* testName,
  39. char* asClientPipeName,
  40. struct EventBase* base,
  41. struct Allocator* alloc)
  42. {
  43. char* args[] = { testName, "angel", asClientPipeName, NULL };
  44. struct Allocator* tempAlloc = Allocator_child(alloc);
  45. char* path = Process_getPath(tempAlloc);
  46. Assert_true(path);
  47. Process_spawn(path, args, base, alloc);
  48. Allocator_free(tempAlloc);
  49. }
  50. /** @return a string representing the address and port to connect to. */
  51. static void initAngel(struct Pipe* asClientPipe,
  52. struct Interface* asCoreIface,
  53. char* asCorePipeName,
  54. struct EventBase* eventBase,
  55. struct Log* logger,
  56. struct Allocator* alloc,
  57. struct Random* rand)
  58. {
  59. Dict admin = Dict_CONST(
  60. String_CONST("bind"), String_OBJ(String_CONST("127.0.0.1")), Dict_CONST(
  61. String_CONST("corePipeName"), String_OBJ(String_CONST(asCorePipeName)), Dict_CONST(
  62. String_CONST("pass"), String_OBJ(String_CONST("abcd")), NULL
  63. )));
  64. Dict message = Dict_CONST(
  65. String_CONST("admin"), Dict_OBJ(&admin), NULL
  66. );
  67. struct Allocator* tempAlloc = Allocator_child(alloc);
  68. struct Message* toAngel = Message_new(0, 1024, tempAlloc);
  69. BencMessageWriter_write(&message, toAngel, NULL);
  70. Log_info(logger, "Writing intial configuration to angel on [%s]", asClientPipe->name);
  71. Interface_sendMessage(&asClientPipe->iface, toAngel);
  72. // This is client->angel->core data, we can throw this away.
  73. //struct Message* angelToCore =
  74. InterfaceWaiter_waitForData(asCoreIface, eventBase, tempAlloc, NULL);
  75. // unterminated string
  76. //Log_info(logger, "Init message from angel to core: [%s]", angelToCore->bytes);
  77. // Send response on behalf of core.
  78. Dict* coreToAngelResp = Dict_new(tempAlloc);
  79. Dict_putString(coreToAngelResp, String_CONST("error"), String_CONST("none"), tempAlloc);
  80. struct Message* coreToAngelMsg = Message_new(0, 256, tempAlloc);
  81. BencMessageWriter_write(coreToAngelResp, coreToAngelMsg, NULL);
  82. Interface_sendMessage(asCoreIface, coreToAngelMsg);
  83. // This is angel->client data, it will tell us which port was bound.
  84. struct Message* angelToClient =
  85. InterfaceWaiter_waitForData(&asClientPipe->iface, eventBase, tempAlloc, NULL);
  86. uint8_t lastByte = angelToClient->bytes[angelToClient->length-1];
  87. angelToClient->bytes[angelToClient->length-1] = '\0';
  88. printf("Response from angel to client: [%s%c]\n", angelToClient->bytes, (char)lastByte);
  89. Allocator_free(tempAlloc);
  90. return;
  91. }
  92. /**
  93. * This spawns itself as the Angel process which spawns itself again as the core process.
  94. * The "core process" pipes all of its inputs back to the originating process
  95. */
  96. struct AdminTestFramework* AdminTestFramework_setUp(int argc, char** argv, char* testName)
  97. {
  98. if (argc > 2 && !CString_strcmp(testName, argv[1]) && !CString_strcmp("angel", argv[2])) {
  99. exit(AngelInit_main(argc-1, &argv[1]));
  100. }
  101. struct Allocator* alloc = MallocAllocator_new(1<<20);
  102. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  103. Assert_true(logwriter);
  104. struct Log* logger = WriterLog_new(logwriter, alloc);
  105. struct EventBase* eventBase = EventBase_new(alloc);
  106. struct Random* rand = Random_new(alloc, logger, NULL);
  107. char asClientPipeName[32] = {0};
  108. Random_base32(rand, (uint8_t*)asClientPipeName, 31);
  109. struct Pipe* asClientPipe = Pipe_named(asClientPipeName, eventBase, NULL, alloc);
  110. asClientPipe->logger = logger;
  111. char asCorePipeName[32] = {0};
  112. Random_base32(rand, (uint8_t*)asCorePipeName, 31);
  113. struct Pipe* asCorePipe = Pipe_named(asCorePipeName, eventBase, NULL, alloc);
  114. asCorePipe->logger = logger;
  115. struct Interface* asCoreIface = FramingInterface_new(65535, &asCorePipe->iface, alloc);
  116. spawnAngel(testName, asClientPipeName, eventBase, alloc);
  117. Log_info(logger, "Initializing Angel");
  118. initAngel(asClientPipe, asCoreIface, (char*)asCorePipe->name, eventBase, logger, alloc, rand);
  119. struct Sockaddr_storage addr;
  120. Assert_true(!Sockaddr_parse("127.0.0.1", &addr));
  121. Log_info(logger, "Binding UDP admin socket");
  122. struct AddrInterface* udpAdmin =
  123. UDPAddrInterface_new(eventBase, &addr.addr, alloc, NULL, logger);
  124. String* password = String_new("abcd", alloc);
  125. struct Admin* admin = Admin_new(udpAdmin, alloc, logger, eventBase, password);
  126. // Now setup the client.
  127. struct AdminClient* client =
  128. AdminClient_new(udpAdmin->addr, password, eventBase, logger, alloc);
  129. Assert_true(client);
  130. return Allocator_clone(alloc, (&(struct AdminTestFramework) {
  131. .admin = admin,
  132. .client = client,
  133. .alloc = alloc,
  134. .eventBase = eventBase,
  135. .logger = logger,
  136. .addr = Sockaddr_clone(udpAdmin->addr, alloc),
  137. .angelInterface = asCoreIface
  138. }));
  139. }
  140. void AdminTestFramework_tearDown(struct AdminTestFramework* framework)
  141. {
  142. Dict* exitCmd = Dict_new(framework->alloc);
  143. Dict_putString(exitCmd, String_CONST("q"), String_CONST("Angel_exit"), framework->alloc);
  144. struct Message* msg = Message_new(0, 256, framework->alloc);
  145. BencMessageWriter_write(exitCmd, msg, NULL);
  146. Interface_sendMessage(framework->angelInterface, msg);
  147. Allocator_free(framework->alloc);
  148. }