threeNodes_test_disabled.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "crypto/Key.h"
  16. #include "io/FileWriter.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Base32.h"
  20. #include "util/Checksum.h"
  21. #include "util/log/WriterLog.h"
  22. #include "test/TestFramework.h"
  23. #include "net/Ducttape_pvt.h"
  24. #include "wire/Headers.h"
  25. #include "wire/Ethernet.h"
  26. #include "interface/tuntap/TUNMessageType.h"
  27. #include <stdio.h>
  28. #define TUNC 3
  29. #define TUNB 2
  30. #define TUNA 1
  31. static uint8_t incomingTunC(struct Message* msg, struct Interface* iface)
  32. {
  33. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  34. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  35. printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length);
  36. *((int*)iface->senderContext) = TUNC;
  37. return 0;
  38. }
  39. static uint8_t incomingTunB(struct Message* msg, struct Interface* iface)
  40. {
  41. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  42. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  43. printf("Message from TUN in node B [%s]\n", msg->bytes);
  44. *((int*)iface->senderContext) = TUNB;
  45. return 0;
  46. }
  47. static uint8_t incomingTunA(struct Message* msg, struct Interface* iface)
  48. {
  49. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  50. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  51. uint8_t buff[1024];
  52. Hex_encode(buff, 1024, msg->bytes, msg->length);
  53. printf("Message from TUN in node A [%s] [%d] [%s]\n", msg->bytes, msg->length, buff);
  54. *((int*)iface->senderContext) = TUNA;
  55. return 0;
  56. }
  57. struct ThreeNodes
  58. {
  59. struct Interface tunIfC;
  60. struct TestFramework* nodeC;
  61. struct Interface tunIfB;
  62. struct TestFramework* nodeB;
  63. struct Interface tunIfA;
  64. struct TestFramework* nodeA;
  65. int messageFrom;
  66. };
  67. static struct ThreeNodes* setUp(struct Allocator* alloc)
  68. {
  69. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  70. struct Log* logger = WriterLog_new(logwriter, alloc);
  71. struct TestFramework* a =
  72. TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
  73. "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
  74. alloc, logger);
  75. //"publicKey": "kmzm4w0kj9bswd5qmx74nu7kusv5pj40vcsmp781j6xxgpd59z00.k",
  76. //"ipv6": "fc41:94b5:0925:7ba9:3959:11ab:a006:367a",
  77. struct TestFramework* b =
  78. TestFramework_setUp("\xea\x8d\x34\x04\xa9\x7c\xe4\xf9\xca\x7e\x24\xe6\xf1\x85\xb9\x3f"
  79. "\x01\x37\xb7\xa1\xf5\x2c\xce\xc0\x2c\xae\x03\xf1\x83\x38\x13\x24",
  80. alloc, logger);
  81. // This address was found by brute force for one which falls between A and C without being
  82. // closer in either direction (XOR is bidirectional address space distance)
  83. // ipv6: fc2e:3273:644e:426f:283d:e3c7:c87c:41c1
  84. struct TestFramework* c =
  85. TestFramework_setUp("\xd8\x54\x3e\x70\xb9\xae\x7c\x41\xbc\x18\xa4\x9a\x9c\xee\xca\x9c"
  86. "\xdc\x45\x01\x96\x6b\xbd\x7e\x76\xcf\x3a\x9f\xbc\x12\xed\x8b\xb4",
  87. alloc, logger);
  88. //"publicKey": "vz21tg07061s8v9mckrvgtfds7j2u5lst8cwl6nqhp81njrh5wg0.k",
  89. //"ipv6": "fc1f:5b96:e1c5:625d:afde:2523:a7fa:383a",
  90. Log_debug(a->logger, "Linking A and B");
  91. TestFramework_linkNodes(b, a);
  92. Log_debug(a->logger, "Linking A and B complete");
  93. Log_debug(a->logger, "Linking B and C");
  94. TestFramework_linkNodes(c, b);
  95. Log_debug(a->logger, "Linking B and C complete");
  96. struct ThreeNodes* out = Allocator_malloc(alloc, sizeof(struct ThreeNodes));
  97. Bits_memcpyConst(out, (&(struct ThreeNodes) {
  98. .tunIfC = {
  99. .allocator = alloc,
  100. .sendMessage = incomingTunC,
  101. .senderContext = &out->messageFrom
  102. },
  103. .nodeC = c,
  104. .tunIfB = {
  105. .allocator = alloc,
  106. .sendMessage = incomingTunB,
  107. .senderContext = &out->messageFrom
  108. },
  109. .nodeB = b,
  110. .tunIfA = {
  111. .allocator = alloc,
  112. .sendMessage = incomingTunA,
  113. .senderContext = &out->messageFrom
  114. },
  115. .nodeA = a
  116. }), sizeof(struct ThreeNodes));
  117. Ducttape_setUserInterface(c->ducttape, &out->tunIfC);
  118. Ducttape_setUserInterface(b->ducttape, &out->tunIfB);
  119. Ducttape_setUserInterface(a->ducttape, &out->tunIfA);
  120. Log_debug(a->logger, "Setup complete.");
  121. return out;
  122. }
  123. static void sendMessage(struct ThreeNodes* tn,
  124. char* message,
  125. struct TestFramework* from,
  126. struct TestFramework* to)
  127. {
  128. struct Message* msg;
  129. Message_STACK(msg, 64, 512);
  130. Bits_memcpy(msg->bytes, message, CString_strlen(message) + 1);
  131. msg->length = CString_strlen(message) + 1;
  132. TestFramework_craftIPHeader(msg, from->ip, to->ip);
  133. msg = Message_clone(msg, from->alloc);
  134. struct Interface* fromIf;
  135. if (from == tn->nodeA) {
  136. fromIf = &tn->tunIfA;
  137. } else if (from == tn->nodeB) {
  138. fromIf = &tn->tunIfB;
  139. } else if (from == tn->nodeC) {
  140. fromIf = &tn->tunIfC;
  141. } else {
  142. Assert_true(false);
  143. }
  144. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  145. fromIf->receiveMessage(msg, fromIf);
  146. if (to == tn->nodeA) {
  147. Assert_true(tn->messageFrom == TUNA);
  148. } else if (to == tn->nodeB) {
  149. Assert_true(tn->messageFrom == TUNB);
  150. } else if (to == tn->nodeC) {
  151. Assert_true(tn->messageFrom == TUNC);
  152. } else {
  153. Assert_true(false);
  154. }
  155. TestFramework_assertLastMessageUnaltered(tn->nodeA);
  156. TestFramework_assertLastMessageUnaltered(tn->nodeB);
  157. TestFramework_assertLastMessageUnaltered(tn->nodeC);
  158. tn->messageFrom = 0;
  159. }
  160. /** Check if nodes A and C can communicate via B without A knowing that C exists. */
  161. int main()
  162. {
  163. struct Allocator* alloc = MallocAllocator_new(1<<22);
  164. struct ThreeNodes* tn = setUp(alloc);
  165. sendMessage(tn, "Hello World!", tn->nodeA, tn->nodeC);
  166. sendMessage(tn, "Hello cjdns!", tn->nodeC, tn->nodeA);
  167. sendMessage(tn, "send", tn->nodeA, tn->nodeC);
  168. sendMessage(tn, "a", tn->nodeC, tn->nodeA);
  169. sendMessage(tn, "few", tn->nodeA, tn->nodeC);
  170. sendMessage(tn, "packets", tn->nodeC, tn->nodeA);
  171. sendMessage(tn, "to", tn->nodeA, tn->nodeC);
  172. sendMessage(tn, "make", tn->nodeC, tn->nodeA);
  173. sendMessage(tn, "sure", tn->nodeA, tn->nodeC);
  174. sendMessage(tn, "the", tn->nodeC, tn->nodeA);
  175. sendMessage(tn, "cryptoauth", tn->nodeA, tn->nodeC);
  176. sendMessage(tn, "can", tn->nodeC, tn->nodeA);
  177. sendMessage(tn, "establish", tn->nodeA, tn->nodeC);
  178. Allocator_free(alloc);
  179. return 0;
  180. }