1
0

DHTModules_handleIncoming_test.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "dht/DHTModule.h"
  16. #include "dht/DHTModuleRegistry.h"
  17. #include "memory/Allocator.h"
  18. #include "memory/MallocAllocator.h"
  19. #include <stdio.h>
  20. struct Context
  21. {
  22. struct DHTMessage* theMessage;
  23. int ret;
  24. };
  25. static int handleIncoming(struct DHTMessage* message, void* vcontext)
  26. {
  27. struct Context* context = (struct Context*) vcontext;
  28. if (message == context->theMessage) {
  29. context->ret = 0;
  30. } else {
  31. context->ret = -2;
  32. }
  33. return 0;
  34. }
  35. static int testInputHandler()
  36. {
  37. struct DHTMessage theMessage;
  38. struct Context context =
  39. {
  40. .theMessage = &theMessage,
  41. .ret = -1
  42. };
  43. struct Context context2 =
  44. {
  45. .theMessage = &theMessage,
  46. .ret = -1
  47. };
  48. struct DHTModule module = {
  49. .name = "TestModule",
  50. .context = &context,
  51. .handleIncoming = handleIncoming
  52. };
  53. struct DHTModule module2 = {
  54. .name = "TestModule2",
  55. .context = &context2,
  56. .handleIncoming = handleIncoming
  57. };
  58. struct Allocator* allocator = MallocAllocator_new(2048);
  59. struct DHTModuleRegistry* reg = DHTModuleRegistry_new(allocator, NULL);
  60. DHTModuleRegistry_register(&module, reg);
  61. DHTModuleRegistry_register(&module2, reg);
  62. DHTModuleRegistry_handleIncoming(&theMessage, reg);
  63. /* This should be ignored. */
  64. DHTModuleRegistry_handleOutgoing(&theMessage, reg);
  65. if (context.ret == -1) {
  66. printf("message not received");
  67. } else if (context.ret == -2) {
  68. printf("wrong message received");
  69. } else if (context2.ret == -1) {
  70. printf("message not received by all handlers.");
  71. } else if (context2.ret == -2) {
  72. printf("wrong message received by second handler.");
  73. } else {
  74. Allocator_free(allocator);
  75. return 0;
  76. }
  77. return -1;
  78. }
  79. int main()
  80. {
  81. return testInputHandler();
  82. }