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> 41 TEST_F(OpcBinaryHandshake, EstablishConnection)
44 const unsigned port =
GetPort();
54 TEST_F(OpcBinaryHandshake, SayingHello)
71 os << hdr << hello <<
flush;
96 TEST_F(OpcBinaryHandshake, OpenSecureChannel_PolicyNone)
100 OpcUa::Binary::SecureConnectionParams params;
102 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
104 std::unique_ptr<OpcUa::IOChannel> secureChannel = OpcUa::Binary::CreateSecureChannel(connection, params);
116 OpcUa::Binary::SecureConnectionParams params;
118 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
120 std::shared_ptr<OpcUa::IOChannel> secureChannel = OpcUa::Binary::CreateSecureChannel(connection, params);
124 OpcUa::Binary::GetEndpointsRequest request;
125 request.EndpointUrl = params.EndpointUrl;
127 io << request <<
flush;
129 OpcUa::Binary::GetEndpointsResponse response;
139 OpcUa::Binary::GetEndpointsRequest request;
141 stream << request <<
flush;
143 OpcUa::Binary::GetEndpointsResponse response;
146 if (response.Endpoints.empty())
148 throw std::logic_error(
"Server returned empty list of endpoints");
151 return response.Endpoints.front();
154 TEST_F(OpcBinaryHandshake, GetCreateSession)
159 SecureConnectionParams params;
161 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
163 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
166 EndpointDescription endpoint =
GetEndpoint(stream);
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;
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);
180 request.RequestedSessionTimeout = 3600000;
181 request.MaxResponseMessageSize = 65536;
183 stream << request <<
flush;
185 OpcUa::Binary::CreateSessionResponse response;
189 ASSERT_EQ(response.TypeId.FourByteData.NamespaceIndex, 0);
199 EndpointDescription endpoint =
GetEndpoint(stream);
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;
208 request.ServerURI = endpoint.ServerDescription.URI;
209 request.EndpointUrl = endpoint.EndpointUrl;
210 request.SessionName =
"libiocuamappings session test";
211 request.ClientNonce = std::vector<uint8_t>(32, 0);
213 request.RequestedSessionTimeout = 3600000;
214 request.MaxResponseMessageSize = 65536;
216 stream << request <<
flush;
218 OpcUa::Binary::CreateSessionResponse response;
228 SecureConnectionParams params;
230 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
232 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
237 ActivateSessionRequest activate;
238 activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
239 activate.LocaleIds.push_back(
"en");
240 stream << activate <<
flush;
242 ActivateSessionResponse response;
255 SecureConnectionParams params;
257 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
259 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
264 ActivateSessionRequest activate;
265 activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
266 activate.LocaleIds.push_back(
"en");
267 stream << activate <<
flush;
269 ActivateSessionResponse response;
272 CloseSessionRequest closeSession;
273 closeSession.Header.SessionAuthenticationToken = session.AuthenticationToken;
274 stream << closeSession <<
flush;
276 CloseSessionResponse closeResponse;
277 stream >> closeResponse;
286 CloseSessionRequest closeSession;
287 closeSession.Header.SessionAuthenticationToken = session.AuthenticationToken;
288 stream << closeSession <<
flush;
290 CloseSessionResponse closeResponse;
291 stream >> closeResponse;
297 ActivateSessionRequest activate;
298 activate.Header.SessionAuthenticationToken = session.AuthenticationToken;
299 activate.LocaleIds.push_back(
"en");
300 stream << activate <<
flush;
302 ActivateSessionResponse response;
311 SecureConnectionParams params;
313 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
315 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
321 OpcUa::Binary::BrowseRequest browse;
322 browse.Header.SessionAuthenticationToken = session.AuthenticationToken;
323 browse.MaxReferenciesPerNode = 2;
325 BrowseDescription desc;
327 desc.NodeToBrowse.TwoByteData.Identifier = 84;
328 desc.Direction = BrowseDirection::FORWARD;
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);
336 stream << browse <<
flush;
338 BrowseResponse response;
343 BrowseNextRequest browseNext;
344 browseNext.Header.SessionAuthenticationToken = session.AuthenticationToken;
345 browseNext.ReleaseContinuationPoints =
false;
346 browseNext.ContinuationPoints.push_back(response.Results[0].ContinuationPoint);
348 stream << browseNext <<
flush;
350 BrowseNextResponse resp;
364 using namespace OpcUa;
369 SecureConnectionParams params;
371 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
373 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
383 value.Node.FourByteData.Identifier =
static_cast<uint8_t
>(ObjectId::RootFolder);
387 OpcUa::Binary::ReadRequest request;
388 request.Header.SessionAuthenticationToken = session.AuthenticationToken;
390 request.TimestampsType = TimestampsToReturn::Neither;
391 request.AttributesToRead.push_back(value);
393 stream << request <<
flush;
409 using namespace OpcUa;
414 SecureConnectionParams params;
416 params.SecurePolicy =
"http://opcfoundation.org/UA/SecurityPolicy#None";
418 std::shared_ptr<OpcUa::IOChannel> secureChannel = CreateSecureChannel(connection, params);
424 OpcUa::Binary::WriteRequest request;
425 request.Header.SessionAuthenticationToken = session.AuthenticationToken;
429 value.Node.FourByteData.Identifier =
static_cast<uint8_t
>(ObjectId::RootFolder);
432 value.Data.
Value.Type = VariantType::STRING;
433 value.Data.
Value.
Value.String.push_back(
"root");
435 request.NodesToWrite.push_back(value);
437 stream << request <<
flush;
TEST_F(OpcBinaryHandshake, EstablishConnection)
void CloseSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
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)
#define EXPECT_TRUE(condition)
OStream< ChannelType > & flush(OStream< ChannelType > &os)
int Write(int fd, const void *buf, unsigned int count)
OpcUa::Binary::CreateSessionResponse CreateSession(OpcUa::Binary::IOStream &stream)
int Read(int fd, void *buf, unsigned int count)
#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...
void ActivateSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
#define ASSERT_EQ(val1, val2)
std::string GetEndpointUrl()
uint32_t ReceiveBufferSize
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)