DHTModules_handleOutgoing_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/DHTMessage.h"
  16. #include "dht/DHTModule.h"
  17. #include "dht/DHTModuleRegistry.h"
  18. #include "memory/Allocator.h"
  19. #include <stdio.h>
  20. struct Context
  21. {
  22. struct DHTMessage* theMessage;
  23. int ret;
  24. };
  25. static int handleOutgoing(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 testOutputHandler()
  36. {
  37. struct DHTMessage theMessage;
  38. struct Context context =
  39. {
  40. .theMessage = &theMessage,
  41. .ret = -1
  42. };
  43. struct DHTModule module = {
  44. .name = "TestModule",
  45. .context = &context,
  46. .handleOutgoing = handleOutgoing
  47. };
  48. struct Allocator* allocator = Allocator_new(2048);
  49. struct DHTModuleRegistry* reg = DHTModuleRegistry_new(allocator, NULL);
  50. DHTModuleRegistry_register(&module, reg);
  51. DHTModuleRegistry_handleOutgoing(&theMessage, reg);
  52. /* These should be ignored. */
  53. DHTModuleRegistry_handleIncoming(&theMessage, reg);
  54. if (context.ret == -1) {
  55. printf("message not received");
  56. } else if (context.ret == -2) {
  57. printf("wrong message received");
  58. } else {
  59. Allocator_free(allocator);
  60. return 0;
  61. }
  62. return -1;
  63. }
  64. int main()
  65. {
  66. return testOutputHandler();
  67. }