binary_serialize_session.cpp
Go to the documentation of this file.
1 
11 #include "common.h"
12 
16 #include <opc/ua/protocol/types.h>
18 
19 #include <algorithm>
20 #include <stdexcept>
21 
22 
23 
24 //-------------------------------------------------------
25 // CreateSessionRequest
26 //-------------------------------------------------------
27 
28 TEST_F(OpcUaBinarySerialization, CreateSessionRequest)
29 {
30 
31  using namespace OpcUa;
32  using namespace OpcUa::Binary;
33 
34  CreateSessionRequest request;
35 
39 
41 
43 
44  request.Parameters.ServerUri = "su";
45  request.Parameters.EndpointUrl = "eu";
46  request.Parameters.SessionName = "sn";
47  request.Parameters.ClientNonce = ByteString(std::vector<uint8_t> {1, 2, 3, 4});
48  request.Parameters.ClientCertificate = ByteString(std::vector<uint8_t> {5, 6, 7, 8});
49  request.Parameters.RequestedSessionTimeout = 1200000;
51 
52  GetStream() << request << flush;
53 
54  const std::vector<char> expectedData =
55  {
56  1, 0, (char)0xcd, 0x1, // TypeId
57  // RequestHeader
60  2, 0, 0, 0, 's', 'u',
61  2, 0, 0, 0, 'e', 'u',
62  2, 0, 0, 0, 's', 'n',
63  4, 0, 0, 0, 1, 2, 3, 4,
64  4, 0, 0, 0, 5, 6, 7, 8,
65  0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
66  2, 0, 0, 0
67  };
68 
69  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
70  ASSERT_EQ(expectedData.size(), RawSize(request));
71 }
72 
74 {
75  using namespace OpcUa;
76  using namespace OpcUa::Binary;
77 
78  const std::vector<char> expectedData =
79  {
80  1, 0, (char)0xcd, 0x1, // TypeId
81  // RequestHeader
84  2, 0, 0, 0, 's', 'u',
85  2, 0, 0, 0, 'e', 'u',
86  2, 0, 0, 0, 's', 'n',
87  4, 0, 0, 0, 1, 2, 3, 4,
88  4, 0, 0, 0, 5, 6, 7, 8,
89  0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
90  2, 0, 0, 0
91  };
92 
93  GetChannel().SetData(expectedData);
94 
95  CreateSessionRequest request;
96  GetStream() >> request;
97 
101 
104 
105  ASSERT_EQ(request.Parameters.ServerUri, "su");
106  ASSERT_EQ(request.Parameters.EndpointUrl, "eu");
107  ASSERT_EQ(request.Parameters.SessionName, "sn");
108  ByteString clientNonce = ByteString(std::vector<uint8_t> {1, 2, 3, 4});
109  ASSERT_EQ(request.Parameters.ClientNonce, clientNonce);
110  ByteString cert = ByteString(std::vector<uint8_t> {5, 6, 7, 8});
111  ASSERT_EQ(request.Parameters.ClientCertificate, cert);
112  ASSERT_EQ(request.Parameters.RequestedSessionTimeout, 1200000);
114 }
115 
116 
117 //----------------------------------------------------
118 // CreateSessionResponse
119 //----------------------------------------------------
120 
121 // TEST_F(OpcUaBinarySerialization, CreateSessionResponse)
122 // {
123 //
124 // using namespace OpcUa;
125 // using namespace OpcUa::Binary;
126 //
127 // CreateSessionResponse response;
128 //
129 // ASSERT_EQ(response.TypeId.Encoding, EV_FOUR_BYTE);
130 // ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
131 // ASSERT_EQ(response.TypeId.FourByteData.Identifier, OpcUa::CREATE_SESSION_RESPONSE);
132 //
133 // FILL_TEST_RESPONSE_HEADER(response.Header);
134 //
135 // response.Parameters.SessionId.Encoding = EV_FOUR_BYTE;
136 // response.Parameters.SessionId.FourByteData.NamespaceIndex = 1;
137 // response.Parameters.SessionId.FourByteData.Identifier = 2;
138 //
139 // response.Parameters.AuthenticationToken.Encoding = EV_FOUR_BYTE;
140 // response.Parameters.AuthenticationToken.FourByteData.NamespaceIndex = 1;
141 // response.Parameters.AuthenticationToken.FourByteData.Identifier = 2;
142 //
143 // response.Parameters.RevisedSessionTimeout = 1200000;
144 // response.Parameters.ServerNonce = ByteString(std::vector<uint8_t>{1,2,3,4});
145 // response.Parameters.ServerCertificate = ByteString(std::vector<uint8_t>{5,6,7,8});
146 // EndpointDescription e;
147 // FILL_TEST_ENDPOINT(e);
148 // response.Parameters.ServerEndpoints.push_back(e);
149 // SignedSoftwareCertificate cert;
150 // cert.CertificateData = ByteString(std::vector<uint8_t>{4,3,2,1});
151 // cert.Signature = ByteString(std::vector<uint8_t>{8,7,6,5});
152 // response.Parameters.ServerSoftwareCertificates.push_back(cert);
153 // response.Parameters.ServerSignature.Signature = ByteString(std::vector<uint8_t>{7,6,5,4});
154 // response.Parameters.ServerSignature.Algorithm = "aes";
155 //
156 // response.Parameters.MaxRequestMessageSize = 0x1000;
157 //
158 // GetStream() << response << flush;
159 //
160 //
161 // const std::vector<char> expectedData = {
162 // 1, 0, (char)0xd0, 0x1, // TypeId
163 // // ResponseHeader
164 // TEST_RESPONSE_HEADER_BINARY_DATA,
165 // 1,1,2,0,
166 // 1,1,2,0,
167 // 0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
168 // 4,0,0,0, 1,2,3,4,
169 // 4,0,0,0, 5,6,7,8,
170 // 1,0,0,0,
171 // TEST_ENDPOINT_BINARY_DATA,
172 // 1,0,0,0, 4,0,0,0, 4,3,2,1, 4,0,0,0, 8,7,6,5,
173 // 4,0,0,0, 7,6,5,4,
174 // 3,0,0,0, 'a','e','s',
175 // 0,0x10,0,0
176 // };
177 //
178 // ASSERT_EQ(expectedData, GetChannel().SerializedData);
179 // ASSERT_EQ(expectedData.size(), RawSize(response));
180 // }
181 //
182 // TEST_F(OpcUaBinaryDeserialization, CreateSessionResponse)
183 // {
184 // using namespace OpcUa;
185 // using namespace OpcUa::Binary;
186 //
187 // const std::vector<char> expectedData = {
188 // 1, 0, (char)0xd0, 0x1, // TypeId
189 // // RequestHeader
190 // TEST_RESPONSE_HEADER_BINARY_DATA,
191 // 1,1,2,0,
192 // 1,1,2,0,
193 // 0, 0, 0, 0, (char)0x80, (char)0x4f, (char)0x32, (char)0x41,
194 // 4,0,0,0, 1,2,3,4,
195 // 4,0,0,0, 5,6,7,8,
196 // 1,0,0,0,
197 // TEST_ENDPOINT_BINARY_DATA,
198 // 1,0,0,0, 4,0,0,0, 4,3,2,1, 4,0,0,0, 8,7,6,5,
199 // 4,0,0,0, 7,6,5,4,
200 // 3,0,0,0, 'a','e','s',
201 // 0,0x10,0,0
202 // };
203 //
204 // GetChannel().SetData(expectedData);
205 //
206 // CreateSessionResponse response;
207 // GetStream() >> response;
208 //
209 // ASSERT_EQ(response.TypeId.Encoding, EV_FOUR_BYTE);
210 // ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
211 // ASSERT_EQ(response.TypeId.FourByteData.Identifier, OpcUa::CREATE_SESSION_RESPONSE);
212 //
213 // ASSERT_RESPONSE_HEADER_EQ(response.Header);
214 //
215 // ASSERT_EQ(response.Parameters.SessionId.Encoding, EV_FOUR_BYTE);
216 // ASSERT_EQ(response.Parameters.SessionId.FourByteData.NamespaceIndex, 1);
217 // ASSERT_EQ(response.Parameters.SessionId.FourByteData.Identifier, 2);
218 //
219 // ASSERT_EQ(response.Parameters.AuthenticationToken.Encoding, EV_FOUR_BYTE);
220 // ASSERT_EQ(response.Parameters.AuthenticationToken.FourByteData.NamespaceIndex, 1);
221 // ASSERT_EQ(response.Parameters.AuthenticationToken.FourByteData.Identifier, 2);
222 //
223 // ASSERT_EQ(response.Parameters.RevisedSessionTimeout, 1200000);
224 //
225 // ByteString serverNonce = ByteString(std::vector<uint8_t>{1,2,3,4});
226 // ASSERT_EQ(response.Parameters.ServerNonce, serverNonce);
227 //
228 // ByteString serverCert = ByteString(std::vector<uint8_t>{5,6,7,8});
229 // ASSERT_EQ(response.Parameters.ServerCertificate, serverCert);
230 //
231 // ASSERT_EQ(response.Parameters.ServerEndpoints.size(), 1);
232 // ASSERT_ENDPOINT_EQ(response.Parameters.ServerEndpoints[0]);
233 //
234 // SignedSoftwareCertificate cert;
235 // cert.CertificateData = ByteString(std::vector<uint8_t>{4,3,2,1});
236 // cert.Signature = ByteString(std::vector<uint8_t>{8,7,6,5});
237 // std::vector<SignedSoftwareCertificate> certs = {cert};
238 // // ASSERT_EQ(response.Parameters.ServerSoftwareCertificates, certs);
239 //
240 // ByteString signature = ByteString(std::vector<uint8_t>{7,6,5,4});
241 // ASSERT_EQ(response.Parameters.ServerSignature.Signature, signature);
242 // ASSERT_EQ(response.Parameters.ServerSignature.Algorithm, "aes");
243 //
244 // ASSERT_EQ(response.Parameters.MaxRequestMessageSize, 0x1000);
245 // }
246 
247 //-------------------------------------------------------------------
248 // ExtensionObjectHeader
249 //-------------------------------------------------------------------
250 
251 TEST_F(OpcUaBinarySerialization, UserIdentifyToken_Anonymous)
252 {
253  using namespace OpcUa;
254  using namespace OpcUa::Binary;
255 
256  UserIdentifyToken token;
257  token.setPolicyId("0");
258  /*
259  ASSERT_EQ(token.Header.TypeId.Encoding, EV_FOUR_BYTE);
260  ASSERT_EQ(token.Header.TypeId.FourByteData.NamespaceIndex, 0);
261  ASSERT_EQ(token.Hader.TypeId.FourByteData.Identifier, USER_IdENTIFY_TOKEN_ANONYMOUS);
262  ASSERT_EQ(token.Header.Encoding, HAS_BINARY_BODY);
263  ASSERT_EQ(token.Anonymous.Data, std::vector{});
264  */
265  GetStream() << token << flush;
266 
267  const std::vector<char> expectedData =
268  {
269  1, 0, (char)0x41, 0x1, // TypeId
271  0x5, 0, 0, 0, 1, 0, 0, 0, '0'
272  };
273 
274  ASSERT_EQ(expectedData, GetChannel().SerializedData);
275  ASSERT_EQ(expectedData.size(), RawSize(token));
276 }
277 
278 TEST_F(OpcUaBinaryDeserialization, UserIdentifyToken_Anonymous)
279 {
280  using namespace OpcUa;
281  using namespace OpcUa::Binary;
282  const std::vector<char> expectedData =
283  {
284  1, 0, (char)0x41, 0x1, // TypeId
286  0x5, 0, 0, 0, 1, 0, 0, 0, '0'
287  };
288 
289  GetChannel().SetData(expectedData);
290 
291  UserIdentifyToken token;
292  GetStream() >> token;
293 
298  std::vector<uint8_t> policy_id = {'0'};
299  ASSERT_EQ(token.PolicyId, policy_id);
300 }
301 
302 
303 //-------------------------------------------------------
304 // ActivateSessionRequest
305 //-------------------------------------------------------
306 
307 // TEST_F(OpcUaBinarySerialization, ActivateSessionRequest)
308 // {
309 //
310 // using namespace OpcUa;
311 // using namespace OpcUa::Binary;
312 //
313 // ActivateSessionRequest request;
314 // request.Parameters.UserIdentityToken.setPolicyId("0");
315 //
316 // ASSERT_EQ(request.TypeId.Encoding, EV_FOUR_BYTE);
317 // ASSERT_EQ(request.TypeId.FourByteData.NamespaceIndex, 0);
318 // ASSERT_EQ(request.TypeId.FourByteData.Identifier, OpcUa::ACTIVATE_SESSION_REQUEST);
319 //
320 // FILL_TEST_REQUEST_HEADER(request.Header);
321 //
322 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.Encoding, EV_FOUR_BYTE);
323 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.FourByteData.NamespaceIndex, 0);
324 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.FourByteData.Identifier, OpcUa::USER_IdENTIFY_TOKEN_ANONYMOUS);
325 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.Encoding, HAS_BINARY_BODY);
326 // std::vector<uint8_t> policy_id = {1,0,0,0,'0'};
327 // ASSERT_EQ(request.Parameters.UserIdentityToken.PolicyId, policy_id);
328 //
329 // GetStream() << request << flush;
330 //
331 // const std::vector<char> expectedData = {
332 // 1, 0, (char)0xd3, 0x1, // TypeId
333 // // RequestHeader
334 // TEST_REQUEST_HEADER_BINARY_DATA,
335 // -1,-1,-1,-1,
336 // -1,-1,-1,-1,
337 //
338 // 0,0,0,0,
339 // 0,0,0,0,
340 //
341 // 1, 0, (char)0x41, 0x1, // TypeId
342 // 1,
343 // 0x5,0,0,0, 1,0,0,0,'0',
344 // -1,-1,-1,-1,
345 // -1,-1,-1,-1
346 // };
347 //
348 // ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
349 // ASSERT_EQ(expectedData.size(), RawSize(request));
350 // }
351 //
352 // TEST_F(OpcUaBinaryDeserialization, ActivateSessionRequest)
353 // {
354 //
355 // using namespace OpcUa;
356 // using namespace OpcUa::Binary;
357 //
358 // const std::vector<char> expectedData = {
359 // 1, 0, (char)0xd3, 0x1, // TypeId
360 // // RequestHeader
361 // TEST_REQUEST_HEADER_BINARY_DATA,
362 // -1,-1,-1,-1,
363 // -1,-1,-1,-1,
364 //
365 // 1,0,0,0, 1,0,0,0, 1,
366 // 0,0,0,0,
367 //
368 // 1, 0, (char)0x41, 0x1, // TypeId
369 // 1,
370 // 0x5,0,0,0, 1,0,0,0,'0',
371 // -1,-1,-1,-1,
372 // -1,-1,-1,-1
373 // };
374 //
375 // GetChannel().SetData(expectedData);
376 //
377 // ActivateSessionRequest request;
378 // GetStream() >> request;
379 //
380 // ASSERT_EQ(request.TypeId.Encoding, EV_FOUR_BYTE);
381 // ASSERT_EQ(request.TypeId.FourByteData.NamespaceIndex, 0);
382 // ASSERT_EQ(request.TypeId.FourByteData.Identifier, OpcUa::ACTIVATE_SESSION_REQUEST);
383 //
384 // ASSERT_REQUEST_HEADER_EQ(request.Header);
385 //
386 // ASSERT_EQ(request.Parameters.ClientSoftwareCertificates.size(), 1);
387 // ASSERT_EQ(request.Parameters.ClientSoftwareCertificates[0].CertificateData, ByteString(std::vector<uint8_t>{1}));
388 //
389 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.Encoding, EV_FOUR_BYTE);
390 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.FourByteData.NamespaceIndex, 0);
391 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.TypeId.FourByteData.Identifier, OpcUa::USER_IdENTIFY_TOKEN_ANONYMOUS);
392 // ASSERT_EQ(request.Parameters.UserIdentityToken.Header.Encoding, HAS_BINARY_BODY);
393 // std::vector<uint8_t> policy_id = {1,0,0,0,'0'};
394 // ASSERT_EQ(request.Parameters.UserIdentityToken.PolicyId, policy_id);
395 // }
396 
397 //-------------------------------------------------------
398 // ActivateSessionResponse
399 //-------------------------------------------------------
400 
401 // TEST_F(OpcUaBinarySerialization, ActivateSessionResponse)
402 // {
403 //
404 // using namespace OpcUa;
405 // using namespace OpcUa::Binary;
406 //
407 // ActivateSessionResponse response;
408 //
409 // ASSERT_EQ(response.TypeId.Encoding, EV_FOUR_BYTE);
410 // ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
411 // ASSERT_EQ(response.TypeId.FourByteData.Identifier, OpcUa::ACTIVATE_SESSION_RESPONSE);
412 //
413 // FILL_TEST_RESPONSE_HEADER(response.Header);
414 // response.Parameters.ServerNonce = ByteString(std::vector<uint8_t>{1,1});
415 // std::vector<StatusCode> results;
416 // results.push_back(StatusCode(1));
417 // results.push_back(StatusCode(2));
418 // response.Parameters.Results = results;
419 //
420 // GetStream() << response << flush;
421 //
422 // const std::vector<char> expectedData = {
423 // 1, 0, (char)0xd6, 0x1, // TypeId
424 // // RequestHeader
425 // TEST_RESPONSE_HEADER_BINARY_DATA,
426 // 1,0,0,0, 1,
427 // 2,0,0,0, 1,0,0,0, 1,0,0,0,
428 // 0,0,0,0,
429 // };
430 //
431 // ASSERT_EQ(expectedData.size(), RawSize(response));
432 // ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
433 // }
434 //
435 // TEST_F(OpcUaBinaryDeserialization, ActivateSessionResponse)
436 // {
437 // using namespace OpcUa;
438 // using namespace OpcUa::Binary;
439 //
440 // const std::vector<char> expectedData = {
441 // 1, 0, (char)0xd6, 0x1, // TypeId
442 // // RequestHeader
443 // TEST_RESPONSE_HEADER_BINARY_DATA,
444 // 1,0,0,0, 1,
445 // 2,0,0,0, 1,0,0,0, 1,0,0,0,
446 // 0,0,0,0,
447 // };
448 //
449 // GetChannel().SetData(expectedData);
450 //
451 // ActivateSessionResponse response;
452 // GetStream() >> response;
453 //
454 // ASSERT_EQ(response.TypeId.Encoding, EV_FOUR_BYTE);
455 // ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
456 // ASSERT_EQ(response.TypeId.FourByteData.Identifier, OpcUa::ACTIVATE_SESSION_RESPONSE);
457 //
458 // ASSERT_RESPONSE_HEADER_EQ(response.Header);
459 //
460 // ASSERT_EQ(response.Parameters.ServerNonce, ByteString(std::vector<uint8_t>{1,1}));
461 // std::vector<StatusCode> results;
462 // results.push_back(StatusCode(1));
463 // results.push_back(StatusCode(2));
464 // ASSERT_EQ(response.Parameters.Results, results);
465 // ASSERT_TRUE(response.Parameters.DiagnosticInfos.empty());
466 // }
467 
468 //-------------------------------------------------------
469 // CloseSessionRequest
470 //-------------------------------------------------------
471 
473 {
474 
475  using namespace OpcUa;
476  using namespace OpcUa::Binary;
477 
478  CloseSessionRequest request;
479 
483 
485 
486  GetStream() << request << flush;
487 
488  const std::vector<char> expectedData =
489  {
490  1, 0, (char)0xd9, 0x1, // TypeId
491  // RequestHeader
493  1
494  };
495 
496  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
497  ASSERT_EQ(expectedData.size(), RawSize(request));
498 }
499 
501 {
502 
503  using namespace OpcUa;
504  using namespace OpcUa::Binary;
505 
506  const std::vector<char> expectedData =
507  {
508  1, 0, (char)0xd9, 0x1, // TypeId
509  // RequestHeader
511  1,
512  };
513 
514  GetChannel().SetData(expectedData);
515 
516  CloseSessionRequest request;
517  GetStream() >> request;
518 
522 
524 
525  ASSERT_EQ(request.DeleteSubscriptions, true);
526 }
527 
528 //-------------------------------------------------------
529 // CloseSessionResponse
530 //-------------------------------------------------------
531 
533 {
534 
535  using namespace OpcUa;
536  using namespace OpcUa::Binary;
537 
538  CloseSessionResponse response;
539 
543 
545 
546  GetStream() << response << flush;
547 
548  const std::vector<char> expectedData =
549  {
550  1, 0, (char)0xdc, 0x1, // TypeId
551  // RequestHeader
553  };
554 
555  ASSERT_EQ(expectedData.size(), RawSize(response));
556  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
557 }
558 
560 {
561 
562  using namespace OpcUa;
563  using namespace OpcUa::Binary;
564 
565  const std::vector<char> expectedData =
566  {
567  1, 0, (char)0xdc, 0x1, // TypeId
568  // RequestHeader
570  1,
571  };
572 
573  GetChannel().SetData(expectedData);
574 
575  CloseSessionResponse response;
576  GetStream() >> response;
577 
581 
583 }
584 
585 
#define TEST_RESPONSE_HEADER_BINARY_DATA
ExtensionObjectEncoding Encoding
Definition: types.h:287
ResponseHeader Header
Definition: session.h:36
OpcUa::CreateSessionParameters Parameters
std::string PrintData(const std::vector< char > &vec)
Test of opc ua binary handshake. GNU LGPL.
TEST_F(OpcUaBinarySerialization, CreateSessionRequest)
Test of opc ua binary handshake. GNU LGPL.
#define ASSERT_REQUEST_HEADER_EQ(header)
std::vector< uint8_t > PolicyId
Definition: types_manual.h:22
OpcUa::ByteString ClientCertificate
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
#define FILL_APPLICATION_DESCRIPTION(desc)
OpcUa::RequestHeader Header
#define FILL_TEST_RESPONSE_HEADER(header)
#define TEST_REQUEST_HEADER_BINARY_DATA
ExpandedNodeId TypeId
Definition: types.h:286
#define FILL_TEST_REQUEST_HEADER(header)
Moved from session.h.
Definition: types_manual.h:19
struct OpcUa::NodeId::FourByteDataType FourByteData
OPC UA Address space part. GNU LGPL.
#define ASSERT_RESPONSE_HEADER_EQ(header)
#define ASSERT_EQ(val1, val2)
RequestHeader Header
Definition: session.h:26
NodeIdEncoding Encoding
Definition: nodeid.h:46
#define TEST_APPLICATION_DESCRIPTION_BINARY_DATA
OpcUa::ApplicationDescription ClientDescription
#define ASSERT_APPLICATION_DESCRIPTION_EQ(desc)
void setPolicyId(const std::string &id)
std::size_t RawSize(const T &obj)
ExtensionObjectHeader Header
Definition: types_manual.h:21


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