1
0

TestFramework.c 8.3 KB

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