VisionaryTMiniDataTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2023 SICK AG, Waldkirch
3 //
4 // SPDX-License-Identifier: Unlicense
5 #include <algorithm>
6 
7 #include "MockTransport.h"
8 #include "VisionaryDataStream.h"
9 #include "VisionaryEndian.h"
10 #include "VisionaryTMiniData.h"
11 #include "gtest/gtest.h"
12 
13 namespace {
14 using ByteBuffer = std::vector<std::uint8_t>;
15 
16 void appendToVector(const ByteBuffer& src, ByteBuffer& dst)
17 {
18  dst.reserve(src.size() + dst.size());
19  dst.insert(dst.end(), src.begin(), src.end());
20 }
21 
22 ByteBuffer uint32ToBEVector(std::uint32_t n)
23 {
24  ByteBuffer retVal;
25  retVal.push_back(static_cast<uint8_t>(n >> 24));
26  retVal.push_back(static_cast<uint8_t>(n >> 16));
27  retVal.push_back(static_cast<uint8_t>(n >> 8));
28  retVal.push_back(static_cast<uint8_t>(n));
29  return retVal;
30 }
31 
32 void setBlobLength(ByteBuffer& blobVector)
33 {
34  auto blobLengthVector = uint32ToBEVector(static_cast<std::uint32_t>(blobVector.size() - 8u));
35  memcpy(&blobVector[4], &blobLengthVector[0], 4u);
36 }
37 
38 const ByteBuffer kMagicBytes = {0x02, 0x02, 0x02, 0x02};
39 const ByteBuffer kProtocolVersion = {0x0, 0x1};
40 const ByteBuffer kPackageType = {0x62u};
41 const ByteBuffer kBlobId = {0x0u, 0x0u};
42 const ByteBuffer kNumSegements = {0x0u, 0x3u};
43 const ByteBuffer kXMLOffset = {0x0u, 0x0u, 0x0u, 0x1Cu};
44 const ByteBuffer kBlobVersion = {0x0u, 0x2u};
45 const std::uint32_t kDataSetSize = 1302528u;
46 const std::string kXMLStr =
47  "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SickRecord xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
48  "xsi:noNamespaceSchemaLocation=\"SickRecord_schema.xsd\"><Revision>SICK V1.10 in "
49  "work</Revision><SchemaChecksum>01020304050607080910111213141516</SchemaChecksum><ChecksumFile>checksum.hex</"
50  "ChecksumFile><RecordDescription><Location>V3SXX5-1</Location><StartDateTime>2023-03-31T11:09:33+02:00</"
51  "StartDateTime><EndDateTime>2023-03-31T11:09:37+02:00</EndDateTime><UserName>default</UserName><RecordToolName>Sick "
52  "Scandata "
53  "Recorder</RecordToolName><RecordToolVersion>v0.4</RecordToolVersion><ShortDescription></ShortDescription></"
54  "RecordDescription><DataSets><DataSetDepthMap id=\"1\" "
55  "datacount=\"1\"><DeviceDescription><Family>V3SXX5-1</Family><Ident>Visionary-T Mini CX V3S105-1x "
56  "2.0.0.457B</Ident><Version>3.0.0.2334</Version><SerialNumber>12345678</SerialNumber><LocationName>not "
57  "defined</LocationName><IPAddress>192.168.136.10</IPAddress></"
58  "DeviceDescription><FormatDescriptionDepthMap><TimestampUTC/><Version>uint16</"
59  "Version><DataStream><Interleaved>false</Interleaved><Width>512</Width><Height>424</"
60  "Height><CameraToWorldTransform><value>1.000000</value><value>0.000000</value><value>0.000000</"
61  "value><value>0.000000</value><value>0.000000</value><value>1.000000</value><value>0.000000</value><value>0.000000</"
62  "value><value>0.000000</value><value>0.000000</value><value>1.000000</value><value>-10.000000</"
63  "value><value>0.000000</value><value>0.000000</value><value>0.000000</value><value>1.000000</value></"
64  "CameraToWorldTransform><CameraMatrix><FX>-366.964999</FX><FY>-367.057999</FY><CX>252.118999</CX><CY>205.213999</"
65  "CY></CameraMatrix><CameraDistortionParams><K1>-0.076050</K1><K2>0.217518</K2><P1>0.000000</P1><P2>0.000000</"
66  "P2><K3>0.000000</K3></CameraDistortionParams><FrameNumber>uint32</FrameNumber><Quality>uint8</"
67  "Quality><Status>uint8</Status><PixelSize><X>1.000000</X><Y>1.000000</Y><Z>0.250000</Z></PixelSize><Distance "
68  "decimalexponent=\"0\" min=\"1\" max=\"16384\">uint16</Distance><Intensity decimalexponent=\"0\" min=\"1\" "
69  "max=\"20000\">uint16</Intensity><Confidence decimalexponent=\"0\" min=\"0\" "
70  "max=\"65535\">uint16</Confidence></DataStream><DeviceInfo><Status>OK</Status></DeviceInfo></"
71  "FormatDescriptionDepthMap><DataLink><FileName>data.bin</FileName><Checksum>01020304050607080910111213141516</"
72  "Checksum></DataLink><OverlayLink><FileName>overlay.xml</FileName></OverlayLink></DataSetDepthMap></DataSets></"
73  "SickRecord>";
74 const ByteBuffer kXMLVec(kXMLStr.begin(), kXMLStr.end());
75 } // namespace
76 
77 using namespace visionary;
78 
79 //---------------------------------------------------------------------------------------
80 TEST(VisionaryTMiniDataTest, InvalidMagicBytes)
81 {
82  ByteBuffer buffer{kMagicBytes};
83  buffer[3] = 0x01;
84  buffer.insert(buffer.end(), 5000, 0);
85 
86  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
87  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
88 
89  dataStream.open(pTransport);
90 
91  ASSERT_FALSE(dataStream.getNextFrame());
92 }
93 
94 //---------------------------------------------------------------------------------------
95 TEST(VisionaryTMiniDataTest, MissingHeader)
96 {
97  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{kMagicBytes}};
98  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
99 
100  dataStream.open(pTransport);
101 
102  ASSERT_FALSE(dataStream.getNextFrame());
103 }
104 
105 //---------------------------------------------------------------------------------------
106 TEST(VisionaryTMiniDataTest, WrongHeader)
107 {
108  ByteBuffer buffer{kMagicBytes};
109  ByteBuffer length = {0x0, 0x0, 0x0, 0x2};
110  ByteBuffer wrongProtocol = {0x0, 0x0};
111  appendToVector(length, buffer);
112 
113  {
114  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
115  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
116 
117  dataStream.open(pTransport);
118 
119  EXPECT_FALSE(dataStream.getNextFrame());
120  }
121 
122  // Wrong Protocol
123  {
124  buffer = kMagicBytes;
125  length = {0x0, 0x0, 0x0, 0x3};
126  appendToVector(length, buffer);
127  appendToVector(wrongProtocol, buffer);
128  appendToVector(kPackageType, buffer);
129 
130  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
131  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
132 
133  dataStream.open(pTransport);
134 
135  EXPECT_FALSE(dataStream.getNextFrame());
136  }
137 
138  // Wrong Package Type
139  {
140  buffer = kMagicBytes;
141  length = {0x0, 0x0, 0x0, 0x3};
142  appendToVector(length, buffer);
143  appendToVector(kProtocolVersion, buffer);
144  buffer.push_back(0x61);
145 
146  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
147  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
148 
149  dataStream.open(pTransport);
150 
151  EXPECT_FALSE(dataStream.getNextFrame());
152  }
153 }
154 
155 //---------------------------------------------------------------------------------------
156 TEST(VisionaryTMiniDataTest, NoDataHandler)
157 {
158  ByteBuffer buffer{kMagicBytes};
159  ByteBuffer length = {0x0, 0x0, 0xFFu, 0xFFu};
160  appendToVector(length, buffer);
161  appendToVector(kProtocolVersion, buffer);
162  appendToVector(kPackageType, buffer);
163  // Append dummy data
164  buffer.insert(buffer.end(), 65536u + 3u, 0u);
165 
166  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
167  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
168 
169  dataStream.setDataHandler(nullptr);
170  dataStream.open(pTransport);
171  EXPECT_FALSE(dataStream.getNextFrame());
172 }
173 
174 //---------------------------------------------------------------------------------------
175 TEST(VisionaryTMiniDataTest, InvalidBlobData)
176 {
177  ByteBuffer buffer{kMagicBytes};
178  ByteBuffer length = {0x0u, 0x0u, 0xFFu, 0xFFu};
179  appendToVector(length, buffer);
180  appendToVector(kProtocolVersion, buffer);
181  appendToVector(kPackageType, buffer);
182  ByteBuffer bufferBase = buffer;
183  // Append dummy data
184  buffer.insert(buffer.end(), 65536u + 3u, 0u);
185  // Invalid segment count
186  {
187  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
188  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
189 
190  dataStream.open(pTransport);
191  EXPECT_FALSE(dataStream.getNextFrame());
192  }
193  // Corrupted XML part
194  buffer = bufferBase;
195  appendToVector(kBlobId, buffer);
196  appendToVector(kNumSegements, buffer);
197  appendToVector(kXMLOffset, buffer);
198  buffer.insert(buffer.end(), 3u, 0x0u);
199  buffer.push_back(0x1u); // set change counter to 1
200  std::uint32_t binaryOffset = static_cast<std::uint32_t>(kXMLVec.size() + 28u);
201  const auto binaryOffsetVec = uint32ToBEVector(binaryOffset);
202  appendToVector(binaryOffsetVec, buffer);
203  buffer.insert(buffer.end(), 4u, 0x0u);
204  const auto footerOffsetVec = uint32ToBEVector(binaryOffset + kDataSetSize + 4u + 8u + 2u + 6u + 8u);
205  appendToVector(footerOffsetVec, buffer);
206  buffer.insert(buffer.end(), 4u, 0x0u);
207  appendToVector(kXMLVec, buffer);
208  const auto bufferWithXMLBase = buffer;
209  buffer.erase(buffer.end() - 10, buffer.end());
210  setBlobLength(buffer);
211 
212  {
213  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
214  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
215 
216  dataStream.open(pTransport);
217  EXPECT_FALSE(dataStream.getNextFrame());
218  }
219 
220  {
221  // Corrupted Binary part
222  buffer = bufferWithXMLBase;
223  ByteBuffer binLengthVec = uint32ToBEVector(kDataSetSize);
224  std::reverse(binLengthVec.begin(), binLengthVec.end());
225  appendToVector(binLengthVec, buffer);
226  buffer.insert(buffer.end(), 8u, 0x0u); // Timestamp
227  appendToVector(kBlobVersion, buffer);
228  buffer.insert(buffer.end(), 6u, 0x0u); // Extended Header
229  buffer.insert(buffer.end(), kDataSetSize, 0x0u); // Add Image Data (4 bytes(CRC) are missing)
230  setBlobLength(buffer);
231 
232  {
233  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
234  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
235 
236  dataStream.open(pTransport);
237  EXPECT_FALSE(dataStream.getNextFrame());
238  }
239  }
240 }
241 
242 //---------------------------------------------------------------------------------------
243 TEST(VisionaryTMiniDataTest, ValidBlobData)
244 {
245  ByteBuffer buffer{kMagicBytes};
246  ByteBuffer length = {0x0u, 0x0u, 0x00u, 0x00u};
247  appendToVector(length, buffer);
248  appendToVector(kProtocolVersion, buffer);
249  appendToVector(kPackageType, buffer);
250  appendToVector(kBlobId, buffer);
251  appendToVector(kNumSegements, buffer);
252  appendToVector(kXMLOffset, buffer);
253  buffer.insert(buffer.end(), 3u, 0x0u);
254  buffer.push_back(0x1u); // set change counter to 1
255  std::uint32_t binaryOffset = static_cast<std::uint32_t>(kXMLVec.size() + 28u);
256  const auto binaryOffsetVec = uint32ToBEVector(binaryOffset);
257  appendToVector(binaryOffsetVec, buffer);
258  buffer.insert(buffer.end(), 4u, 0x0u);
259  const auto footerOffsetVec = uint32ToBEVector(binaryOffset + kDataSetSize + 4u + 8u + 2u + 6u + 8u);
260  appendToVector(footerOffsetVec, buffer);
261  buffer.insert(buffer.end(), 4u, 0x0u);
262  appendToVector(kXMLVec, buffer);
263  ByteBuffer binLengthVec = uint32ToBEVector(kDataSetSize);
264  std::reverse(binLengthVec.begin(), binLengthVec.end());
265  appendToVector(binLengthVec, buffer);
266  buffer.insert(buffer.end(), 8u, 0x0u); // Timestamp
267  appendToVector(kBlobVersion, buffer);
268  buffer.insert(buffer.end(), 6u, 0x0u); // Extended Header
269  buffer.insert(buffer.end(), kDataSetSize, 0x0u); // Add Image Data
270  buffer.insert(buffer.end(), 4u, 0x0u); // CRC
271  appendToVector(binLengthVec, buffer);
272  setBlobLength(buffer);
273 
274  std::unique_ptr<ITransport> pTransport{new visionary_test::MockTransport{buffer}};
275  VisionaryDataStream dataStream{std::make_shared<VisionaryTMiniData>()};
276 
277  dataStream.open(pTransport);
278  EXPECT_TRUE(dataStream.getNextFrame());
279 }
VisionaryEndian.h
visionary
Definition: MD5.cpp:44
VisionaryDataStream.h
visionary::VisionaryDataStream::setDataHandler
void setDataHandler(std::shared_ptr< VisionaryData > dataHandler)
Definition: VisionaryDataStream.cpp:213
MockTransport.h
visionary_test::MockTransport
Definition: MockTransport.h:98
VisionaryTMiniData.h
length
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
visionary::VisionaryDataStream
Definition: VisionaryDataStream.h:14
TEST
TEST(VisionaryTMiniDataTest, InvalidMagicBytes)
Definition: VisionaryTMiniDataTest.cpp:80
visionary_test::ByteBuffer
std::vector< std::uint8_t > ByteBuffer
Definition: MockTransport.h:18
visionary::VisionaryDataStream::open
bool open(const std::string &hostname, std::uint16_t port, std::uint32_t timeoutMs=5000u)
Definition: VisionaryDataStream.cpp:27


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:56:19