TestFramework.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/Ca.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->msgbytes % 4) || !"alignment fault");
  62. Assert_true(!(Message_getCapacity(msg) % 4) || !"length fault");
  63. Assert_true(((int)Message_getCapacity(msg) >= Message_getLength(msg)) || !"length fault0");
  64. Log_debug(srcTf->logger, "Transferring message to [%p] - message length [%d]\n",
  65. (void*)dest, Message_getLength(msg));
  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 (Message_getAlloc(msg)) {
  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, Message_getAlloc(msg));
  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. return Iface_send(dest, sendMsg);
  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. bool enableNoise)
  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, enableNoise);
  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. EventEmitter_regPathfinderIface(nc->ee, &spf->eventIf);
  125. #ifndef SUBNODE
  126. struct Pathfinder* pf = Pathfinder_register(allocator, logger, base, rand, NULL);
  127. pf->fullVerify = true;
  128. EventEmitter_regPathfinderIface(nc->ee, &pf->eventIf);
  129. #endif
  130. SubnodePathfinder_start(spf);
  131. struct TestFramework* tf = Allocator_calloc(allocator, sizeof(struct TestFramework), 1);
  132. Identity_set(tf);
  133. tf->alloc = allocator;
  134. tf->rand = rand;
  135. tf->eventBase = base;
  136. tf->logger = logger;
  137. tf->nc = nc;
  138. tf->tunIf = &nc->tunAdapt->tunIf;
  139. tf->publicKey = nc->myAddress->key;
  140. tf->ip = nc->myAddress->ip6.bytes;
  141. #ifndef SUBNODE
  142. tf->pathfinder = pf;
  143. #endif
  144. tf->subnodePathfinder = spf;
  145. tf->scheme = scheme;
  146. return tf;
  147. }
  148. void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf)
  149. {
  150. if (!tf->lastMsg) {
  151. return;
  152. }
  153. struct Message* a = tf->lastMsg;
  154. struct Message* b = tf->lastMsgBackup;
  155. Assert_true(Message_getLength(a) == Message_getLength(b));
  156. Assert_true(Message_getPadding(a) == Message_getPadding(b));
  157. Assert_true(!Bits_memcmp(a->msgbytes, b->msgbytes, Message_getLength(a)));
  158. }
  159. void TestFramework_linkNodes(struct TestFramework* client,
  160. struct TestFramework* server,
  161. bool beacon)
  162. {
  163. // ifaceA is the client, ifaceB is the server
  164. struct TestFramework_Link* link =
  165. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  166. Identity_set(link);
  167. link->clientIf.send = sendClient;
  168. link->serverIf.send = sendServer;
  169. link->client = client;
  170. link->server = server;
  171. struct InterfaceController_Iface* clientIci = InterfaceController_newIface(
  172. client->nc->ifController, String_CONST("client"), client->alloc);
  173. link->clientIfNum = clientIci->ifNum;
  174. Iface_plumb(&link->clientIf, &clientIci->addrIf);
  175. struct InterfaceController_Iface* serverIci = InterfaceController_newIface(
  176. server->nc->ifController, String_CONST("server"), server->alloc);
  177. link->serverIfNum = serverIci->ifNum;
  178. Iface_plumb(&link->serverIf, &serverIci->addrIf);
  179. if (beacon) {
  180. int ret = InterfaceController_beaconState(client->nc->ifController,
  181. link->clientIfNum,
  182. InterfaceController_beaconState_newState_ACCEPT);
  183. Assert_true(!ret);
  184. ret = InterfaceController_beaconState(server->nc->ifController,
  185. link->serverIfNum,
  186. InterfaceController_beaconState_newState_SEND);
  187. Assert_true(!ret);
  188. } else {
  189. // Except that it has an authorizedPassword added.
  190. Ca_addUser(String_CONST("abcdefg123"), String_CONST("TEST"), server->nc->ca);
  191. // Client has pubKey and passwd for the server.
  192. InterfaceController_bootstrapPeer(client->nc->ifController,
  193. link->clientIfNum,
  194. server->publicKey,
  195. Sockaddr_LOOPBACK,
  196. String_CONST("abcdefg123"),
  197. NULL,
  198. NULL,
  199. Version_CURRENT_PROTOCOL);
  200. }
  201. }
  202. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  203. {
  204. Er_assert(Message_eshift(msg, Headers_IP6Header_SIZE));
  205. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->msgbytes;
  206. ip->versionClassAndFlowLabel = 0;
  207. ip->flowLabelLow_be = 0;
  208. ip->payloadLength_be = Endian_hostToBigEndian16(Message_getLength(msg) - Headers_IP6Header_SIZE);
  209. ip->nextHeader = 123; // made up number
  210. ip->hopLimit = 255;
  211. Bits_memcpy(ip->sourceAddr, srcAddr, 16);
  212. Bits_memcpy(ip->destinationAddr, destAddr, 16);
  213. Headers_setIpVersion(ip);
  214. }
  215. int TestFramework_peerCount(struct TestFramework* node)
  216. {
  217. struct Allocator* alloc = Allocator_child(node->alloc);
  218. struct InterfaceController_PeerStats* statsOut = NULL;
  219. int len = InterfaceController_getPeerStats(node->nc->ifController, alloc, &statsOut);
  220. int out = 0;
  221. for (int i = 0; i < len; i++) {
  222. out += (statsOut[i].state == InterfaceController_PeerState_ESTABLISHED);
  223. }
  224. Allocator_free(alloc);
  225. return out;
  226. }
  227. int TestFramework_sessionCount(struct TestFramework* node)
  228. {
  229. struct Allocator* alloc = Allocator_child(node->alloc);
  230. struct SessionManager_HandleList* list = SessionManager_getHandleList(node->nc->sm, alloc);
  231. int count = 0;
  232. for (int i = 0; i < list->length; i++) {
  233. struct SessionManager_Session* sess =
  234. SessionManager_sessionForHandle(list->handles[i], node->nc->sm);
  235. count += (Ca_getState(sess->caSession) == Ca_State_ESTABLISHED);
  236. }
  237. Allocator_free(alloc);
  238. return count;
  239. }