blob: 60444d3de1557c56f916edcce5614a227e37c4e5 [file] [log] [blame]
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07001#include "aos/events/logging/lzma_encoder.h"
2
Austin Schuh3bd4c402020-11-06 18:19:06 -08003#include "gmock/gmock.h"
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07004#include "gtest/gtest.h"
5
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "aos/events/logging/buffer_encoder_param_test.h"
7#include "aos/util/file.h"
8
Austin Schuhbe91b342022-06-27 00:53:45 -07009DECLARE_int32(lzma_threads);
10
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070011namespace aos::logger::testing {
12
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -070013INSTANTIATE_TEST_SUITE_P(
Austin Schuhbe91b342022-06-27 00:53:45 -070014 MtLzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070015 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070016 FLAGS_lzma_threads = 3;
Austin Schuh48d10d62022-10-16 22:19:23 -070017 return std::make_unique<LzmaEncoder>(max_message_size,
18 2, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070019 }),
20 ::testing::Values([](std::string_view filename) {
21 return std::make_unique<LzmaDecoder>(filename);
22 }),
23 ::testing::Range(0, 100)));
24
25INSTANTIATE_TEST_SUITE_P(
26 MtLzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070027 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070028 FLAGS_lzma_threads = 3;
Austin Schuh48d10d62022-10-16 22:19:23 -070029 return std::make_unique<LzmaEncoder>(max_message_size,
30 5, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070031 }),
32 ::testing::Values([](std::string_view filename) {
33 return std::make_unique<ThreadedLzmaDecoder>(filename);
34 }),
35 ::testing::Range(0, 100)));
36
37INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070038 Lzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070039 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070040 FLAGS_lzma_threads = 1;
Austin Schuh48d10d62022-10-16 22:19:23 -070041 return std::make_unique<LzmaEncoder>(max_message_size,
42 2, 4096);
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070043 }),
44 ::testing::Values([](std::string_view filename) {
45 return std::make_unique<LzmaDecoder>(filename);
46 }),
47 ::testing::Range(0, 100)));
48
Tyler Chatow7df60832021-07-15 21:18:36 -070049INSTANTIATE_TEST_SUITE_P(
50 LzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070051 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070052 FLAGS_lzma_threads = 1;
Austin Schuh48d10d62022-10-16 22:19:23 -070053 return std::make_unique<LzmaEncoder>(max_message_size,
54 5, 4096);
Tyler Chatow7df60832021-07-15 21:18:36 -070055 }),
56 ::testing::Values([](std::string_view filename) {
57 return std::make_unique<ThreadedLzmaDecoder>(filename);
58 }),
59 ::testing::Range(0, 100)));
60
Austin Schuh3bd4c402020-11-06 18:19:06 -080061// Tests that we return as much of the file as we can read if the end is
62// corrupted.
63TEST_F(BufferEncoderBaseTest, CorruptedBuffer) {
64 std::uniform_int_distribution<int> quantity_distribution(20, 60);
65 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
66 const std::string file_path = std::string(test_dir) + "/foo";
67
68 std::vector<std::vector<uint8_t>> encoded_buffers;
69 {
70 const int encode_chunks = quantity_distribution(*random_number_generator());
Austin Schuh48d10d62022-10-16 22:19:23 -070071 const auto encoder = std::make_unique<LzmaEncoder>(
72 BufferEncoderBaseTest::kMaxMessageSize, 2);
Austin Schuh3bd4c402020-11-06 18:19:06 -080073 encoded_buffers = CreateAndEncode(encode_chunks, encoder.get());
74 encoder->Finish();
75
76 std::string contents = "";
77 for (auto span : encoder->queue()) {
78 absl::StrAppend(
79 &contents,
80 std::string_view(reinterpret_cast<const char *>(span.data()),
81 span.size()));
82 }
83 aos::util::WriteStringToFileOrDie(
84 file_path, contents.substr(0, contents.size() - 200));
85 }
86
87 const size_t total_encoded_size = TotalSize(encoded_buffers);
88
89 // Try decoding in multiple random chunkings.
90 for (int i = 0; i < 20; ++i) {
91 const auto decoder = std::make_unique<LzmaDecoder>(file_path);
92 std::vector<std::vector<uint8_t>> decoded_buffers;
93 size_t total_decoded_size = 0;
94 while (true) {
95 const int chunk_size = quantity_distribution(*random_number_generator());
96 std::vector<uint8_t> chunk(chunk_size);
97 const size_t read_result =
98 decoder->Read(chunk.data(), chunk.data() + chunk_size);
99 // Eventually we'll get here, once the decoder is really sure it's done.
100 if (read_result == 0) {
101 // Sanity check the math in the test code.
102 LOG(INFO) << "Decoded " << total_decoded_size << " encoded "
103 << total_encoded_size;
104 CHECK_EQ(total_decoded_size, TotalSize(decoded_buffers));
105 break;
106 }
107 // If we're at the end, trim off the 0s so our comparison later works out.
108 chunk.resize(read_result);
109 total_decoded_size += read_result;
110 decoded_buffers.emplace_back(std::move(chunk));
111 }
112 auto flattened_encoded = Flatten(encoded_buffers);
113 auto flattened_decoded = Flatten(decoded_buffers);
114
115 ASSERT_LE(flattened_decoded.size(), flattened_encoded.size());
116 flattened_encoded.resize(flattened_decoded.size());
117
118 ASSERT_THAT(flattened_decoded, ::testing::Eq(flattened_encoded));
119 }
120}
121
Brian Silvermanf59fe3f2020-09-22 21:04:09 -0700122} // namespace aos::logger::testing