check_server_binary_messages.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_standard.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 = 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_standard;
  32. config.logger = UA_Log_Stdout;
  33. UA_Server *server = UA_Server_new(config);
  34. for(size_t i = 0; i < files; i++) {
  35. UA_ByteString msg = readFile(filenames[i]);
  36. UA_Boolean reallocated;
  37. UA_StatusCode retval = UA_Connection_completeMessages(&c, &msg, &reallocated);
  38. if(retval == UA_STATUSCODE_GOOD && msg.length > 0)
  39. UA_Server_processBinaryMessage(server, &c, &msg);
  40. UA_ByteString_deleteMembers(&msg);
  41. }
  42. UA_Server_delete(server);
  43. UA_Connection_deleteMembers(&c);
  44. }
  45. END_TEST
  46. static Suite *testSuite_binaryMessages(void) {
  47. Suite *s = suite_create("Test server with messages stored in text files");
  48. TCase *tc_messages = tcase_create("binary messages");
  49. tcase_add_test(tc_messages, processMessage);
  50. suite_add_tcase(s, tc_messages);
  51. return s;
  52. }
  53. int main(int argc, char **argv) {
  54. if(argc < 2)
  55. return EXIT_FAILURE;
  56. filenames = &argv[1];
  57. files = argc - 1;
  58. int number_failed = 0;
  59. Suite *s;
  60. SRunner *sr;
  61. s = testSuite_binaryMessages();
  62. sr = srunner_create(s);
  63. srunner_set_fork_status(sr, CK_NOFORK);
  64. srunner_run_all(sr, CK_NORMAL);
  65. number_failed += srunner_ntests_failed(sr);
  66. srunner_free(sr);
  67. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  68. }