binary_handshake.cpp
Go to the documentation of this file.
1 
11 #include "common.h"
12 
13 #include <gtest/gtest.h>
14 #include <opc/ua/attribute_ids.h>
15 #include <opc/ua/object_ids.h>
16 #include <opc/ua/protocol/attribute.h>
22 #include <opc/ua/protocol/view.h>
23 
24 namespace
25 {
26 class OpcBinaryHandshake : public ::testing::Test
27 {
28 protected:
29  virtual void SetUp()
30  {
31  }
32 
33  virtual void TearDown()
34  {
35  }
36 };
37 }
38 
39 using namespace OpcUa::Binary;
40 
41 TEST_F(OpcBinaryHandshake, EstablishConnection)
42 {
43  const std::string host = GetHost();
44  const unsigned port = GetPort();
45  std::unique_ptr<OpcUa::RemoteConnection> connect = OpcUa::Connect(host, port);
46  ASSERT_EQ(connect->GetHost(), host);
47  ASSERT_EQ(connect->GetPort(), port);
48 }
49 
50 //-----------------------------------------------------------------------
51 // Hello
52 //-----------------------------------------------------------------------
53 
54 TEST_F(OpcBinaryHandshake, SayingHello)
55 {
56  std::shared_ptr<OpcUa::RemoteConnection> connection(OpcUa::Connect(GetHost(), GetPort()));
57  OpcUa::Binary::OStream os(connection);
58  OpcUa::Binary::IStream is(connection);
59 
61  hello.ProtocolVersion = 0;
65  hello.MaxChunkCount = 1;
66  hello.EndpointUrl = GetEndpoint();
67 
69  hdr.AddSize(RawSize(hello));
70 
71  os << hdr << hello << flush;
72 
73  OpcUa::Binary::Header ackHeader;
74  is >> ackHeader;
76  ASSERT_EQ(ackHeader.Chunk, OpcUa::Binary::CHT_SINGLE);
77 
79 
80  ASSERT_EQ(ackHeader.Size, RawSize(ack) + RawSize(ackHeader));
81 
82  ASSERT_NO_THROW(is >> ack);
83 }
84 
85 //----------------------------------------------------------------------
86 // OpenSecureChannel
87 //----------------------------------------------------------------------
88 
90 {
91  std::stringstream s;
92  s << "opc.tcp://" << GetHost() << ":" << GetPort();
93  return s.str();
94 }
95 
96 TEST_F(OpcBinaryHandshake, OpenSecureChannel_PolicyNone)
97 {
98  std::shared_ptr<OpcUa::RemoteConnection> connection(OpcUa::Connect(GetHost(), GetPort()));
99 
100  OpcUa::Binary::SecureConnectionParams params;
101  params.EndpointUrl = GetEndpointUrl();
102  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
103 
104  std::unique_ptr<OpcUa::IOChannel> secureChannel = OpcUa::Binary::CreateSecureChannel(connection, params);
105 }
106 
107 
108 //----------------------------------------------------------------------
109 // GetEndpoints
110 //----------------------------------------------------------------------
111 
112 TEST_F(OpcBinaryHandshake, GetEndpoints)
113 {
114  std::shared_ptr<OpcUa::RemoteConnection> connection(OpcUa::Connect(GetHost(), GetPort()));
115 
116  OpcUa::Binary::SecureConnectionParams params;
117  params.EndpointUrl = GetEndpointUrl();
118  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
119 
120  std::shared_ptr<OpcUa::IOChannel> secureChannel = OpcUa::Binary::CreateSecureChannel(connection, params);
121 
122  using OpcUa::Binary::flush;
123 
124  OpcUa::Binary::GetEndpointsRequest request;
125  request.EndpointUrl = params.EndpointUrl;
126  OpcUa::Binary::IOStream io(secureChannel);
127  io << request << flush;
128 
129  OpcUa::Binary::GetEndpointsResponse response;
130  ASSERT_NO_THROW(io >> response);
131 }
132 
133 //----------------------------------------------------------------------
134 // CreateSession
135 //----------------------------------------------------------------------
136 
137 EndpointDescription GetEndpoint(OpcUa::Binary::IOStream & stream)
138 {
139  OpcUa::Binary::GetEndpointsRequest request;
140  request.EndpointUrl = GetEndpointUrl();
141  stream << request << flush;
142 
143  OpcUa::Binary::GetEndpointsResponse response;
144  stream >> response;
145 
146  if (response.Endpoints.empty())
147  {
148  throw std::logic_error("Server returned empty list of endpoints");
149  }
150 
151  return response.Endpoints.front();
152 }
153 
154 TEST_F(OpcBinaryHandshake, GetCreateSession)
155 {
156  using namespace OpcUa::Binary;
157  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
158 
159  SecureConnectionParams params;
160  params.EndpointUrl = GetEndpointUrl();
161  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
162 
163  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
164 
165  IOStream stream(secureChannel);
166  EndpointDescription endpoint = GetEndpoint(stream);
167 
168  CreateSessionRequest request;
169  request.ClientDescription.URI = "http://treww.org/libopcua";
170  request.ClientDescription.ProductURI = "http://treww.org/libopcua";
171  request.ClientDescription.Name.Encoding = HAS_TEXT;
172  request.ClientDescription.Name.Text = "libopcuamappings";
173  request.ClientDescription.Type = ApplicationType::Client;
174 
175  request.ServerURI = endpoint.ServerDescription.URI;
176  request.EndpointUrl = endpoint.EndpointUrl;
177  request.SessionName = "libiocuamappings session test";
178  request.ClientNonce = std::vector<uint8_t>(32, 0);
179 // request.ClientCertificate = GetCertificate();
180  request.RequestedSessionTimeout = 3600000;
181  request.MaxResponseMessageSize = 65536;
182 
183  stream << request << flush;
184 
185  OpcUa::Binary::CreateSessionResponse response;
186  ASSERT_NO_THROW(stream >> response);
187 
188  ASSERT_EQ(response.TypeId.Encoding, EV_FOUR_BYTE);
189  ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
190  ASSERT_EQ(response.TypeId.FourByteData.Identifier, OpcUa::CREATE_SESSION_RESPONSE);
191 }
192 
193 //----------------------------------------------------------------------
194 // ActivateSession
195 //----------------------------------------------------------------------
196 
197 OpcUa::Binary::CreateSessionResponse CreateSession(OpcUa::Binary::IOStream & stream)
198 {
199  EndpointDescription endpoint = GetEndpoint(stream);
200 
201  CreateSessionRequest request;
202  request.ClientDescription.URI = "http://treww.org/libopcua";
203  request.ClientDescription.ProductURI = "http://treww.org/libopcua";
204  request.ClientDescription.Name.Encoding = HAS_TEXT;
205  request.ClientDescription.Name.Text = "libopcuamappings";
206  request.ClientDescription.Type = ApplicationType::Client;
207 
208  request.ServerURI = endpoint.ServerDescription.URI;
209  request.EndpointUrl = endpoint.EndpointUrl; // TODO make just endpoint.URL;
210  request.SessionName = "libiocuamappings session test";
211  request.ClientNonce = std::vector<uint8_t>(32, 0);
212 // request.ClientCertificate = GetCertificate();
213  request.RequestedSessionTimeout = 3600000;
214  request.MaxResponseMessageSize = 65536;
215 
216  stream << request << flush;
217 
218  OpcUa::Binary::CreateSessionResponse response;
219  stream >> response;
220  return response;
221 }
222 
223 TEST_F(OpcBinaryHandshake, ActivateSession)
224 {
225  using namespace OpcUa::Binary;
226  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
227 
228  SecureConnectionParams params;
229  params.EndpointUrl = GetEndpointUrl();
230  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
231 
232  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
233 
234  IOStream stream(secureChannel);
235  CreateSessionResponse session = CreateSession(stream);
236 
237  ActivateSessionRequest activate;
238  activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
239  activate.LocaleIds.push_back("en");
240  stream << activate << flush;
241 
242  ActivateSessionResponse response;
243  stream >> response;
244 
245 }
246 //----------------------------------------------------------------------
247 // CloseSession
248 //----------------------------------------------------------------------
249 
250 TEST_F(OpcBinaryHandshake, CloseSession)
251 {
252  using namespace OpcUa::Binary;
253  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
254 
255  SecureConnectionParams params;
256  params.EndpointUrl = GetEndpointUrl();
257  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
258 
259  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
260 
261  IOStream stream(secureChannel);
262  CreateSessionResponse session = CreateSession(stream);
263 
264  ActivateSessionRequest activate;
265  activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
266  activate.LocaleIds.push_back("en");
267  stream << activate << flush;
268 
269  ActivateSessionResponse response;
270  stream >> response;
271 
272  CloseSessionRequest closeSession;
273  closeSession.Header.SessionAuthenticationToken = session.AuthenticationToken;
274  stream << closeSession << flush;
275 
276  CloseSessionResponse closeResponse;
277  stream >> closeResponse;
278 }
279 
280 //----------------------------------------------------------------------
281 // Browse
282 //----------------------------------------------------------------------
283 
284 void CloseSession(OpcUa::Binary::IOStream & stream, const OpcUa::Binary::CreateSessionResponse & session)
285 {
286  CloseSessionRequest closeSession;
287  closeSession.Header.SessionAuthenticationToken = session.AuthenticationToken;
288  stream << closeSession << flush;
289 
290  CloseSessionResponse closeResponse;
291  stream >> closeResponse;
292 }
293 
294 void ActivateSession(OpcUa::Binary::IOStream & stream, const OpcUa::Binary::CreateSessionResponse & session)
295 {
296  using namespace OpcUa::Binary;
297  ActivateSessionRequest activate;
298  activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
299  activate.LocaleIds.push_back("en");
300  stream << activate << flush;
301 
302  ActivateSessionResponse response;
303  stream >> response;
304 }
305 
306 TEST_F(OpcBinaryHandshake, Browse)
307 {
308  using namespace OpcUa::Binary;
309  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
310 
311  SecureConnectionParams params;
312  params.EndpointUrl = GetEndpointUrl();
313  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
314 
315  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
316 
317  IOStream stream(secureChannel);
318  CreateSessionResponse session = CreateSession(stream);
319  ActivateSession(stream, session);
320 
321  OpcUa::Binary::BrowseRequest browse;
322  browse.Header.SessionAuthenticationToken = session.AuthenticationToken;
323  browse.MaxReferenciesPerNode = 2;
324 
325  BrowseDescription desc;
326  desc.NodeToBrowse.Encoding = EV_TWO_BYTE;
327  desc.NodeToBrowse.TwoByteData.Identifier = 84; // root node.
328  desc.Direction = BrowseDirection::FORWARD;
329  desc.ReferenceTypeId.Encoding = EV_TWO_BYTE;
330  desc.ReferenceTypeId.TwoByteData.Identifier = 33;
331  desc.IncludeSubtypes = true;
332  desc.NodeClasses = NodeClass::Unspecified;
333  desc.ResultMask = BrowseResultMask::All;
334  browse.NodesToBrowse.push_back(desc);
335 
336  stream << browse << flush;
337 
338  BrowseResponse response;
339  stream >> response;
340 
341  EXPECT_TRUE(!response.Results.empty());
342 
343  BrowseNextRequest browseNext;
344  browseNext.Header.SessionAuthenticationToken = session.AuthenticationToken;
345  browseNext.ReleaseContinuationPoints = false;
346  browseNext.ContinuationPoints.push_back(response.Results[0].ContinuationPoint);
347 
348  stream << browseNext << flush;
349 
350  BrowseNextResponse resp;
351  stream >> resp;
352 
353  EXPECT_TRUE(!response.Results.empty());
354 
355  CloseSession(stream, session);
356 }
357 
358 //----------------------------------------------------------------------
359 // Read
360 //----------------------------------------------------------------------
361 
362 TEST_F(OpcBinaryHandshake, Read)
363 {
364  using namespace OpcUa;
365  using namespace OpcUa::Binary;
366 
367  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
368 
369  SecureConnectionParams params;
370  params.EndpointUrl = GetEndpointUrl();
371  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
372 
373  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
374 
375  IOStream stream(secureChannel);
376  CreateSessionResponse session = CreateSession(stream);
377  ActivateSession(stream, session);
378 
379 
380 
381  ReadValueId value;
382  value.Node.Encoding = EV_FOUR_BYTE;
383  value.Node.FourByteData.Identifier = static_cast<uint8_t>(ObjectId::RootFolder);
384  value.Attribute = AttributeId::DisplayName;
385 
386 
387  OpcUa::Binary::ReadRequest request;
388  request.Header.SessionAuthenticationToken = session.AuthenticationToken;
389  request.MaxAge = 0;
390  request.TimestampsType = TimestampsToReturn::Neither;
391  request.AttributesToRead.push_back(value);
392 
393  stream << request << flush;
394 
395  ReadResponse response;
396  stream >> response;
397 
398  EXPECT_TRUE(!response.Results.empty());
399 
400  CloseSession(stream, session);
401 }
402 
403 //----------------------------------------------------------------------
404 // Write
405 //----------------------------------------------------------------------
406 
407 TEST_F(OpcBinaryHandshake, Write)
408 {
409  using namespace OpcUa;
410  using namespace OpcUa::Binary;
411 
412  std::shared_ptr<RemoteConnection> connection(Connect(GetHost(), GetPort()));
413 
414  SecureConnectionParams params;
415  params.EndpointUrl = GetEndpointUrl();
416  params.SecurePolicy = "http://opcfoundation.org/UA/SecurityPolicy#None";
417 
418  std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
419 
420  IOStream stream(secureChannel);
421  CreateSessionResponse session = CreateSession(stream);
422  ActivateSession(stream, session);
423 
424  OpcUa::Binary::WriteRequest request;
425  request.Header.SessionAuthenticationToken = session.AuthenticationToken;
426 
427  WriteValue value;
428  value.Node.Encoding = EV_FOUR_BYTE;
429  value.Node.FourByteData.Identifier = static_cast<uint8_t>(ObjectId::RootFolder);
430  value.Attribute = AttributeId::DisplayName;
431  value.Data.Encoding = DATA_VALUE;
432  value.Data.Value.Type = VariantType::STRING;
433  value.Data.Value.Value.String.push_back("root");
434 
435  request.NodesToWrite.push_back(value);
436 
437  stream << request << flush;
438 
439  WriteResponse response;
440  stream >> response;
441 
442  EXPECT_TRUE(!response.StatusCodes.empty());
443 
444  CloseSession(stream, session);
445 }
446 
int GetPort()
TEST_F(OpcBinaryHandshake, EstablishConnection)
void CloseSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
XmlRpcServer s
const uint8_t HAS_TEXT
Definition: types.h:130
bool connect(ros_opcua_srvs::Connect::Request &req, ros_opcua_srvs::Connect::Response &res)
std::string GetHost()
Test of opc ua binary handshake. GNU LGPL.
std::vector< T > Browse(const NodeId &node, NodeClass nodeClassMask, Services::SharedPtr services)
Definition: model_impl.h:31
#define EXPECT_TRUE(condition)
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
virtual void SetUp()
int Write(int fd, const void *buf, unsigned int count)
OpcUa::Binary::CreateSessionResponse CreateSession(OpcUa::Binary::IOStream &stream)
std::size_t AddSize(std::size_t size)
int Read(int fd, void *buf, unsigned int count)
virtual void TearDown()
#define ASSERT_NO_THROW(statement)
OPC UA Address space part. GNU LGPL.
#define OPCUA_DEFAULT_BUFFER_SIZE
Opc binary stream. Stream classes perform Serialization/Deserialization of opc ua structures...
Definition: stream.h:22
void ActivateSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
#define ASSERT_EQ(val1, val2)
std::string GetEndpointUrl()
const uint8_t DATA_VALUE
Definition: data_value.h:18
OpcUa::DataValue Value
EndpointDescription GetEndpoint(OpcUa::Binary::IOStream &stream)
std::unique_ptr< RemoteConnection > Connect(const std::string &host, unsigned port, const Common::Logger::SharedPtr &logger)
std::size_t RawSize(const T &obj)


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