TestFramework.c 9.9 KB

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