check_server_binary_messages.c 1.9 KB

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