00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "common.h"
00012
00013 using namespace testing;
00014 using namespace OpcUa;
00015
00016 class NodeDeserialization : public OpcUaBinaryDeserialization
00017 {
00018 };
00019
00020 class NodeSerialization : public OpcUaBinarySerialization
00021 {
00022 };
00023
00024 class NodeComparing : public OpcUaBinarySerialization
00025 {
00026 };
00027
00028
00029 TEST(NodeId, DefaultConstructor)
00030 {
00031 NodeId id;
00032 ASSERT_EQ(id.Encoding, EV_TWO_BYTE);
00033 ASSERT_EQ(id.TwoByteData.Identifier, 0);
00034 ASSERT_EQ(id.ServerIndex, 0);
00035 ASSERT_EQ(id.NamespaceURI, std::string());
00036 }
00037
00038 TEST(NodeId, NumericConstructor)
00039 {
00040 NodeId id(99, 1);
00041 ASSERT_EQ(id.Encoding, EV_NUMERIC);
00042 ASSERT_EQ(id.NumericData.Identifier, 99);
00043 ASSERT_EQ(id.GetNamespaceIndex(), 1);
00044 }
00045
00046 TEST(Node, StringConstructor)
00047 {
00048 NodeId id("StrId", 10);
00049 ASSERT_EQ(id.Encoding, EV_STRING);
00050 ASSERT_EQ(id.GetStringIdentifier(), "StrId");
00051 ASSERT_EQ(id.GetNamespaceIndex(), 10);
00052 }
00053
00054 TEST(Node, ConstructFromMessageId)
00055 {
00056 NodeId id(ACTIVATE_SESSION_REQUEST);
00057 ASSERT_EQ(id.Encoding, EV_FOUR_BYTE);
00058 ASSERT_EQ(id.FourByteData.Identifier, ACTIVATE_SESSION_REQUEST);
00059 ASSERT_EQ(id.ServerIndex, 0);
00060 ASSERT_EQ(id.NamespaceURI, std::string());
00061 }
00062
00063 TEST(Node, ConstructFromReferenceId)
00064 {
00065 NodeId id(ReferenceId::HasChild);
00066 ASSERT_EQ(id.Encoding, EV_NUMERIC);
00067 ASSERT_EQ(id.NumericData.Identifier, static_cast<uint16_t>(ReferenceId::HasChild));
00068 ASSERT_EQ(id.ServerIndex, 0);
00069 ASSERT_EQ(id.NamespaceURI, std::string());
00070 }
00071
00072 TEST(Node, EqualIfSameType)
00073 {
00074 NodeId id1(ReferenceId::HasChild);
00075 NodeId id2(ReferenceId::HasChild);
00076 ASSERT_EQ(id1, id2);
00077 }
00078
00079 TEST(Node, NotEqualByReferenceId)
00080 {
00081 NodeId id1(ReferenceId::HasChild);
00082 NodeId id2(ReferenceId::Organizes);
00083 ASSERT_NE(id1, id2);
00084 }
00085
00086 TEST(Node, NotEqualByObjectId)
00087 {
00088 NodeId id(ReferenceId::Organizes);
00089 ASSERT_NE(id, ObjectId::RootFolder);
00090 }
00091
00092 TEST(Node, EqualIfDifferentTypeButEqualIdentifier)
00093 {
00094 NodeId id1;
00095 id1.Encoding = EV_TWO_BYTE;
00096 id1.TwoByteData.Identifier = 1;
00097
00098 NodeId id2;
00099 id2.Encoding = EV_FOUR_BYTE;
00100 id2.FourByteData.Identifier = 1;
00101 ASSERT_EQ(id1, id2);
00102 }
00103
00104 TEST(Node, DefferentIfDifferentNameSpace)
00105 {
00106 NodeId id1;
00107 id1.Encoding = EV_FOUR_BYTE;
00108 id1.FourByteData.NamespaceIndex = 1;
00109 id1.FourByteData.Identifier = 1;
00110
00111 NodeId id2;
00112 id2.Encoding = EV_FOUR_BYTE;
00113 id2.FourByteData.NamespaceIndex = 2;
00114 id2.FourByteData.Identifier = 1;
00115
00116 ASSERT_NE(id1, id2);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 TEST_F(NodeDeserialization, TwoByte)
00126 {
00127 const std::vector<char> expectedData = {
00128 EV_TWO_BYTE,
00129 1
00130 };
00131
00132 GetChannel().SetData(expectedData);
00133
00134 NodeId id;
00135 GetStream() >> id;
00136
00137 ASSERT_EQ(id.Encoding, EV_TWO_BYTE);
00138 ASSERT_EQ(id.TwoByteData.Identifier, 0x1);
00139 }
00140
00141 TEST_F(NodeDeserialization, FourByte)
00142 {
00143 using namespace OpcUa;
00144
00145 const std::vector<char> expectedData = {
00146 EV_FOUR_BYTE,
00147 1,
00148 2, 0
00149 };
00150
00151 GetChannel().SetData(expectedData);
00152
00153 NodeId id;
00154 GetStream() >> id;
00155
00156 ASSERT_EQ(id.Encoding, EV_FOUR_BYTE);
00157 ASSERT_EQ(id.FourByteData.NamespaceIndex, 0x1);
00158 ASSERT_EQ(id.FourByteData.Identifier, 0x2);
00159 }
00160
00161 TEST_F(NodeDeserialization, Numeric)
00162 {
00163 using namespace OpcUa;
00164
00165 const std::vector<char> expectedData = {
00166 EV_NUMERIC,
00167 1, 0,
00168 2, 0, 0, 0
00169 };
00170
00171 GetChannel().SetData(expectedData);
00172
00173 NodeId id;
00174 GetStream() >> id;
00175
00176 ASSERT_EQ(id.Encoding, EV_NUMERIC);
00177 ASSERT_EQ(id.NumericData.NamespaceIndex, 0x1);
00178 ASSERT_EQ(id.NumericData.Identifier, 0x2);
00179 }
00180
00181 TEST_F(NodeDeserialization, String)
00182 {
00183 using namespace OpcUa;
00184
00185 const std::vector<char> expectedData = {
00186 EV_STRING,
00187 1, 0,
00188 2, 0, 0, 0,
00189 'i', 'd'
00190 };
00191
00192 GetChannel().SetData(expectedData);
00193
00194 NodeId id;
00195 GetStream() >> id;
00196
00197 ASSERT_EQ(id.Encoding, EV_STRING);
00198 ASSERT_EQ(id.StringData.NamespaceIndex, 0x1);
00199 ASSERT_EQ(id.StringData.Identifier, "id");
00200 }
00201
00202 TEST_F(NodeDeserialization, Guid)
00203 {
00204 using namespace OpcUa;
00205
00206 const std::vector<char> expectedData = {
00207 EV_BYTE_STRING,
00208 1, 0,
00209 4, 0, 0, 0,
00210 1, 2, 3, 4
00211 };
00212
00213 GetChannel().SetData(expectedData);
00214
00215 NodeId id;
00216 GetStream() >> id;
00217
00218 ASSERT_EQ(id.Encoding, EV_BYTE_STRING);
00219 ASSERT_EQ(id.BinaryData.NamespaceIndex, 0x1);
00220 std::vector<uint8_t> expectedBytes = {1, 2, 3, 4};
00221 ASSERT_EQ(id.BinaryData.Identifier, expectedBytes);
00222 }
00223
00224 TEST_F(NodeDeserialization, ByteString)
00225 {
00226 using namespace OpcUa;
00227
00228 const std::vector<char> expectedData = {
00229 EV_GUId,
00230 1, 0,
00231 4, 3, 2, 1,
00232 6, 5,
00233 8, 7,
00234 1, 2, 3, 4, 5, 6, 7, 8
00235 };
00236
00237 GetChannel().SetData(expectedData);
00238
00239 NodeId id;
00240 GetStream() >> id;
00241
00242 ASSERT_EQ(id.Encoding, EV_GUId);
00243 ASSERT_EQ(id.GuidData.NamespaceIndex, 0x1);
00244 ASSERT_EQ(id.GuidData.Identifier.Data1, 0x01020304);
00245 ASSERT_EQ(id.GuidData.Identifier.Data2, 0x0506);
00246 ASSERT_EQ(id.GuidData.Identifier.Data3, 0x0708);
00247 ASSERT_EQ(id.GuidData.Identifier.Data4[0], 0x01);
00248 ASSERT_EQ(id.GuidData.Identifier.Data4[1], 0x02);
00249 ASSERT_EQ(id.GuidData.Identifier.Data4[2], 0x03);
00250 ASSERT_EQ(id.GuidData.Identifier.Data4[3], 0x04);
00251 ASSERT_EQ(id.GuidData.Identifier.Data4[4], 0x05);
00252 ASSERT_EQ(id.GuidData.Identifier.Data4[5], 0x06);
00253 ASSERT_EQ(id.GuidData.Identifier.Data4[6], 0x07);
00254 ASSERT_EQ(id.GuidData.Identifier.Data4[7], 0x08);
00255 }
00256
00257 TEST_F(NodeDeserialization, NamespaceUri)
00258 {
00259 using namespace OpcUa;
00260
00261 const std::vector<char> expectedData = {
00262 int8_t(EV_STRING | EV_NAMESPACE_URI_FLAG),
00263 1, 0,
00264 2, 0, 0, 0,
00265 'i', 'd',
00266 3, 0, 0, 0,
00267 'u', 'r', 'i'
00268 };
00269
00270 GetChannel().SetData(expectedData);
00271
00272 NodeId id;
00273 GetStream() >> id;
00274
00275 ASSERT_EQ(id.Encoding, uint8_t(EV_STRING | EV_NAMESPACE_URI_FLAG));
00276 ASSERT_EQ(id.StringData.NamespaceIndex, 0x1);
00277 ASSERT_EQ(id.StringData.Identifier, "id");
00278 ASSERT_EQ(id.NamespaceURI, "uri");
00279 }
00280
00281 TEST_F(NodeDeserialization, ServerIndexFlag)
00282 {
00283 using namespace OpcUa;
00284
00285 const std::vector<char> expectedData = {
00286 int8_t(EV_STRING | EV_Server_INDEX_FLAG),
00287 1, 0,
00288 2, 0, 0, 0,
00289 'i', 'd',
00290 1, 0, 0, 0,
00291 };
00292
00293 GetChannel().SetData(expectedData);
00294
00295 NodeId id;
00296 GetStream() >> id;
00297
00298 ASSERT_EQ(id.Encoding, uint8_t(EV_STRING | EV_Server_INDEX_FLAG));
00299 ASSERT_EQ(id.StringData.NamespaceIndex, 0x1);
00300 ASSERT_EQ(id.StringData.Identifier, "id");
00301 ASSERT_EQ(id.ServerIndex, 1);
00302 }
00303
00304 TEST_F(NodeDeserialization, NamespaceUriAndServerIndex)
00305 {
00306 using namespace OpcUa;
00307
00308 const std::vector<char> expectedData = {
00309 int8_t(EV_STRING | EV_NAMESPACE_URI_FLAG | EV_Server_INDEX_FLAG),
00310 1, 0,
00311 2, 0, 0, 0,
00312 'i', 'd',
00313 3, 0, 0, 0,
00314 'u', 'r', 'i',
00315 1, 0, 0, 0
00316 };
00317
00318 GetChannel().SetData(expectedData);
00319
00320 NodeId id;
00321 GetStream() >> id;
00322
00323 ASSERT_EQ(id.Encoding, uint8_t(EV_STRING | EV_NAMESPACE_URI_FLAG | EV_Server_INDEX_FLAG));
00324 ASSERT_EQ(id.StringData.NamespaceIndex, 0x1);
00325 ASSERT_EQ(id.StringData.Identifier, "id");
00326 ASSERT_EQ(id.NamespaceURI, "uri");
00327 ASSERT_EQ(id.ServerIndex, 1);
00328 }
00329
00330
00331
00332
00333
00334 TEST_F(NodeSerialization, ToByte)
00335 {
00336 using namespace OpcUa;
00337 using namespace OpcUa::Binary;
00338 NodeId id;
00339 id.Encoding = EV_TWO_BYTE;
00340 id.TwoByteData.Identifier = 0x1;
00341
00342 const std::vector<char> expectedData = {
00343 EV_TWO_BYTE,
00344 1
00345 };
00346
00347 GetStream() << id << flush;
00348
00349 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00350 ASSERT_EQ(expectedData.size(), RawSize(id));
00351 }
00352
00353 TEST_F(NodeSerialization, FourByte)
00354 {
00355 using namespace OpcUa;
00356 using namespace OpcUa::Binary;
00357 NodeId id;
00358 id.Encoding = EV_FOUR_BYTE;
00359 id.FourByteData.NamespaceIndex = 0x1;
00360 id.FourByteData.Identifier = 0x2;
00361
00362 const std::vector<char> expectedData = {
00363 EV_FOUR_BYTE,
00364 1,
00365 2, 0
00366 };
00367
00368 GetStream() << id << flush;
00369
00370 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00371 ASSERT_EQ(expectedData.size(), RawSize(id));
00372 }
00373
00374 TEST_F(NodeSerialization, Numeric)
00375 {
00376 using namespace OpcUa;
00377 using namespace OpcUa::Binary;
00378 NodeId id;
00379 id.Encoding = EV_NUMERIC;
00380 id.NumericData.NamespaceIndex = 0x1;
00381 id.NumericData.Identifier = 0x2;
00382
00383 const std::vector<char> expectedData = {
00384 EV_NUMERIC,
00385 1, 0,
00386 2, 0, 0, 0
00387 };
00388
00389 GetStream() << id << flush;
00390
00391 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00392 ASSERT_EQ(expectedData.size(), RawSize(id));
00393 }
00394
00395 TEST_F(NodeSerialization, String)
00396 {
00397 using namespace OpcUa;
00398 using namespace OpcUa::Binary;
00399 NodeId id;
00400 id.Encoding = EV_STRING;
00401 id.StringData.NamespaceIndex = 0x1;
00402 id.StringData.Identifier = "id";
00403
00404 const std::vector<char> expectedData = {
00405 EV_STRING,
00406 1, 0,
00407 2, 0, 0, 0,
00408 'i', 'd'
00409 };
00410
00411 GetStream() << id << flush;
00412
00413 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00414 ASSERT_EQ(expectedData.size(), RawSize(id));
00415 }
00416
00417 TEST_F(NodeSerialization, ByteString)
00418 {
00419 using namespace OpcUa;
00420 using namespace OpcUa::Binary;
00421 NodeId id;
00422 id.Encoding = EV_BYTE_STRING;
00423 id.BinaryData.NamespaceIndex = 0x1;
00424 id.BinaryData.Identifier = {1, 2, 3, 4};
00425
00426 const std::vector<char> expectedData = {
00427 EV_BYTE_STRING,
00428 1, 0,
00429 4, 0, 0, 0,
00430 1, 2, 3, 4
00431 };
00432
00433 GetStream() << id << flush;
00434
00435 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00436 ASSERT_EQ(expectedData.size(), RawSize(id));
00437 }
00438
00439 TEST_F(NodeSerialization, Guid)
00440 {
00441 using namespace OpcUa;
00442 using namespace OpcUa::Binary;
00443 NodeId id;
00444 id.Encoding = EV_GUId;
00445 id.GuidData.NamespaceIndex = 0x1;
00446 id.GuidData.Identifier.Data1 = 0x01020304;
00447 id.GuidData.Identifier.Data2 = 0x0506;
00448 id.GuidData.Identifier.Data3 = 0x0708;
00449 id.GuidData.Identifier.Data4[0] = 0x01;
00450 id.GuidData.Identifier.Data4[1] = 0x02;
00451 id.GuidData.Identifier.Data4[2] = 0x03;
00452 id.GuidData.Identifier.Data4[3] = 0x04;
00453 id.GuidData.Identifier.Data4[4] = 0x05;
00454 id.GuidData.Identifier.Data4[5] = 0x06;
00455 id.GuidData.Identifier.Data4[6] = 0x07;
00456 id.GuidData.Identifier.Data4[7] = 0x08;
00457
00458 const std::vector<char> expectedData = {
00459 EV_GUId,
00460 1, 0,
00461 4, 3, 2, 1,
00462 6, 5,
00463 8, 7,
00464 1, 2, 3, 4, 5, 6, 7, 8
00465 };
00466
00467 GetStream() << id << flush;
00468
00469 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00470 ASSERT_EQ(expectedData.size(), RawSize(id));
00471 }
00472
00473 TEST_F(NodeSerialization, NamespaceUri)
00474 {
00475 using namespace OpcUa;
00476 using namespace OpcUa::Binary;
00477 ExpandedNodeId id;
00478 id.Encoding = static_cast<NodeIdEncoding>(EV_STRING | EV_NAMESPACE_URI_FLAG);
00479 id.StringData.NamespaceIndex = 0x1;
00480 id.StringData.Identifier = "id";
00481 id.NamespaceURI = "uri";
00482
00483 const std::vector<char> expectedData = {
00484 int8_t(EV_STRING | EV_NAMESPACE_URI_FLAG),
00485 1, 0,
00486 2, 0, 0, 0,
00487 'i', 'd',
00488 3, 0, 0, 0,
00489 'u', 'r', 'i'
00490 };
00491
00492 GetStream() << id << flush;
00493
00494 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00495 ASSERT_EQ(expectedData.size(), RawSize(id));
00496 }
00497
00498 TEST_F(NodeSerialization, ServerIndexFlag)
00499 {
00500 using namespace OpcUa;
00501 using namespace OpcUa::Binary;
00502 ExpandedNodeId id;
00503 id.Encoding = static_cast<NodeIdEncoding>(EV_STRING | EV_Server_INDEX_FLAG);
00504 id.StringData.NamespaceIndex = 0x1;
00505 id.StringData.Identifier = "id";
00506 id.ServerIndex = 1;
00507
00508 const std::vector<char> expectedData = {
00509 int8_t(EV_STRING | EV_Server_INDEX_FLAG),
00510 1, 0,
00511 2, 0, 0, 0,
00512 'i', 'd',
00513 1, 0, 0, 0,
00514 };
00515
00516 GetStream() << id << flush;
00517
00518 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00519 ASSERT_EQ(expectedData.size(), RawSize(id));
00520 }
00521
00522 TEST_F(NodeSerialization, NamespaceUriAndServerIndex)
00523 {
00524 using namespace OpcUa;
00525 using namespace OpcUa::Binary;
00526 ExpandedNodeId id;
00527 id.Encoding = static_cast<NodeIdEncoding>(EV_STRING | EV_NAMESPACE_URI_FLAG | EV_Server_INDEX_FLAG);
00528 id.StringData.NamespaceIndex = 0x1;
00529 id.StringData.Identifier = "id";
00530 id.NamespaceURI = "uri";
00531 id.ServerIndex = 1;
00532
00533 const std::vector<char> expectedData = {
00534 int8_t(EV_STRING | EV_NAMESPACE_URI_FLAG | EV_Server_INDEX_FLAG),
00535 1, 0,
00536 2, 0, 0, 0,
00537 'i', 'd',
00538 3, 0, 0, 0,
00539 'u', 'r', 'i',
00540 1, 0, 0, 0
00541 };
00542
00543 GetStream() << id << flush;
00544
00545 ASSERT_EQ(expectedData, GetChannel().SerializedData);
00546 ASSERT_EQ(expectedData.size(), RawSize(id));
00547 }
00548
00549
00550
00551
00552
00553 TEST_F(NodeComparing, TwoByteForLess)
00554 {
00555 EXPECT_TRUE(TwoByteNodeId(1) < TwoByteNodeId(2));
00556 }
00557
00558 TEST_F(NodeComparing, TwoByteForNotLess)
00559 {
00560 EXPECT_FALSE(TwoByteNodeId(2) < TwoByteNodeId(2));
00561 }
00562
00563 TEST_F(NodeComparing, TwoByteForEqual)
00564 {
00565 EXPECT_TRUE(TwoByteNodeId(2) == TwoByteNodeId(2));
00566 }
00567
00568 TEST_F(NodeComparing, FourByteForLess)
00569 {
00570 EXPECT_TRUE(FourByteNodeId(1) < FourByteNodeId(2));
00571 }
00572
00573 TEST_F(NodeComparing, FourByteForNotLess)
00574 {
00575 EXPECT_FALSE(FourByteNodeId(2) < FourByteNodeId(2));
00576 }
00577
00578 TEST_F(NodeComparing, FourByteForEqual)
00579 {
00580 EXPECT_TRUE(FourByteNodeId(2) == FourByteNodeId(2));
00581 }
00582
00583 TEST_F(NodeComparing, NumericForLess)
00584 {
00585 EXPECT_TRUE(NumericNodeId(1) < NumericNodeId(2));
00586 }
00587
00588 TEST_F(NodeComparing, NumericNotLess)
00589 {
00590 EXPECT_FALSE(NumericNodeId(2) < NumericNodeId(2));
00591 }
00592
00593 TEST_F(NodeComparing, NumericEqual)
00594 {
00595 EXPECT_TRUE(NumericNodeId(2) == NumericNodeId(2));
00596 }
00597
00598 TEST_F(NodeComparing, StringForLess)
00599 {
00600 EXPECT_TRUE(StringNodeId("1") < StringNodeId("2"));
00601 }
00602
00603 TEST_F(NodeComparing, StringNotLess)
00604 {
00605 EXPECT_FALSE(StringNodeId("2") < StringNodeId("2"));
00606 }
00607
00608 TEST_F(NodeComparing, StringEqual)
00609 {
00610 EXPECT_TRUE(StringNodeId("2") == StringNodeId("2"));
00611 }
00612
00613 TEST_F(NodeComparing, BinaryForLess)
00614 {
00615 EXPECT_TRUE(BinaryNodeId({1, 1}) < BinaryNodeId({2, 2}));
00616 }
00617
00618 TEST_F(NodeComparing, BinaryNotLess)
00619 {
00620 EXPECT_FALSE(BinaryNodeId({2, 3}) < BinaryNodeId({2, 3}));
00621 }
00622
00623 TEST_F(NodeComparing, BinaryEqual)
00624 {
00625 EXPECT_TRUE(BinaryNodeId({2, 2}) == BinaryNodeId({2,2}));
00626 }
00627
00628 TEST_F(NodeComparing, GuidForLess)
00629 {
00630 Guid l;
00631 l.Data1 = 1;
00632 Guid r;
00633 r.Data1 = 2;
00634 EXPECT_TRUE(GuidNodeId(l) < GuidNodeId(r));
00635 }
00636
00637 TEST_F(NodeComparing, GuidNotLess)
00638 {
00639 Guid l;
00640 l.Data1 = 1;
00641 Guid r;
00642 r.Data1 = 1;
00643 EXPECT_FALSE(GuidNodeId(l) < GuidNodeId(r));
00644 }
00645
00646 TEST_F(NodeComparing, GuidEqual)
00647 {
00648 Guid l;
00649 l.Data1 = 1;
00650 Guid r;
00651 r.Data1 = 1;
00652 EXPECT_TRUE(GuidNodeId(l) == GuidNodeId(r));
00653 }
00654
00655 TEST_F(NodeComparing, NodesEqual)
00656 {
00657 uint32_t id = 1;
00658 uint32_t ns = 2;
00659
00660 NodeId node1 = NumericNodeId(id, ns);
00661 NodeId node2;
00662 node2 = node1;
00663 ASSERT_EQ(node1, node2);
00664 ASSERT_EQ(node1, NodeId(node1));
00665 }
00666
00667 TEST(NodeId, SettingNamespaceUri_And_Index)
00668 {
00669 uint32_t id = 1;
00670 uint32_t ns = 2;
00671 NodeId node = NumericNodeId(id, ns);
00672 node.SetNamespaceURI("uri");
00673 node.SetServerIndex(3);
00674
00675 ASSERT_TRUE(node.HasNamespaceURI());
00676 ASSERT_TRUE(node.HasServerIndex());
00677 }