TestFramework.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #ifndef SUBNODE
  19. #include "dht/Pathfinder.h"
  20. #endif
  21. #include "io/Writer.h"
  22. #include "io/FileWriter.h"
  23. #include "util/log/Log.h"
  24. #include "memory/MallocAllocator.h"
  25. #include "memory/Allocator.h"
  26. #include "switch/SwitchCore.h"
  27. #include "subnode/SubnodePathfinder.h"
  28. #include "test/TestFramework.h"
  29. #include "util/log/WriterLog.h"
  30. #include "util/events/EventBase.h"
  31. #include "net/SwitchPinger.h"
  32. #include "net/ControlHandler.h"
  33. #include "net/InterfaceController.h"
  34. #include "interface/ASynchronizer.h"
  35. #include "interface/Iface.h"
  36. #include "tunnel/IpTunnel.h"
  37. #include "net/EventEmitter.h"
  38. #include "net/SessionManager.h"
  39. #include "net/UpperDistributor.h"
  40. #include "net/TUNAdapter.h"
  41. #include "wire/Headers.h"
  42. // Needed just to get the encoding scheme that we're using
  43. #define NumberCompress_OLD_CODE
  44. #include "switch/NumberCompress.h"
  45. struct TestFramework_Link
  46. {
  47. struct Iface clientIf;
  48. struct Iface serverIf;
  49. struct TestFramework* client;
  50. struct TestFramework* server;
  51. int serverIfNum;
  52. int clientIfNum;
  53. Identity
  54. };
  55. static Iface_DEFUN sendTo(struct Message* msg,
  56. struct Iface* dest,
  57. struct TestFramework* srcTf,
  58. struct TestFramework* destTf)
  59. {
  60. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  61. Assert_true(!(msg->capacity % 4) || !"length fault");
  62. Assert_true(((int)msg->capacity >= msg->length) || !"length fault0");
  63. Log_debug(srcTf->logger, "Transferring message to [%p] - message length [%d]\n",
  64. (void*)dest, msg->length);
  65. // Store the original message and a copy of the original so they can be compared later.
  66. srcTf->lastMsgBackup = Message_clone(msg, srcTf->alloc);
  67. srcTf->lastMsg = msg;
  68. if (msg->alloc) {
  69. // If it's a message which was buffered inside of CryptoAuth then it will be freed
  70. // so by adopting the allocator we can hold it in memory.
  71. Allocator_adopt(srcTf->alloc, msg->alloc);
  72. }
  73. // Copy the original and send that to the other end.
  74. // Can't use Iface_next() when not sending the original msg.
  75. struct Message* sendMsg = Message_clone(msg, destTf->alloc);
  76. Iface_send(dest, sendMsg);
  77. return 0;
  78. }
  79. static Iface_DEFUN sendClient(struct Message* msg, struct Iface* clientIf)
  80. {
  81. struct TestFramework_Link* link =
  82. Identity_containerOf(clientIf, struct TestFramework_Link, clientIf);
  83. return sendTo(msg, &link->serverIf, link->client, link->server);
  84. }
  85. static Iface_DEFUN sendServer(struct Message* msg, struct Iface* serverIf)
  86. {
  87. struct TestFramework_Link* link =
  88. Identity_containerOf(serverIf, struct TestFramework_Link, serverIf);
  89. return sendTo(msg, &link->clientIf, link->server, link->client);
  90. }
  91. struct TestFramework* TestFramework_setUp(char* privateKey,
  92. struct Allocator* allocator,
  93. struct EventBase* base,
  94. struct Random* rand,
  95. struct Log* logger)
  96. {
  97. if (!logger) {
  98. struct Writer* logwriter = FileWriter_new(stdout, allocator);
  99. logger = WriterLog_new(logwriter, allocator);
  100. }
  101. if (!rand) {
  102. rand = Random_new(allocator, logger, NULL);
  103. }
  104. if (!base) {
  105. base = EventBase_new(allocator);
  106. }
  107. uint64_t pks[4];
  108. if (!privateKey) {
  109. Random_longs(rand, pks, 4);
  110. privateKey = (char*)pks;
  111. }
  112. struct EncodingScheme* scheme = NumberCompress_defineScheme(allocator);
  113. struct NetCore* nc =
  114. NetCore_new(privateKey, allocator, base, rand, logger);
  115. struct RouteGen* rg = RouteGen_new(allocator, logger);
  116. struct GlobalConfig* globalConf = GlobalConfig_new(allocator);
  117. struct IpTunnel* ipTunnel =
  118. IpTunnel_new(logger, base, allocator, rand, rg, globalConf);
  119. Iface_plumb(&nc->tunAdapt->ipTunnelIf, &ipTunnel->tunInterface);
  120. Iface_plumb(&nc->upper->ipTunnelIf, &ipTunnel->nodeInterface);
  121. struct SubnodePathfinder* spf = SubnodePathfinder_new(
  122. allocator, logger, base, rand, nc->myAddress, privateKey, scheme);
  123. struct ASynchronizer* spfAsync = ASynchronizer_new(allocator, base, logger);
  124. Iface_plumb(&spfAsync->ifA, &spf->eventIf);
  125. EventEmitter_regPathfinderIface(nc->ee, &spfAsync->ifB);
  126. #ifndef SUBNODE
  127. struct Pathfinder* pf = Pathfinder_register(allocator, logger, base, rand, NULL, scheme);
  128. pf->fullVerify = true;
  129. struct ASynchronizer* pfAsync = ASynchronizer_new(allocator, base, logger);
  130. Iface_plumb(&pfAsync->ifA, &pf->eventIf);
  131. EventEmitter_regPathfinderIface(nc->ee, &pfAsync->ifB);
  132. #endif
  133. SubnodePathfinder_start(spf);
  134. struct TestFramework* tf = Allocator_calloc(allocator, sizeof(struct TestFramework), 1);
  135. Identity_set(tf);
  136. tf->alloc = allocator;
  137. tf->rand = rand;
  138. tf->eventBase = base;
  139. tf->logger = logger;
  140. tf->nc = nc;
  141. tf->tunIf = &nc->tunAdapt->tunIf;
  142. tf->publicKey = nc->myAddress->key;
  143. tf->ip = nc->myAddress->ip6.bytes;
  144. #ifndef SUBNODE
  145. tf->pathfinder = pf;
  146. #endif
  147. tf->subnodePathfinder = spf;
  148. tf->scheme = scheme;
  149. return tf;
  150. }
  151. void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf)
  152. {
  153. if (!tf->lastMsg) {
  154. return;
  155. }
  156. struct Message* a = tf->lastMsg;
  157. struct Message* b = tf->lastMsgBackup;
  158. Assert_true(a->length == b->length);
  159. Assert_true(a->padding == b->padding);
  160. Assert_true(!Bits_memcmp(a->bytes, b->bytes, a->length));
  161. }
  162. void TestFramework_linkNodes(struct TestFramework* client,
  163. struct TestFramework* server,
  164. bool beacon)
  165. {
  166. // ifaceA is the client, ifaceB is the server
  167. struct TestFramework_Link* link =
  168. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  169. Identity_set(link);
  170. link->clientIf.send = sendClient;
  171. link->serverIf.send = sendServer;
  172. link->client = client;
  173. link->server = server;
  174. struct InterfaceController_Iface* clientIci = InterfaceController_newIface(
  175. client->nc->ifController, String_CONST("client"), client->alloc);
  176. link->clientIfNum = clientIci->ifNum;
  177. Iface_plumb(&link->clientIf, &clientIci->addrIf);
  178. struct InterfaceController_Iface* serverIci = InterfaceController_newIface(
  179. server->nc->ifController, String_CONST("server"), server->alloc);
  180. link->serverIfNum = serverIci->ifNum;
  181. Iface_plumb(&link->serverIf, &serverIci->addrIf);
  182. if (beacon) {
  183. int ret = InterfaceController_beaconState(client->nc->ifController,
  184. link->clientIfNum,
  185. InterfaceController_beaconState_newState_ACCEPT);
  186. Assert_true(!ret);
  187. ret = InterfaceController_beaconState(server->nc->ifController,
  188. link->serverIfNum,
  189. InterfaceController_beaconState_newState_SEND);
  190. Assert_true(!ret);
  191. } else {
  192. // Except that it has an authorizedPassword added.
  193. CryptoAuth_addUser(String_CONST("abcdefg123"), String_CONST("TEST"), server->nc->ca);
  194. // Client has pubKey and passwd for the server.
  195. InterfaceController_bootstrapPeer(client->nc->ifController,
  196. link->clientIfNum,
  197. server->publicKey,
  198. Sockaddr_LOOPBACK,
  199. String_CONST("abcdefg123"),
  200. NULL,
  201. NULL,
  202. client->alloc);
  203. }
  204. }
  205. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  206. {
  207. Message_shift(msg, Headers_IP6Header_SIZE, NULL);
  208. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->bytes;
  209. ip->versionClassAndFlowLabel = 0;
  210. ip->flowLabelLow_be = 0;
  211. ip->payloadLength_be = Endian_hostToBigEndian16(msg->length - Headers_IP6Header_SIZE);
  212. ip->nextHeader = 123; // made up number
  213. ip->hopLimit = 255;
  214. Bits_memcpy(ip->sourceAddr, srcAddr, 16);
  215. Bits_memcpy(ip->destinationAddr, destAddr, 16);
  216. Headers_setIpVersion(ip);
  217. }
  218. int TestFramework_peerCount(struct TestFramework* node)
  219. {
  220. struct Allocator* alloc = Allocator_child(node->alloc);
  221. struct InterfaceController_PeerStats* statsOut = NULL;
  222. int len = InterfaceController_getPeerStats(node->nc->ifController, alloc, &statsOut);
  223. int out = 0;
  224. for (int i = 0; i < len; i++) {
  225. out += (statsOut[i].state == InterfaceController_PeerState_ESTABLISHED);
  226. }
  227. Allocator_free(alloc);
  228. return out;
  229. }