TestFramework.c 9.6 KB

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