浏览代码

decode custom type from extensionobject

Julius Pfrommer 7 年之前
父节点
当前提交
414526c3a9
共有 1 个文件被更改,包括 48 次插入2 次删除
  1. 48 2
      tests/check_types_custom.c

+ 48 - 2
tests/check_types_custom.c

@@ -1,6 +1,6 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public 
-*  License, v. 2.0. If a copy of the MPL was not distributed with this 
-*  file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ * License, v. 2.0. If a copy of the MPL was not distributed with this 
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "ua_types.h"
 #include "ua_types_generated_handling.h"
@@ -8,6 +8,12 @@
 #include "ua_util.h"
 #include "check.h"
 
+/* The standard-defined datatypes are stored in the global array UA_TYPES. User
+ * can create their own UA_CUSTOM_TYPES array (the name doesn't matter) and
+ * provide it to the server / client. The type will be automatically decoded if
+ * possible.
+ */
+
 /* The custom datatype for describing a 3d position */
 
 typedef struct {
@@ -102,6 +108,45 @@ START_TEST(parseCustomScalar) {
     UA_ByteString_deleteMembers(&buf);
 } END_TEST
 
+START_TEST(parseCustomScalarExtensionObject) {
+    Point p;
+    p.x = 1.0;
+    p.y = 2.0;
+    p.z = 3.0;
+
+    UA_ExtensionObject eo;
+    UA_ExtensionObject_init(&eo);
+
+    eo.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
+    eo.content.decoded.data = &p;
+    eo.content.decoded.type = &PointType;
+
+    size_t buflen = UA_calcSizeBinary(&eo, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
+    UA_ByteString buf;
+    UA_StatusCode retval = UA_ByteString_allocBuffer(&buf, buflen);
+    ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
+
+    size_t offset = 0;
+    retval = UA_encodeBinary(&eo, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT], NULL, NULL,
+                             &buf, &offset);
+    ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_ExtensionObject eo2;
+    size_t offset2 = 0;
+    retval = UA_decodeBinary(&buf, &offset2, &eo2, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT], 1, &PointType);
+    ck_assert_int_eq(offset2, offset);
+    ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
+
+    ck_assert_int_eq(eo2.encoding, UA_EXTENSIONOBJECT_DECODED);
+    ck_assert_ptr_eq(eo2.content.decoded.type, &PointType);
+
+    Point *p2 = (Point*)eo2.content.decoded.data;
+    ck_assert(p.x == p2->x);
+        
+    UA_ExtensionObject_deleteMembers(&eo2);
+    UA_ByteString_deleteMembers(&buf);
+} END_TEST
+
 START_TEST(parseCustomArray) {
     Point ps[10];
     for(size_t i = 0; i < 10; ++i) {
@@ -152,6 +197,7 @@ int main(void) {
     Suite *s  = suite_create("Test Custom DataType Encoding");
     TCase *tc = tcase_create("test cases");
     tcase_add_test(tc, parseCustomScalar);
+    tcase_add_test(tc, parseCustomScalarExtensionObject);
     tcase_add_test(tc, parseCustomArray);
     suite_add_tcase(s, tc);