TestFramework.c 9.8 KB

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