1
0

DHTModules_handleOutgoing_test.c 2.0 KB

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