TestFramework.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef TestFramework_H
  16. #define TestFramework_H
  17. #include "memory/Allocator.h"
  18. #include "dht/DHTModules.h"
  19. #include "dht/CJDHTConstants.h"
  20. static int catchOutgoing(struct DHTMessage* message, void* vcontext);
  21. static int bounceMessage(struct DHTMessage* message, void* vcontext);
  22. static void TestFramework_registerBouncerModule(struct DHTModuleRegistry* registry,
  23. struct Allocator* allocator)
  24. __attribute__((unused));
  25. static void TestFramework_registerBouncerModule(struct DHTModuleRegistry* registry,
  26. struct Allocator* allocator)
  27. {
  28. struct DHTModule* module =
  29. Allocator_clone(allocator, (&(struct DHTModule) {
  30. .context = registry,
  31. .handleIncoming = bounceMessage
  32. });
  33. DHTModules_register(module, registry);
  34. }
  35. static void TestFramework_registerOutputCatcher(struct DHTMessage** messagePointer,
  36. struct DHTModuleRegistry* registry,
  37. struct Allocator* allocator)
  38. {
  39. struct DHTModule* module =
  40. Allocator_clone(allocator, (&(struct DHTModule) {
  41. .context = messagePointer,
  42. .handleOutgoing = catchOutgoing
  43. });
  44. DHTModules_register(module, registry);
  45. }
  46. // A little module which just sends an empty reply to any incoming message.
  47. // Used to get a response from the store.
  48. static int bounceMessage(struct DHTMessage* message, void* vcontext)
  49. {
  50. struct DHTModuleRegistry* registry = (struct DHTModuleRegistry*) vcontext;
  51. struct DHTMessage* reply =
  52. message->allocator->malloc(sizeof(struct DHTMessage), message->allocator);
  53. reply->replyTo = message;
  54. reply->allocator = message->allocator;
  55. reply->asDict = Dict_new(reply->allocator);
  56. String* queryType = Dict_getString(message->asDict, CJDHTConstants_QUERY);
  57. printf("bouncing message %s", queryType->bytes);
  58. DHTModules_handleOutgoing(reply, registry);
  59. return 0;
  60. }
  61. // A module to catch responses and make a pointer to the message available.
  62. static int catchOutgoing(struct DHTMessage* message, void* vcontext)
  63. {
  64. *((struct DHTMessage**)vcontext) = message;
  65. return 0;
  66. }