TestFramework.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_true(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 RumorMill* rumorMill = RumorMill_new(allocator, myAddress, 64);
  95. struct NodeStore* nodeStore = NodeStore_new(myAddress, allocator, logger, rumorMill);
  96. struct RouterModule* routerModule =
  97. RouterModule_register(registry, allocator, publicKey, base, logger, rand, nodeStore);
  98. struct SearchRunner* searchRunner = SearchRunner_new(nodeStore,
  99. logger,
  100. base,
  101. routerModule,
  102. myAddress->ip6.bytes,
  103. rumorMill,
  104. allocator);
  105. SerializationModule_register(registry, logger, allocator);
  106. struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand, NULL);
  107. struct Ducttape* dt =
  108. Ducttape_register((uint8_t*)privateKey, registry, routerModule, searchRunner,
  109. switchCore, base, allocator, logger, ipTun, rand);
  110. struct SwitchPinger* sp =
  111. SwitchPinger_new(&dt->switchPingerIf, base, rand, logger, myAddress, allocator);
  112. // Interfaces.
  113. struct InterfaceController* ifController =
  114. DefaultInterfaceController_new(ca,
  115. switchCore,
  116. routerModule,
  117. rumorMill,
  118. logger,
  119. base,
  120. sp,
  121. rand,
  122. allocator);
  123. struct TestFramework* tf = Allocator_clone(allocator, (&(struct TestFramework) {
  124. .alloc = allocator,
  125. .rand = rand,
  126. .eventBase = base,
  127. .logger = logger,
  128. .switchCore = switchCore,
  129. .ducttape = dt,
  130. .cryptoAuth = ca,
  131. .router = routerModule,
  132. .switchPinger = sp,
  133. .ifController = ifController,
  134. .publicKey = publicKey,
  135. .ip = myAddress->ip6.bytes
  136. }));
  137. Identity_set(tf);
  138. return tf;
  139. }
  140. void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf)
  141. {
  142. if (!tf->lastMsg) {
  143. return;
  144. }
  145. struct Message* a = tf->lastMsg;
  146. struct Message* b = tf->lastMsgBackup;
  147. Assert_true(a->length == b->length);
  148. Assert_true(a->padding == b->padding);
  149. Assert_true(!Bits_memcmp(a->bytes, b->bytes, a->length));
  150. }
  151. void TestFramework_linkNodes(struct TestFramework* client, struct TestFramework* server)
  152. {
  153. // ifaceA is the client, ifaceB is the server
  154. struct TestFramework_Link* link =
  155. Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
  156. Bits_memcpyConst(link, (&(struct TestFramework_Link) {
  157. .srcIf = {
  158. .sendMessage = sendTo,
  159. .senderContext = link,
  160. .allocator = client->alloc
  161. },
  162. .destIf = {
  163. .sendMessage = sendTo,
  164. .senderContext = link,
  165. .allocator = client->alloc
  166. },
  167. .src = client,
  168. .dest = server
  169. }), sizeof(struct TestFramework_Link));
  170. Identity_set(link);
  171. // server knows nothing about the client.
  172. InterfaceController_registerPeer(server->ifController, NULL, NULL, true, false, &link->destIf);
  173. // Except that it has an authorizedPassword added.
  174. CryptoAuth_addUser(String_CONST("abcdefg1234"), 1, String_CONST("TEST"), server->cryptoAuth);
  175. // Client has pubKey and passwd for the server.
  176. InterfaceController_registerPeer(client->ifController,
  177. server->publicKey,
  178. String_CONST("abcdefg1234"),
  179. false,
  180. false,
  181. &link->srcIf);
  182. }
  183. void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16])
  184. {
  185. Message_shift(msg, Headers_IP6Header_SIZE, NULL);
  186. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->bytes;
  187. ip->versionClassAndFlowLabel = 0;
  188. ip->flowLabelLow_be = 0;
  189. ip->payloadLength_be = Endian_hostToBigEndian16(msg->length - Headers_IP6Header_SIZE);
  190. ip->nextHeader = 123; // made up number
  191. ip->hopLimit = 255;
  192. Bits_memcpyConst(ip->sourceAddr, srcAddr, 16);
  193. Bits_memcpyConst(ip->destinationAddr, destAddr, 16);
  194. Headers_setIpVersion(ip);
  195. }