1
0

AdminTestFramework.c 7.4 KB

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