|
20 | 20 | #include <chrono>
|
21 | 21 | #include <memory>
|
22 | 22 |
|
| 23 | +#include <boost/uuid/detail/sha1.hpp> |
| 24 | +#include <boost/algorithm/hex.hpp> |
| 25 | + |
23 | 26 | #include <gtest/gtest.h>
|
24 | 27 | #include <olp/core/client/HRN.h>
|
25 | 28 | #include <olp/core/logging/Log.h>
|
26 | 29 | #include <olp/dataservice/read/VersionedLayerClient.h>
|
27 | 30 |
|
28 | 31 | #include "MemoryTestBase.h"
|
29 | 32 |
|
| 33 | +std::string ComputeSha1(const char* buffer, size_t length) { |
| 34 | + using boost::uuids::detail::sha1; |
| 35 | + sha1 hash; |
| 36 | + sha1::digest_type digest; |
| 37 | + sha1::digest_type digest_rotated; |
| 38 | + |
| 39 | + hash.process_bytes(buffer, length); |
| 40 | + hash.get_digest(digest); |
| 41 | + |
| 42 | + // rotate bytes |
| 43 | + for (int i = 0; i < 5; ++i) |
| 44 | + { |
| 45 | + const char* tmp = reinterpret_cast<char*>(&digest); |
| 46 | + char* res = reinterpret_cast<char*>(&digest_rotated); |
| 47 | + res[i*4] = tmp[i*4+3]; |
| 48 | + res[i*4+1] = tmp[i*4+2]; |
| 49 | + res[i*4+2] = tmp[i*4+1]; |
| 50 | + res[i*4+3] = tmp[i*4]; |
| 51 | + } |
| 52 | + |
| 53 | + const auto charDigest = reinterpret_cast<const char*>(&digest_rotated); |
| 54 | + std::string result; |
| 55 | + boost::algorithm::hex(charDigest, charDigest + sizeof(sha1::digest_type), |
| 56 | + std::back_inserter(result)); |
| 57 | + return result; |
| 58 | +} |
| 59 | + |
| 60 | +bool Validate(const std::vector<unsigned char>& buffer) { |
| 61 | + // 40 first bytes is a string representation of hash |
| 62 | + const char* bytes = reinterpret_cast<const char*>( buffer.data() ); |
| 63 | + std::string hash(bytes, bytes + 40); |
| 64 | + std::string computed_hash = ComputeSha1(bytes + 40, buffer.size() - 40); |
| 65 | + OLP_SDK_LOG_CRITICAL_INFO_F("HASH", "HASHES: %s %s", hash.c_str(), computed_hash.c_str()); |
| 66 | + return hash == computed_hash; |
| 67 | +} |
| 68 | + |
30 | 69 | namespace {
|
31 | 70 | using TestFunction = std::function<void(uint8_t thread_id)>;
|
32 | 71 |
|
@@ -183,53 +222,9 @@ TEST_P(MemoryTest, ReadNPartitionsFromVersionedLayer) {
|
183 | 222 | request, [&](olp::dataservice::read::DataResponse response) {
|
184 | 223 | if (response.IsSuccessful()) {
|
185 | 224 | success_responses_.fetch_add(1);
|
186 |
| - } else { |
187 |
| - failed_responses_.fetch_add(1); |
188 |
| - ReportError(response.GetError()); |
189 |
| - } |
190 |
| - }); |
191 |
| - |
192 |
| - RandomlyCancel(std::move(token)); |
193 |
| - |
194 |
| - std::this_thread::sleep_for( |
195 |
| - GetSleepPeriod(parameter.requests_per_second)); |
196 |
| - } |
197 |
| - }); |
198 |
| -} |
199 |
| - |
200 |
| -TEST_P(MemoryTest, PrefetchPartitionsFromVersionedLayer) { |
201 |
| - // Enable only errors to have a short output. |
202 |
| - olp::logging::Log::setLevel(olp::logging::Level::Warning); |
203 |
| - |
204 |
| - const auto& parameter = GetParam(); |
205 |
| - |
206 |
| - auto settings = CreateCatalogClientSettings(); |
207 |
| - |
208 |
| - StartThreads([=](uint8_t /*thread_id*/) { |
209 |
| - olp::dataservice::read::VersionedLayerClient service_client( |
210 |
| - kCatalog, kVersionedLayerId, boost::none, settings); |
211 |
| - |
212 |
| - const auto end_timestamp = |
213 |
| - std::chrono::steady_clock::now() + parameter.runtime; |
214 |
| - |
215 |
| - while (end_timestamp > std::chrono::steady_clock::now()) { |
216 |
| - const auto level = 10; |
217 |
| - const auto tile_count = 1 << level; |
218 |
| - |
219 |
| - std::vector<olp::geo::TileKey> tile_keys = { |
220 |
| - olp::geo::TileKey::FromRowColumnLevel(rand() % tile_count, |
221 |
| - rand() % tile_count, level)}; |
222 |
| - |
223 |
| - auto request = olp::dataservice::read::PrefetchTilesRequest() |
224 |
| - .WithMaxLevel(level + 2) |
225 |
| - .WithMinLevel(level) |
226 |
| - .WithTileKeys(tile_keys); |
227 |
| - total_requests_.fetch_add(1); |
228 |
| - auto token = service_client.PrefetchTiles( |
229 |
| - std::move(request), |
230 |
| - [&](olp::dataservice::read::PrefetchTilesResponse response) { |
231 |
| - if (response.IsSuccessful()) { |
232 |
| - success_responses_.fetch_add(1); |
| 225 | + if (!Validate(*response.GetResult())) { |
| 226 | + abort(); |
| 227 | + } |
233 | 228 | } else {
|
234 | 229 | failed_responses_.fetch_add(1);
|
235 | 230 | ReportError(response.GetError());
|
|
0 commit comments