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