binary_serialize_attribute.cpp
Go to the documentation of this file.
1 
11 #include "common.h"
12 
18 
19 #include <algorithm>
20 #include <stdexcept>
21 
22 
23 //-------------------------------------------------------
24 // AttributeId
25 // //TODO Add check for ivalid attributeId deserialization. It must throw.
26 //-------------------------------------------------------
27 
29 {
30 
31  using namespace OpcUa::Binary;
32  using OpcUa::AttributeId;
33 
34  GetStream() << AttributeId::Value << flush;
35 
36  const std::vector<char> expectedData =
37  {
38  13, 0, 0, 0
39  };
40 
41  ASSERT_EQ(expectedData.size(), RawSize(AttributeId::Value));
42  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
43 }
44 
46 {
47  using namespace OpcUa::Binary;
48  using OpcUa::AttributeId;
49 
50  const std::vector<char> expectedData =
51  {
52  13, 0, 0, 0
53  };
54 
55  GetChannel().SetData(expectedData);
56 
57  AttributeId id;
58  GetStream() >> id;
59 
61 }
62 
63 
64 //-------------------------------------------------------
65 // TimestampsToReturn
66 // TODO Add test for invalid Timestamps to return deserialization.
67 //-------------------------------------------------------
68 
69 TEST_F(OpcUaBinarySerialization, TimestampsToReturn)
70 {
71 
72  using namespace OpcUa;
73  using namespace OpcUa::Binary;
74 
75  GetStream() << TimestampsToReturn::Neither << flush;
76 
77  const std::vector<char> expectedData =
78  {
79  3, 0, 0, 0
80  };
81 
82  ASSERT_EQ(expectedData.size(), RawSize(TimestampsToReturn::Neither));
83  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
84 }
85 
87 {
88  using namespace OpcUa;
89  using namespace OpcUa::Binary;
90 
91  const std::vector<char> expectedData =
92  {
93  3, 0, 0, 0
94  };
95 
96  GetChannel().SetData(expectedData);
97 
98  TimestampsToReturn stamps;
99  GetStream() >> stamps;
100 
102 }
103 
104 //-------------------------------------------------------
105 // ReadValueId
106 //-------------------------------------------------------
107 
109 {
110 
111  using namespace OpcUa;
112  using namespace OpcUa::Binary;
113  using OpcUa::AttributeId;
114 
115  ReadValueId attr;
116 
117  attr.NodeId.Encoding = EV_TWO_BYTE;
118  attr.NodeId.TwoByteData.Identifier = 1;
120  attr.IndexRange = "1,2";
121  attr.DataEncoding.NamespaceIndex = 2;
122  attr.DataEncoding.Name = "test";
123 
124  GetStream() << attr << flush;
125 
126  const std::vector<char> expectedData =
127  {
128  0, 1,
129  13, 0, 0, 0,
130  3, 0, 0, 0, '1', ',', '2',
131  2, 0,
132  4, 0, 0, 0, 't', 'e', 's', 't'
133  };
134 
135  ASSERT_EQ(expectedData.size(), RawSize(attr));
136  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
137 }
138 
140 {
141  using namespace OpcUa;
142  using namespace OpcUa::Binary;
143  using OpcUa::AttributeId;
144 
145  const std::vector<char> expectedData =
146  {
147  0, 1,
148  13, 0, 0, 0,
149  3, 0, 0, 0, '1', ',', '2',
150  2, 0,
151  4, 0, 0, 0, 't', 'e', 's', 't'
152  };
153 
154  GetChannel().SetData(expectedData);
155 
156  ReadValueId attr;
157  GetStream() >> attr;
158 
163  ASSERT_EQ(attr.DataEncoding.Name, "test");
164 }
165 
166 //-------------------------------------------------------
167 // ReadRequest
168 //-------------------------------------------------------
169 
171 {
172  using namespace OpcUa;
173  OpcUa::ReadValueId attr;
174 
176  attr.NodeId.TwoByteData.Identifier = 1;
178  attr.IndexRange = "1,2";
179  attr.DataEncoding.NamespaceIndex = 2;
180  attr.DataEncoding.Name = "test";
181 
182  return attr;
183 }
184 
186 {
187  using namespace OpcUa;
188  using namespace OpcUa::Binary;
189 
190  ReadRequest request;
191 
195 
197 
198  request.Parameters.MaxAge = 1200000;
200 
201  request.Parameters.AttributesToRead.push_back(CreateReadValueId());
202 
203  GetStream() << request << flush;
204 
205  const std::vector<char> expectedData =
206  {
207  1, 0, (char)0x77, 0x2, // TypeId
208  // RequestHeader
210 
211  0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
212  3, 0, 0, 0,
213 
214  1, 0, 0, 0,
215  0, 1,
216  13, 0, 0, 0,
217  3, 0, 0, 0, '1', ',', '2',
218  2, 0,
219  4, 0, 0, 0, 't', 'e', 's', 't'
220 
221  };
222 
223  ASSERT_EQ(expectedData.size(), RawSize(request));
224  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
225 }
226 
228 {
229 
230  using namespace OpcUa;
231  using namespace OpcUa::Binary;
232 
233  const std::vector<char> expectedData =
234  {
235  1, 0, (char)0x77, 0x2, // TypeId
236  // RequestHeader
238 
239  0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
240  3, 0, 0, 0,
241 
242  1, 0, 0, 0,
243  0, 1,
244  13, 0, 0, 0,
245  3, 0, 0, 0, '1', ',', '2',
246  2, 0,
247  4, 0, 0, 0, 't', 'e', 's', 't'
248 
249  };
250 
251  GetChannel().SetData(expectedData);
252  ReadRequest request;
253  GetStream() >> request;
254 
258 
260 
261  ASSERT_EQ(request.Parameters.MaxAge, 1200000);
263 
264  ASSERT_EQ(request.Parameters.AttributesToRead.size(), 1);
265 
267 
268  ASSERT_EQ(request.Parameters.AttributesToRead[0].NodeId.Encoding, EV_TWO_BYTE);
269  ASSERT_EQ(request.Parameters.AttributesToRead[0].NodeId.TwoByteData.Identifier, 1);
271  ASSERT_EQ(request.Parameters.AttributesToRead[0].DataEncoding.NamespaceIndex, 2);
272  ASSERT_EQ(request.Parameters.AttributesToRead[0].DataEncoding.Name, "test");
273 
274 }
275 
276 //-------------------------------------------------------
277 // ReadResponse
278 //-------------------------------------------------------
279 
281 {
282 
283  using namespace OpcUa;
284  using namespace OpcUa::Binary;
285 
286  ReadResponse resp;
287 
291 
293 
295 
296  ASSERT_NO_THROW(GetStream() << resp << flush);
297 
298  char encodingMask = static_cast<char>(OpcUa::DATA_VALUE);
299  char variantMask = static_cast<char>(OpcUa::VariantType::QUALIFIED_NAME);
300  const std::vector<char> expectedData =
301  {
302  1, 0, (char)0x7A, 0x2, // TypeId
303  // RequestHeader
305  1, 0, 0, 0,
306  encodingMask,
307  variantMask,
308  1, 0,
309  4, 0, 0, 0, 'R', 'o', 'o', 't',
310  (char)0xFF, (char)0xFF, (char)0xFF, (char)0xFF
311  };
312 
313  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
314  ASSERT_EQ(expectedData.size(), RawSize(resp));
315 }
316 
317 TEST_F(OpcUaBinaryDeserialization, ReadResponse_with_QualifiedName_as_value)
318 {
319 
320  using namespace OpcUa;
321  using namespace OpcUa::Binary;
322 
323  char variantMask = static_cast<uint8_t>(VariantType::QUALIFIED_NAME);
324  char encodingMask = DATA_VALUE;
325 
326  const std::vector<char> expectedData =
327  {
328  1, 0, (char)0x7A, 0x2, // TypeId
329  // RequestHeader
331  1, 0, 0, 0,
332  encodingMask,
333  variantMask,
334  1, 0,
335  4, 0, 0, 0, 'R', 'o', 'o', 't',
336  0, 0, 0, 0
337  };
338 
339  GetChannel().SetData(expectedData);
340  ReadResponse resp;
341  GetStream() >> resp;
342 
346 
348  ASSERT_EQ(resp.Results.size(), 1);
349 }
350 
351 
353 {
354 
355  using namespace OpcUa;
356  using namespace OpcUa::Binary;
357 
358  char variantMask = static_cast<uint8_t>(VariantType::BOOLEAN);
359  char encodingMask =
360  DATA_VALUE |
366 
367  const std::vector<char> expectedData =
368  {
369  1, 0, (char)0x7A, 0x2, // TypeId
370  // RequestHeader
372 
373  // Results
374  1, 0, 0, 0,
375 
376  encodingMask,
377  variantMask, 1,
378  1, 0, 0, 0,
379  2, 0, 0, 0, 0, 0, 0, 0,
380  3, 0,
381  4, 0, 0, 0, 0, 0, 0, 0,
382  5, 0,
383 
384  0, 0, 0, 0
385  };
386 
387  GetChannel().SetData(expectedData);
388  ReadResponse resp;
389  GetStream() >> resp;
390 
394 
396  ASSERT_EQ(resp.Results.size(), 1);
397 }
398 
399 //-------------------------------------------------------
400 // WriteValue
401 //-------------------------------------------------------
402 
404 {
405  using namespace OpcUa;
406  using namespace OpcUa::Binary;
407 
408  WriteValue value;
409  value.NodeId.Encoding = EV_FOUR_BYTE;
410  value.NodeId.FourByteData.Identifier = 1;
412  value.Value.Encoding = DATA_VALUE;
413  value.Value.Value = true;
414 
415  GetStream() << value << flush;
416 
417  const std::vector<char> expectedData =
418  {
419  1, 0, 1, 0, // Node
420  4, 0, 0, 0, // AttributeId
421  -1, -1, -1, -1, // NumericRange
422  1, 1, 1 // NodesToWrite
423  };
424 
425  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
426  ASSERT_EQ(expectedData.size(), RawSize(value));
427 }
428 
430 {
431 
432  using namespace OpcUa;
433  using namespace OpcUa::Binary;
434 
435  const std::vector<char> expectedData =
436  {
437  1, 0, 1, 0, // Node
438  4, 0, 0, 0, // AttributeId
439  -1, -1, -1, -1, // NumericRange
440  1, 1, 1 // NodesToWrite
441  };
442 
443 
444  GetChannel().SetData(expectedData);
445  WriteValue value;
446  GetStream() >> value;
447 
453  ASSERT_EQ(value.Value.Value.As<bool>(), true);
454 }
455 
456 
457 //-------------------------------------------------------
458 // WriteRequest
459 //-------------------------------------------------------
460 
462 {
463 
464  using namespace OpcUa;
465  using namespace OpcUa::Binary;
466 
467  WriteRequest request;
468 
472 
474 
475  WriteValue value;
476  value.NodeId.Encoding = EV_FOUR_BYTE;
477  value.NodeId.FourByteData.Identifier = 1;
479  value.Value.Encoding = DATA_VALUE;
480  value.Value.Value = true;
481 
482  request.Parameters.NodesToWrite.push_back(value);
483 
484  GetStream() << request << flush;
485 
486  const std::vector<char> expectedData =
487  {
488  1, 0, (char)0xA1, 0x2, // TypeId
489  // RequestHeader
491 
492  1, 0, 0, 0,
493 
494  1, 0, 1, 0, // Node
495  4, 0, 0, 0, // AttributeId
496  -1, -1, -1, -1, // NumericRange
497  1, 1, 1 // NodesToWrite
498  };
499 
500  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
501  ASSERT_EQ(expectedData.size(), RawSize(request));
502 }
503 
505 {
506 
507  using namespace OpcUa;
508  using namespace OpcUa::Binary;
509 
510  const std::vector<char> expectedData =
511  {
512  1, 0, (char)0xA1, 0x2, // TypeId
513  // RequestHeader
515 
516  1, 0, 0, 0,
517  1, 0, 1, 0, // Node
518  4, 0, 0, 0, // AttributeId
519  -1, -1, -1, -1, // NumericRange
520  1, 1, 1 // Value
521  };
522 
523 
524 
525  GetChannel().SetData(expectedData);
526  WriteRequest request;
527  GetStream() >> request;
528 
529 
533 
535 
536 
537  ASSERT_EQ(request.Parameters.NodesToWrite.size(), 1);
538  ASSERT_EQ(request.Parameters.NodesToWrite[0].NodeId.Encoding, EV_FOUR_BYTE);
539  ASSERT_EQ(request.Parameters.NodesToWrite[0].NodeId.FourByteData.Identifier, 1);
541  ASSERT_EQ(request.Parameters.NodesToWrite[0].Value.Encoding, DATA_VALUE);
542  ASSERT_EQ(request.Parameters.NodesToWrite[0].Value.Value.Type(), VariantType::BOOLEAN);
543  ASSERT_EQ(request.Parameters.NodesToWrite[0].Value.Value.As<bool>(), true);
544 }
545 
546 //-------------------------------------------------------
547 // WriteResponse
548 //-------------------------------------------------------
549 
551 {
552  using namespace OpcUa;
553  using namespace OpcUa::Binary;
554 
555  WriteResponse resp;
556 
560 
562 
563  resp.Results.push_back(static_cast<StatusCode>(1));
564 
567  info.LocalizedText = 4;
568  resp.DiagnosticInfos.push_back(info);
569 
570  GetStream() << resp << flush;
571 
572  const std::vector<char> expectedData =
573  {
574  1, 0, (char)0xA4, 0x2, // TypeId
575  // RequestHeader
577 
578  1, 0, 0, 0, 1, 0, 0, 0,
579  1, 0, 0, 0, static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT), 4, 0, 0, 0
580  };
581 
582  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
583  ASSERT_EQ(expectedData.size(), RawSize(resp));
584 }
585 
587 {
588  using namespace OpcUa;
589  using namespace OpcUa::Binary;
590 
591  const std::vector<char> expectedData =
592  {
593  1, 0, (char)0xA4, 0x2, // TypeId
594  // RequestHeader
596 
597  1, 0, 0, 0, 1, 0, 0, 0,
598  1, 0, 0, 0, static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT), 4, 0, 0, 0
599  };
600 
601  GetChannel().SetData(expectedData);
602  WriteResponse resp;
603  GetStream() >> resp;
604 
605 
609 
611 
612  ASSERT_EQ(resp.Results.size(), 1);
613  ASSERT_EQ(resp.Results[0], static_cast<OpcUa::StatusCode>(1));
614 
615  ASSERT_EQ(resp.DiagnosticInfos.size(), 1);
616  ASSERT_EQ(resp.DiagnosticInfos[0].EncodingMask, static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT));
617  ASSERT_EQ(resp.DiagnosticInfos[0].LocalizedText, 4);
618 }
619 
std::vector< OpcUa::ReadValueId > AttributesToRead
int32_t LocalizedText
Definition: types.h:217
#define TEST_RESPONSE_HEADER_BINARY_DATA
DiagnosticInfoMask
Definition: types.h:200
std::string PrintData(const std::vector< char > &vec)
Test of opc ua binary handshake. GNU LGPL.
const uint8_t DATA_VALUE_STATUS_CODE
Definition: data_value.h:19
std::vector< OpcUa::StatusCode > Results
std::vector< OpcUa::DiagnosticInfo > DiagnosticInfos
OpcUa::TimestampsToReturn TimestampsToReturn
OpcUa::RequestHeader Header
OpcUa::QualifiedName DataEncoding
OpcUa::ReadValueId CreateReadValueId()
#define ASSERT_REQUEST_HEADER_EQ(header)
OpcUa::NodeId TypeId
OpcUa::NodeId TypeId
uint16_t NamespaceIndex
Definition: types.h:73
const uint8_t DATA_VALUE_Server_TIMESTAMP
Definition: data_value.h:21
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
OpcUa::ResponseHeader Header
struct OpcUa::NodeId::TwoByteDataType TwoByteData
OpcUa::ReadParameters Parameters
std::vector< OpcUa::DataValue > Results
std::string IndexRange
TimestampsToReturn
Definition: enums.h:237
#define FILL_TEST_RESPONSE_HEADER(header)
const uint8_t DATA_VALUE_SOURCE_PICOSECONDS
Definition: data_value.h:22
#define TEST_REQUEST_HEADER_BINARY_DATA
#define FILL_TEST_REQUEST_HEADER(header)
OpcUa::RequestHeader Header
OpcUa::NodeId TypeId
DiagnosticInfoMask EncodingMask
Definition: types.h:214
struct OpcUa::NodeId::FourByteDataType FourByteData
VariantType Type() const
const uint8_t DATA_VALUE_SOURCE_TIMESTAMP
Definition: data_value.h:20
#define ASSERT_NO_THROW(statement)
OPC UA Address space part. GNU LGPL.
const uint8_t DATA_VALUE_Server_PICOSECONDS
Definition: data_value.h:23
#define ASSERT_RESPONSE_HEADER_EQ(header)
const char Root[]
Definition: strings.h:137
OpcUa::NodeId TypeId
std::vector< OpcUa::WriteValue > NodesToWrite
#define ASSERT_EQ(val1, val2)
OpcUa::WriteParameters Parameters
std::string Name
Definition: types.h:74
NodeIdEncoding Encoding
Definition: nodeid.h:46
OpcUa::AttributeId AttributeId
const uint8_t DATA_VALUE
Definition: data_value.h:18
TEST_F(OpcUaBinarySerialization, AttributeId)
Test of opc ua binary attributes. GNU LGPL.
OpcUa::DataValue Value
uint8_t Encoding
Definition: data_value.h:28
T As() const
Definition: variant.h:271
OpcUa::NodeId NodeId
OpcUa::NodeId NodeId
OpcUa::ResponseHeader Header
OpcUa::AttributeId AttributeId
std::size_t RawSize(const T &obj)


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