check_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 <open62541/client.h>
  6. #include "ua_util_internal.h"
  7. #include "check.h"
  8. START_TEST(EndpointUrl_split) {
  9. UA_String hostname = UA_STRING_NULL;
  10. UA_String path = UA_STRING_NULL;
  11. UA_UInt16 port = 0;
  12. // check for too short url
  13. UA_String endPointUrl = UA_STRING("inv.ali:/");
  14. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  15. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  16. // check for opc.tcp:// protocol
  17. endPointUrl = UA_STRING("inv.ali://");
  18. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  19. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  20. // empty url
  21. endPointUrl = UA_STRING("opc.tcp://");
  22. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  23. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  24. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  25. ck_assert_uint_eq(port, 0);
  26. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  27. // only hostname
  28. endPointUrl = UA_STRING("opc.tcp://hostname");
  29. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  30. UA_String expected = UA_STRING("hostname");
  31. ck_assert(UA_String_equal(&hostname, &expected));
  32. ck_assert_uint_eq(port, 0);
  33. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  34. // empty port
  35. endPointUrl = UA_STRING("opc.tcp://hostname:");
  36. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  37. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  38. ck_assert(UA_String_equal(&hostname, &expected));
  39. ck_assert_uint_eq(port, 0);
  40. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  41. // specific port
  42. endPointUrl = UA_STRING("opc.tcp://hostname:1234");
  43. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  44. UA_STATUSCODE_GOOD);
  45. ck_assert(UA_String_equal(&hostname, &expected));
  46. ck_assert_uint_eq(port, 1234);
  47. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  48. // IPv6
  49. endPointUrl = UA_STRING("opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path");
  50. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  51. expected = UA_STRING("[2001:0db8:85a3::8a2e:0370:7334]");
  52. UA_String expectedPath = UA_STRING("path");
  53. ck_assert(UA_String_equal(&hostname, &expected));
  54. ck_assert_uint_eq(port, 1234);
  55. ck_assert(UA_String_equal(&path, &expectedPath));
  56. // invalid IPv6: missing ]
  57. endPointUrl = UA_STRING("opc.tcp://[2001:0db8:85a3::8a2e:0370:7334");
  58. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  59. // empty hostname
  60. endPointUrl = UA_STRING("opc.tcp://:");
  61. port = 0;
  62. path = UA_STRING_NULL;
  63. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  64. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  65. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  66. ck_assert_uint_eq(port, 0);
  67. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  68. // empty hostname and no port
  69. endPointUrl = UA_STRING("opc.tcp:///");
  70. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  71. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  72. ck_assert_uint_eq(port, 0);
  73. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  74. // overlength port
  75. endPointUrl = UA_STRING("opc.tcp://hostname:12345678");
  76. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  77. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  78. // port not a number
  79. endPointUrl = UA_STRING("opc.tcp://hostname:6x6");
  80. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  81. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  82. expected = UA_STRING("hostname");
  83. ck_assert(UA_String_equal(&hostname, &expected));
  84. ck_assert_uint_eq(port, 0);
  85. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  86. // no port but path
  87. endPointUrl = UA_STRING("opc.tcp://hostname/");
  88. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  89. ck_assert(UA_String_equal(&hostname, &expected));
  90. ck_assert_uint_eq(port, 0);
  91. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  92. // port and path
  93. endPointUrl = UA_STRING("opc.tcp://hostname:1234/path");
  94. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  95. ck_assert(UA_String_equal(&hostname, &expected));
  96. ck_assert_uint_eq(port, 1234);
  97. ck_assert(UA_String_equal(&path, &expectedPath));
  98. // port and path with a slash
  99. endPointUrl = UA_STRING("opc.tcp://hostname:1234/path/");
  100. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  101. ck_assert(UA_String_equal(&hostname, &expected));
  102. ck_assert_uint_eq(port, 1234);
  103. ck_assert(UA_String_equal(&path, &expectedPath));
  104. }
  105. END_TEST
  106. START_TEST(EndpointUrl_ethernet) {
  107. UA_String target;
  108. UA_UInt16 vid = 0;
  109. UA_Byte pcp = 0;
  110. UA_String expected;
  111. // check for too short url
  112. UA_String endPointUrl = UA_STRING("inv.ali:/");
  113. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  114. UA_STATUSCODE_BADINTERNALERROR);
  115. ck_assert_uint_eq(vid, 0);
  116. ck_assert_uint_eq(pcp, 0);
  117. // long enough, but malformed
  118. endPointUrl = UA_STRING("opc.eth.//target:");
  119. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  120. UA_STATUSCODE_BADINTERNALERROR);
  121. // valid without vid and pcp but leading ':'
  122. endPointUrl = UA_STRING("opc.eth://target:");
  123. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  124. UA_STATUSCODE_BADINTERNALERROR);
  125. ck_assert_uint_eq(vid, 0);
  126. ck_assert_uint_eq(pcp, 0);
  127. // without pcp and vid as non decimal
  128. endPointUrl = UA_STRING("opc.eth://target:abc");
  129. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  130. UA_STATUSCODE_BADINTERNALERROR);
  131. ck_assert_uint_eq(vid, 0);
  132. ck_assert_uint_eq(pcp, 0);
  133. // pcp as non decimal
  134. endPointUrl = UA_STRING("opc.eth://target:100.abc");
  135. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  136. UA_STATUSCODE_BADINTERNALERROR);
  137. ck_assert_uint_eq(vid, 100);
  138. ck_assert_uint_eq(pcp, 0);
  139. // valid without pcp but leading '.'
  140. endPointUrl = UA_STRING("opc.eth://target:100.");
  141. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  142. UA_STATUSCODE_BADINTERNALERROR);
  143. ck_assert_uint_eq(vid, 100);
  144. ck_assert_uint_eq(pcp, 0);
  145. // valid without pcp
  146. endPointUrl = UA_STRING("opc.eth://target:100");
  147. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  148. UA_STATUSCODE_GOOD);
  149. expected = UA_STRING("target");
  150. ck_assert(UA_String_equal(&target, &expected));
  151. ck_assert_uint_eq(vid, 100);
  152. ck_assert_uint_eq(pcp, 0);
  153. // valid
  154. endPointUrl = UA_STRING("opc.eth://target:100.7");
  155. ck_assert_uint_eq(UA_parseEndpointUrlEthernet(&endPointUrl, &target, &vid, &pcp),
  156. UA_STATUSCODE_GOOD);
  157. expected = UA_STRING("target");
  158. ck_assert(UA_String_equal(&target, &expected));
  159. ck_assert_uint_eq(vid, 100);
  160. ck_assert_uint_eq(pcp, 7);
  161. }
  162. END_TEST
  163. START_TEST(readNumber) {
  164. UA_UInt32 result;
  165. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"x", 1, &result), 0);
  166. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"1x", 2, &result), 1);
  167. ck_assert_uint_eq(result, 1);
  168. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"123456789", 9, &result), 9);
  169. ck_assert_uint_eq(result, 123456789);
  170. }
  171. END_TEST
  172. START_TEST(readNumberWithBase) {
  173. UA_UInt32 result;
  174. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"g", 1, &result, 16), 0);
  175. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"f", 1, &result, 16), 1);
  176. ck_assert_uint_eq(result, 15);
  177. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"F", 1, &result, 16), 1);
  178. ck_assert_uint_eq(result, 15);
  179. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"1x", 2, &result, 16), 1);
  180. ck_assert_uint_eq(result, 1);
  181. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"12345678", 9, &result, 16), 8);
  182. ck_assert_uint_eq(result, 0x12345678);
  183. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"123456789", 9, &result, 8), 7);
  184. ck_assert_uint_eq(result, 01234567);
  185. }
  186. END_TEST
  187. START_TEST(StatusCode_msg) {
  188. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  189. return;
  190. #endif
  191. // first element in table
  192. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOOD), "Good");
  193. // just some randomly picked status codes
  194. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADNOCOMMUNICATION),
  195. "BadNoCommunication");
  196. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOODNODATA), "GoodNoData");
  197. // last element in table
  198. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
  199. "BadMaxConnectionsReached");
  200. // an invalid status code
  201. ck_assert_str_eq(UA_StatusCode_name(0x80123456), "Unknown StatusCode");
  202. }
  203. END_TEST
  204. static void assertNodeIdString(const UA_String *gotStr, const char* expectedStr) {
  205. size_t expectedStringLength = strlen(expectedStr);
  206. ck_assert_uint_eq(gotStr->length, expectedStringLength);
  207. char *gotChars = (char*)UA_malloc(gotStr->length+1);
  208. memcpy(gotChars, gotStr->data, gotStr->length);
  209. gotChars[gotStr->length] = 0;
  210. ck_assert_str_eq(gotChars, expectedStr);
  211. UA_free(gotChars);
  212. }
  213. START_TEST(idToStringNull) {
  214. UA_String str = UA_STRING_NULL;
  215. ck_assert_int_eq(UA_NodeId_toString(NULL, &str), UA_STATUSCODE_GOOD);
  216. } END_TEST
  217. START_TEST(idToStringNumeric) {
  218. UA_NodeId n;
  219. UA_String str = UA_STRING_NULL;
  220. n = UA_NODEID_NUMERIC(0,0);
  221. UA_NodeId_toString(&n, &str);
  222. assertNodeIdString(&str, "i=0");
  223. n = UA_NODEID_NUMERIC(12345,1234567890);
  224. UA_NodeId_toString(&n, &str);
  225. assertNodeIdString(&str, "ns=12345;i=1234567890");
  226. n = UA_NODEID_NUMERIC(0xFFFF,0xFFFFFFFF);
  227. UA_NodeId_toString(&n, &str);
  228. assertNodeIdString(&str, "ns=65535;i=4294967295");
  229. UA_String_deleteMembers(&str);
  230. } END_TEST
  231. START_TEST(idToStringString) {
  232. UA_NodeId n;
  233. UA_String str = UA_STRING_NULL;
  234. n = UA_NODEID_STRING(0,"");
  235. UA_NodeId_toString(&n, &str);
  236. assertNodeIdString(&str, "s=");
  237. n = UA_NODEID_STRING(54321,"Some String");
  238. UA_NodeId_toString(&n, &str);
  239. assertNodeIdString(&str, "ns=54321;s=Some String");
  240. n = UA_NODEID_STRING(0,"Some String");
  241. UA_NodeId_toString(&n, &str);
  242. assertNodeIdString(&str, "s=Some String");
  243. UA_String_deleteMembers(&str);
  244. } END_TEST
  245. START_TEST(idToStringGuid) {
  246. UA_NodeId n;
  247. UA_String str = UA_STRING_NULL;
  248. UA_Guid g = UA_GUID_NULL;
  249. n = UA_NODEID_GUID(0,UA_GUID_NULL);
  250. UA_NodeId_toString(&n, &str);
  251. assertNodeIdString(&str, "g=00000000-0000-0000-0000-000000000000");
  252. g.data1 = 0xA123456C;
  253. g.data2 = 0x0ABC;
  254. g.data3 = 0x1A2B;
  255. g.data4[0] = 0x81;
  256. g.data4[1] = 0x5F;
  257. g.data4[2] = 0x68;
  258. g.data4[3] = 0x72;
  259. g.data4[4] = 0x12;
  260. g.data4[5] = 0xAA;
  261. g.data4[6] = 0xEE;
  262. g.data4[7] = 0x1B;
  263. n = UA_NODEID_GUID(65535,g);
  264. UA_NodeId_toString(&n, &str);
  265. assertNodeIdString(&str, "ns=65535;g=a123456c-0abc-1a2b-815f-687212aaee1b");
  266. g.data1 = 0xFFFFFFFF;
  267. g.data2 = 0xFFFF;
  268. g.data3 = 0xFFFF;
  269. g.data4[0] = 0xFF;
  270. g.data4[1] = 0xFF;
  271. g.data4[2] = 0xFF;
  272. g.data4[3] = 0xFF;
  273. g.data4[4] = 0xFF;
  274. g.data4[5] = 0xFF;
  275. g.data4[6] = 0xFF;
  276. g.data4[7] = 0xFF;
  277. n = UA_NODEID_GUID(65535,g);
  278. UA_NodeId_toString(&n, &str);
  279. assertNodeIdString(&str, "ns=65535;g=ffffffff-ffff-ffff-ffff-ffffffffffff");
  280. UA_String_deleteMembers(&str);
  281. } END_TEST
  282. START_TEST(idToStringByte) {
  283. UA_NodeId n;
  284. UA_String str = UA_STRING_NULL;
  285. n.namespaceIndex = 0;
  286. n.identifierType = UA_NODEIDTYPE_BYTESTRING;
  287. n.identifier.byteString.data = NULL;
  288. n.identifier.byteString.length = 0;
  289. UA_NodeId_toString(&n, &str);
  290. assertNodeIdString(&str, "b=");
  291. UA_ByteString bs = UA_BYTESTRING_NULL;
  292. bs.length = 1;
  293. bs.data = (UA_Byte*)UA_malloc(bs.length);
  294. bs.data[0] = 0x00;
  295. n.identifier.byteString = bs;
  296. n.namespaceIndex = 123;
  297. UA_NodeId_toString(&n, &str);
  298. assertNodeIdString(&str, "ns=123;b=AA==");
  299. UA_free(bs.data);
  300. bs.length = 1;
  301. bs.data = (UA_Byte*)UA_malloc(bs.length);
  302. bs.data[0] = 0x2C;
  303. n.identifier.byteString = bs;
  304. n.namespaceIndex = 123;
  305. UA_NodeId_toString(&n, &str);
  306. assertNodeIdString(&str, "ns=123;b=LA==");
  307. UA_free(bs.data);
  308. bs.length = 5;
  309. bs.data = (UA_Byte*)UA_malloc(bs.length);
  310. bs.data[0] = 0x21;
  311. bs.data[1] = 0x83;
  312. bs.data[2] = 0xE0;
  313. bs.data[3] = 0x54;
  314. bs.data[4] = 0x78;
  315. n.identifier.byteString = bs;
  316. n.namespaceIndex = 599;
  317. UA_NodeId_toString(&n, &str);
  318. assertNodeIdString(&str, "ns=599;b=IYPgVHg=");
  319. UA_free(bs.data);
  320. UA_String_deleteMembers(&str);
  321. } END_TEST
  322. static Suite* testSuite_Utils(void) {
  323. Suite *s = suite_create("Utils");
  324. TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
  325. tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
  326. suite_add_tcase(s,tc_endpointUrl_split);
  327. TCase *tc_endpointUrl_ethernet = tcase_create("EndpointUrl_ethernet");
  328. tcase_add_test(tc_endpointUrl_ethernet, EndpointUrl_ethernet);
  329. suite_add_tcase(s,tc_endpointUrl_ethernet);
  330. TCase *tc_utils = tcase_create("Utils");
  331. tcase_add_test(tc_utils, readNumber);
  332. tcase_add_test(tc_utils, readNumberWithBase);
  333. tcase_add_test(tc_utils, StatusCode_msg);
  334. suite_add_tcase(s,tc_utils);
  335. TCase *tc1 = tcase_create("test nodeid string");
  336. tcase_add_test(tc1, idToStringNull);
  337. tcase_add_test(tc1, idToStringNumeric);
  338. tcase_add_test(tc1, idToStringString);
  339. tcase_add_test(tc1, idToStringGuid);
  340. tcase_add_test(tc1, idToStringByte);
  341. suite_add_tcase(s, tc1);
  342. return s;
  343. }
  344. int main(void) {
  345. Suite *s = testSuite_Utils();
  346. SRunner *sr = srunner_create(s);
  347. srunner_set_fork_status(sr, CK_NOFORK);
  348. srunner_run_all(sr,CK_NORMAL);
  349. int number_failed = srunner_ntests_failed(sr);
  350. srunner_free(sr);
  351. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  352. }