check_server_binary_messages.c 2.1 KB

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