forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_align_buffer_test.cpp
More file actions
241 lines (186 loc) · 9.82 KB
/
Copy pathstring_align_buffer_test.cpp
File metadata and controls
241 lines (186 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include "common_test_utils/test_assertions.hpp"
#include "openvino/runtime/string_aligned_buffer.hpp"
namespace ov::test {
using testing::HasSubstr;
using StringAlignedBufferTest = testing::Test;
using header_element_t = int32_t; // Type of a single element in the header (strings_count and offsets)
namespace {
/// @brief Creates a packed string tensor byte stream from given header and strings data.
/// @param header The header elements of the string tensor.
/// @param strings The string data of the tensor.
/// @return A vector of bytes representing the packed string tensor.
std::vector<uint8_t> pack_string_tensor(const std::vector<header_element_t>& header,
const std::vector<uint8_t>& strings = {}) {
std::vector<uint8_t> tensor;
tensor.reserve(header.size() * sizeof(header_element_t) + strings.size());
for (const auto value : header) {
const uint8_t* characters = reinterpret_cast<const uint8_t*>(&value);
tensor.insert(tensor.end(), characters, characters + sizeof(header_element_t));
}
tensor.insert(tensor.end(), strings.begin(), strings.end());
return tensor;
}
/// @brief Introduces invalid elements count in the header of the packed string tensor to trigger error handling in
/// unpacking function.
/// @param tensor The packed string tensor.
/// @param elements_count The invalid elements count to be set in the header.
void tamper_with_elements_count(std::vector<uint8_t>& tensor, const header_element_t elements_count) {
constexpr size_t elements_count_index = 0;
const uint8_t* characters = reinterpret_cast<const uint8_t*>(&elements_count);
std::copy(characters, characters + sizeof(header_element_t), tensor.begin() + elements_count_index);
}
} // namespace
TEST_F(StringAlignedBufferTest, default_ctor) {
StringAlignedBuffer buffer;
ASSERT_EQ(buffer.get_ptr(), nullptr);
EXPECT_EQ(buffer.get_num_elements(), 0);
EXPECT_EQ(buffer.size(), 0);
}
TEST_F(StringAlignedBufferTest, create_not_initialized_but_init_before_destruction) {
constexpr size_t exp_size = 4 * sizeof(std::string) + 1;
StringAlignedBuffer buffer{4, exp_size, 64, false};
ASSERT_NE(buffer.get_ptr(), nullptr);
EXPECT_EQ(buffer.get_num_elements(), 4);
EXPECT_EQ(buffer.size(), exp_size);
// uninitialized buffer must be initialized by user before dtor call to avoid segfault
std::uninitialized_fill_n(buffer.get_ptr<std::string>(), buffer.get_num_elements(), std::string{});
}
TEST_F(StringAlignedBufferTest, create_initialized) {
constexpr size_t exp_size = sizeof(std::string) * 5;
StringAlignedBuffer buffer(5, exp_size, 8, true);
ASSERT_NE(buffer.get_ptr(), nullptr);
EXPECT_EQ(buffer.get_num_elements(), 5);
EXPECT_EQ(buffer.size(), exp_size);
}
TEST_F(StringAlignedBufferTest, create_initialized_not_enough_space) {
constexpr size_t exp_size = sizeof(std::string) * 5;
OV_EXPECT_THROW(StringAlignedBuffer buffer(5, exp_size - 1, 8, true),
AssertFailure,
HasSubstr("is not enough to store 5 std::string objects"));
}
TEST_F(StringAlignedBufferTest, create_not_initialized_not_enough_space) {
constexpr size_t exp_size = sizeof(std::string) * 5;
OV_EXPECT_THROW(StringAlignedBuffer buffer(5, exp_size - 1, 8, false),
AssertFailure,
HasSubstr("is not enough to store 5 std::string objects"));
}
class SharedStringAlignedBufferTest : public testing::Test {
protected:
const size_t exp_size = 11 * sizeof(std::string);
const std::string msg_at_0 = "test input message at index 0";
StringAlignedBuffer buffer{11, exp_size, 0, true};
};
TEST_F(SharedStringAlignedBufferTest, dtor_not_destroy_input_buffer) {
*buffer.get_ptr<std::string>() = msg_at_0;
{
SharedStringAlignedBuffer shared_buffer(buffer.get_ptr<char>(), buffer.size());
ASSERT_EQ(shared_buffer.get_ptr(), buffer.get_ptr());
EXPECT_EQ(shared_buffer.get_num_elements(), buffer.get_num_elements());
EXPECT_EQ(shared_buffer.size(), exp_size);
EXPECT_EQ(shared_buffer.get_ptr<const std::string>()[0], msg_at_0);
}
ASSERT_NE(buffer.get_ptr(), nullptr);
EXPECT_EQ(buffer.get_num_elements(), 11);
EXPECT_EQ(buffer.size(), exp_size);
EXPECT_EQ(buffer.get_ptr<const std::string>()[0], msg_at_0);
}
TEST_F(SharedStringAlignedBufferTest, create_with_smaller_size_than_input_buffer) {
buffer.get_ptr<std::string>()[0] = msg_at_0;
const SharedStringAlignedBuffer shared_buffer{buffer.get_ptr<char>(), sizeof(std::string)};
ASSERT_EQ(shared_buffer.get_ptr(), buffer.get_ptr());
EXPECT_EQ(shared_buffer.get_num_elements(), 1);
EXPECT_EQ(shared_buffer.size(), sizeof(std::string));
EXPECT_EQ(shared_buffer.get_ptr<std::string>()[0], msg_at_0);
ASSERT_NE(buffer.get_ptr(), nullptr);
EXPECT_EQ(*buffer.get_ptr<const std::string>(), msg_at_0);
}
TEST(StringUnpackTensorTest, ZeroNumberOfStringsYieldsEmptyBuffer) {
constexpr auto strings_count = 0;
constexpr auto last_end = 0;
const auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, last_end});
const auto result = AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size());
EXPECT_EQ(result->get_num_elements(), 0);
}
TEST(StringUnpackTensorTest, MissingNumberOfStringsFails) {
const std::vector<uint8_t> strings = {'0', '1'};
const auto tensor = pack_string_tensor(std::vector<header_element_t>{}, strings);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("no strings count in the packed string tensor"));
}
TEST(StringUnpackTensorTest, NegativeNumberOfStringsFails) {
constexpr auto strings_count = -1;
constexpr auto last_end = 0;
const auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, last_end});
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("negative number of strings"));
}
TEST(StringUnpackTensorTest, HeaderSizeOverflowFails) {
constexpr auto strings_count = 1;
constexpr auto strings_count_tampered = std::numeric_limits<header_element_t>::max();
if (strings_count_tampered < std::numeric_limits<size_t>::max() / sizeof(header_element_t)) {
GTEST_SKIP() << "Header size calculation does not overflow on this platform";
}
const std::vector<uint8_t> strings = {'0', '1', '2', '3', '4'};
auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, 0, 5}, strings);
tamper_with_elements_count(tensor, strings_count_tampered);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("header size overflow detected"));
}
TEST(StringUnpackTensorTest, HeaderSizeBeyondBufferFails) {
constexpr auto strings_count = 2;
constexpr auto strings_count_tampered = 10;
const std::vector<uint8_t> strings = {'0', '1', '2', '3', '4'};
auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, 0, 3, 5}, strings);
tamper_with_elements_count(tensor, strings_count_tampered);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("header exceeds provided buffer size"));
}
TEST(StringUnpackTensorTest, NegativeOffsetsFails) {
constexpr auto strings_count = 2;
const std::vector<uint8_t> strings = {'0', '1', '2', '3', '4'};
const auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, -3, 2, 5}, strings);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("negative string offset in the packed string tensor"));
}
TEST(StringUnpackTensorTest, DecreasingOffsetsFails) {
constexpr auto strings_count = 2;
const std::vector<uint8_t> strings = {'0', '1', '2', '3', '4'};
const auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, 0, 5, 3}, strings);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("begin offset greater than end offset"));
}
TEST(StringUnpackTensorTest, OffsetBeyondBufferFails) {
constexpr auto strings_count = 2;
const std::vector<uint8_t> strings = {'0', '1', '2', '3', '4'};
const auto tensor = pack_string_tensor(std::vector<header_element_t>{strings_count, 0, 10, 20}, strings);
OV_EXPECT_THROW(AttributeAdapter<std::shared_ptr<StringAlignedBuffer>>::unpack_string_tensor(
reinterpret_cast<const char*>(tensor.data()),
tensor.size()),
AssertFailure,
HasSubstr("string offset exceeds buffer bounds"));
}
} // namespace ov::test