check_server_binary_messages.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "check.h"
  7. #include "ua_server.h"
  8. #include "ua_server_internal.h"
  9. #include "ua_config_default.h"
  10. #include "ua_log_stdout.h"
  11. #include "testing_networklayers.h"
  12. size_t files;
  13. char **filenames;
  14. static UA_ByteString readFile(char *filename) {
  15. UA_ByteString buf = UA_BYTESTRING_NULL;
  16. size_t length;
  17. FILE *f = NULL;
  18. #ifdef WIN32
  19. fopen_s(&f, filename,"r");
  20. #else
  21. f = fopen(filename,"r");
  22. #endif
  23. if(f) {
  24. fseek(f, 0, SEEK_END);
  25. length = ftell(f);
  26. rewind(f);
  27. buf.data = (UA_Byte*)UA_malloc(length);
  28. fread(buf.data, sizeof(char), length, f);
  29. buf.length = length;
  30. fclose(f);
  31. }
  32. return buf;
  33. }
  34. START_TEST(processMessage) {
  35. UA_Connection c = createDummyConnection();
  36. UA_ServerConfig *config = UA_ServerConfig_new_default();
  37. UA_Server *server = UA_Server_new(config);
  38. for(size_t i = 0; i < files; i++) {
  39. UA_ByteString msg = readFile(filenames[i]);
  40. UA_Server_processBinaryMessage(server, &c, &msg);
  41. UA_ByteString_deleteMembers(&msg);
  42. }
  43. UA_Server_run_shutdown(server);
  44. UA_Server_delete(server);
  45. UA_ServerConfig_delete(config);
  46. UA_Connection_deleteMembers(&c);
  47. }
  48. END_TEST
  49. static Suite *testSuite_binaryMessages(void) {
  50. Suite *s = suite_create("Test server with messages stored in text files");
  51. TCase *tc_messages = tcase_create("binary messages");
  52. tcase_add_test(tc_messages, processMessage);
  53. suite_add_tcase(s, tc_messages);
  54. return s;
  55. }
  56. int main(int argc, char **argv) {
  57. if(argc < 2)
  58. return EXIT_FAILURE;
  59. filenames = &argv[1];
  60. files = argc - 1;
  61. int number_failed = 0;
  62. Suite *s;
  63. SRunner *sr;
  64. s = testSuite_binaryMessages();
  65. sr = srunner_create(s);
  66. srunner_set_fork_status(sr, CK_NOFORK);
  67. srunner_run_all(sr, CK_NORMAL);
  68. number_failed += srunner_ntests_failed(sr);
  69. srunner_free(sr);
  70. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  71. }