00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "common.h"
00012
00013 #include <opc/ua/protocol/extension_identifiers.h>
00014 #include <opc/ua/protocol/message_identifiers.h>
00015 #include <opc/ua/protocol/binary/stream.h>
00016 #include <opc/ua/protocol/data_value.h>
00017 #include <opc/ua/protocol/strings.h>
00018 #include <opc/ua/protocol/types.h>
00019 #include <opc/ua/protocol/variant.h>
00020
00021 #include <algorithm>
00022 #include <stdexcept>
00023
00024
00025
00026
00027
00028 TEST_F(OpcUaBinarySerialization, DataValue_NULL)
00029 {
00030 using namespace OpcUa;
00031 using namespace OpcUa::Binary;
00032
00033 DataValue data;
00034
00035 GetStream() << data << flush;
00036
00037 const std::vector<char> expectedData = {
00038 0
00039 };
00040
00041 ASSERT_EQ(expectedData.size(), RawSize(data));
00042 ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
00043 }
00044
00045 TEST_F(OpcUaBinarySerialization, DataValue_Value)
00046 {
00047
00048 using namespace OpcUa;
00049 using namespace OpcUa::Binary;
00050
00051 DataValue data;
00052 data.Encoding = DATA_VALUE;
00053 data.Value = true;
00054
00055 GetStream() << data << flush;
00056
00057 char encodingMask = static_cast<uint8_t>(VariantType::BOOLEAN);
00058 const std::vector<char> expectedData = {
00059 1,
00060 encodingMask, 1
00061 };
00062
00063 ASSERT_EQ(expectedData.size(), RawSize(data));
00064 ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
00065 }
00066
00067 TEST_F(OpcUaBinarySerialization, DataValue_Full)
00068 {
00069
00070 using namespace OpcUa;
00071 using namespace OpcUa::Binary;
00072
00073 char encodingMask =
00074 DATA_VALUE |
00075 DATA_VALUE_STATUS_CODE |
00076 DATA_VALUE_SOURCE_TIMESTAMP |
00077 DATA_VALUE_Server_TIMESTAMP |
00078 DATA_VALUE_SOURCE_PICOSECONDS |
00079 DATA_VALUE_Server_PICOSECONDS;
00080
00081 DataValue data;
00082 data.Encoding = encodingMask;
00083 data.Value = QualifiedName(1, OpcUa::Names::Root);
00084 data.Status = static_cast<StatusCode>(1);
00085 data.SourceTimestamp.Value = 2;
00086 data.SourcePicoseconds = 3;
00087 data.ServerTimestamp.Value = 4;
00088 data.ServerPicoseconds = 5;
00089
00090 GetStream() << data << flush;
00091
00092
00093 char variantMask = static_cast<uint8_t>(VariantType::QUALIFIED_NAME);
00094 const std::vector<char> expectedData = {
00095 encodingMask,
00096 variantMask,
00097 1, 0,
00098 4,0,0,0,
00099 'R', 'o', 'o', 't',
00100 1,0,0,0,
00101 2,0,0,0,0,0,0,0,
00102 3,0,
00103 4,0,0,0,0,0,0,0,
00104 5,0
00105 };
00106
00107 ASSERT_EQ(expectedData.size(), RawSize(data));
00108 ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
00109 }
00110
00111
00112
00113
00114
00115
00116 TEST_F(OpcUaBinaryDeserialization, DataValue_NUL)
00117 {
00118 using namespace OpcUa;
00119 using namespace OpcUa::Binary;
00120
00121 const std::vector<char> expectedData = {
00122 0
00123 };
00124
00125 GetChannel().SetData(expectedData);
00126
00127 DataValue data;
00128 GetStream() >> data;
00129
00130 ASSERT_EQ(data.Encoding, 0);
00131 }
00132
00133 TEST_F(OpcUaBinaryDeserialization, DataValue_Value)
00134 {
00135 using namespace OpcUa;
00136 using namespace OpcUa::Binary;
00137
00138 char encodingMask = static_cast<uint8_t>(VariantType::BOOLEAN);
00139 const std::vector<char> expectedData = {
00140 1,
00141 encodingMask, 1
00142 };
00143
00144 GetChannel().SetData(expectedData);
00145
00146 DataValue data;
00147 GetStream() >> data;
00148
00149 ASSERT_EQ(data.Encoding, DATA_VALUE);
00150 ASSERT_FALSE(data.Value.IsNul());
00151 }
00152
00153 TEST_F(OpcUaBinaryDeserialization, DataValue_Full)
00154 {
00155 using namespace OpcUa;
00156 using namespace OpcUa::Binary;
00157
00158 char encodingMask =
00159 DATA_VALUE |
00160 DATA_VALUE_STATUS_CODE |
00161 DATA_VALUE_SOURCE_TIMESTAMP |
00162 DATA_VALUE_Server_TIMESTAMP |
00163 DATA_VALUE_SOURCE_PICOSECONDS |
00164 DATA_VALUE_Server_PICOSECONDS;
00165
00166
00167 char variantMask = static_cast<uint8_t>(VariantType::BOOLEAN);
00168 const std::vector<char> expectedData = {
00169 encodingMask,
00170 variantMask, 1,
00171 1,0,0,0,
00172 2,0,0,0,0,0,0,0,
00173 3,0,
00174 4,0,0,0,0,0,0,0,
00175 5,0
00176 };
00177
00178 GetChannel().SetData(expectedData);
00179
00180 DataValue data;
00181 GetStream() >> data;
00182
00183 ASSERT_EQ(data.Encoding, encodingMask);
00184 ASSERT_FALSE(data.Value.IsNul());
00185 ASSERT_EQ(data.Status, static_cast<StatusCode>(1));
00186 ASSERT_EQ(data.SourceTimestamp, 2);
00187 ASSERT_EQ(data.SourcePicoseconds, 3);
00188 ASSERT_EQ(data.ServerTimestamp, 4);
00189 ASSERT_EQ(data.ServerPicoseconds, 5);
00190 }
00191
00192
00193 TEST(DataValue, ConstructivbeFromDataValue)
00194 {
00195 using namespace OpcUa;
00196 NodeId node(ObjectId::RootFolder);
00197 DataValue data;
00198 data = node;
00199 ASSERT_TRUE(data.Encoding && DATA_VALUE);
00200 ASSERT_TRUE(data.Value.Type() == VariantType::NODE_Id);
00201
00202 DataValue newValue(data);
00203 ASSERT_TRUE(newValue.Encoding && DATA_VALUE);
00204 ASSERT_EQ(newValue.Value.Type(), VariantType::NODE_Id);
00205 ASSERT_NO_THROW(newValue.Value.As<NodeId>());
00206 }
00207
00208 TEST(DataValue, ComparableByValue)
00209 {
00210 using namespace OpcUa;
00211 DataValue data1(uint32_t(1));
00212 DataValue data2(uint32_t(1));
00213
00214 ASSERT_EQ(data1, data2);
00215 }