utils.cpp
Go to the documentation of this file.
1 
12 #include <opc/ua/protocol/guid.h>
14 #include <gtest/gtest.h>
15 #include <stdexcept>
16 
17 using namespace testing;
18 using namespace OpcUa;
19 
20 TEST(DateTime, FixedTimeT_to_DateTime)
21 {
22  time_t timet03_10_1980 = 321494400;
23  unsigned usec = 1;
24  const DateTime dateTime_03_10_1980 = DateTime::FromTimeT(timet03_10_1980, usec);
25  ASSERT_EQ(dateTime_03_10_1980, 138495 * 24 * 3600LL * 10000000LL + 10);
26 }
27 
28 TEST(DateTime, FixedDateTime_to_TimeT)
29 {
30  const DateTime dateTime_03_10_1980(138495 * 24 * 3600LL * 10000000LL);
31  time_t timet_03_10_1980 = DateTime::ToTimeT(dateTime_03_10_1980);
32  ASSERT_EQ(timet_03_10_1980, 321494400);
33 }
34 
35 TEST(DateTime, ToTimeT_And_Back)
36 {
37  time_t timet = time(0);
38  const time_t t = DateTime::ToTimeT(DateTime::FromTimeT(timet));
39  ASSERT_EQ(t, timet);
40 }
41 
42 TEST(DateTime, FromTimeT)
43 {
44  time_t t = 1;
45  const time_t converted = OpcUa::DateTime::ToTimeT(OpcUa::DateTime::FromTimeT(t));
46  ASSERT_EQ(t, converted);
47 }
48 
49 TEST(DateTime, FromDateTime)
50 {
53  ASSERT_EQ(t / 10000000LL * 10000000LL, converted);
54 }
55 
56 TEST(DateTime, ZeroDateTime_ThrowInvalidArgument)
57 {
58  DateTime t(0);
59  ASSERT_THROW(OpcUa::DateTime::ToTimeT(t), std::invalid_argument);
60 }
61 
62 TEST(DateTime, ZeroTimeT)
63 {
64  time_t t = 0;
65  const DateTime converted = OpcUa::DateTime::FromTimeT(t);
66  const DateTime expected(134774LL * 24 * 3600 * 10000000LL);
67  ASSERT_EQ(converted, expected);
68 }
69 
70 
72 {
73  OpcUa::Guid guid;
74  guid.Data1 = 0x01020304;
75  guid.Data2 = 0x0506;
76  guid.Data3 = 0x0708;
77  guid.Data4[0] = 0x09;
78  guid.Data4[1] = 0x0A;
79  guid.Data4[2] = 0x0B;
80  guid.Data4[3] = 0x0C;
81  guid.Data4[4] = 0x0D;
82  guid.Data4[5] = 0x0E;
83  guid.Data4[6] = 0x0F;
84  guid.Data4[7] = 0x10;
85 
86  std::string converted = OpcUa::ToString(guid);
87  EXPECT_EQ(converted, "01020304-0506-0708-090A-0B0C0D0E0F10");
88 }
89 
90 TEST(Guid, FromString)
91 {
92  OpcUa::Guid guid;
93  guid.Data1 = 0x01020304;
94  guid.Data2 = 0x0506;
95  guid.Data3 = 0x0708;
96  guid.Data4[0] = 0x09;
97  guid.Data4[1] = 0x0A;
98  guid.Data4[2] = 0x0B;
99  guid.Data4[3] = 0x0C;
100  guid.Data4[4] = 0x0D;
101  guid.Data4[5] = 0x0E;
102  guid.Data4[6] = 0x0F;
103  guid.Data4[7] = 0x10;
104 
105  const OpcUa::Guid converted = OpcUa::ToGuid("01020304-0506-0708-090A-0B0C0D0E0F10");
106  EXPECT_EQ(converted, guid);
107 }
108 
109 TEST(Guid, InvalidString)
110 {
111  EXPECT_EQ(OpcUa::ToGuid("01020304-0506-0708-090A-0B0C0D0E0F10S"), OpcUa::Guid()); // 37 symbols
112  EXPECT_EQ(OpcUa::ToGuid("0102030400506007080090A0B0C0D0E0F10"), OpcUa::Guid());
113 }
114 
115 TEST(NodeId, NumericToString)
116 {
118  std::string strId = OpcUa::ToString(id);
119  ASSERT_EQ(strId, "ns=2;i=1;");
120 }
121 
122 TEST(NodeId, NumericFromString)
123 {
124  OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
125  OpcUa::NodeId converted = OpcUa::ToNodeId("ns=2;i=1;");
126  ASSERT_EQ(expected, converted);
127 }
128 
129 TEST(NodeId, StringToString)
130 {
131  OpcUa::NodeId id = OpcUa::StringNodeId("string", 1);
132  std::string strId = OpcUa::ToString(id);
133  ASSERT_EQ(strId, "ns=1;s=string;");
134 }
135 
136 TEST(NodeId, StringFromString)
137 {
138  OpcUa::NodeId expected = OpcUa::StringNodeId("str", 2);
139  OpcUa::NodeId converted = OpcUa::ToNodeId("ns=2;s=str;");
140  ASSERT_EQ(expected, converted);
141 }
142 
143 TEST(NodeId, GuidToString)
144 {
145  OpcUa::Guid guid;
146  guid.Data1 = 0x01020304;
147  guid.Data2 = 0x0506;
148  guid.Data3 = 0x0708;
149  guid.Data4[0] = 0x09;
150  guid.Data4[1] = 0x0A;
151  guid.Data4[2] = 0x0B;
152  guid.Data4[3] = 0x0C;
153  guid.Data4[4] = 0x0D;
154  guid.Data4[5] = 0x0E;
155  guid.Data4[6] = 0x0F;
156  guid.Data4[7] = 0x10;
157 
158  OpcUa::NodeId id = OpcUa::GuidNodeId(guid, 1);
159  std::string strId = OpcUa::ToString(id);
160  ASSERT_EQ(strId, "ns=1;g=01020304-0506-0708-090A-0B0C0D0E0F10;");
161 }
162 
163 TEST(NodeId, GuidFromString)
164 {
165  OpcUa::Guid guid;
166  guid.Data1 = 0x01020304;
167  guid.Data2 = 0x0506;
168  guid.Data3 = 0x0708;
169  guid.Data4[0] = 0x09;
170  guid.Data4[1] = 0x0A;
171  guid.Data4[2] = 0x0B;
172  guid.Data4[3] = 0x0C;
173  guid.Data4[4] = 0x0D;
174  guid.Data4[5] = 0x0E;
175  guid.Data4[6] = 0x0F;
176  guid.Data4[7] = 0x10;
177 
178  OpcUa::NodeId expected = OpcUa::GuidNodeId(guid, 2);
179  OpcUa::NodeId converted = OpcUa::ToNodeId("ns=1;g=01020304-0506-0708-090A-0B0C0D0E0F10;");
180  ASSERT_EQ(expected.Encoding, converted.Encoding);
181 
182  OpcUa::Guid expectedGuid = converted.GetGuidIdentifier();
183  ASSERT_EQ(guid.Data1, expectedGuid.Data1);
184 }
185 
186 TEST(NodeId, NamespaceUriToString)
187 {
189  id.SetNamespaceURI("uri");
190 
191  std::string strId = OpcUa::ToString(id);
192  ASSERT_EQ(strId, "nsu=uri;ns=2;i=1;");
193 }
194 
195 TEST(NodeId, NamespaceUriFromString)
196 {
197  OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
198  expected.SetNamespaceURI("uri");
199 
200  OpcUa::NodeId converted = OpcUa::ToNodeId("nsu=uri;ns=2;i=1;");
201  ASSERT_EQ(converted, expected);
202 }
203 
204 TEST(NodeId, ServerIndexToString)
205 {
207  id.SetServerIndex(3);
208 
209  std::string strId = OpcUa::ToString(id);
210  ASSERT_EQ(strId, "srv=3;ns=2;i=1;");
211 }
212 
213 TEST(NodeId, ServerIndexFromString)
214 {
215  OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
216  expected.SetServerIndex(3);
217 
218  OpcUa::NodeId converted = OpcUa::ToNodeId("srv=3;ns=2;i=1;");
219  ASSERT_EQ(converted, expected);
220 }
221 
222 TEST(NodeId, ServerIndexAndNamespaceUriToString)
223 {
225  id.SetServerIndex(3);
226  id.SetNamespaceURI("uri");
227 
228  std::string strId = OpcUa::ToString(id);
229  ASSERT_EQ(strId, "srv=3;nsu=uri;ns=2;i=1;");
230 }
231 
232 TEST(NodeId, ServerIndexAndNamespaceUriString)
233 {
234  OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
235  expected.SetServerIndex(3);
236  expected.SetNamespaceURI("uri");
237 
238  OpcUa::NodeId converted = OpcUa::ToNodeId("srv=3;nsu=uri;ns=2;i=1;");
239  ASSERT_EQ(converted, expected);
240 }
241 
242 TEST(NodeId, WithDefaultNamespace)
243 {
244  OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
245  OpcUa::NodeId converted = OpcUa::ToNodeId("i=1;", 2);
246  ASSERT_EQ(expected, converted);
247 }
uint16_t Data2
Definition: guid.h:22
static DateTime FromTimeT(time_t t, unsigned usec=0)
time
Definition: server.py:52
uint32_t Data1
Definition: guid.h:21
Guid GetGuidIdentifier() const
Definition: nodeid.cpp:82
static time_t ToTimeT(DateTime dateTime)
#define ASSERT_THROW(statement, expected_exception)
void SetNamespaceURI(const std::string &uri)
Definition: nodeid.cpp:480
void SetServerIndex(uint32_t index)
Definition: nodeid.cpp:486
uint16_t Data3
Definition: guid.h:23
NodeId GuidNodeId(Guid value, uint16_t namespaceIndex=0)
Definition: nodeid.h:245
TEST(GTestEnvVarTest, Dummy)
OPC UA Address space part. GNU LGPL.
Guid ToGuid(const std::string &str)
uint8_t Data4[8]
Definition: guid.h:24
#define ASSERT_EQ(val1, val2)
NodeId ToNodeId(const std::string &str, uint32_t defaultNamespace=0)
NodeIdEncoding Encoding
Definition: nodeid.h:46
std::string ToString(const AttributeId &value)
static DateTime Current()
NodeId StringNodeId(std::string value, uint16_t namespaceIndex=0)
Definition: nodeid.h:227
NodeId NumericNodeId(uint32_t value, uint16_t namespaceIndex=0)
Definition: nodeid.h:218
#define EXPECT_EQ(expected, actual)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08