blob: 2eb1281fcd1c4e4bf82951d3afebda947a3deb39 [file] [log] [blame]
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07001#include "aos/events/logging/lzma_encoder.h"
2
Austin Schuh99f7c6a2024-06-25 22:07:44 -07003#include "absl/flags/declare.h"
4#include "absl/flags/flag.h"
Austin Schuh3bd4c402020-11-06 18:19:06 -08005#include "gmock/gmock.h"
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07006#include "gtest/gtest.h"
7
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "aos/events/logging/buffer_encoder_param_test.h"
Austin Schuh6bdcc372024-06-27 14:49:11 -07009#include "aos/testing/tmpdir.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070010#include "aos/util/file.h"
11
Austin Schuh99f7c6a2024-06-25 22:07:44 -070012ABSL_DECLARE_FLAG(int32_t, lzma_threads);
Austin Schuhbe91b342022-06-27 00:53:45 -070013
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070014namespace aos::logger::testing {
15
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -070016INSTANTIATE_TEST_SUITE_P(
Austin Schuhbe91b342022-06-27 00:53:45 -070017 MtLzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070018 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070019 absl::SetFlag(&FLAGS_lzma_threads, 3);
Austin Schuh48d10d62022-10-16 22:19:23 -070020 return std::make_unique<LzmaEncoder>(max_message_size,
21 2, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070022 }),
23 ::testing::Values([](std::string_view filename) {
24 return std::make_unique<LzmaDecoder>(filename);
25 }),
26 ::testing::Range(0, 100)));
27
28INSTANTIATE_TEST_SUITE_P(
29 MtLzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070030 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070031 absl::SetFlag(&FLAGS_lzma_threads, 3);
Austin Schuh48d10d62022-10-16 22:19:23 -070032 return std::make_unique<LzmaEncoder>(max_message_size,
33 5, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070034 }),
35 ::testing::Values([](std::string_view filename) {
36 return std::make_unique<ThreadedLzmaDecoder>(filename);
37 }),
38 ::testing::Range(0, 100)));
39
40INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070041 Lzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070042 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070043 absl::SetFlag(&FLAGS_lzma_threads, 1);
Austin Schuh48d10d62022-10-16 22:19:23 -070044 return std::make_unique<LzmaEncoder>(max_message_size,
45 2, 4096);
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070046 }),
47 ::testing::Values([](std::string_view filename) {
48 return std::make_unique<LzmaDecoder>(filename);
49 }),
50 ::testing::Range(0, 100)));
51
Tyler Chatow7df60832021-07-15 21:18:36 -070052INSTANTIATE_TEST_SUITE_P(
53 LzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070054 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070055 absl::SetFlag(&FLAGS_lzma_threads, 1);
Austin Schuh48d10d62022-10-16 22:19:23 -070056 return std::make_unique<LzmaEncoder>(max_message_size,
57 5, 4096);
Tyler Chatow7df60832021-07-15 21:18:36 -070058 }),
59 ::testing::Values([](std::string_view filename) {
60 return std::make_unique<ThreadedLzmaDecoder>(filename);
61 }),
62 ::testing::Range(0, 100)));
63
Austin Schuh3bd4c402020-11-06 18:19:06 -080064// Tests that we return as much of the file as we can read if the end is
65// corrupted.
66TEST_F(BufferEncoderBaseTest, CorruptedBuffer) {
67 std::uniform_int_distribution<int> quantity_distribution(20, 60);
Austin Schuh6bdcc372024-06-27 14:49:11 -070068 const std::string file_path = aos::testing::TestTmpDir() + "/foo";
Austin Schuh3bd4c402020-11-06 18:19:06 -080069
70 std::vector<std::vector<uint8_t>> encoded_buffers;
71 {
72 const int encode_chunks = quantity_distribution(*random_number_generator());
Austin Schuh48d10d62022-10-16 22:19:23 -070073 const auto encoder = std::make_unique<LzmaEncoder>(
74 BufferEncoderBaseTest::kMaxMessageSize, 2);
Austin Schuh3bd4c402020-11-06 18:19:06 -080075 encoded_buffers = CreateAndEncode(encode_chunks, encoder.get());
76 encoder->Finish();
77
78 std::string contents = "";
79 for (auto span : encoder->queue()) {
80 absl::StrAppend(
81 &contents,
82 std::string_view(reinterpret_cast<const char *>(span.data()),
83 span.size()));
84 }
85 aos::util::WriteStringToFileOrDie(
86 file_path, contents.substr(0, contents.size() - 200));
87 }
88
89 const size_t total_encoded_size = TotalSize(encoded_buffers);
90
91 // Try decoding in multiple random chunkings.
92 for (int i = 0; i < 20; ++i) {
93 const auto decoder = std::make_unique<LzmaDecoder>(file_path);
94 std::vector<std::vector<uint8_t>> decoded_buffers;
95 size_t total_decoded_size = 0;
96 while (true) {
97 const int chunk_size = quantity_distribution(*random_number_generator());
98 std::vector<uint8_t> chunk(chunk_size);
99 const size_t read_result =
100 decoder->Read(chunk.data(), chunk.data() + chunk_size);
101 // Eventually we'll get here, once the decoder is really sure it's done.
102 if (read_result == 0) {
103 // Sanity check the math in the test code.
104 LOG(INFO) << "Decoded " << total_decoded_size << " encoded "
105 << total_encoded_size;
106 CHECK_EQ(total_decoded_size, TotalSize(decoded_buffers));
107 break;
108 }
109 // If we're at the end, trim off the 0s so our comparison later works out.
110 chunk.resize(read_result);
111 total_decoded_size += read_result;
112 decoded_buffers.emplace_back(std::move(chunk));
113 }
114 auto flattened_encoded = Flatten(encoded_buffers);
115 auto flattened_decoded = Flatten(decoded_buffers);
116
117 ASSERT_LE(flattened_decoded.size(), flattened_encoded.size());
118 flattened_encoded.resize(flattened_decoded.size());
119
120 ASSERT_THAT(flattened_decoded, ::testing::Eq(flattened_encoded));
121 }
122}
123
Brian Silvermanf59fe3f2020-09-22 21:04:09 -0700124} // namespace aos::logger::testing