MultiInterface_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "interface/MultiInterface.h"
  17. #include "interface/Interface.h"
  18. #include "interface/InterfaceController.h"
  19. #include "memory/Allocator.h"
  20. #include "memory/MallocAllocator.h"
  21. #include "wire/Message.h"
  22. #include "benc/String.h"
  23. #define KEY_SIZE 20
  24. #define ENTRY_COUNT 64
  25. #define CYCLES 128
  26. struct Entry {
  27. uint8_t key[KEY_SIZE];
  28. };
  29. struct Context
  30. {
  31. struct InterfaceController ic;
  32. struct Interface* receivedOn;
  33. struct Message* lastSent;
  34. };
  35. static uint8_t sendMessage(struct Message* msg, struct Interface* iface)
  36. {
  37. return 0;
  38. }
  39. static uint8_t recieveInternal(struct Message* msg, struct Interface* iface)
  40. {
  41. struct Context* ctx = iface->receiverContext;
  42. ctx->receivedOn = iface;
  43. return 0;
  44. }
  45. static int registerPeer(struct InterfaceController* ic,
  46. uint8_t herPublicKey[32],
  47. String* password,
  48. bool requireAuth,
  49. bool transient,
  50. struct Interface* iface)
  51. {
  52. iface->receiveMessage = recieveInternal;
  53. iface->receiverContext = ic;
  54. return 0;
  55. }
  56. static enum InterfaceController_PeerState getPeerState(struct Interface* iface)
  57. {
  58. return InterfaceController_PeerState_HANDSHAKE;
  59. }
  60. int main()
  61. {
  62. struct Allocator* alloc = MallocAllocator_new(1<<20);
  63. struct Random* rand = Random_new(alloc, NULL, NULL);
  64. // mock interface controller.
  65. struct Context ctx = {
  66. .ic = {
  67. .registerPeer = registerPeer,
  68. .getPeerState = getPeerState
  69. }
  70. };
  71. struct Interface externalIf = {
  72. .sendMessage = sendMessage,
  73. .allocator = alloc,
  74. .senderContext = &ctx
  75. };
  76. /*struct MultiInterface* mif = */MultiInterface_new(KEY_SIZE, &externalIf, &ctx.ic, NULL);
  77. struct Entry* entries = Allocator_malloc(alloc, sizeof(struct Entry) * ENTRY_COUNT);
  78. Random_bytes(rand, (uint8_t*)entries, ENTRY_COUNT * sizeof(struct Entry));
  79. struct Interface** ifaces = Allocator_calloc(alloc, sizeof(char*), ENTRY_COUNT);
  80. // seed the list with some near collisions.
  81. for (int i = 0; i < 10; i++) {
  82. int rnd = (((uint32_t*)entries)[i] >> 1) % ENTRY_COUNT;
  83. ((uint32_t*) (&entries[rnd]))[0] = ((uint32_t*) (&entries[i]))[0];
  84. }
  85. for (int i = 0; i < CYCLES; i++) {
  86. int rnd = ((uint32_t*)entries)[i] % ENTRY_COUNT;
  87. struct Entry* entry = &entries[rnd];
  88. struct Interface* iface = ifaces[rnd];
  89. struct Message* msg;
  90. Message_STACK(msg, 0, 128);
  91. Message_push(msg, "hello world", 12, NULL);
  92. Message_push(msg, entry, 16, NULL);
  93. externalIf.receiveMessage(msg, &externalIf);
  94. //printf("Received message for iface [%u] from [%p]\n", rnd, (void*)ctx.receivedOn);
  95. if (iface) {
  96. Assert_true(ctx.receivedOn == iface);
  97. } else {
  98. ifaces[rnd] = ctx.receivedOn;
  99. }
  100. }
  101. Allocator_free(alloc);
  102. return 0;
  103. }