123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #include <stdlib.h>
- #include "ua_types.h"
- #include "ua_client.h"
- #include "check.h"
- START_TEST(EndpointUrl_split) {
- // check for null
- ck_assert_uint_eq(UA_EndpointUrl_split(NULL, NULL, NULL, NULL), UA_STATUSCODE_BADINVALIDARGUMENT);
- char hostname[256];
- UA_UInt16 port;
- const char* path;
- // check for max url length
- // this string has 256 chars
- char *overlength = "wEgfH2Sqe8AtFcUqX6VnyvZz6A4AZtbKRvGwQWvtPLrt7aaLb6wtqFzqQ2dLYLhTwJpAuVbsRTGfjvP2kvsVSYQLLeGuPjJyYnMt5e8TqtmYuPTb78uuAx7KyQB9ce95eacs3Jp32KMNtb7BTuKjQ236MnMX3mFWYAkALcj5axpQnFaGyU3HvpYrX24FTEztuZ3zpNnqBWQyHPVa6efGTzmUXMADxjw3AbG5sTGzDca7rucsfQRAZby8ZWKm66pV";
- ck_assert_uint_eq(UA_EndpointUrl_split(overlength, hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
- // check for too short url
- ck_assert_uint_eq(UA_EndpointUrl_split("inv.ali:/", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
- // check for opc.tcp:// protocol
- ck_assert_uint_eq(UA_EndpointUrl_split("inv.ali://", hostname, &port, &path), UA_STATUSCODE_BADATTRIBUTEIDINVALID);
- // empty url
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_uint_eq(strlen(hostname), 0);
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path, NULL);
- // only hostname
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path, NULL);
- // empty port
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path, NULL);
- // specific port
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 1234);
- ck_assert_ptr_eq(path, NULL);
- // IPv6
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"[2001:0db8:85a3::8a2e:0370:7334]");
- ck_assert_uint_eq(port, 1234);
- ck_assert_str_eq(path, "path");
- // empty hostname
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://:", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_uint_eq(strlen(hostname),0);
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path, NULL);
- // empty hostname and no port
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp:///", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_uint_eq(strlen(hostname),0);
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path,0);
- // overlength port
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:12345678", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
- // too high port
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:65536", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
- // port not a number
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:6x6", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
- // no port but path
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname/", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 0);
- ck_assert_ptr_eq(path, 0);
- // port and path
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, &port, &path), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 1234);
- ck_assert_str_eq(path, "path");
- // full url, but only hostname required
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, NULL, NULL), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- // full url, but only hostname and port required
- ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, &port, NULL), UA_STATUSCODE_GOOD);
- ck_assert_str_eq(hostname,"hostname");
- ck_assert_uint_eq(port, 1234);
- }
- END_TEST
- START_TEST(readNumber) {
- UA_UInt32 result;
- ck_assert_uint_eq(UA_readNumber(NULL, 0, NULL), 0);
- ck_assert_uint_eq(UA_readNumber((UA_Byte*)"x", 1, &result), 0);
- ck_assert_uint_eq(UA_readNumber((UA_Byte*)"1x", 2, &result), 1);
- ck_assert_uint_eq(result, 1);
- ck_assert_uint_eq(UA_readNumber((UA_Byte*)"123456789", 9, &result), 9);
- ck_assert_uint_eq(result, 123456789);
- }
- END_TEST
- START_TEST(StatusCode_msg) {
- #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
- ck_assert_str_eq(UA_StatusCode_msg(UA_STATUSCODE_GOOD), "StatusCode descriptions not available");
- return;
- #endif
- // first element in table
- ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_GOOD), "Success / No error");
- ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOOD), "Good");
- // just some randomly picked status codes
- ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_BADNOCOMMUNICATION),
- "Communication with the data source is defined");
- ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADNOCOMMUNICATION),
- "BadNoCommunication");
- ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_GOODNODATA),
- "No data exists for the requested time range or event filter.");
- ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOODNODATA), "GoodNoData");
- // last element in table
- ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
- "The operation could not be finished because all available connections are in use.");
- ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
- "BadMaxConnectionsReached");
- // an invalid status code
- ck_assert_str_eq(UA_StatusCode_explanation(0x80123456), "Unknown StatusCode");
- ck_assert_str_eq(UA_StatusCode_name(0x80123456), "Unknown");
- }
- END_TEST
- static Suite* testSuite_Utils(void) {
- Suite *s = suite_create("Utils");
- TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
- tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
- suite_add_tcase(s,tc_endpointUrl_split);
- TCase *tc_utils = tcase_create("Utils");
- tcase_add_test(tc_utils, readNumber);
- tcase_add_test(tc_utils, StatusCode_msg);
- suite_add_tcase(s,tc_utils);
- return s;
- }
- int main(void) {
- Suite *s = testSuite_Utils();
- SRunner *sr = srunner_create(s);
- srunner_set_fork_status(sr, CK_NOFORK);
- srunner_run_all(sr,CK_NORMAL);
- int number_failed = srunner_ntests_failed(sr);
- srunner_free(sr);
- return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|