check_server_binary_messages.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "check.h"
  4. #include "ua_server.h"
  5. #include "ua_server_internal.h"
  6. #include "ua_config_standard.h"
  7. #include "ua_log_stdout.h"
  8. #include "testing_networklayers.h"
  9. size_t files;
  10. char **filenames;
  11. static UA_ByteString readFile(char *filename) {
  12. UA_ByteString buf = UA_BYTESTRING_NULL;
  13. size_t length;
  14. FILE *f = fopen(filename,"r");
  15. if(f) {
  16. fseek(f, 0, SEEK_END);
  17. length = ftell(f);
  18. rewind(f);
  19. buf.data = malloc(length);
  20. fread(buf.data, sizeof(char), length, f);
  21. buf.length = length;
  22. fclose(f);
  23. }
  24. return buf;
  25. }
  26. START_TEST(processMessage) {
  27. UA_Connection c = createDummyConnection();
  28. UA_ServerConfig config = UA_ServerConfig_standard;
  29. config.logger = UA_Log_Stdout;
  30. UA_Server *server = UA_Server_new(config);
  31. for(size_t i = 0; i < files; i++) {
  32. UA_ByteString msg = readFile(filenames[i]);
  33. UA_Boolean reallocated;
  34. UA_StatusCode retval = UA_Connection_completeMessages(&c, &msg, &reallocated);
  35. if(retval == UA_STATUSCODE_GOOD && msg.length > 0)
  36. UA_Server_processBinaryMessage(server, &c, &msg);
  37. UA_ByteString_deleteMembers(&msg);
  38. }
  39. UA_Server_delete(server);
  40. UA_Connection_deleteMembers(&c);
  41. }
  42. END_TEST
  43. static Suite *testSuite_binaryMessages(void) {
  44. Suite *s = suite_create("Test server with messages stored in text files");
  45. TCase *tc_messages = tcase_create("binary messages");
  46. tcase_add_test(tc_messages, processMessage);
  47. suite_add_tcase(s, tc_messages);
  48. return s;
  49. }
  50. int main(int argc, char **argv) {
  51. if(argc < 2)
  52. return EXIT_FAILURE;
  53. filenames = &argv[1];
  54. files = argc - 1;
  55. int number_failed = 0;
  56. Suite *s;
  57. SRunner *sr;
  58. s = testSuite_binaryMessages();
  59. sr = srunner_create(s);
  60. srunner_set_fork_status(sr, CK_NOFORK);
  61. srunner_run_all(sr, CK_NORMAL);
  62. number_failed += srunner_ntests_failed(sr);
  63. srunner_free(sr);
  64. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  65. }