check_utils.c 14 KB

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