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 <https://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/UpperDistributor.h"
  37. #include "net/TUNAdapter.h"
  38. #include "wire/Headers.h"
  39. struct TestFramework_Link
  40. {
  41. struct Iface clientIf;
  42. struct Iface serverIf;
  43. struct TestFramework* client;
  44. struct TestFramework* server;
  45. int serverIfNum;
  46. int clientIfNum;
  47. Identity
  48. };
  49. static Iface_DEFUN sendTo(struct Message* msg,
  50. struct Iface* dest,
  51. struct TestFramework* srcTf,
  52. struct TestFramework* destTf)
  53. {
  54. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  55. Assert_true(!(msg->capacity % 4) || !"length fault");
  56. Assert_true(((int)msg->capacity >= msg->length) || !"length fault0");
  57. Log_debug(srcTf->logger, "Transferring message to [%p] - message length [%d]\n",
  58. (void*)dest, msg->length);
  59. // Store the original message and a copy of the original so they can be compared later.
  60. srcTf->lastMsgBackup = Message_clone(msg, srcTf->alloc);
  61. srcTf->lastMsg = msg;
  62. if (msg->alloc) {
  63. // If it's a message which was buffered inside of CryptoAuth then it will be freed
  64. // so by adopting the allocator we can hold it in memory.
  65. Allocator_adopt(srcTf->alloc, msg->alloc);
  66. }
  67. // Copy the original and send that to the other end.
  68. // Can't use Iface_next() because we are not sending the original msg.
  69. struct Message* sendMsg = Message_clone(msg, destTf->alloc);
  70. Iface_send(dest, sendMsg);
  71. return 0;
  72. }
  73. static Iface_DEFUN sendClient(struct Message* msg, struct Iface* clientIf)
  74. {
  75. struct TestFramework_Link* link =
  76. Identity_containerOf(clientIf, struct TestFramework_Link, clientIf);
  77. return sendTo(msg, &link->serverIf, link->client, link->server);
  78. }
  79. static Iface_DEFUN sendServer(struct Message* msg, struct Iface* serverIf)
  80. {
  81. struct TestFramework_Link* link =
  82. Identity_containerOf(serverIf, struct TestFramework_Link, serverIf);
  83. return sendTo(msg, &link->clientIf, link->server, link->client);
  84. }
  85. struct TestFramework* TestFramework_setUp(char* privateKey,
  86. struct Allocator* allocator,
  87. struct EventBase* base,
  88. struct Random* rand,
  89. struct Log* logger)
  90. {
  91. if (!logger) {
  92. struct Writer* logwriter = FileWriter_new(stdout, allocator);
  93. logger = WriterLog_new(logwriter, allocator);
  94. }
  95. if (!rand) {
  96. rand = Random_new(allocator, logger, NULL);
  97. }
  98. if (!base) {
  99. base = EventBase_new(allocator);
  100. }
  101. uint64_t pks[4];
  102. if (!privateKey) {
  103. Random_longs(rand, pks, 4);
  104. privateKey = (char*)pks;
  105. }
  106. struct NetCore* nc = NetCore_new(privateKey, allocator, base, rand, logger);
  107. struct Pathfinder* pf = Pathfinder_register(allocator, logger, base, rand, NULL);
  108. pf->fullVerify = true;
  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. }