tests/protocol/common.h
Go to the documentation of this file.
1 
11 #ifndef __OPC_UA_TESTS_COMMON_H__
12 #define __OPC_UA_TESTS_COMMON_H__
13 
14 //#include <gmock/gmock.h>
15 #include <gtest/gtest.h>
16 
21 #include <opc/ua/protocol/types.h>
23 
24 #include <algorithm>
25 #include <stdexcept>
26 #include <stdlib.h>
27 #include <string>
28 
29 inline std::string PrintData(const std::vector<char> & vec)
30 {
31  std::stringstream stream;
32 
33  for (std::size_t i = 0; i < vec.size(); ++i)
34  {
35  stream << "0x" << std::setfill('0') << std::setw(2) << std::hex << ((int)vec[i] & 0xff) << ' ';
36 
37  if ((i + 1) % 4 == 0)
38  {
39  stream << ' ';
40  }
41 
42  if ((i + 1) % 32 == 0)
43  {
44  stream << std::endl;
45  }
46  }
47 
48  return stream.str();
49 }
50 
52 {
53  if (const char * host = getenv("OPCUA_HOST"))
54  {
55  return host;
56  }
57 
58  return "localhost";
59 }
60 
61 inline int GetPort()
62 {
63  if (char * port = getenv("OPCUA_PORT"))
64  {
65  return atoi(port);
66  }
67 
68  return 4841;
69 }
70 
72 {
73  if (char * endpoint = getenv("OPCUA_ENDPOINT"))
74  {
75  return endpoint;
76  }
77 
78  return "opc.tcp://localhost:4841";
79 }
80 
82 {
83 public:
84  virtual void Send(const char * data, std::size_t size)
85  {
86  SerializedData = std::vector<char>(data, data + size);
87  }
88 
89  virtual ~OutputChannel()
90  {
91  }
92 
93  std::vector<char> SerializedData;
94 };
95 
97 
99 {
100 public:
102  : Channel(new OutputChannel)
103  , Stream(new BinaryOStream(Channel))
104  {
105  }
106 
107 protected:
108  virtual void SetUp()
109  {
110  }
111 
112  virtual void TearDown()
113  {
114  }
115 
117  {
118  return *Channel;
119  }
120 
122  {
123  return *Stream;
124  }
125 
126 private:
127  std::shared_ptr<OutputChannel> Channel;
128  std::unique_ptr<BinaryOStream> Stream;
129 };
130 
132 {
133 public:
135  {
136  CurPos = SerializedData.begin();
137  }
138 
139  virtual ~InputChannel()
140  {
141  }
142 
143  virtual std::size_t Receive(char * data, std::size_t size)
144  {
145  if (CurPos == SerializedData.end())
146  {
147  return 0;
148  }
149 
150  const std::size_t dist = static_cast<std::size_t>(std::distance(CurPos, SerializedData.end()));
151  const std::size_t minSize = std::min(size, dist);
152  std::copy(CurPos, CurPos + minSize, data);
153  CurPos += minSize;
154  return minSize;
155  }
156 
157  void SetData(const std::vector<char> & data)
158  {
159  SerializedData = data;
160  CurPos = SerializedData.begin();
161  }
162 
163  bool IsEmpty() const
164  {
165  return CurPos == SerializedData.end();
166  }
167 
168 private:
169  std::vector<char> SerializedData;
170  std::vector<char>::iterator CurPos;
171 };
172 
173 
175 
177 {
178 public:
180  : Channel(new InputChannel)
181  , Stream(new BinaryIStream(Channel))
182  {
183  }
184 
185 protected:
186  virtual void SetUp()
187  {
188  }
189 
190  virtual void TearDown()
191  {
192  }
193 
195  {
196  return *Channel;
197  }
198 
200  {
201  return *Stream;
202  }
203 
204 protected:
205  std::shared_ptr<InputChannel> Channel;
206  std::unique_ptr<BinaryIStream> Stream;
207 };
208 
209 
210 
211 
212 #define TEST_RESPONSE_HEADER_BINARY_DATA \
213  1,0,0,0,0,0,0,0, \
214  2,0,0,0, \
215  3,0,0,0, \
216  static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT | DIM_INNER_DIAGNOSTIC_INFO), 4,0,0,0, \
217  DIM_ADDITIONAL_INFO, 3, 0, 0, 0, 'a', 'd', 'd', \
218  2,0,0,0, 3,0,0,0, 's','t','r', 3,0,0,0, 's','t','r', \
219  EV_TWO_BYTE, 7, \
220  8
221 
222 #define FILL_TEST_RESPONSE_HEADER(header) \
223  header.Timestamp.Value = 1; \
224  header.RequestHandle = 2; \
225  header.ServiceResult = static_cast<StatusCode>(3); \
226  header.InnerDiagnostics.EncodingMask = static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT | DIM_INNER_DIAGNOSTIC_INFO); \
227  header.InnerDiagnostics.LocalizedText = 4; \
228  header.InnerDiagnostics.InnerDiagnostics.reset(new DiagnosticInfo()); \
229  header.InnerDiagnostics.InnerDiagnostics->EncodingMask = DIM_ADDITIONAL_INFO; \
230  header.InnerDiagnostics.InnerDiagnostics->AdditionalInfo = "add"; \
231  header.StringTable = std::vector<std::string>(2, std::string("str")); \
232  header.Additional.TypeId.Encoding = EV_TWO_BYTE; \
233  header.Additional.TypeId.TwoByteData.Identifier = 7; \
234  header.Additional.Encoding = 8;
235 
236 #define ASSERT_RESPONSE_HEADER_EQ(header) \
237  ASSERT_EQ(header.Timestamp.Value, 1); \
238  ASSERT_EQ(header.RequestHandle, 2); \
239  ASSERT_EQ(header.ServiceResult, static_cast<StatusCode>(3)); \
240  ASSERT_EQ(header.InnerDiagnostics.EncodingMask, static_cast<DiagnosticInfoMask>(DIM_LOCALIZED_TEXT | DIM_INNER_DIAGNOSTIC_INFO)); \
241  ASSERT_EQ(header.InnerDiagnostics.LocalizedText, 4); \
242  ASSERT_TRUE(static_cast<bool>(header.InnerDiagnostics.InnerDiagnostics)); \
243  ASSERT_EQ(header.InnerDiagnostics.InnerDiagnostics->EncodingMask, DIM_ADDITIONAL_INFO); \
244  ASSERT_EQ(header.InnerDiagnostics.InnerDiagnostics->AdditionalInfo, "add"); \
245  ASSERT_EQ(header.StringTable, std::vector<std::string>(2, std::string("str"))); \
246  ASSERT_EQ(header.Additional.TypeId.Encoding, EV_TWO_BYTE); \
247  ASSERT_EQ(header.Additional.TypeId.TwoByteData.Identifier, 7); \
248  ASSERT_EQ(header.Additional.Encoding, 8);
249 
250 
251 #define TEST_REQUEST_HEADER_BINARY_DATA \
252  EV_TWO_BYTE, 1, \
253  2, 0, 0, 0, 0, 0, 0, 0, \
254  3, 0, 0, 0, \
255  4, 0, 0, 0, \
256  5, 0, 0, 0, \
257  'a', 'u', 'd', 'i', 't', \
258  5, 0, 0, 0, \
259  0, \
260  6, \
261  8
262 
263 #define FILL_TEST_REQUEST_HEADER(header) \
264  header.SessionAuthenticationToken.Encoding = EV_TWO_BYTE; \
265  header.SessionAuthenticationToken.TwoByteData.Identifier = 1; \
266  header.UtcTime.Value = 2; \
267  header.RequestHandle = 3; \
268  header.ReturnDiagnostics = 4; \
269  header.AuditEntryId = "audit"; \
270  header.Timeout = 5; \
271  header.Additional.TypeId.Encoding = EV_TWO_BYTE; \
272  header.Additional.TypeId.TwoByteData.Identifier = 6; \
273  header.Additional.Encoding = 8;
274 
275 #define ASSERT_REQUEST_HEADER_EQ(header) \
276  ASSERT_EQ(header.SessionAuthenticationToken.Encoding, EV_TWO_BYTE); \
277  ASSERT_EQ(header.SessionAuthenticationToken.TwoByteData.Identifier, 1); \
278  ASSERT_EQ(header.UtcTime.Value, 2); \
279  ASSERT_EQ(header.RequestHandle, 3); \
280  ASSERT_EQ(header.ReturnDiagnostics, 4); \
281  ASSERT_EQ(header.AuditEntryId, "audit"); \
282  ASSERT_EQ(header.Timeout, 5); \
283  ASSERT_EQ(header.Additional.TypeId.Encoding, EV_TWO_BYTE); \
284  ASSERT_EQ(header.Additional.TypeId.TwoByteData.Identifier, 6); \
285  ASSERT_EQ(header.Additional.Encoding, 8);
286 
287 
288 #define FILL_APPLICATION_DESCRIPTION(desc) \
289  desc.ApplicationUri = "u"; \
290  desc.ProductUri = "pu"; \
291  desc.ApplicationName.Encoding = HAS_LOCALE | HAS_TEXT; \
292  desc.ApplicationName.Locale = "RU"; \
293  desc.ApplicationName.Text = "text"; \
294  desc.ApplicationType = ApplicationType::Client; \
295  desc.GatewayServerUri = "gw"; \
296  desc.DiscoveryProfileUri = "dpu"; \
297  desc.DiscoveryUrls.push_back("du");
298 
299 #define TEST_APPLICATION_DESCRIPTION_BINARY_DATA \
300  1,0,0,0, 'u', \
301  2,0,0,0, 'p','u', \
302  3, \
303  2,0,0,0, 'R','U', \
304  4,0,0,0, 't','e','x','t', \
305  1,0,0,0, \
306  2,0,0,0, 'g','w', \
307  3,0,0,0, 'd','p','u', \
308  1,0,0,0, 2,0,0,0, 'd','u'
309 
310 #define ASSERT_APPLICATION_DESCRIPTION_EQ(desc) \
311  ASSERT_EQ(desc.ApplicationUri, "u"); \
312  ASSERT_EQ(desc.ProductUri, "pu"); \
313  ASSERT_EQ(desc.ApplicationName.Encoding, HAS_LOCALE | HAS_TEXT); \
314  ASSERT_EQ(desc.ApplicationName.Locale, "RU"); \
315  ASSERT_EQ(desc.ApplicationName.Text, "text"); \
316  ASSERT_EQ(desc.ApplicationType, ApplicationType::Client); \
317  ASSERT_EQ(desc.GatewayServerUri, "gw"); \
318  ASSERT_EQ(desc.DiscoveryProfileUri, "dpu"); \
319  ASSERT_EQ(desc.DiscoveryUrls, std::vector<std::string>(1,"du"));
320 
321 #define FILL_TEST_ENDPOINT(endpoint) \
322  endpoint.EndpointUrl = "eu"; \
323  FILL_APPLICATION_DESCRIPTION(endpoint.Server); \
324  endpoint.ServerCertificate = ByteString(std::vector<uint8_t>{1,2,3,4}); \
325  endpoint.SecurityMode = MessageSecurityMode::None; \
326  endpoint.SecurityPolicyUri = "spu"; \
327  UserTokenPolicy token; \
328  token.PolicyId = "pi"; \
329  token.TokenType = UserTokenType::UserName; \
330  token.IssuedTokenType = "itt"; \
331  token.IssuerEndpointUrl = "ieu"; \
332  token.SecurityPolicyUri = "spu"; \
333  endpoint.UserIdentityTokens.push_back(token); \
334  endpoint.TransportProfileUri = "tpu"; \
335  endpoint.SecurityLevel = 3;
336 
337 #define TEST_ENDPOINT_BINARY_DATA \
338  2,0,0,0, 'e','u', \
339  TEST_APPLICATION_DESCRIPTION_BINARY_DATA, \
340  4,0,0,0, 1,2,3,4, \
341  1,0,0,0, \
342  3,0,0,0, 's','p','u', \
343  1,0,0,0, \
344  2,0,0,0, 'p','i', \
345  1,0,0,0, \
346  3,0,0,0, 'i','t','t', \
347  3,0,0,0, 'i','e','u', \
348  3,0,0,0, 's','p','u', \
349  3,0,0,0, 't','p','u', \
350  3
351 
352 #define ASSERT_ENDPOINT_EQ(e) \
353  ASSERT_EQ(e.EndpointUrl, "eu"); \
354  ASSERT_APPLICATION_DESCRIPTION_EQ(e.Server); \
355  const ByteString certificate = ByteString(std::vector<uint8_t>{1,2,3,4}); \
356  ASSERT_EQ(e.ServerCertificate, certificate); \
357  ASSERT_EQ(e.SecurityMode, MessageSecurityMode::None); \
358  ASSERT_EQ(e.SecurityPolicyUri, "spu"); \
359  ASSERT_EQ(e.UserIdentityTokens.size(), 1); \
360  ASSERT_EQ(e.UserIdentityTokens[0].PolicyId, "pi"); \
361  ASSERT_EQ(e.UserIdentityTokens[0].TokenType, UserTokenType::UserName); \
362  ASSERT_EQ(e.UserIdentityTokens[0].IssuedTokenType, "itt"); \
363  ASSERT_EQ(e.UserIdentityTokens[0].IssuerEndpointUrl, "ieu"); \
364  ASSERT_EQ(e.UserIdentityTokens[0].SecurityPolicyUri, "spu"); \
365  ASSERT_EQ(e.TransportProfileUri, "tpu"); \
366  ASSERT_EQ(e.SecurityLevel, 3);
367 
368 #endif // __OPC_UA_TESTS_COMMON_H__
369 
std::vector< char >::iterator CurPos
std::string GetHost()
std::vector< char > SerializedData
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
std::string PrintData(const std::vector< char > &vec)
Test of opc ua binary handshake. GNU LGPL.
OpcUa::Binary::IStream< InputChannel > BinaryIStream
virtual void Send(const char *data, std::size_t size)
std::unique_ptr< BinaryOStream > Stream
virtual std::size_t Receive(char *data, std::size_t size)
OpcUa::Binary::OStream< OutputChannel > BinaryOStream
std::shared_ptr< OutputChannel > Channel
int GetPort()
bool IsEmpty() const
std::shared_ptr< InputChannel > Channel
std::vector< char > SerializedData
void SetData(const std::vector< char > &data)
std::string GetEndpoint()
std::unique_ptr< BinaryIStream > Stream


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