check_utils.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <open62541/client.h>
  5. #include "ua_util_internal.h"
  6. #include <stdlib.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. START_TEST(idOrderNs) {
  323. UA_NodeId id_ns1 = UA_NODEID_NUMERIC(1, 12345);
  324. UA_NodeId id_ns3 = UA_NODEID_NUMERIC(3, 12345);
  325. ck_assert(UA_NodeId_order(&id_ns1, &id_ns1) == UA_ORDER_EQ);
  326. ck_assert(UA_NodeId_order(&id_ns1, &id_ns3) == UA_ORDER_LESS);
  327. ck_assert(UA_NodeId_order(&id_ns3, &id_ns1) == UA_ORDER_MORE);
  328. } END_TEST
  329. START_TEST(idOrderIdentifier) {
  330. UA_NodeId id_num = UA_NODEID_NUMERIC(1, 12345);
  331. UA_NodeId id_str = UA_NODEID_STRING(1, "asdf");
  332. ck_assert(UA_NodeId_order(&id_num, &id_num) == UA_ORDER_EQ);
  333. ck_assert(UA_NodeId_order(&id_num, &id_str) == UA_ORDER_LESS);
  334. ck_assert(UA_NodeId_order(&id_str, &id_num) == UA_ORDER_MORE);
  335. } END_TEST
  336. START_TEST(idOrderNumeric) {
  337. UA_NodeId id_num_12345 = UA_NODEID_NUMERIC(1, 12345);
  338. UA_NodeId id_num_23456 = UA_NODEID_NUMERIC(1, 23456);
  339. ck_assert(UA_NodeId_order(&id_num_12345, &id_num_12345) == UA_ORDER_EQ);
  340. ck_assert(UA_NodeId_order(&id_num_12345, &id_num_23456) == UA_ORDER_LESS);
  341. ck_assert(UA_NodeId_order(&id_num_23456, &id_num_12345) == UA_ORDER_MORE);
  342. } END_TEST
  343. START_TEST(idOrderGuid) {
  344. // See also https://github.com/open62541/open62541/pull/2904#issuecomment-514111395
  345. // 00000000-FFFF-FFFF-FFFFFFFFFFFF,
  346. UA_Guid guid1 = {
  347. 0,
  348. 0xffff,
  349. 0xffff,
  350. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
  351. };
  352. // 00000001-0000-0000-000000000000
  353. UA_Guid guid2 = {
  354. 0x1,
  355. 0,
  356. 0,
  357. { 0, 0, 0, 0, 0, 0, 0, 0 }
  358. };
  359. // 00000000-0000-FFFF-FFFFFFFFFFFF
  360. UA_Guid guid3 = {
  361. 0,
  362. 0,
  363. 0xffff,
  364. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
  365. };
  366. // 00000000-0001-0000-000000000000
  367. UA_Guid guid4 = {
  368. 0,
  369. 0x1,
  370. 0,
  371. { 0, 0, 0, 0, 0, 0, 0, 0 }
  372. };
  373. // 00000000-0000-0000-FFFFFFFFFFFF
  374. UA_Guid guid5 = {
  375. 0,
  376. 0,
  377. 0,
  378. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
  379. };
  380. // 00000000-0000-0001-000000000000
  381. UA_Guid guid6 = {
  382. 0,
  383. 0,
  384. 0x1,
  385. { 0, 0, 0, 0, 0, 0, 0, 0 }
  386. };
  387. // 00000000-0000-0000-000000000000
  388. UA_Guid guid7 = {
  389. 0,
  390. 0,
  391. 0,
  392. { 0, 0, 0, 0, 0, 0, 0, 0 }
  393. };
  394. // 00000000-0000-0000-000000000001
  395. UA_Guid guid8 = {
  396. 0,
  397. 0,
  398. 0,
  399. { 0, 0, 0, 0, 0, 0, 0, 0x1 }
  400. };
  401. UA_NodeId id_guid_1 = UA_NODEID_GUID(1, guid1);
  402. UA_NodeId id_guid_2 = UA_NODEID_GUID(1, guid2);
  403. ck_assert(UA_NodeId_order(&id_guid_1, &id_guid_1) == UA_ORDER_EQ);
  404. ck_assert(UA_NodeId_order(&id_guid_1, &id_guid_2) == UA_ORDER_LESS);
  405. ck_assert(UA_NodeId_order(&id_guid_2, &id_guid_1) == UA_ORDER_MORE);
  406. UA_NodeId id_guid_3 = UA_NODEID_GUID(1, guid3);
  407. UA_NodeId id_guid_4 = UA_NODEID_GUID(1, guid4);
  408. ck_assert(UA_NodeId_order(&id_guid_3, &id_guid_3) == UA_ORDER_EQ);
  409. ck_assert(UA_NodeId_order(&id_guid_3, &id_guid_4) == UA_ORDER_LESS);
  410. ck_assert(UA_NodeId_order(&id_guid_4, &id_guid_3) == UA_ORDER_MORE);
  411. UA_NodeId id_guid_5 = UA_NODEID_GUID(1, guid5);
  412. UA_NodeId id_guid_6 = UA_NODEID_GUID(1, guid6);
  413. ck_assert(UA_NodeId_order(&id_guid_5, &id_guid_5) == UA_ORDER_EQ);
  414. ck_assert(UA_NodeId_order(&id_guid_5, &id_guid_2) == UA_ORDER_LESS);
  415. ck_assert(UA_NodeId_order(&id_guid_6, &id_guid_5) == UA_ORDER_MORE);
  416. UA_NodeId id_guid_7 = UA_NODEID_GUID(1, guid7);
  417. UA_NodeId id_guid_8 = UA_NODEID_GUID(1, guid8);
  418. ck_assert(UA_NodeId_order(&id_guid_7, &id_guid_7) == UA_ORDER_EQ);
  419. ck_assert(UA_NodeId_order(&id_guid_7, &id_guid_8) == UA_ORDER_LESS);
  420. ck_assert(UA_NodeId_order(&id_guid_8, &id_guid_7) == UA_ORDER_MORE);
  421. } END_TEST
  422. START_TEST(idOrderString) {
  423. UA_NodeId id_str_a = UA_NODEID_STRING(1, "aaaaa");
  424. UA_NodeId id_str_b = UA_NODEID_STRING(1, "baa");
  425. ck_assert(UA_NodeId_order(&id_str_a, &id_str_a) == UA_ORDER_EQ);
  426. ck_assert(UA_NodeId_order(&id_str_a, &id_str_b) == UA_ORDER_LESS);
  427. ck_assert(UA_NodeId_order(&id_str_b, &id_str_a) == UA_ORDER_MORE);
  428. UA_NodeId id_str_c = UA_NODEID_STRING(1, "cddd");
  429. UA_NodeId id_str_d = UA_NODEID_STRING(1, "dddd");
  430. ck_assert(UA_NodeId_order(&id_str_c, &id_str_c) == UA_ORDER_EQ);
  431. ck_assert(UA_NodeId_order(&id_str_c, &id_str_d) == UA_ORDER_LESS);
  432. ck_assert(UA_NodeId_order(&id_str_d, &id_str_c) == UA_ORDER_MORE);
  433. } END_TEST
  434. static Suite* testSuite_Utils(void) {
  435. Suite *s = suite_create("Utils");
  436. TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
  437. tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
  438. suite_add_tcase(s,tc_endpointUrl_split);
  439. TCase *tc_endpointUrl_ethernet = tcase_create("EndpointUrl_ethernet");
  440. tcase_add_test(tc_endpointUrl_ethernet, EndpointUrl_ethernet);
  441. suite_add_tcase(s,tc_endpointUrl_ethernet);
  442. TCase *tc_utils = tcase_create("Utils");
  443. tcase_add_test(tc_utils, readNumber);
  444. tcase_add_test(tc_utils, readNumberWithBase);
  445. tcase_add_test(tc_utils, StatusCode_msg);
  446. suite_add_tcase(s,tc_utils);
  447. TCase *tc1 = tcase_create("test nodeid string");
  448. tcase_add_test(tc1, idToStringNull);
  449. tcase_add_test(tc1, idToStringNumeric);
  450. tcase_add_test(tc1, idToStringString);
  451. tcase_add_test(tc1, idToStringGuid);
  452. tcase_add_test(tc1, idToStringByte);
  453. suite_add_tcase(s, tc1);
  454. TCase *tc2 = tcase_create("test nodeid order");
  455. tcase_add_test(tc1, idOrderNs);
  456. tcase_add_test(tc1, idOrderIdentifier);
  457. tcase_add_test(tc1, idOrderNumeric);
  458. tcase_add_test(tc1, idOrderGuid);
  459. tcase_add_test(tc1, idOrderString);
  460. suite_add_tcase(s, tc2);
  461. return s;
  462. }
  463. int main(void) {
  464. Suite *s = testSuite_Utils();
  465. SRunner *sr = srunner_create(s);
  466. srunner_set_fork_status(sr, CK_NOFORK);
  467. srunner_run_all(sr,CK_NORMAL);
  468. int number_failed = srunner_ntests_failed(sr);
  469. srunner_free(sr);
  470. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  471. }