check_client_highlevel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <ua_types.h>
  8. #include "ua_server.h"
  9. #include "ua_client.h"
  10. #include "ua_config_standard.h"
  11. #include "ua_network_tcp.h"
  12. #include "check.h"
  13. UA_Server *server;
  14. UA_Boolean *running;
  15. UA_ServerNetworkLayer nl;
  16. pthread_t server_thread;
  17. UA_Client *client;
  18. #define CUSTOM_NS "http://open62541.org/ns/test"
  19. #define CUSTOM_NS_UPPER "http://open62541.org/ns/Test"
  20. static void *serverloop(void *_) {
  21. while (*running)
  22. UA_Server_run_iterate(server, true);
  23. return NULL;
  24. }
  25. static void setup(void) {
  26. running = UA_Boolean_new();
  27. *running = true;
  28. UA_ServerConfig config = UA_ServerConfig_standard;
  29. nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  30. config.networkLayers = &nl;
  31. config.networkLayersSize = 1;
  32. server = UA_Server_new(config);
  33. ck_assert_uint_eq(2, UA_Server_addNamespace(server, CUSTOM_NS));
  34. UA_Server_run_startup(server);
  35. pthread_create(&server_thread, NULL, serverloop, NULL);
  36. client = UA_Client_new(UA_ClientConfig_standard);
  37. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  38. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  39. }
  40. static void teardown(void) {
  41. *running = false;
  42. UA_Client_disconnect(client);
  43. UA_Client_delete(client);
  44. pthread_join(server_thread, NULL);
  45. UA_Server_run_shutdown(server);
  46. UA_Boolean_delete(running);
  47. UA_Server_delete(server);
  48. nl.deleteMembers(&nl);
  49. }
  50. START_TEST(Misc_State)
  51. {
  52. UA_ClientState state = UA_Client_getState(client);
  53. ck_assert_uint_eq(state, UA_CLIENTSTATE_CONNECTED);
  54. }
  55. END_TEST
  56. START_TEST(Misc_NamespaceGetIndex)
  57. {
  58. UA_UInt16 idx;
  59. UA_String ns = UA_STRING(CUSTOM_NS);
  60. UA_StatusCode retval = UA_Client_NamespaceGetIndex(client, &ns, &idx);
  61. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  62. ck_assert_uint_eq(idx, 2);
  63. // namespace uri is case sensitive
  64. ns = UA_STRING(CUSTOM_NS_UPPER);
  65. retval = UA_Client_NamespaceGetIndex(client, &ns, &idx);
  66. ck_assert_uint_eq(retval, UA_STATUSCODE_BADNOTFOUND);
  67. }
  68. END_TEST
  69. UA_NodeId newReferenceTypeId;
  70. UA_NodeId newObjectTypeId;
  71. UA_NodeId newDataTypeId;
  72. UA_NodeId newVariableTypeId;
  73. UA_NodeId newObjectId;
  74. UA_NodeId newVariableId;
  75. UA_NodeId newMethodId;
  76. UA_NodeId newViewId;
  77. START_TEST(Node_Add)
  78. {
  79. UA_StatusCode retval;
  80. // Create custom reference type 'HasSubSubType' as child of HasSubtype
  81. {
  82. UA_ReferenceTypeAttributes attr;
  83. UA_ReferenceTypeAttributes_init(&attr);
  84. attr.description = UA_LOCALIZEDTEXT("en_US", "Some HasSubSubType");
  85. attr.displayName = UA_LOCALIZEDTEXT("en_US", "HasSubSubType");
  86. retval = UA_Client_addReferenceTypeNode(client, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  87. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  88. UA_QUALIFIEDNAME(1, "HasSubSubType"), attr, &newReferenceTypeId);
  89. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  90. }
  91. // Create TestObjectType SubType within BaseObjectType
  92. {
  93. UA_ObjectTypeAttributes attr;
  94. UA_ObjectTypeAttributes_init(&attr);
  95. attr.description = UA_LOCALIZEDTEXT("en_US", "Some TestObjectType");
  96. attr.displayName = UA_LOCALIZEDTEXT("en_US", "TestObjectType");
  97. retval = UA_Client_addObjectTypeNode(client, UA_NODEID_NULL,
  98. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  100. UA_QUALIFIEDNAME(1, "TestObjectType"), attr, &newObjectTypeId);
  101. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  102. }
  103. // Create Int128 DataType within Integer Datatype
  104. {
  105. UA_DataTypeAttributes attr;
  106. UA_DataTypeAttributes_init(&attr);
  107. attr.description = UA_LOCALIZEDTEXT("en_US", "Some Int128");
  108. attr.displayName = UA_LOCALIZEDTEXT("en_US", "Int128");
  109. retval = UA_Client_addDataTypeNode(client, UA_NODEID_NULL,
  110. UA_NODEID_NUMERIC(0, UA_NS0ID_INTEGER),
  111. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  112. UA_QUALIFIEDNAME(1, "Int128"), attr, &newDataTypeId);
  113. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  114. }
  115. // Create PointType VariableType within BaseDataVariableType
  116. {
  117. UA_VariableTypeAttributes attr;
  118. UA_VariableTypeAttributes_init(&attr);
  119. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  120. attr.valueRank = 1; /* array with one dimension */
  121. UA_UInt32 arrayDims[1] = {2};
  122. attr.arrayDimensions = arrayDims;
  123. attr.arrayDimensionsSize = 1;
  124. attr.displayName = UA_LOCALIZEDTEXT("en_US", "PointType");
  125. /* a matching default value is required */
  126. UA_Double zero[2] = {0.0, 0.0};
  127. UA_Variant_setArray(&attr.value, zero, 2, &UA_TYPES[UA_TYPES_INT32]);
  128. retval = UA_Client_addVariableTypeNode(client, UA_NODEID_NULL,
  129. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEVARIABLETYPE),
  130. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  131. UA_QUALIFIEDNAME(1, "PointType"), attr, &newVariableTypeId);
  132. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  133. }
  134. // create Coordinates Object within ObjectsFolder
  135. {
  136. UA_ObjectAttributes attr;
  137. UA_ObjectAttributes_init(&attr);
  138. attr.description = UA_LOCALIZEDTEXT("en_US", "Some Coordinates");
  139. attr.displayName = UA_LOCALIZEDTEXT("en_US", "Coordinates");
  140. retval = UA_Client_addObjectNode(client, UA_NODEID_NULL,
  141. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  142. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  143. UA_QUALIFIEDNAME(1, "Coordinates"),
  144. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), attr, &newObjectId);
  145. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  146. }
  147. // create Variable 'Top' within Coordinates Object
  148. {
  149. UA_VariableAttributes attr;
  150. UA_VariableAttributes_init(&attr);
  151. attr.description = UA_LOCALIZEDTEXT("en_US", "Top Coordinate");
  152. attr.displayName = UA_LOCALIZEDTEXT("en_US", "Top");
  153. UA_Int32 values[2];
  154. values[1] = 10;
  155. values[2] = 20;
  156. UA_Variant_setArray(&attr.value, values, 2, &UA_TYPES[UA_TYPES_INT32]);
  157. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  158. attr.valueRank = 1; /* array with one dimension */
  159. UA_UInt32 arrayDims[1] = {2};
  160. attr.arrayDimensions = arrayDims;
  161. attr.arrayDimensionsSize = 1;
  162. retval = UA_Client_addVariableNode(client, UA_NODEID_NULL,
  163. newObjectId,
  164. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  165. UA_QUALIFIEDNAME(1, "Top"),
  166. newVariableTypeId, attr, &newVariableId);
  167. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  168. }
  169. // create Method 'Dummy' within Coordinates Object. Fails with BADNODECLASSINVALID
  170. {
  171. // creating a method from a client does not yet make much sense since the corresponding
  172. // action code can not be set from the client side
  173. UA_MethodAttributes attr;
  174. UA_MethodAttributes_init(&attr);
  175. attr.description = UA_LOCALIZEDTEXT("en_US", "Dummy method");
  176. attr.displayName = UA_LOCALIZEDTEXT("en_US", "Dummy");
  177. attr.executable = true;
  178. attr.userExecutable = true;
  179. retval = UA_Client_addMethodNode(client, UA_NODEID_NULL,
  180. newObjectId,
  181. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  182. UA_QUALIFIEDNAME(1, "Dummy"),
  183. attr, &newMethodId);
  184. ck_assert_uint_eq(retval, UA_STATUSCODE_BADNODECLASSINVALID);
  185. }
  186. // create View 'AllTopCoordinates' whithin Views Folder
  187. {
  188. UA_ViewAttributes attr;
  189. UA_ViewAttributes_init(&attr);
  190. attr.description = UA_LOCALIZEDTEXT("en_US", "List of all top coordinates");
  191. attr.displayName = UA_LOCALIZEDTEXT("en_US", "AllTopCoordinates");
  192. retval = UA_Client_addViewNode(client, UA_NODEID_NULL,
  193. UA_NODEID_NUMERIC(0, UA_NS0ID_VIEWSFOLDER),
  194. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  195. UA_QUALIFIEDNAME(1, "AllTopCoordinates"),
  196. attr, &newViewId);
  197. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  198. }
  199. // Add 'Top' to view
  200. {
  201. retval = UA_Client_addReference(client, newViewId, UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  202. UA_TRUE, UA_STRING_NULL,
  203. UA_EXPANDEDNODEID_NUMERIC(1, newObjectId.identifier.numeric),
  204. UA_NODECLASS_VARIABLE);
  205. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  206. }
  207. // Delete 'Top' from view
  208. {
  209. retval = UA_Client_deleteReference(client, newViewId, UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  210. UA_TRUE, UA_EXPANDEDNODEID_NUMERIC(1, newObjectId.identifier.numeric), UA_TRUE);
  211. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  212. }
  213. // Delete 'AllTopCoordinates' view
  214. {
  215. retval = UA_Client_deleteNode(client, newViewId, UA_TRUE);
  216. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  217. }
  218. }
  219. END_TEST
  220. START_TEST(Node_Browse)
  221. {
  222. // Browse node in server folder
  223. {
  224. UA_BrowseRequest bReq;
  225. UA_BrowseRequest_init(&bReq);
  226. // normally is set to 0, to get all the nodes, but we want to test browse next
  227. bReq.requestedMaxReferencesPerNode = 1;
  228. bReq.nodesToBrowse = UA_BrowseDescription_new();
  229. bReq.nodesToBrowseSize = 1;
  230. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  231. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
  232. UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
  233. ck_assert_uint_eq(bResp.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  234. ck_assert_uint_eq(bResp.resultsSize, 1);
  235. ck_assert_uint_eq(bResp.results[0].statusCode, UA_STATUSCODE_GOOD);
  236. ck_assert_uint_eq(bResp.results[0].referencesSize, 1);
  237. /* References might have a different order in generated nodesets */
  238. /* UA_ReferenceDescription *ref = &(bResp.results[0].references[0]); */
  239. /* ck_assert_uint_eq(ref->nodeId.nodeId.identifier.numeric, UA_NS0ID_SERVERTYPE); */
  240. // browse next
  241. UA_BrowseNextRequest bNextReq;
  242. UA_BrowseNextRequest_init(&bNextReq);
  243. // normally is set to 0, to get all the nodes, but we want to test browse next
  244. bNextReq.releaseContinuationPoints = UA_FALSE;
  245. bNextReq.continuationPoints = &bResp.results[0].continuationPoint;
  246. bNextReq.continuationPointsSize = 1;
  247. UA_BrowseNextResponse bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  248. ck_assert_uint_eq(bNextResp.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  249. ck_assert_uint_eq(bNextResp.resultsSize, 1);
  250. ck_assert_uint_eq(bNextResp.results[0].statusCode, UA_STATUSCODE_GOOD);
  251. ck_assert_uint_eq(bNextResp.results[0].referencesSize, 1);
  252. /* ref = &(bNextResp.results[0].references[0]); */
  253. /* ck_assert_uint_eq(ref->nodeId.nodeId.identifier.numeric, UA_NS0ID_SERVER_NAMESPACEARRAY); */
  254. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  255. bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  256. ck_assert_uint_eq(bNextResp.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  257. ck_assert_uint_eq(bNextResp.resultsSize, 1);
  258. ck_assert_uint_eq(bNextResp.results[0].statusCode, UA_STATUSCODE_GOOD);
  259. ck_assert_uint_eq(bNextResp.results[0].referencesSize, 1);
  260. /* ref = &(bNextResp.results[0].references[0]); */
  261. /* ck_assert_uint_eq(ref->nodeId.nodeId.identifier.numeric, UA_NS0ID_SERVER_SERVERARRAY); */
  262. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  263. // release continuation point. Result is then empty
  264. bNextReq.releaseContinuationPoints = UA_TRUE;
  265. bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  266. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  267. ck_assert_uint_eq(bNextResp.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  268. ck_assert_uint_eq(bNextResp.resultsSize, 0);
  269. UA_BrowseRequest_deleteMembers(&bReq);
  270. UA_BrowseResponse_deleteMembers(&bResp);
  271. // already deleted by browse request
  272. bNextReq.continuationPoints = NULL;
  273. bNextReq.continuationPointsSize = 0;
  274. UA_BrowseNextRequest_deleteMembers(&bNextReq);
  275. }
  276. }
  277. END_TEST
  278. START_TEST(Node_Register)
  279. {
  280. {
  281. UA_RegisterNodesRequest req;
  282. UA_RegisterNodesRequest_init(&req);
  283. req.nodesToRegister = UA_NodeId_new();
  284. req.nodesToRegister[0] = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  285. req.nodesToRegisterSize = 1;
  286. UA_RegisterNodesResponse res = UA_Client_Service_registerNodes(client, req);
  287. ck_assert_uint_eq(res.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  288. ck_assert_uint_eq(res.registeredNodeIdsSize, 1);
  289. UA_UnregisterNodesRequest reqUn;
  290. UA_UnregisterNodesRequest_init(&reqUn);
  291. reqUn.nodesToUnregister = UA_NodeId_new();
  292. reqUn.nodesToUnregister[0] = res.registeredNodeIds[0];
  293. reqUn.nodesToUnregisterSize = 1;
  294. UA_UnregisterNodesResponse resUn = UA_Client_Service_unregisterNodes(client, reqUn);
  295. ck_assert_uint_eq(resUn.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  296. UA_UnregisterNodesRequest_deleteMembers(&reqUn);
  297. UA_UnregisterNodesResponse_deleteMembers(&resUn);
  298. UA_RegisterNodesRequest_deleteMembers(&req);
  299. UA_RegisterNodesResponse_deleteMembers(&res);
  300. }
  301. }
  302. END_TEST
  303. static Suite *testSuite_Client(void) {
  304. Suite *s = suite_create("Client Highlevel");
  305. TCase *tc_misc = tcase_create("Client Highlevel Misc");
  306. tcase_add_checked_fixture(tc_misc, setup, teardown);
  307. tcase_add_test(tc_misc, Misc_State);
  308. tcase_add_test(tc_misc, Misc_NamespaceGetIndex);
  309. suite_add_tcase(s, tc_misc);
  310. TCase *tc_nodes = tcase_create("Client Highlevel Node Management");
  311. tcase_add_checked_fixture(tc_nodes, setup, teardown);
  312. tcase_add_test(tc_nodes, Node_Add);
  313. tcase_add_test(tc_nodes, Node_Browse);
  314. tcase_add_test(tc_nodes, Node_Register);
  315. suite_add_tcase(s, tc_nodes);
  316. return s;
  317. }
  318. int main(void) {
  319. Suite *s = testSuite_Client();
  320. SRunner *sr = srunner_create(s);
  321. srunner_set_fork_status(sr, CK_NOFORK);
  322. srunner_run_all(sr, CK_NORMAL);
  323. int number_failed = srunner_ntests_failed(sr);
  324. srunner_free(sr);
  325. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  326. }