1
0

TestFramework.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/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);
  109. struct ASynchronizer* pfAsync = ASynchronizer_new(allocator, base, logger);
  110. Iface_plumb(&pfAsync->ifA, &pf->eventIf);
  111. EventEmitter_regPathfinderIface(nc->ee, &pfAsync->ifB);
  112. struct TestFramework* tf = Allocator_calloc(allocator, sizeof(struct TestFramework), 1);
  113. Identity_set(tf);
  114. tf->alloc = allocator;
  115. tf->rand = rand;
  116. tf->eventBase = base;
  117. tf->logger = logger;
  118. tf->nc = nc;
  119. tf->tunIf = &nc->tunAdapt->tunIf;
  120. tf->publicKey = nc->myAddress->key;
  121. tf->ip = nc->myAddress->ip6.bytes;
  122. tf->pathfinder = pf;
  123. return tf;
  124. }
  125. void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf)
  126. {
  127. if (!tf->lastMsg) {
  128. return;
  129. }
  130. struct Message* a = tf->lastMsg;
  131. struct Message* b = tf->lastMsgBackup;
  132. Assert_true(a->length == b->length);
  133. Assert_true(a->padding == b->padding);
  134. Assert_true(!Bits_memcmp(a->bytes, b->bytes, a->length));
  135. }
  136. void TestFramework_linkNodes(struct TestFramework* client,
  137. struct TestFramework* server,
  138. bool beacon)
  139. {
  140. // ifaceA is the client, ifaceB is the server
  141. struct TestFramework_Link* link =
  142. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  143. Identity_set(link);
  144. link->clientIf.send = sendClient;
  145. link->serverIf.send = sendServer;
  146. link->client = client;
  147. link->server = server;
  148. struct InterfaceController_Iface* clientIci = InterfaceController_newIface(
  149. client->nc->ifController, String_CONST("client"), client->alloc);
  150. link->clientIfNum = clientIci->ifNum;
  151. Iface_plumb(&link->clientIf, &clientIci->addrIf);
  152. struct InterfaceController_Iface* serverIci = InterfaceController_newIface(
  153. server->nc->ifController, String_CONST("server"), server->alloc);
  154. link->serverIfNum = serverIci->ifNum;
  155. Iface_plumb(&link->serverIf, &serverIci->addrIf);
  156. if (beacon) {
  157. int ret = InterfaceController_beaconState(client->nc->ifController,
  158. link->clientIfNum,
  159. InterfaceController_beaconState_newState_ACCEPT);
  160. Assert_true(!ret);
  161. ret = InterfaceController_beaconState(server->nc->ifController,
  162. link->serverIfNum,
  163. InterfaceController_beaconState_newState_SEND);
  164. Assert_true(!ret);
  165. } else {
  166. // Except that it has an authorizedPassword added.
  167. CryptoAuth_addUser(String_CONST("abcdefg123"), String_CONST("TEST"), server->nc->ca);
  168. // Client has pubKey and passwd for the server.
  169. InterfaceController_bootstrapPeer(client->nc->ifController,
  170. link->clientIfNum,
  171. server->publicKey,
  172. Sockaddr_LOOPBACK,
  173. String_CONST("abcdefg123"),
  174. NULL,
  175. NULL,
  176. client->alloc);
  177. }
  178. }
  179. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  180. {
  181. Message_shift(msg, Headers_IP6Header_SIZE, NULL);
  182. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->bytes;
  183. ip->versionClassAndFlowLabel = 0;
  184. ip->flowLabelLow_be = 0;
  185. ip->payloadLength_be = Endian_hostToBigEndian16(msg->length - Headers_IP6Header_SIZE);
  186. ip->nextHeader = 123; // made up number
  187. ip->hopLimit = 255;
  188. Bits_memcpy(ip->sourceAddr, srcAddr, 16);
  189. Bits_memcpy(ip->destinationAddr, destAddr, 16);
  190. Headers_setIpVersion(ip);
  191. }