| 1 | /* |
| 2 | * Copyright (c) 2024 |
| 3 | * Tim Woodall. All rights reserved |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | * |
| 27 | * SPDX short identifier: BSD-2-Clause |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * faketape test suite |
| 32 | */ |
| 33 | |
| 34 | #include "config.h" // IWYU pragma: keep |
| 35 | #include "faketape-lib.h" |
| 36 | |
| 37 | #include <fcntl.h> // O_WRONLY |
| 38 | #include <stdint.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <unistd.h> |
| 42 | #include <algorithm> |
| 43 | #include <array> |
| 44 | #include <iostream> |
| 45 | #include <memory> |
| 46 | #include <sstream> |
| 47 | #include <stdexcept> |
| 48 | #include <string> |
| 49 | #include <vector> |
| 50 | |
| 51 | #define CONFIG_CATCH_MAIN |
| 52 | |
| 53 | #include <catch2/catch_test_macros.hpp> |
| 54 | |
| 55 | using Response = FakeTape::Response; |
| 56 | |
| 57 | class temptape : public FakeTape { |
| 58 | private: |
| 59 | char dirnam[16] = "faketape.XXXXXX"; |
| 60 | std::string filename; |
| 61 | public: |
| 62 | temptape() { |
| 63 | if (!mkdtemp(dirnam)) throw std::runtime_error("mkdtemp failed"); |
| 64 | filename = std::string(dirnam) + "/faketape.img"; |
| 65 | debug = std::make_unique<std::ostringstream>(); |
| 66 | } |
| 67 | ~temptape() { |
| 68 | close(); |
| 69 | if(unlink(filename.c_str()) == -1) |
| 70 | std::cerr << "Unlink of " << filename << " failed" << std::endl; |
| 71 | if(rmdir(dirnam) == -1) |
| 72 | std::cerr << "Unlink of " << dirnam << " failed" << std::endl; |
| 73 | if (trace()) |
| 74 | CHECK(reinterpret_cast<std::ostringstream*>(debug.get())->str().size()); |
| 75 | else |
| 76 | CHECK(reinterpret_cast<std::ostringstream*>(debug.get())->str().size() == 0); |
| 77 | trace(false); |
| 78 | } |
| 79 | Response open(int flags) { |
| 80 | return FakeTape::open(filename, flags); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | TEST_CASE("FilesCanBeCreatedOpenedAndClosed") { |
| 85 | temptape faketape; |
| 86 | std::vector<uint8_t> databuffer; |
| 87 | |
| 88 | auto postest = [&faketape](uint32_t faketapeo, size_t fblk, size_t blk) -> bool { |
| 89 | CHECK(faketape.getFileNo() == faketapeo); |
| 90 | CHECK(faketape.getFileBlock() == fblk); |
| 91 | CHECK(faketape.getPos() == blk); |
| 92 | return faketape.getFileNo() == faketapeo && faketape.getFileBlock() == fblk && faketape.getPos() == blk; |
| 93 | }; |
| 94 | |
| 95 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 96 | REQUIRE(postest(0,0,0)); |
| 97 | REQUIRE(faketape.writeData((const uint8_t*)"file1", 5) == Response::OK); |
| 98 | REQUIRE(postest(0,1,1)); |
| 99 | faketape.close(); |
| 100 | REQUIRE(faketape.open(O_WRONLY) == Response::OK); |
| 101 | REQUIRE(postest(1,0,2)); |
| 102 | faketape.trace(true); |
| 103 | REQUIRE(faketape.writeData((const uint8_t*)"file2", 5) == Response::OK); |
| 104 | REQUIRE(postest(1,1,3)); |
| 105 | faketape.close(); |
| 106 | |
| 107 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 108 | REQUIRE(faketape.rewind() == Response::OK); |
| 109 | |
| 110 | std::vector<uint8_t> data(10); |
| 111 | size_t rl; |
| 112 | |
| 113 | REQUIRE(postest(0,0,0)); |
| 114 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 115 | REQUIRE(postest(0,1,1)); |
| 116 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 117 | REQUIRE(postest(1,0,2)); |
| 118 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 119 | REQUIRE(postest(1,1,3)); |
| 120 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 121 | REQUIRE(postest(2,0,4)); |
| 122 | faketape.close(); |
| 123 | } |
| 124 | |
| 125 | TEST_CASE("test_writeSomeData") { |
| 126 | std::vector<uint8_t> databuffer; |
| 127 | |
| 128 | temptape faketape; |
| 129 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 130 | |
| 131 | for (size_t i = 0; i<4096; ++i) { |
| 132 | std::string data = std::to_string(i); |
| 133 | REQUIRE(faketape.writeDataAsync(reinterpret_cast<const uint8_t*>(data.data()), data.size()) == Response::OK); |
| 134 | } |
| 135 | /* Write End-of-File */ |
| 136 | faketape.writeDataAsync(nullptr, 0); |
| 137 | faketape.close(); |
| 138 | |
| 139 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 140 | |
| 141 | REQUIRE(faketape.rewind() == Response::OK); |
| 142 | |
| 143 | std::vector<uint8_t> data(10); |
| 144 | size_t rl; |
| 145 | for (size_t i = 0; i<4096; ++i) { |
| 146 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 147 | |
| 148 | std::string want = std::to_string(i); |
| 149 | std::string got = std::string(data.data(), data.data() + rl); |
| 150 | |
| 151 | REQUIRE(want == got); |
| 152 | } |
| 153 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 154 | REQUIRE(rl == 0); |
| 155 | faketape.close(); |
| 156 | } |
| 157 | |
| 158 | TEST_CASE("test_writeTwoFiles") { |
| 159 | std::vector<uint8_t> databuffer; |
| 160 | |
| 161 | temptape faketape; |
| 162 | |
| 163 | for(int l = 0; l<2; ++l) { |
| 164 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 165 | |
| 166 | for (size_t i = 0; i<4096; ++i) { |
| 167 | std::string data = std::to_string(i) + std::to_string(l); |
| 168 | REQUIRE(faketape.writeDataAsync(reinterpret_cast<const uint8_t*>(data.data()), data.size()) == Response::OK); |
| 169 | } |
| 170 | faketape.close(); |
| 171 | } |
| 172 | |
| 173 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 174 | REQUIRE(faketape.rewind() == Response::OK); |
| 175 | faketape.close(); |
| 176 | |
| 177 | for(int l = 0; l<2; ++l) { |
| 178 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 179 | |
| 180 | std::vector<uint8_t> data(10); |
| 181 | size_t rl; |
| 182 | for (size_t i = 0; i<4096; ++i) { |
| 183 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 184 | |
| 185 | std::string want = std::to_string(i) + std::to_string(l); |
| 186 | std::string got = std::string(data.data(), data.data() + rl); |
| 187 | |
| 188 | REQUIRE(want == got); |
| 189 | } |
| 190 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 191 | REQUIRE(rl == 0); |
| 192 | faketape.close(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | TEST_CASE("test_seeking") { |
| 197 | std::vector<uint8_t> databuffer; |
| 198 | |
| 199 | temptape faketape; |
| 200 | |
| 201 | for(int l = 0; l<2; ++l) { |
| 202 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 203 | |
| 204 | for (size_t i = 0; i<4096; ++i) { |
| 205 | std::string data = std::to_string(i>4097?i-4097:i) + std::to_string(l); |
| 206 | REQUIRE(faketape.writeDataAsync(reinterpret_cast<const uint8_t*>(data.data()), data.size()) == Response::OK); |
| 207 | } |
| 208 | faketape.close(); |
| 209 | } |
| 210 | |
| 211 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 212 | |
| 213 | REQUIRE(faketape.blockSeek(1) == Response::OK); |
| 214 | |
| 215 | for (size_t i = 1; i<8192; i+=100) { |
| 216 | REQUIRE(faketape.blockSeek(i) == Response::OK); |
| 217 | std::vector<uint8_t> data(10); |
| 218 | size_t rl; |
| 219 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 220 | |
| 221 | std::string want = std::to_string(i>4097?i-4097:i) + std::to_string(i>4097?1:0); |
| 222 | std::string got = std::string(data.data(), data.data() + rl); |
| 223 | |
| 224 | REQUIRE(want == got); |
| 225 | } |
| 226 | |
| 227 | for (size_t j = 1; j<8192; j+=100) { |
| 228 | size_t i = 8191 - j; |
| 229 | REQUIRE(faketape.blockSeek(i) == Response::OK); |
| 230 | std::vector<uint8_t> data(10); |
| 231 | size_t rl; |
| 232 | REQUIRE(faketape.readData(data.data(), 10, &rl) == Response::OK); |
| 233 | |
| 234 | std::string want = std::to_string(i>4097?i-4097:i) + std::to_string(i>4097?1:0); |
| 235 | std::string got = std::string(data.data(), data.data() + rl); |
| 236 | |
| 237 | REQUIRE(want == got); |
| 238 | } |
| 239 | |
| 240 | faketape.close(); |
| 241 | } |
| 242 | |
| 243 | TEST_CASE("test_zerocompression") { |
| 244 | std::vector<uint8_t> databuffer; |
| 245 | |
| 246 | SECTION("write and read back blocks of zero bytes") { |
| 247 | constexpr std::array<size_t, 5> tests = {1, 2, 3, 4007, 1<<24}; |
| 248 | for(size_t sz : tests) { |
| 249 | temptape faketape; |
| 250 | static constexpr size_t repeat = 1100; |
| 251 | std::vector<uint8_t> data(sz); |
| 252 | |
| 253 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 254 | |
| 255 | for (size_t i = 0; i<repeat; ++i) { |
| 256 | REQUIRE(faketape.writeDataAsync(data.data(), data.size()) == Response::OK); |
| 257 | } |
| 258 | faketape.close(); |
| 259 | |
| 260 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 261 | REQUIRE(faketape.rewind() == Response::OK); |
| 262 | |
| 263 | data.resize(std::min(sz+5, FakeTape::getMaxBlockSize())); |
| 264 | for (size_t i = 0; i<repeat; ++i) { |
| 265 | size_t rl; |
| 266 | REQUIRE(faketape.readData(data.data(), data.size(), &rl) == Response::OK); |
| 267 | REQUIRE(rl == sz); |
| 268 | REQUIRE (std::find_if(data.data(), data.data()+sz, [](uint8_t b){ return b!=0; }) == data.data()+sz); |
| 269 | } |
| 270 | faketape.close(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | SECTION("check that blocks of zero data use no tape except for the index blocks") { |
| 275 | uint8_t data[4096] = {0}; |
| 276 | |
| 277 | temptape faketape; |
| 278 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 279 | |
| 280 | for (size_t i = 0; i<4096; ++i) { |
| 281 | REQUIRE(faketape.writeDataAsync(data, sizeof data) == Response::OK); |
| 282 | } |
| 283 | faketape.close(); |
| 284 | |
| 285 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 286 | REQUIRE(faketape.rewind() == Response::OK); |
| 287 | |
| 288 | for (size_t i = 0; i<4096; ++i) { |
| 289 | size_t rl; |
| 290 | REQUIRE(faketape.readData(data, sizeof data, &rl) == Response::OK); |
| 291 | faketape.sync(); |
| 292 | off_t p = faketape.getDiskPosForTest(); |
| 293 | auto calcBlockPos = [](size_t x) -> size_t { |
| 294 | ++x; |
| 295 | if (x < FakeTape::recsInFirstBlock) |
| 296 | return 1; |
| 297 | x-=FakeTape::recsInFirstBlock; |
| 298 | return 2 + x/FakeTape::recsPerBlock; |
| 299 | }; |
| 300 | off_t targetp = 4096 * calcBlockPos(i); //maxblocks is private in FakeTape |
| 301 | REQUIRE(p == targetp); |
| 302 | } |
| 303 | faketape.close(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | TEST_CASE("CheckThatMaxBlockSizeIsEnforced") { |
| 308 | temptape faketape; |
| 309 | std::vector<uint8_t> databuffer; |
| 310 | |
| 311 | REQUIRE(faketape.setMaxBlockSize((1<<24)+1) == Response::ERROR); |
| 312 | REQUIRE(faketape.getMaxBlockSize() == 1<<24); |
| 313 | |
| 314 | REQUIRE(faketape.setMaxBlockSize(1024) == Response::OK); |
| 315 | REQUIRE(faketape.getMaxBlockSize() == 1024); |
| 316 | |
| 317 | REQUIRE(faketape.open(O_WRONLY|O_CREAT) == Response::OK); |
| 318 | databuffer.resize(1025); |
| 319 | |
| 320 | REQUIRE(faketape.writeDataAsync(databuffer.data(), 1024) == Response::OK); |
| 321 | REQUIRE(faketape.writeDataAsync(databuffer.data(), 1025) == Response::ERROR); |
| 322 | faketape.close(); |
| 323 | |
| 324 | REQUIRE(faketape.open(O_RDONLY) == Response::OK); |
| 325 | REQUIRE(faketape.rewind() == Response::OK); |
| 326 | |
| 327 | size_t rl; |
| 328 | REQUIRE(faketape.readData(databuffer.data(), 1025, &rl) == Response::ERROR); |
| 329 | REQUIRE(faketape.readData(databuffer.data(), 1024, &rl) == Response::OK); |
| 330 | REQUIRE(rl == 1024); |
| 331 | REQUIRE(faketape.readData(databuffer.data(), 1024, &rl) == Response::OK); |
| 332 | REQUIRE(rl == 0); |
| 333 | faketape.close(); |
| 334 | |
| 335 | REQUIRE(faketape.setMaxBlockSize(1<<24) == Response::OK); |
| 336 | REQUIRE(faketape.getMaxBlockSize() == 1<<24); |
| 337 | |
| 338 | } |
| 339 | |
| 340 | /* vim: set sw=8 sts=8 ts=8 noexpandtab: */ |