check_server_binary_messages.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_delete(server);
  39. UA_ServerConfig_delete(config);
  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. }