Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 1 | // Copyright 2021 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <chrono> |
| 17 | #include <filesystem> |
| 18 | #include <fstream> |
| 19 | #include <string> |
| 20 | #include <thread> |
| 21 | |
| 22 | #include "gtest/gtest.h" |
| 23 | #include "pw_assert/check.h" |
| 24 | #include "pw_bytes/array.h" |
| 25 | #include "pw_log/log.h" |
| 26 | #include "pw_rpc/integration_testing.h" |
| 27 | #include "pw_status/status.h" |
| 28 | #include "pw_sync/binary_semaphore.h" |
| 29 | #include "pw_thread_stl/options.h" |
| 30 | #include "pw_transfer/client.h" |
| 31 | #include "pw_transfer_test/test_server.raw_rpc.pb.h" |
| 32 | |
| 33 | namespace pw::transfer { |
| 34 | namespace { |
| 35 | |
| 36 | using namespace std::chrono_literals; |
| 37 | |
| 38 | // TODO(hepler): Use more iterations when the pw_transfer synchronization issues |
| 39 | // that make this flaky are fixed. |
| 40 | constexpr int kIterations = 1; |
| 41 | |
| 42 | constexpr auto kData512 = bytes::Initialized<512>([](size_t i) { return i; }); |
| 43 | constexpr auto kData8192 = bytes::Initialized<8192>([](size_t i) { return i; }); |
Erik Gilling | 0aa0bef | 2022-03-10 09:49:26 -0800 | [diff] [blame] | 44 | constexpr auto kDataHdlcEscape = bytes::Initialized<8192>(0x7e); |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 45 | |
| 46 | std::filesystem::path directory; |
| 47 | |
| 48 | // Reads the file that represents the transfer with the specific ID. |
| 49 | std::string GetContent(uint32_t transfer_id) { |
| 50 | std::ifstream stream(directory / std::to_string(transfer_id), |
| 51 | std::ios::binary | std::ios::ate); |
| 52 | std::string contents(stream.tellg(), '\0'); |
| 53 | |
| 54 | stream.seekg(0, std::ios::beg); |
| 55 | PW_CHECK(stream.read(contents.data(), contents.size())); |
| 56 | |
| 57 | return contents; |
| 58 | } |
| 59 | |
| 60 | // Drops the null terminator from a string literal. |
| 61 | template <size_t kLengthWithNull> |
| 62 | ConstByteSpan AsByteSpan(const char (&data)[kLengthWithNull]) { |
| 63 | constexpr size_t kLength = kLengthWithNull - 1; |
| 64 | PW_CHECK_INT_EQ('\0', data[kLength], "Expecting null for last character"); |
| 65 | return std::as_bytes(std::span(data, kLength)); |
| 66 | } |
| 67 | |
| 68 | constexpr ConstByteSpan AsByteSpan(ConstByteSpan data) { return data; } |
| 69 | |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 70 | thread::Options& TransferThreadOptions() { |
| 71 | static thread::stl::Options options; |
| 72 | return options; |
| 73 | } |
| 74 | |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 75 | // Test fixture for pw_transfer tests. Clears the transfer files before and |
| 76 | // after each test. |
| 77 | class TransferIntegration : public ::testing::Test { |
| 78 | protected: |
| 79 | TransferIntegration() |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 80 | : transfer_thread_(chunk_buffer_, encode_buffer_), |
| 81 | system_thread_(TransferThreadOptions(), transfer_thread_), |
| 82 | client_(rpc::integration_test::client(), |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 83 | rpc::integration_test::kChannelId, |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 84 | transfer_thread_, |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 85 | 256), |
| 86 | test_server_client_(rpc::integration_test::client(), |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 87 | rpc::integration_test::kChannelId) { |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 88 | ClearFiles(); |
| 89 | } |
| 90 | |
| 91 | ~TransferIntegration() { |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 92 | ClearFiles(); |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 93 | transfer_thread_.Terminate(); |
| 94 | system_thread_.join(); |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Sets the content of a transfer ID and returns a MemoryReader for that data. |
| 98 | template <typename T> |
| 99 | void SetContent(uint32_t transfer_id, const T& content) { |
| 100 | const ConstByteSpan data = AsByteSpan(content); |
| 101 | std::ofstream stream(directory / std::to_string(transfer_id), |
| 102 | std::ios::binary); |
| 103 | PW_CHECK( |
| 104 | stream.write(reinterpret_cast<const char*>(data.data()), data.size())); |
| 105 | |
| 106 | sync::BinarySemaphore reload_complete; |
| 107 | rpc::RawUnaryReceiver call = test_server_client_.ReloadTransferFiles( |
| 108 | {}, [&reload_complete](ConstByteSpan, Status) { |
| 109 | reload_complete.release(); |
| 110 | }); |
| 111 | PW_CHECK(reload_complete.try_acquire_for(3s)); |
| 112 | } |
| 113 | |
| 114 | auto OnCompletion() { |
| 115 | return [this](Status status) { |
| 116 | last_status_ = status; |
| 117 | completed_.release(); |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | // Checks that a read transfer succeeded and that the data matches the |
| 122 | // expected data. |
| 123 | void ExpectReadData(ConstByteSpan expected) { |
| 124 | ASSERT_EQ(WaitForCompletion(), OkStatus()); |
| 125 | ASSERT_EQ(expected.size(), read_buffer_.size()); |
| 126 | |
| 127 | EXPECT_TRUE(std::equal(read_buffer_.begin(), |
| 128 | read_buffer_.end(), |
| 129 | std::as_bytes(std::span(expected)).begin())); |
| 130 | } |
| 131 | |
| 132 | // Checks that a write transfer succeeded and that the written contents match. |
| 133 | void ExpectWriteData(uint32_t transfer_id, ConstByteSpan expected) { |
| 134 | ASSERT_EQ(WaitForCompletion(), OkStatus()); |
| 135 | |
| 136 | const std::string written = GetContent(transfer_id); |
| 137 | ASSERT_EQ(expected.size(), written.size()); |
| 138 | |
| 139 | ConstByteSpan bytes = std::as_bytes(std::span(written)); |
| 140 | EXPECT_TRUE(std::equal(bytes.begin(), bytes.end(), expected.begin())); |
| 141 | } |
| 142 | |
| 143 | // Waits for the transfer to complete and returns the status. |
| 144 | Status WaitForCompletion() { |
| 145 | PW_CHECK(completed_.try_acquire_for(3s)); |
| 146 | return last_status_; |
| 147 | } |
| 148 | |
| 149 | // Exact match the size of kData8192 to test filling the receiving buffer. |
| 150 | stream::MemoryWriterBuffer<kData8192.size()> read_buffer_; |
| 151 | |
| 152 | Client& client() { return client_; } |
| 153 | |
| 154 | private: |
| 155 | static void ClearFiles() { |
| 156 | for (const auto& entry : std::filesystem::directory_iterator(directory)) { |
| 157 | if (!entry.is_regular_file()) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if (const std::string name = entry.path().filename().string(); |
| 162 | std::all_of(name.begin(), name.end(), [](char c) { |
| 163 | return std::isdigit(c); |
| 164 | })) { |
| 165 | PW_LOG_DEBUG("Clearing transfer file %s", name.c_str()); |
| 166 | std::filesystem::remove(entry.path()); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 171 | std::byte chunk_buffer_[512]; |
| 172 | std::byte encode_buffer_[512]; |
| 173 | transfer::Thread<2, 2> transfer_thread_; |
| 174 | thread::Thread system_thread_; |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 175 | |
| 176 | Client client_; |
| 177 | |
| 178 | pw_rpc::raw::TestServer::Client test_server_client_; |
| 179 | Status last_status_ = Status::Unknown(); |
| 180 | sync::BinarySemaphore completed_; |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | TEST_F(TransferIntegration, Read_UnknownId) { |
| 184 | SetContent(123, "hello"); |
| 185 | |
| 186 | ASSERT_EQ(OkStatus(), client().Read(456, read_buffer_, OnCompletion())); |
| 187 | |
| 188 | EXPECT_EQ(Status::NotFound(), WaitForCompletion()); |
| 189 | } |
| 190 | |
| 191 | #define PW_TRANSFER_TEST_READ(name, content) \ |
| 192 | TEST_F(TransferIntegration, Read_##name) { \ |
| 193 | for (int i = 0; i < kIterations; ++i) { \ |
| 194 | const ConstByteSpan data = AsByteSpan(content); \ |
| 195 | SetContent(__LINE__, data); \ |
| 196 | ASSERT_EQ(OkStatus(), \ |
| 197 | client().Read(__LINE__, read_buffer_, OnCompletion())); \ |
| 198 | ExpectReadData(data); \ |
| 199 | read_buffer_.clear(); \ |
| 200 | } \ |
| 201 | } \ |
| 202 | static_assert(true, "Semicolons are required") |
| 203 | |
| 204 | PW_TRANSFER_TEST_READ(Empty, ""); |
| 205 | PW_TRANSFER_TEST_READ(SingleByte_1, "\0"); |
| 206 | PW_TRANSFER_TEST_READ(SingleByte_2, "?"); |
| 207 | PW_TRANSFER_TEST_READ(SmallData, "hunter2"); |
| 208 | PW_TRANSFER_TEST_READ(LargeData, kData512); |
| 209 | |
| 210 | TEST_F(TransferIntegration, Write_UnknownId) { |
| 211 | constexpr std::byte kData[] = {std::byte{0}, std::byte{1}, std::byte{2}}; |
| 212 | stream::MemoryReader reader(kData); |
| 213 | |
| 214 | ASSERT_EQ(OkStatus(), client().Write(99, reader, OnCompletion())); |
| 215 | EXPECT_EQ(Status::NotFound(), WaitForCompletion()); |
| 216 | |
| 217 | SetContent(99, "something"); |
| 218 | ASSERT_EQ(OkStatus(), client().Write(100, reader, OnCompletion())); |
| 219 | EXPECT_EQ(Status::NotFound(), WaitForCompletion()); |
| 220 | } |
| 221 | |
| 222 | #define PW_TRANSFER_TEST_WRITE(name, content) \ |
| 223 | TEST_F(TransferIntegration, Write_##name) { \ |
| 224 | for (int i = 0; i < kIterations; ++i) { \ |
| 225 | SetContent(__LINE__, "This is junk data that should be overwritten!"); \ |
| 226 | const ConstByteSpan data = AsByteSpan(content); \ |
| 227 | stream::MemoryReader reader(data); \ |
| 228 | ASSERT_EQ(OkStatus(), client().Write(__LINE__, reader, OnCompletion())); \ |
| 229 | ExpectWriteData(__LINE__, data); \ |
| 230 | } \ |
| 231 | } \ |
| 232 | static_assert(true, "Semicolons are required") |
| 233 | |
| 234 | PW_TRANSFER_TEST_WRITE(Empty, ""); |
| 235 | PW_TRANSFER_TEST_WRITE(SingleByte_1, "\0"); |
| 236 | PW_TRANSFER_TEST_WRITE(SingleByte_2, "?"); |
| 237 | PW_TRANSFER_TEST_WRITE(SmallData, "hunter2"); |
| 238 | PW_TRANSFER_TEST_WRITE(LargeData, kData512); |
Erik Gilling | 0aa0bef | 2022-03-10 09:49:26 -0800 | [diff] [blame] | 239 | PW_TRANSFER_TEST_WRITE(HdlcEscape, kDataHdlcEscape); |
Wyatt Hepler | 6f6b6a1 | 2021-11-24 13:45:48 -0800 | [diff] [blame] | 240 | |
| 241 | } // namespace |
| 242 | } // namespace pw::transfer |
| 243 | |
| 244 | int main(int argc, char* argv[]) { |
| 245 | if (!pw::rpc::integration_test::InitializeClient(argc, argv, "PORT DIRECTORY") |
| 246 | .ok()) { |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | if (argc != 3) { |
| 251 | PW_LOG_INFO("Usage: %s PORT DIRECTORY", argv[0]); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | pw::transfer::directory = argv[2]; |
| 256 | |
| 257 | return RUN_ALL_TESTS(); |
| 258 | } |