TestFramework.c 7.7 KB

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