check_pubsub_subscribe.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. *
  5. * Copyright (c) 2019 Kalycito Infotech Private Limited
  6. */
  7. #include <open62541/plugin/pubsub_udp.h>
  8. #include <open62541/server_config_default.h>
  9. #include <open62541/server_pubsub.h>
  10. #include <check.h>
  11. #include <time.h>
  12. #include "open62541/types_generated_encoding_binary.h"
  13. #include "ua_pubsub.h"
  14. #include "ua_server_internal.h"
  15. #define UA_SUBSCRIBER_PORT 4801 /* Port for Subscriber*/
  16. #define PUBLISH_INTERVAL 100 /* Publish interval*/
  17. #define DATASET_WRITERD_ID 62541 /* DataSet Writer Id*/
  18. #define WRITER_GROUP_ID 100 /* Writer group Id */
  19. /* Global declaration for test cases */
  20. UA_Server *server = NULL;
  21. UA_ServerConfig *config = NULL;
  22. UA_NodeId connection_test;
  23. UA_NodeId readerGroupTest;
  24. UA_NodeId publishedDataSetTest;
  25. /* setup() is to create an environment for test cases */
  26. static void setup(void) {
  27. /*Add setup by creating new server with valid configuration */
  28. server = UA_Server_new();
  29. config = UA_Server_getConfig(server);
  30. UA_ServerConfig_setMinimal(config, UA_SUBSCRIBER_PORT, NULL);
  31. UA_Server_run_startup(server);
  32. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  33. if(!config->pubsubTransportLayers) {
  34. UA_ServerConfig_clean(config);
  35. }
  36. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  37. config->pubsubTransportLayersSize++;
  38. /* Add connection to the server */
  39. UA_PubSubConnectionConfig connectionConfig;
  40. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  41. connectionConfig.name = UA_STRING("UADP Test Connection");
  42. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4801/")};
  43. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  44. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  45. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  46. UA_Server_addPubSubConnection(server, &connectionConfig, &connection_test);
  47. }
  48. /* teardown() is to delete the environment set for test cases */
  49. static void teardown(void) {
  50. /*Call server delete functions */
  51. UA_Server_run_shutdown(server);
  52. UA_Server_delete(server);
  53. }
  54. START_TEST(AddReaderGroupWithValidConfiguration) {
  55. /* To test if ReaderGroup has been added to the connection with valid configuration */
  56. UA_StatusCode retVal;
  57. UA_ReaderGroupConfig readerGroupConfig;
  58. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  59. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  60. UA_NodeId localreaderGroup;
  61. retVal = UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  62. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  63. size_t readerGroupCount = 0;
  64. UA_ReaderGroup *readerGroup;
  65. LIST_FOREACH(readerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection_test)->readerGroups, listEntry){
  66. readerGroupCount++;
  67. }
  68. /* Check readerGroup count */
  69. ck_assert_int_eq(readerGroupCount, 1);
  70. /* To Do: RemoveReaderGroup operation should be carried out when UA_Server_delete has been called */
  71. UA_Server_removeReaderGroup(server, localreaderGroup);
  72. } END_TEST
  73. START_TEST(AddReaderGroupWithNullConfig) {
  74. /* Check the status of adding ReaderGroup when NULL configuration is given */
  75. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  76. retVal |= UA_Server_addReaderGroup(server, connection_test, NULL, NULL);
  77. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  78. size_t readerGroupCount = 0;
  79. UA_ReaderGroup *readerGroup;
  80. LIST_FOREACH(readerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection_test)->readerGroups, listEntry){
  81. readerGroupCount++;
  82. }
  83. /* Check readerGroup count */
  84. ck_assert_int_eq(readerGroupCount, 0);
  85. } END_TEST
  86. START_TEST(AddReaderGroupWithInvalidConnectionId) {
  87. /* Check status of adding ReaderGroup with invalid connection identifier */
  88. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  89. UA_ReaderGroupConfig readerGroupConfig;
  90. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  91. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  92. retVal |= UA_Server_addReaderGroup(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &readerGroupConfig, NULL);
  93. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  94. size_t readerGroupCount = 0;
  95. UA_ReaderGroup *readerGroup;
  96. LIST_FOREACH(readerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection_test)->readerGroups, listEntry){
  97. readerGroupCount++;
  98. }
  99. /* Check readerGroup count */
  100. ck_assert_int_eq(readerGroupCount, 0);
  101. } END_TEST
  102. START_TEST(RemoveReaderGroupWithInvalidIdentifier) {
  103. /* Check status of removing ReaderGroup when giving invalid ReaderGroup identifier */
  104. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  105. UA_ReaderGroupConfig readerGroupConfig;
  106. UA_NodeId localreaderGroup;
  107. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  108. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  109. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  110. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  111. /* Delete the added readerGroup */
  112. retVal |= UA_Server_removeReaderGroup(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX));
  113. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  114. size_t readerGroupCount = 0;
  115. UA_ReaderGroup *readerGroup;
  116. LIST_FOREACH(readerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection_test)->readerGroups, listEntry){
  117. readerGroupCount++;
  118. }
  119. /* Check readerGroup count */
  120. ck_assert_int_eq(readerGroupCount, 1);
  121. UA_Server_removeReaderGroup(server, localreaderGroup);
  122. } END_TEST
  123. START_TEST(UpdateReaderGroupWithInvalidIdentifier) {
  124. /* Check status of updating ReaderGroup with invalid identifier */
  125. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  126. UA_ReaderGroupConfig readerGroupConfig;
  127. UA_NodeId localreaderGroup;
  128. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  129. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  130. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  131. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  132. retVal |= UA_Server_ReaderGroup_updateConfig(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), NULL);
  133. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  134. UA_Server_removeReaderGroup(server, localreaderGroup);
  135. } END_TEST
  136. START_TEST(GetReaderGroupConfigWithInvalidConfig) {
  137. /* Check status of getting ReaderGroup configuration with invalid configuration */
  138. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  139. UA_ReaderGroupConfig readerGroupConfig;
  140. UA_NodeId localreaderGroup;
  141. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  142. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  143. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  144. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  145. retVal |= UA_Server_ReaderGroup_getConfig(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), NULL);
  146. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  147. UA_Server_removeReaderGroup(server, localreaderGroup);
  148. } END_TEST
  149. START_TEST(GetReaderGroupConfigWithInvalidIdentifier) {
  150. /* Check status of getting ReaderGroup configuration with invlaid identifier */
  151. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  152. UA_ReaderGroupConfig readerGroupConfig;
  153. UA_NodeId localreaderGroup;
  154. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  155. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  156. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  157. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  158. retVal |= UA_Server_ReaderGroup_getConfig(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &readerGroupConfig);
  159. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  160. UA_Server_removeReaderGroup(server, localreaderGroup);
  161. } END_TEST
  162. START_TEST(GetReaderGroupConfigWithValidConfig) {
  163. /* Check status of getting ReaderGroup configuration with valid parameters */
  164. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  165. UA_NodeId localreaderGroup;
  166. UA_ReaderGroupConfig readerGroupConfig;
  167. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  168. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  169. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  170. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  171. retVal |= UA_Server_ReaderGroup_getConfig(server, localreaderGroup, &readerGroupConfig);
  172. /* To Do: DeleteMembers of readergroup config has to be a separate function */
  173. UA_String_deleteMembers (&readerGroupConfig.name);
  174. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  175. } END_TEST
  176. START_TEST(AddDataSetReaderWithValidConfiguration) {
  177. /* Check status of adding DataSetReader to ReaderGroup with valid parameters*/
  178. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  179. UA_DataSetReaderConfig dataSetreaderConfig;
  180. UA_NodeId localreaderGroup;
  181. UA_ReaderGroupConfig readerGroupConfig;
  182. UA_NodeId localDataSetreader;
  183. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  184. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  185. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  186. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  187. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  188. dataSetreaderConfig.name = UA_STRING("DataSetreader Test");
  189. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  190. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  191. } END_TEST
  192. START_TEST(AddDataSetReaderWithNullConfig) {
  193. /* Check status of adding DataSetReader to ReaderGroup with invalid parameters */
  194. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  195. UA_DataSetReaderConfig dataSetreaderConfig;
  196. UA_NodeId localreaderGroup;
  197. UA_ReaderGroupConfig readerGroupConfig;
  198. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  199. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  200. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  201. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  202. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  203. dataSetreaderConfig.name = UA_STRING("DataSetreader Test ");
  204. /* Remove the added DataSetReader */
  205. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, NULL, NULL);
  206. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  207. } END_TEST
  208. START_TEST(RemoveDataSetReaderWithValidConfiguration) {
  209. /* Check status of adding DataSetReader to ReaderGroup with valid parameters */
  210. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  211. UA_DataSetReaderConfig dataSetreaderConfig;
  212. UA_NodeId localreaderGroup;
  213. UA_ReaderGroupConfig readerGroupConfig;
  214. UA_NodeId localDataSetreader;
  215. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  216. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  217. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  218. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  219. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  220. dataSetreaderConfig.name = UA_STRING("DataSetReader Test ");
  221. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  222. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  223. retVal |= UA_Server_removeDataSetReader(server, localDataSetreader);
  224. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  225. } END_TEST
  226. START_TEST(RemoveDataSetReaderWithInvalidIdentifier) {
  227. /* Check status of removing DataSetReader from ReaderGroup with invalid parameters */
  228. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  229. UA_DataSetReaderConfig dataSetreaderConfig;
  230. UA_NodeId localreaderGroup;
  231. UA_ReaderGroupConfig readerGroupConfig;
  232. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  233. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  234. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  235. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  236. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  237. dataSetreaderConfig.name = UA_STRING("DataSetReader Test ");
  238. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, NULL, NULL);
  239. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  240. /* Remove the added DataSetReader */
  241. retVal |= UA_Server_removeDataSetReader(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX));
  242. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  243. } END_TEST
  244. START_TEST(UpdateDataSetReaderConfigWithInvalidId) {
  245. /* Check status of updatting DataSetReader with invalid configuration */
  246. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  247. UA_DataSetReaderConfig dataSetreaderConfig;
  248. UA_NodeId localreaderGroup;
  249. UA_NodeId localDataSetreader;
  250. UA_ReaderGroupConfig readerGroupConfig;
  251. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  252. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  253. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  254. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  255. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  256. retVal |= UA_Server_addDataSetReader(server, localreaderGroup,
  257. &dataSetreaderConfig, &localDataSetreader);
  258. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  259. retVal |= UA_Server_DataSetReader_updateConfig(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX),
  260. localreaderGroup, &dataSetreaderConfig );
  261. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  262. } END_TEST
  263. START_TEST(GetDataSetReaderConfigWithValidConfiguration) {
  264. /* Check status of getting DataSetReader with Valid configuration */
  265. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  266. UA_DataSetReaderConfig dataSetreaderConfig;
  267. UA_NodeId localreaderGroup;
  268. UA_NodeId localDataSetreader;
  269. UA_ReaderGroupConfig readerGroupConfig;
  270. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  271. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  272. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  273. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  274. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  275. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  276. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  277. retVal |= UA_Server_DataSetReader_getConfig(server, localDataSetreader, &dataSetreaderConfig);
  278. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  279. } END_TEST
  280. START_TEST(GetDataSetReaderConfigWithInvalidConfiguration) {
  281. /* Check status of getting DataSetReader with Invalid configuration */
  282. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  283. UA_DataSetReaderConfig dataSetreaderConfig;
  284. UA_NodeId localreaderGroup;
  285. UA_NodeId localDataSetreader;
  286. UA_ReaderGroupConfig readerGroupConfig;
  287. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  288. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  289. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  290. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  291. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  292. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  293. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  294. retVal |= UA_Server_DataSetReader_getConfig(server, localDataSetreader, NULL);
  295. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  296. } END_TEST
  297. START_TEST(GetDataSetReaderConfigWithInvalidIdentifier) {
  298. /* Check status of getting DataSetReader with Invalid Identifier */
  299. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  300. UA_DataSetReaderConfig dataSetreaderConfig;
  301. UA_NodeId localreaderGroup;
  302. UA_NodeId localDataSetreader;
  303. UA_ReaderGroupConfig readerGroupConfig;
  304. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  305. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  306. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  307. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  308. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  309. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  310. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  311. retVal |= UA_Server_DataSetReader_getConfig(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &dataSetreaderConfig);
  312. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  313. } END_TEST
  314. START_TEST(CreateTargetVariableWithInvalidConfiguration) {
  315. /* Check status to create target variable with invalid configuration */
  316. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  317. UA_DataSetReaderConfig dataSetreaderConfig;
  318. UA_NodeId localreaderGroup;
  319. UA_NodeId localDataSetreader;
  320. UA_TargetVariablesDataType localTargetVariable;
  321. UA_ReaderGroupConfig readerGroupConfig;
  322. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  323. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  324. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  325. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  326. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  327. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  328. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  329. retVal |= UA_Server_DataSetReader_createTargetVariables(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &localTargetVariable);
  330. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  331. } END_TEST
  332. START_TEST(AddTargetVariableWithInvalidConfiguration) {
  333. /* Check status to create target variable with invalid configuration */
  334. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  335. UA_DataSetReaderConfig dataSetreaderConfig;
  336. UA_NodeId localreaderGroup;
  337. UA_NodeId localDataSetreader;
  338. UA_ReaderGroupConfig readerGroupConfig;
  339. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  340. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  341. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroup);
  342. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  343. memset(&dataSetreaderConfig, 0, sizeof(dataSetreaderConfig));
  344. retVal |= UA_Server_addDataSetReader(server, localreaderGroup, &dataSetreaderConfig, &localDataSetreader);
  345. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  346. retVal |= UA_Server_DataSetReader_addTargetVariables(NULL, &localreaderGroup, localDataSetreader, UA_PUBSUB_SDS_TARGET);
  347. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  348. } END_TEST
  349. START_TEST(AddTargetVariableWithValidConfiguration) {
  350. /* Check status after creating target variables with Valid configuration */
  351. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  352. UA_DataSetReaderConfig dataSetReaderConfig;
  353. UA_NodeId folderId;
  354. UA_QualifiedName folderBrowseName;
  355. UA_NodeId localreaderGroupIdentifier;
  356. UA_NodeId localDataSetreaderIdentifier;
  357. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  358. UA_ReaderGroupConfig readerGroupConfig;
  359. memset(&readerGroupConfig, 0, sizeof(readerGroupConfig));
  360. readerGroupConfig.name = UA_STRING("ReaderGroup Test");
  361. retVal |= UA_Server_addReaderGroup(server, connection_test, &readerGroupConfig, &localreaderGroupIdentifier);
  362. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  363. memset(&dataSetReaderConfig, 0, sizeof(UA_DataSetReaderConfig));
  364. UA_DataSetMetaDataType *pMetaData = &dataSetReaderConfig.dataSetMetaData;
  365. UA_DataSetMetaDataType_init (pMetaData);
  366. pMetaData->name = UA_STRING ("DataSet Test");
  367. /* Static definition of number of fields size to 4 to create four different
  368. * targetVariables of distinct datatype */
  369. pMetaData->fieldsSize = 1;
  370. pMetaData->fields = (UA_FieldMetaData*)UA_Array_new (pMetaData->fieldsSize,
  371. &UA_TYPES[UA_TYPES_FIELDMETADATA]);
  372. /* DateTime DataType */
  373. UA_FieldMetaData_init (&pMetaData->fields[0]);
  374. UA_NodeId_copy (&UA_TYPES[UA_TYPES_DATETIME].typeId,
  375. &pMetaData->fields[0].dataType);
  376. pMetaData->fields[0].builtInType = UA_NS0ID_DATETIME;
  377. pMetaData->fields[0].valueRank = -1; /* scalar */
  378. retVal |= UA_Server_addDataSetReader(server, localreaderGroupIdentifier, &dataSetReaderConfig, &localDataSetreaderIdentifier);
  379. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  380. folderBrowseName = UA_QUALIFIEDNAME (1, "Subscribed Variables");
  381. UA_Server_addObjectNode (server, UA_NODEID_NULL,
  382. UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER),
  383. UA_NODEID_NUMERIC (0, UA_NS0ID_ORGANIZES),
  384. folderBrowseName, UA_NODEID_NUMERIC (0,
  385. UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL, &folderId);
  386. retVal |= UA_Server_DataSetReader_addTargetVariables(server, &folderId, localDataSetreaderIdentifier, UA_PUBSUB_SDS_TARGET);
  387. UA_free(pMetaData->fields);
  388. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  389. } END_TEST
  390. START_TEST(SinglePublishSubscribe) {
  391. /* To check status after running both publisher and subscriber */
  392. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  393. UA_PublishedDataSetConfig pdsConfig;
  394. UA_NodeId dataSetWriter;
  395. UA_NodeId readerIdentifier;
  396. UA_NodeId writerGroup;
  397. UA_DataSetReaderConfig readerConfig;
  398. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  399. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  400. pdsConfig.name = UA_STRING("PublishedDataSet Test");
  401. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSetTest);
  402. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  403. /* Data Set Field */
  404. UA_NodeId dataSetFieldId;
  405. UA_DataSetFieldConfig dataSetFieldConfig;
  406. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  407. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  408. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  409. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  410. dataSetFieldConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  411. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  412. UA_Server_addDataSetField(server, publishedDataSetTest, &dataSetFieldConfig, &dataSetFieldId);
  413. /* Writer group */
  414. UA_WriterGroupConfig writerGroupConfig;
  415. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  416. writerGroupConfig.name = UA_STRING("WriterGroup Test");
  417. writerGroupConfig.publishingInterval = PUBLISH_INTERVAL;
  418. writerGroupConfig.enabled = UA_FALSE;
  419. writerGroupConfig.writerGroupId = WRITER_GROUP_ID;
  420. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  421. retVal |= UA_Server_addWriterGroup(server, connection_test, &writerGroupConfig, &writerGroup);
  422. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  423. /* DataSetWriter */
  424. UA_DataSetWriterConfig dataSetWriterConfig;
  425. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  426. dataSetWriterConfig.name = UA_STRING("DataSetWriter Test");
  427. dataSetWriterConfig.dataSetWriterId = DATASET_WRITERD_ID;
  428. dataSetWriterConfig.keyFrameCount = 10;
  429. retVal |= UA_Server_addDataSetWriter(server, writerGroup, publishedDataSetTest, &dataSetWriterConfig, &dataSetWriter);
  430. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  431. /* Reader Group */
  432. UA_ReaderGroupConfig readerGroupConfig;
  433. memset (&readerGroupConfig, 0, sizeof (UA_ReaderGroupConfig));
  434. readerGroupConfig.name = UA_STRING ("ReaderGroup Test");
  435. retVal |= UA_Server_addReaderGroup (server, connection_test, &readerGroupConfig,
  436. &readerGroupTest);
  437. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  438. /* Data Set Reader */
  439. memset (&readerConfig, 0, sizeof (UA_DataSetReaderConfig));
  440. readerConfig.name = UA_STRING ("DataSetReader Test");
  441. readerConfig.dataSetWriterId = DATASET_WRITERD_ID;
  442. /* Setting up Meta data configuration in DataSetReader for DateTime DataType */
  443. UA_DataSetMetaDataType *pMetaData = &readerConfig.dataSetMetaData;
  444. /* FilltestMetadata function in subscriber implementation */
  445. UA_DataSetMetaDataType_init (pMetaData);
  446. pMetaData->name = UA_STRING ("DataSet Test");
  447. /* Static definition of number of fields size to 1 to create one
  448. targetVariable */
  449. pMetaData->fieldsSize = 1;
  450. pMetaData->fields = (UA_FieldMetaData*)UA_Array_new (pMetaData->fieldsSize,
  451. &UA_TYPES[UA_TYPES_FIELDMETADATA]);
  452. /* DateTime DataType */
  453. UA_FieldMetaData_init (&pMetaData->fields[0]);
  454. UA_NodeId_copy (&UA_TYPES[UA_TYPES_DATETIME].typeId,
  455. &pMetaData->fields[0].dataType);
  456. pMetaData->fields[0].builtInType = UA_NS0ID_DATETIME;
  457. pMetaData->fields[0].valueRank = -1; /* scalar */
  458. retVal |= UA_Server_addDataSetReader (server, readerGroupTest, &readerConfig,
  459. &readerIdentifier);
  460. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  461. /* Add Subscribed Variables */
  462. UA_NodeId folderId;
  463. UA_String folderName = readerConfig.dataSetMetaData.name;
  464. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  465. UA_QualifiedName folderBrowseName;
  466. if (folderName.length > 0) {
  467. oAttr.displayName.locale = UA_STRING ("en-US");
  468. oAttr.displayName.text = folderName;
  469. folderBrowseName.namespaceIndex = 1;
  470. folderBrowseName.name = folderName;
  471. }
  472. else {
  473. oAttr.displayName = UA_LOCALIZEDTEXT ("en-US", "Subscribed Variables");
  474. folderBrowseName = UA_QUALIFIEDNAME (1, "Subscribed Variables");
  475. }
  476. UA_Server_addObjectNode (server, UA_NODEID_NULL,
  477. UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER),
  478. UA_NODEID_NUMERIC (0, UA_NS0ID_ORGANIZES),
  479. folderBrowseName, UA_NODEID_NUMERIC (0,
  480. UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL, &folderId);
  481. retVal |= UA_Server_DataSetReader_addTargetVariables (server, &folderId,
  482. readerIdentifier, UA_PUBSUB_SDS_TARGET);
  483. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  484. /* run server - publisher and subscriber */
  485. UA_Server_run_iterate(server,true);
  486. UA_Server_run_iterate(server,true);
  487. UA_free(pMetaData->fields);
  488. }END_TEST
  489. int main(void) {
  490. TCase *tc_add_pubsub_readergroup = tcase_create("PubSub readerGroup items handling");
  491. tcase_add_checked_fixture(tc_add_pubsub_readergroup, setup, teardown);
  492. /* Test cases for ReaderGroup functionality */
  493. tcase_add_test(tc_add_pubsub_readergroup, AddReaderGroupWithValidConfiguration);
  494. tcase_add_test(tc_add_pubsub_readergroup, AddReaderGroupWithNullConfig);
  495. tcase_add_test(tc_add_pubsub_readergroup, AddReaderGroupWithInvalidConnectionId);
  496. tcase_add_test(tc_add_pubsub_readergroup, RemoveReaderGroupWithInvalidIdentifier);
  497. tcase_add_test(tc_add_pubsub_readergroup, UpdateReaderGroupWithInvalidIdentifier);
  498. tcase_add_test(tc_add_pubsub_readergroup, GetReaderGroupConfigWithInvalidConfig);
  499. tcase_add_test(tc_add_pubsub_readergroup, GetReaderGroupConfigWithInvalidIdentifier);
  500. tcase_add_test(tc_add_pubsub_readergroup, GetReaderGroupConfigWithValidConfig);
  501. /* Test cases for DataSetReader functionality */
  502. tcase_add_test(tc_add_pubsub_readergroup, AddDataSetReaderWithValidConfiguration);
  503. tcase_add_test(tc_add_pubsub_readergroup, AddDataSetReaderWithNullConfig);
  504. tcase_add_test(tc_add_pubsub_readergroup, RemoveDataSetReaderWithValidConfiguration);
  505. tcase_add_test(tc_add_pubsub_readergroup, RemoveDataSetReaderWithInvalidIdentifier);
  506. tcase_add_test(tc_add_pubsub_readergroup, UpdateDataSetReaderConfigWithInvalidId);
  507. tcase_add_test(tc_add_pubsub_readergroup, GetDataSetReaderConfigWithValidConfiguration);
  508. tcase_add_test(tc_add_pubsub_readergroup, GetDataSetReaderConfigWithInvalidConfiguration);
  509. tcase_add_test(tc_add_pubsub_readergroup, GetDataSetReaderConfigWithInvalidIdentifier);
  510. tcase_add_test(tc_add_pubsub_readergroup, CreateTargetVariableWithInvalidConfiguration);
  511. tcase_add_test(tc_add_pubsub_readergroup, AddTargetVariableWithInvalidConfiguration);
  512. tcase_add_test(tc_add_pubsub_readergroup, AddTargetVariableWithValidConfiguration);
  513. /*Test case to run both publisher and subscriber */
  514. TCase *tc_pubsub_publish_subscribe = tcase_create("Publisher publishing and Subscriber subscribing");
  515. tcase_add_checked_fixture(tc_pubsub_publish_subscribe, setup, teardown);
  516. tcase_add_test(tc_pubsub_publish_subscribe, SinglePublishSubscribe);
  517. Suite *suite = suite_create("PubSub readerGroups/reader/Fields handling and publishing");
  518. suite_add_tcase(suite, tc_add_pubsub_readergroup);
  519. suite_add_tcase(suite, tc_pubsub_publish_subscribe);
  520. SRunner *suiteRunner = srunner_create(suite);
  521. srunner_set_fork_status(suiteRunner, CK_NOFORK);
  522. srunner_run_all(suiteRunner,CK_NORMAL);
  523. int number_failed = srunner_ntests_failed(suiteRunner);
  524. srunner_free(suiteRunner);
  525. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  526. }