TestFramework.c 7.5 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 "crypto/random/Random.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "crypto/AddressCalc.h"
  18. #include "dht/Pathfinder.h"
  19. #include "io/Writer.h"
  20. #include "io/FileWriter.h"
  21. #include "util/log/Log.h"
  22. #include "memory/MallocAllocator.h"
  23. #include "memory/Allocator.h"
  24. #include "switch/SwitchCore.h"
  25. #include "test/TestFramework.h"
  26. #include "util/log/WriterLog.h"
  27. #include "util/events/EventBase.h"
  28. #include "net/SwitchPinger.h"
  29. #include "net/ControlHandler.h"
  30. #include "net/InterfaceController.h"
  31. #include "interface/Iface.h"
  32. #include "tunnel/IpTunnel.h"
  33. #include "net/EventEmitter.h"
  34. #include "net/SessionManager.h"
  35. #include "net/SwitchAdapter.h"
  36. #include "net/ConverterV15.h"
  37. #include "net/UpperDistributor.h"
  38. #include "net/TUNAdapter.h"
  39. #include "wire/Headers.h"
  40. struct TestFramework_Link
  41. {
  42. struct Iface clientIf;
  43. struct Iface serverIf;
  44. struct TestFramework* client;
  45. struct TestFramework* server;
  46. int serverIfNum;
  47. int clientIfNum;
  48. Identity
  49. };
  50. static Iface_DEFUN sendTo(struct Message* msg,
  51. struct Iface* dest,
  52. struct TestFramework* srcTf,
  53. struct TestFramework* destTf)
  54. {
  55. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  56. Assert_true(!(msg->capacity % 4) || !"length fault");
  57. Assert_true(((int)msg->capacity >= msg->length) || !"length fault0");
  58. Log_debug(srcTf->logger, "Transferring message to [%p] - message length [%d]\n",
  59. (void*)dest, msg->length);
  60. // Store the original message and a copy of the original so they can be compared later.
  61. srcTf->lastMsgBackup = Message_clone(msg, srcTf->alloc);
  62. srcTf->lastMsg = msg;
  63. if (msg->alloc) {
  64. // If it's a message which was buffered inside of CryptoAuth then it will be freed
  65. // so by adopting the allocator we can hold it in memory.
  66. Allocator_adopt(srcTf->alloc, msg->alloc);
  67. }
  68. // Copy the original and send that to the other end.
  69. // Can't use Iface_next() when not sending the original msg.
  70. struct Message* sendMsg = Message_clone(msg, destTf->alloc);
  71. Iface_send(dest, sendMsg);
  72. return 0;
  73. }
  74. static Iface_DEFUN sendClient(struct Message* msg, struct Iface* clientIf)
  75. {
  76. struct TestFramework_Link* link =
  77. Identity_containerOf(clientIf, struct TestFramework_Link, clientIf);
  78. return sendTo(msg, &link->serverIf, link->client, link->server);
  79. }
  80. static Iface_DEFUN sendServer(struct Message* msg, struct Iface* serverIf)
  81. {
  82. struct TestFramework_Link* link =
  83. Identity_containerOf(serverIf, struct TestFramework_Link, serverIf);
  84. return sendTo(msg, &link->clientIf, link->server, link->client);
  85. }
  86. struct TestFramework* TestFramework_setUp(char* privateKey,
  87. struct Allocator* allocator,
  88. struct EventBase* base,
  89. struct Random* rand,
  90. struct Log* logger)
  91. {
  92. if (!logger) {
  93. struct Writer* logwriter = FileWriter_new(stdout, allocator);
  94. logger = WriterLog_new(logwriter, allocator);
  95. }
  96. if (!rand) {
  97. rand = Random_new(allocator, logger, NULL);
  98. }
  99. if (!base) {
  100. base = EventBase_new(allocator);
  101. }
  102. uint64_t pks[4];
  103. if (!privateKey) {
  104. Random_longs(rand, pks, 4);
  105. privateKey = (char*)pks;
  106. }
  107. struct NetCore* nc = NetCore_new(privateKey, allocator, base, rand, logger);
  108. struct Pathfinder* pf = Pathfinder_register(allocator, logger, base, rand, NULL, nc->ee);
  109. struct TestFramework* tf = Allocator_calloc(allocator, sizeof(struct TestFramework), 1);
  110. Identity_set(tf);
  111. tf->alloc = allocator;
  112. tf->rand = rand;
  113. tf->eventBase = base;
  114. tf->logger = logger;
  115. tf->nc = nc;
  116. tf->tunIf = &nc->tunAdapt->tunIf;
  117. tf->publicKey = nc->myAddress->key;
  118. tf->ip = nc->myAddress->ip6.bytes;
  119. tf->pathfinder = pf;
  120. return tf;
  121. }
  122. void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf)
  123. {
  124. if (!tf->lastMsg) {
  125. return;
  126. }
  127. struct Message* a = tf->lastMsg;
  128. struct Message* b = tf->lastMsgBackup;
  129. Assert_true(a->length == b->length);
  130. Assert_true(a->padding == b->padding);
  131. Assert_true(!Bits_memcmp(a->bytes, b->bytes, a->length));
  132. }
  133. void TestFramework_linkNodes(struct TestFramework* client,
  134. struct TestFramework* server,
  135. bool beacon)
  136. {
  137. // ifaceA is the client, ifaceB is the server
  138. struct TestFramework_Link* link =
  139. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  140. Identity_set(link);
  141. link->clientIf.send = sendClient;
  142. link->serverIf.send = sendServer;
  143. link->client = client;
  144. link->server = server;
  145. struct InterfaceController_Iface* clientIci = InterfaceController_newIface(
  146. client->nc->ifController, String_CONST("client"), client->alloc);
  147. link->clientIfNum = clientIci->ifNum;
  148. Iface_plumb(&link->clientIf, &clientIci->addrIf);
  149. struct InterfaceController_Iface* serverIci = InterfaceController_newIface(
  150. server->nc->ifController, String_CONST("server"), server->alloc);
  151. link->serverIfNum = serverIci->ifNum;
  152. Iface_plumb(&link->serverIf, &serverIci->addrIf);
  153. if (beacon) {
  154. int ret = InterfaceController_beaconState(client->nc->ifController,
  155. link->clientIfNum,
  156. InterfaceController_beaconState_newState_ACCEPT);
  157. Assert_true(!ret);
  158. ret = InterfaceController_beaconState(server->nc->ifController,
  159. link->serverIfNum,
  160. InterfaceController_beaconState_newState_SEND);
  161. Assert_true(!ret);
  162. } else {
  163. // Except that it has an authorizedPassword added.
  164. CryptoAuth_addUser(String_CONST("abcdefg123"), 1, String_CONST("TEST"), server->nc->ca);
  165. // Client has pubKey and passwd for the server.
  166. InterfaceController_bootstrapPeer(client->nc->ifController,
  167. link->clientIfNum,
  168. server->publicKey,
  169. Sockaddr_LOOPBACK,
  170. String_CONST("abcdefg123"),
  171. client->alloc);
  172. }
  173. }
  174. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  175. {
  176. Message_shift(msg, Headers_IP6Header_SIZE, NULL);
  177. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->bytes;
  178. ip->versionClassAndFlowLabel = 0;
  179. ip->flowLabelLow_be = 0;
  180. ip->payloadLength_be = Endian_hostToBigEndian16(msg->length - Headers_IP6Header_SIZE);
  181. ip->nextHeader = 123; // made up number
  182. ip->hopLimit = 255;
  183. Bits_memcpyConst(ip->sourceAddr, srcAddr, 16);
  184. Bits_memcpyConst(ip->destinationAddr, destAddr, 16);
  185. Headers_setIpVersion(ip);
  186. }