check_utils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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*)"1x", 2, &result, 16), 1);
  173. ck_assert_uint_eq(result, 1);
  174. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"12345678", 9, &result, 16), 8);
  175. ck_assert_uint_eq(result, 0x12345678);
  176. ck_assert_uint_eq(UA_readNumberWithBase((UA_Byte*)"123456789", 9, &result, 8), 7);
  177. ck_assert_uint_eq(result, 01234567);
  178. }
  179. END_TEST
  180. START_TEST(StatusCode_msg) {
  181. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  182. return;
  183. #endif
  184. // first element in table
  185. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOOD), "Good");
  186. // just some randomly picked status codes
  187. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADNOCOMMUNICATION),
  188. "BadNoCommunication");
  189. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOODNODATA), "GoodNoData");
  190. // last element in table
  191. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
  192. "BadMaxConnectionsReached");
  193. // an invalid status code
  194. ck_assert_str_eq(UA_StatusCode_name(0x80123456), "Unknown StatusCode");
  195. }
  196. END_TEST
  197. static void assertNodeIdString(const UA_String *gotStr, const char* expectedStr) {
  198. size_t expectedStringLength = strlen(expectedStr);
  199. ck_assert_uint_eq(gotStr->length, expectedStringLength);
  200. char *gotChars = (char*)UA_malloc(gotStr->length+1);
  201. memcpy(gotChars, gotStr->data, gotStr->length);
  202. gotChars[gotStr->length] = 0;
  203. ck_assert_str_eq(gotChars, expectedStr);
  204. UA_free(gotChars);
  205. }
  206. START_TEST(idToStringNumeric) {
  207. UA_NodeId n;
  208. UA_String str = UA_STRING_NULL;
  209. n = UA_NODEID_NUMERIC(0,0);
  210. UA_NodeId_toString(&n, &str);
  211. assertNodeIdString(&str, "i=0");
  212. n = UA_NODEID_NUMERIC(12345,1234567890);
  213. UA_NodeId_toString(&n, &str);
  214. assertNodeIdString(&str, "ns=12345;i=1234567890");
  215. n = UA_NODEID_NUMERIC(0xFFFF,0xFFFFFFFF);
  216. UA_NodeId_toString(&n, &str);
  217. assertNodeIdString(&str, "ns=65535;i=4294967295");
  218. UA_String_deleteMembers(&str);
  219. } END_TEST
  220. START_TEST(idToStringString) {
  221. UA_NodeId n;
  222. UA_String str = UA_STRING_NULL;
  223. n = UA_NODEID_STRING(0,"");
  224. UA_NodeId_toString(&n, &str);
  225. assertNodeIdString(&str, "s=");
  226. n = UA_NODEID_STRING(54321,"Some String");
  227. UA_NodeId_toString(&n, &str);
  228. assertNodeIdString(&str, "ns=54321;s=Some String");
  229. n = UA_NODEID_STRING(0,"Some String");
  230. UA_NodeId_toString(&n, &str);
  231. assertNodeIdString(&str, "s=Some String");
  232. UA_String_deleteMembers(&str);
  233. } END_TEST
  234. START_TEST(idToStringGuid) {
  235. UA_NodeId n;
  236. UA_String str = UA_STRING_NULL;
  237. UA_Guid g = UA_GUID_NULL;
  238. n = UA_NODEID_GUID(0,UA_GUID_NULL);
  239. UA_NodeId_toString(&n, &str);
  240. assertNodeIdString(&str, "g=00000000-0000-0000-0000-000000000000");
  241. g.data1 = 0xA123456C;
  242. g.data2 = 0x0ABC;
  243. g.data3 = 0x1A2B;
  244. g.data4[0] = 0x81;
  245. g.data4[1] = 0x5F;
  246. g.data4[2] = 0x68;
  247. g.data4[3] = 0x72;
  248. g.data4[4] = 0x12;
  249. g.data4[5] = 0xAA;
  250. g.data4[6] = 0xEE;
  251. g.data4[7] = 0x1B;
  252. n = UA_NODEID_GUID(65535,g);
  253. UA_NodeId_toString(&n, &str);
  254. assertNodeIdString(&str, "ns=65535;g=a123456c-0abc-1a2b-815f-687212aaee1b");
  255. g.data1 = 0xFFFFFFFF;
  256. g.data2 = 0xFFFF;
  257. g.data3 = 0xFFFF;
  258. g.data4[0] = 0xFF;
  259. g.data4[1] = 0xFF;
  260. g.data4[2] = 0xFF;
  261. g.data4[3] = 0xFF;
  262. g.data4[4] = 0xFF;
  263. g.data4[5] = 0xFF;
  264. g.data4[6] = 0xFF;
  265. g.data4[7] = 0xFF;
  266. n = UA_NODEID_GUID(65535,g);
  267. UA_NodeId_toString(&n, &str);
  268. assertNodeIdString(&str, "ns=65535;g=ffffffff-ffff-ffff-ffff-ffffffffffff");
  269. UA_String_deleteMembers(&str);
  270. } END_TEST
  271. START_TEST(idToStringByte) {
  272. UA_NodeId n;
  273. UA_String str = UA_STRING_NULL;
  274. n.namespaceIndex = 0;
  275. n.identifierType = UA_NODEIDTYPE_BYTESTRING;
  276. n.identifier.byteString.data = NULL;
  277. n.identifier.byteString.length = 0;
  278. UA_NodeId_toString(&n, &str);
  279. assertNodeIdString(&str, "b=");
  280. UA_ByteString bs = UA_BYTESTRING_NULL;
  281. bs.length = 1;
  282. bs.data = (UA_Byte*)UA_malloc(bs.length);
  283. bs.data[0] = 0x00;
  284. n.identifier.byteString = bs;
  285. n.namespaceIndex = 123;
  286. UA_NodeId_toString(&n, &str);
  287. assertNodeIdString(&str, "ns=123;b=AA==");
  288. UA_free(bs.data);
  289. bs.length = 1;
  290. bs.data = (UA_Byte*)UA_malloc(bs.length);
  291. bs.data[0] = 0x2C;
  292. n.identifier.byteString = bs;
  293. n.namespaceIndex = 123;
  294. UA_NodeId_toString(&n, &str);
  295. assertNodeIdString(&str, "ns=123;b=LA==");
  296. UA_free(bs.data);
  297. bs.length = 5;
  298. bs.data = (UA_Byte*)UA_malloc(bs.length);
  299. bs.data[0] = 0x21;
  300. bs.data[1] = 0x83;
  301. bs.data[2] = 0xE0;
  302. bs.data[3] = 0x54;
  303. bs.data[4] = 0x78;
  304. n.identifier.byteString = bs;
  305. n.namespaceIndex = 599;
  306. UA_NodeId_toString(&n, &str);
  307. assertNodeIdString(&str, "ns=599;b=IYPgVHg=");
  308. UA_free(bs.data);
  309. UA_String_deleteMembers(&str);
  310. } END_TEST
  311. static Suite* testSuite_Utils(void) {
  312. Suite *s = suite_create("Utils");
  313. TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
  314. tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
  315. suite_add_tcase(s,tc_endpointUrl_split);
  316. TCase *tc_endpointUrl_ethernet = tcase_create("EndpointUrl_ethernet");
  317. tcase_add_test(tc_endpointUrl_ethernet, EndpointUrl_ethernet);
  318. suite_add_tcase(s,tc_endpointUrl_ethernet);
  319. TCase *tc_utils = tcase_create("Utils");
  320. tcase_add_test(tc_utils, readNumber);
  321. tcase_add_test(tc_utils, readNumberWithBase);
  322. tcase_add_test(tc_utils, StatusCode_msg);
  323. suite_add_tcase(s,tc_utils);
  324. TCase *tc1 = tcase_create("test nodeid string");
  325. tcase_add_test(tc1, idToStringNumeric);
  326. tcase_add_test(tc1, idToStringString);
  327. tcase_add_test(tc1, idToStringGuid);
  328. tcase_add_test(tc1, idToStringByte);
  329. suite_add_tcase(s, tc1);
  330. return s;
  331. }
  332. int main(void) {
  333. Suite *s = testSuite_Utils();
  334. SRunner *sr = srunner_create(s);
  335. srunner_set_fork_status(sr, CK_NOFORK);
  336. srunner_run_all(sr,CK_NORMAL);
  337. int number_failed = srunner_ntests_failed(sr);
  338. srunner_free(sr);
  339. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  340. }