AdminTestFramework.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/BencSerializer.h"
  23. #include "benc/serialization/standard/StandardBencSerializer.h"
  24. #include "crypto/random/Random.h"
  25. #include "memory/Allocator.h"
  26. #include "memory/MallocAllocator.h"
  27. #include "interface/FramingInterface.h"
  28. #include "interface/addressable/UDPAddrInterface.h"
  29. #include "io/ArrayReader.h"
  30. #include "io/ArrayWriter.h"
  31. #include "io/FileWriter.h"
  32. #include "io/Writer.h"
  33. #include "util/events/EventBase.h"
  34. #include "util/events/Pipe.h"
  35. #include "util/events/Process.h"
  36. #include "util/Assert.h"
  37. #include "util/log/Log.h"
  38. #include "util/log/WriterLog.h"
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. static void spawnAngel(char* testName,
  42. char* asClientPipeName,
  43. struct EventBase* base,
  44. struct Allocator* alloc)
  45. {
  46. char* args[] = { testName, "angel", asClientPipeName, NULL };
  47. struct Allocator* tempAlloc = Allocator_child(alloc);
  48. char* path = Process_getPath(tempAlloc);
  49. Assert_true(path);
  50. Process_spawn(path, args, base, alloc);
  51. Allocator_free(tempAlloc);
  52. }
  53. /** @return a string representing the address and port to connect to. */
  54. static void initAngel(struct Pipe* asClientPipe,
  55. struct Interface* asCoreIface,
  56. char* asCorePipeName,
  57. struct EventBase* eventBase,
  58. struct Log* logger,
  59. struct Allocator* alloc,
  60. struct Random* rand)
  61. {
  62. Dict admin = Dict_CONST(
  63. String_CONST("bind"), String_OBJ(String_CONST("127.0.0.1")), Dict_CONST(
  64. String_CONST("corePipeName"), String_OBJ(String_CONST(asCorePipeName)), Dict_CONST(
  65. String_CONST("pass"), String_OBJ(String_CONST("abcd")), NULL
  66. )));
  67. Dict message = Dict_CONST(
  68. String_CONST("admin"), Dict_OBJ(&admin), NULL
  69. );
  70. struct Allocator* tempAlloc = Allocator_child(alloc);
  71. #define BUFFER_SZ 1023
  72. uint8_t buff[BUFFER_SZ + 1] = {0};
  73. struct Writer* w = ArrayWriter_new(buff, BUFFER_SZ, tempAlloc);
  74. StandardBencSerializer_get()->serializeDictionary(w, &message);
  75. struct Message* toAngel = Allocator_malloc(tempAlloc, sizeof(struct Message) + w->bytesWritten);
  76. toAngel->bytes = (uint8_t*) (&toAngel[1]);
  77. toAngel->length = toAngel->capacity = w->bytesWritten;
  78. toAngel->padding = 0;
  79. toAngel->alloc = tempAlloc;
  80. Bits_memcpy(toAngel->bytes, buff, toAngel->length);
  81. Log_info(logger, "Writing intial configuration to angel on [%s] config: [%s]",
  82. asClientPipe->name, buff);
  83. Interface_sendMessage(&asClientPipe->iface, toAngel);
  84. // This is client->angel->core data, we can throw this away.
  85. //struct Message* angelToCore =
  86. InterfaceWaiter_waitForData(asCoreIface, eventBase, tempAlloc, NULL);
  87. // unterminated string
  88. //Log_info(logger, "Init message from angel to core: [%s]", angelToCore->bytes);
  89. // Send response on behalf of core.
  90. char* coreToAngelResponse = " PADDING "
  91. "d"
  92. "5:error" "4:none"
  93. "e";
  94. struct Message* m = &(struct Message) {
  95. .bytes = (uint8_t*) coreToAngelResponse,
  96. .length = CString_strlen(coreToAngelResponse),
  97. .padding = 0,
  98. .capacity = CString_strlen(coreToAngelResponse)
  99. };
  100. Message_shift(m, -24, NULL);
  101. m = Message_clone(m, tempAlloc);
  102. Interface_sendMessage(asCoreIface, m);
  103. // This is angel->client data, it will tell us which port was bound.
  104. struct Message* angelToClient =
  105. InterfaceWaiter_waitForData(&asClientPipe->iface, eventBase, tempAlloc, NULL);
  106. printf("Response from angel to client: [%s]\n", angelToClient->bytes);
  107. Allocator_free(tempAlloc);
  108. return;
  109. }
  110. /**
  111. * This spawns itself as the Angel process which spawns itself again as the core process.
  112. * The "core process" pipes all of its inputs back to the originating process
  113. */
  114. struct AdminTestFramework* AdminTestFramework_setUp(int argc, char** argv, char* testName)
  115. {
  116. if (argc > 2 && !CString_strcmp(testName, argv[1]) && !CString_strcmp("angel", argv[2])) {
  117. exit(AngelInit_main(argc-1, &argv[1]));
  118. }
  119. struct Allocator* alloc = MallocAllocator_new(1<<20);
  120. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  121. Assert_true(logwriter);
  122. struct Log* logger = WriterLog_new(logwriter, alloc);
  123. struct EventBase* eventBase = EventBase_new(alloc);
  124. struct Random* rand = Random_new(alloc, logger, NULL);
  125. char asClientPipeName[32] = {0};
  126. Random_base32(rand, (uint8_t*)asClientPipeName, 31);
  127. struct Pipe* asClientPipe = Pipe_named(asClientPipeName, eventBase, NULL, alloc);
  128. asClientPipe->logger = logger;
  129. char asCorePipeName[32] = {0};
  130. Random_base32(rand, (uint8_t*)asCorePipeName, 31);
  131. struct Pipe* asCorePipe = Pipe_named(asCorePipeName, eventBase, NULL, alloc);
  132. asCorePipe->logger = logger;
  133. struct Interface* asCoreIface = FramingInterface_new(65535, &asCorePipe->iface, alloc);
  134. spawnAngel(testName, asClientPipeName, eventBase, alloc);
  135. Log_info(logger, "Initializing Angel");
  136. initAngel(asClientPipe, asCoreIface, (char*)asCorePipe->name, eventBase, logger, alloc, rand);
  137. struct Sockaddr_storage addr;
  138. Assert_true(!Sockaddr_parse("127.0.0.1", &addr));
  139. Log_info(logger, "Binding UDP admin socket");
  140. struct AddrInterface* udpAdmin =
  141. UDPAddrInterface_new(eventBase, &addr.addr, alloc, NULL, logger);
  142. String* password = String_new("abcd", alloc);
  143. struct Admin* admin = Admin_new(udpAdmin, alloc, logger, eventBase, password);
  144. // Now setup the client.
  145. struct AdminClient* client =
  146. AdminClient_new(udpAdmin->addr, password, eventBase, logger, alloc);
  147. Assert_true(client);
  148. return Allocator_clone(alloc, (&(struct AdminTestFramework) {
  149. .admin = admin,
  150. .client = client,
  151. .alloc = alloc,
  152. .eventBase = eventBase,
  153. .logger = logger,
  154. .addr = Sockaddr_clone(udpAdmin->addr, alloc),
  155. .angelInterface = asCoreIface
  156. }));
  157. }
  158. void AdminTestFramework_tearDown(struct AdminTestFramework* framework)
  159. {
  160. char buff[128] = " PADDING "
  161. "d"
  162. "1:q" "10:Angel_exit"
  163. "e";
  164. char* start = CString_strchr(buff, 'd');
  165. struct Message m = {
  166. .bytes = (uint8_t*) start,
  167. .length = CString_strlen(start),
  168. .padding = start - buff
  169. };
  170. struct Message* mp = Message_clone(&m, framework->alloc);
  171. Interface_sendMessage(framework->angelInterface, mp);
  172. Allocator_free(framework->alloc);
  173. }