Process_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "crypto/random/Random.h"
  17. #include "util/events/EventBase.h"
  18. #include "util/events/PipeServer.h"
  19. #include "util/events/Pipe.h"
  20. #include "util/events/Timeout.h"
  21. #include "memory/Allocator.h"
  22. #include "memory/MallocAllocator.h"
  23. #include "util/events/Process.h"
  24. #include "util/log/Log.h"
  25. #include "util/log/FileWriterLog.h"
  26. #include "util/CString.h"
  27. #include "util/Assert.h"
  28. #include "wire/Message.h"
  29. #include "wire/Error.h"
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #define MESSAGE "IT WORKS!"
  37. #define MESSAGEB "INDEED"
  38. struct Context {
  39. struct Iface iface;
  40. struct Allocator* alloc;
  41. struct EventBase* base;
  42. struct Log* log;
  43. // Parent only
  44. int fd;
  45. // child only
  46. char* name;
  47. Identity
  48. };
  49. static void onConnectionParent(struct PipeServer* p, struct Sockaddr* addr)
  50. {
  51. struct Context* c = Identity_check((struct Context*) p->userData);
  52. struct Allocator* alloc = Allocator_child(c->alloc);
  53. struct Message* msg = Message_new(0, 256, alloc);
  54. Er_assert(Message_epush(msg, MESSAGE, CString_strlen(MESSAGE) + 1));
  55. Er_assert(AddrIface_pushAddr(msg, addr));
  56. if (!Defined(win32)) {
  57. Message_setAssociatedFd(msg, c->fd);
  58. }
  59. printf("Parent sending message [%s] len [%d]\n", MESSAGE, Message_getLength(msg));
  60. Iface_send(&c->iface, msg);
  61. Allocator_free(alloc);
  62. }
  63. static Iface_DEFUN receiveMessageParent(struct Message* msg, struct Iface* iface)
  64. {
  65. struct Context* c = Identity_check((struct Context*) iface);
  66. Er_assert(AddrIface_popAddr(msg));
  67. Assert_true(Message_getLength(msg) == (int)CString_strlen(MESSAGEB)+1);
  68. Assert_true(!Bits_memcmp(msg->msgbytes, MESSAGEB, CString_strlen(MESSAGEB)+1));
  69. Allocator_free(c->alloc);
  70. return NULL;
  71. }
  72. static void timeout(void* vNULL)
  73. {
  74. Assert_true(!"timed out.");
  75. }
  76. static void onConnectionChild(struct Pipe* p, int status)
  77. {
  78. Assert_true(!status);
  79. printf("Child connected\n");
  80. }
  81. static Iface_DEFUN receiveMessageChild(struct Message* msg, struct Iface* iface)
  82. {
  83. struct Context* c = Identity_check((struct Context*) iface);
  84. struct Message* m = Message_clone(msg, c->alloc);
  85. printf("Child received message\n");
  86. Assert_true(Message_getLength(m) == (int)CString_strlen(MESSAGE)+1);
  87. Assert_true(!Bits_memcmp(m->msgbytes, MESSAGE, CString_strlen(MESSAGE)+1));
  88. if (!Defined(win32)) {
  89. int fd = Message_getAssociatedFd(msg);
  90. if (lseek(fd, 0, SEEK_SET) < 0) {
  91. printf("lseek(%d) failed: errno %s\n", fd, strerror(errno));
  92. Assert_failure("lseek()");
  93. }
  94. uint8_t* buf = Allocator_calloc(Message_getAlloc(msg), 2048, 1);
  95. if (read(fd, buf, 1024) < 0) {
  96. printf("read(%d) failed: errno %s\n", fd, strerror(errno));
  97. Assert_failure("read()");
  98. }
  99. if (CString_strncmp(buf, c->name, 1024)) {
  100. printf("want: %s\n"
  101. "got: %s", c->name, buf);
  102. Assert_failure("file content is wrong");
  103. }
  104. }
  105. Er_assert(Message_eshift(m, -((int)CString_strlen(MESSAGE))));
  106. Er_assert(Message_epush(m, MESSAGEB, CString_strlen(MESSAGEB)));
  107. Iface_send(&c->iface, m);
  108. // shutdown
  109. Allocator_free(c->alloc);
  110. return NULL;
  111. }
  112. static void child(char* name, struct Context* ctx)
  113. {
  114. struct Pipe* pipe = Er_assert(Pipe_named(name, ctx->base, ctx->log, ctx->alloc));
  115. pipe->onConnection = onConnectionChild;
  116. pipe->userData = ctx;
  117. ctx->iface.send = receiveMessageChild;
  118. ctx->name = name;
  119. Iface_plumb(&ctx->iface, &pipe->iface);
  120. Timeout_setTimeout(timeout, NULL, 2000, ctx->base, ctx->alloc);
  121. EventBase_beginLoop(ctx->base);
  122. }
  123. int main(int argc, char** argv)
  124. {
  125. struct Allocator* alloc = MallocAllocator_new(1<<20);
  126. struct EventBase* eb = EventBase_new(alloc);
  127. struct Log* log = FileWriterLog_new(stdout, alloc);
  128. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  129. Identity_set(ctx);
  130. ctx->alloc = alloc;
  131. ctx->base = eb;
  132. ctx->log = log;
  133. ctx->iface.send = receiveMessageParent;
  134. if (argc > 3 && !CString_strcmp("Process_test", argv[1]) && !CString_strcmp("child", argv[2])) {
  135. child(argv[3], ctx);
  136. return 0;
  137. }
  138. struct Random* rand = Random_new(alloc, log, NULL);
  139. char randName[32] = {0};
  140. Random_base32(rand, (uint8_t*)randName, 31);
  141. String* name = String_printf(alloc, "%s%scjdns-test-%s", Pipe_PATH, Pipe_PATH_SEP, randName);
  142. if (!Defined(win32)) {
  143. String* textName =
  144. String_printf(alloc, "%s%scjdns-test-%s.txt", Pipe_PATH, Pipe_PATH_SEP, randName);
  145. int fd = open(textName->bytes, O_CREAT | O_TRUNC | O_RDWR, 0600);
  146. Assert_true(fd >= 0);
  147. Assert_true(write(fd, name->bytes, name->len) == ((ssize_t)name->len));
  148. ctx->fd = fd;
  149. unlink(textName->bytes);
  150. }
  151. struct PipeServer* pipe = PipeServer_named(name->bytes, eb, NULL, log, alloc);
  152. pipe->userData = ctx;
  153. pipe->onConnection = onConnectionParent;
  154. Iface_plumb(&ctx->iface, &pipe->iface.iface);
  155. char* path = Process_getPath(alloc);
  156. Assert_true(path != NULL);
  157. #ifdef win32
  158. Assert_true(CString_strstr(path, ":\\") == path + 1); /* C:\ */
  159. Assert_true(CString_strstr(path, ".exe"));
  160. #elif openbsd
  161. Assert_true(path[0] == 'b'); // Process_getPath returns relative paths on openbsd
  162. #elif netbsd
  163. Assert_true(path[0] == 'b'); // Process_getPath returns relative paths on netbsd too
  164. #else
  165. Assert_true(path[0] == '/');
  166. #endif
  167. char* args[] = { "Process_test", "child", name->bytes, NULL };
  168. Assert_true(!Process_spawn(path, args, alloc, NULL));
  169. Timeout_setTimeout(timeout, NULL, 2000, eb, alloc);
  170. EventBase_beginLoop(eb);
  171. unlink(name->bytes);
  172. return 0;
  173. }