TestFramework.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "crypto/AddressCalc.h"
  18. #include "dht/ReplyModule.h"
  19. #include "dht/dhtcore/RouterModule.h"
  20. #include "dht/dhtcore/SearchRunner.h"
  21. #include "dht/SerializationModule.h"
  22. #include "dht/EncodingSchemeModule.h"
  23. #include "dht/dhtcore/Router_new.h"
  24. #include "io/Writer.h"
  25. #include "io/FileWriter.h"
  26. #include "util/log/Log.h"
  27. #include "memory/MallocAllocator.h"
  28. #include "memory/Allocator.h"
  29. #include "switch/SwitchCore.h"
  30. #include "test/TestFramework.h"
  31. #include "util/log/WriterLog.h"
  32. #include "util/events/EventBase.h"
  33. #include "net/SwitchPinger.h"
  34. #include "interface/InterfaceController.h"
  35. #include "crypto_scalarmult_curve25519.h"
  36. struct TestFramework_Link
  37. {
  38. struct Interface srcIf;
  39. struct Interface destIf;
  40. struct TestFramework* src;
  41. struct TestFramework* dest;
  42. Identity
  43. };
  44. static uint8_t sendTo(struct Message* msg, struct Interface* iface)
  45. {
  46. struct TestFramework_Link* link =
  47. Identity_check((struct TestFramework_Link*)iface->senderContext);
  48. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  49. Assert_true(!(msg->capacity % 4) || !"length fault");
  50. Assert_true(((int)msg->capacity >= msg->length) || !"length fault0");
  51. struct Interface* dest;
  52. struct TestFramework* srcTf;
  53. if (&link->destIf == iface) {
  54. dest = &link->srcIf;
  55. srcTf = link->dest;
  56. } else if (&link->srcIf == iface) {
  57. dest = &link->destIf;
  58. srcTf = link->src;
  59. } else {
  60. Assert_true(false);
  61. }
  62. printf("Transferring message to [%p] - message length [%d]\n", (void*)dest, msg->length);
  63. // Store the original message and a copy of the original so they can be compared later.
  64. srcTf->lastMsgBackup = Message_clone(msg, srcTf->alloc);
  65. srcTf->lastMsg = msg;
  66. if (msg->alloc) {
  67. // If it's a message which was buffered inside of CryptoAuth then it will be freed
  68. // so by adopting the allocator we can hold it in memory.
  69. Allocator_adopt(srcTf->alloc, msg->alloc);
  70. }
  71. // Copy the original and send that to the other end.
  72. struct Message* sendMsg = Message_clone(msg, dest->allocator);
  73. return dest->receiveMessage(sendMsg, dest);
  74. }
  75. struct TestFramework* TestFramework_setUp(char* privateKey,
  76. struct Allocator* allocator,
  77. struct EventBase* base,
  78. struct Random* rand,
  79. struct Log* logger)
  80. {
  81. if (!logger) {
  82. struct Writer* logwriter = FileWriter_new(stdout, allocator);
  83. logger = WriterLog_new(logwriter, allocator);
  84. }
  85. if (!rand) {
  86. rand = Random_new(allocator, logger, NULL);
  87. }
  88. if (!base) {
  89. base = EventBase_new(allocator);
  90. }
  91. uint64_t pks[4];
  92. if (!privateKey) {
  93. Random_longs(rand, pks, 4);
  94. privateKey = (char*)pks;
  95. }
  96. uint8_t* publicKey = Allocator_malloc(allocator, 32);
  97. crypto_scalarmult_curve25519_base(publicKey, (uint8_t*)privateKey);
  98. struct Address* myAddress = Allocator_calloc(allocator, sizeof(struct Address), 1);
  99. Bits_memcpyConst(myAddress->key, publicKey, 32);
  100. AddressCalc_addressForPublicKey(myAddress->ip6.bytes, publicKey);
  101. struct SwitchCore* switchCore = SwitchCore_new(logger, allocator, base);
  102. struct CryptoAuth* ca = CryptoAuth_new(allocator, (uint8_t*)privateKey, base, logger, rand);
  103. struct DHTModuleRegistry* registry = DHTModuleRegistry_new(allocator);
  104. ReplyModule_register(registry, allocator);
  105. struct RumorMill* rumorMill = RumorMill_new(allocator, myAddress, 64, logger, "");
  106. struct NodeStore* nodeStore = NodeStore_new(myAddress, allocator, logger, rumorMill);
  107. struct RouterModule* routerModule =
  108. RouterModule_register(registry, allocator, publicKey, base, logger, rand, nodeStore);
  109. struct SearchRunner* searchRunner = SearchRunner_new(nodeStore,
  110. logger,
  111. base,
  112. routerModule,
  113. myAddress->ip6.bytes,
  114. rumorMill,
  115. allocator);
  116. EncodingSchemeModule_register(registry, logger, allocator);
  117. SerializationModule_register(registry, logger, allocator);
  118. struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand, NULL);
  119. struct Router* router = Router_new(routerModule, nodeStore, searchRunner, allocator);
  120. struct Ducttape* dt =
  121. Ducttape_register((uint8_t*)privateKey, registry, router,
  122. switchCore, base, allocator, logger, ipTun, rand);
  123. struct SwitchPinger* sp =
  124. SwitchPinger_new(&dt->switchPingerIf, base, rand, logger, myAddress, allocator);
  125. // Interfaces.
  126. struct InterfaceController* ifController =
  127. InterfaceController_new(ca, switchCore, router, rumorMill,
  128. logger, base, sp, rand, allocator);
  129. struct TestFramework* tf = Allocator_clone(allocator, (&(struct TestFramework) {
  130. .alloc = allocator,
  131. .rand = rand,
  132. .eventBase = base,
  133. .logger = logger,
  134. .switchCore = switchCore,
  135. .ducttape = dt,
  136. .cryptoAuth = ca,
  137. .router = routerModule,
  138. .switchPinger = sp,
  139. .ifController = ifController,
  140. .publicKey = publicKey,
  141. .nodeStore = nodeStore,
  142. .ip = myAddress->ip6.bytes
  143. }));
  144. Identity_set(tf);
  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(a->length == b->length);
  155. Assert_true(a->padding == b->padding);
  156. Assert_true(!Bits_memcmp(a->bytes, b->bytes, a->length));
  157. }
  158. void TestFramework_linkNodes(struct TestFramework* client, struct TestFramework* server)
  159. {
  160. // ifaceA is the client, ifaceB is the server
  161. struct TestFramework_Link* link =
  162. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  163. Bits_memcpyConst(link, (&(struct TestFramework_Link) {
  164. .srcIf = {
  165. .sendMessage = sendTo,
  166. .senderContext = link,
  167. .allocator = client->alloc
  168. },
  169. .destIf = {
  170. .sendMessage = sendTo,
  171. .senderContext = link,
  172. .allocator = client->alloc
  173. },
  174. .src = client,
  175. .dest = server
  176. }), sizeof(struct TestFramework_Link));
  177. Identity_set(link);
  178. // server knows nothing about the client.
  179. InterfaceController_registerPeer(server->ifController, NULL, NULL, true, false, &link->destIf);
  180. // Except that it has an authorizedPassword added.
  181. CryptoAuth_addUser(String_CONST("abcdefg1234"), 1, String_CONST("TEST"), server->cryptoAuth);
  182. // Client has pubKey and passwd for the server.
  183. InterfaceController_registerPeer(client->ifController,
  184. server->publicKey,
  185. String_CONST("abcdefg1234"),
  186. false,
  187. false,
  188. &link->srcIf);
  189. }
  190. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  191. {
  192. Message_shift(msg, Headers_IP6Header_SIZE, NULL);
  193. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->bytes;
  194. ip->versionClassAndFlowLabel = 0;
  195. ip->flowLabelLow_be = 0;
  196. ip->payloadLength_be = Endian_hostToBigEndian16(msg->length - Headers_IP6Header_SIZE);
  197. ip->nextHeader = 123; // made up number
  198. ip->hopLimit = 255;
  199. Bits_memcpyConst(ip->sourceAddr, srcAddr, 16);
  200. Bits_memcpyConst(ip->destinationAddr, destAddr, 16);
  201. Headers_setIpVersion(ip);
  202. }