1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020, Shaun Ruffell, Thomas Lauf.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <sstream>
#include <memory>
#include <string>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmake.h>
#include <timew.h>
#include <test.h>
#include <AtomicFile.h>
#include <FS.h>
#include <TempDir.h>
#ifdef FIU_ENABLE
#include <fiu.h>
#include <fiu-control.h>
// This class is a helper to make sure we turn off libfiu after the test so that
// we can properly write to stdout / stderr.
class FIU
{
public:
FIU()
{
enable();
}
FIU(const FIU&) = delete;
FIU& operator= (const FIU&) = delete;
~FIU()
{
disable ();
std::cout << cbuffer.str();
std::stringstream().swap(cbuffer);
}
void enable ()
{
for (auto test_point : test_points)
{
fiu_enable_external(test_point, 1, NULL, 0, fiu_cb);
}
}
void disable ()
{
for (auto test_point : test_points)
{
fiu_disable(test_point);
}
}
private:
static const std::vector <const char *> test_points;
static std::stringstream cbuffer;
static int external_cb_was_called;
static int fiu_cb(const char *name, int *failnum,
void **failinfo, unsigned int *flags)
{
(void)name;
(void)flags;
external_cb_was_called++;
// For debugging the tests themselves...
// cbuffer << "fiu_cb called for " << name << '\n';
*failinfo = (void *) EIO;
return *failnum;
}
};
const std::vector <const char *> FIU::test_points {
"posix/stdio/gp/fputs",
"posix/io/rw/write",
};
std::stringstream FIU::cbuffer;
int FIU::external_cb_was_called = 0;
//////////////////////////////////////////////////////////////////////////////
// Since AtomicFile is primarily for keeping the database consistent in the
// presence of filesystem errors, these tests use libfiu to ensure that
// AtomicFile behaves as intended when the underlying system calls fail.
int fiu_test (UnitTest& t)
{
TempDir tempDir;
fiu_init (0);
try { FIU fiu; AtomicFile ("test.txt").write_raw("This is test"); t.fail ("AtomicFile::write_raw throws on error"); }
catch (...) { t.pass ("AtomicFile::write_raw throws on error"); }
try { FIU fiu; AtomicFile::finalize_all (); t.fail ("AtomicFile::finalize_all() throws on error"); }
catch (...) { t.pass ("AtomicFile::finalize_all() throws on error"); }
try { FIU fiu; AtomicFile::reset (); AtomicFile::finalize_all (); t.pass ("AtomicFile::reset clears failure state"); }
catch (...) { t.fail ("AtomicFile::reset clears failure state"); }
File::write ("test.txt", "line1\n");
{
AtomicFile file ("test.txt");
try { FIU fiu; file.append ("append1\n"); t.fail ("AtomicFile::append throws on error"); }
catch (...) { t.pass ("AtomicFile::append throws on error"); }
std::string contents {"should-not-see-this"};
file.read (contents);
t.is (contents, "", "AtomicFile::append did not partially fill the file.");
}
try { FIU fiu; AtomicFile::finalize_all (); t.fail ("AtomicFile::append failures prevent finalization"); }
catch (...) { t.pass ("AtomicFile::append failures prevent finalization"); }
return 0;
}
#else
#define fiu_init(flags) 0
#define fiu_fail(name) 0
#define fiu_failinfo() NULL
#define fiu_do_on(name, action)
#define fiu_exit_on(name)
#define fiu_return_on(name, retval)
int fiu_test (UnitTest& t)
{
t.skip ("AtomicFile::write_raw throws on error");
t.skip ("AtomicFile::finalize_all() throws on error");
t.skip ("AtomicFile::reset clears failure state");
t.skip ("AtomicFile::append throws on error");
t.skip ("AtomicFile::append did not partially fill the file.");
t.skip ("AtomicFile::append failures prevent finalization");
return 0;
}
#endif // FIU_ENABLE
//////////////////////////////////////////////////////////////////////////////
int test (UnitTest& t)
{
std::string test_name;
// This will create and change to a temporary directory that will be cleaned
// up when the destructor is run
TempDir tempDir;
std::string goldenText = "1\n";
std::string contents;
std::string expected;
Path firstFilename ("test-1.txt");
Path secondFilename ("test-2.txt");
{
AtomicFile file (firstFilename);
file.write_raw (goldenText);
file.close ();
}
t.is (firstFilename.exists (), false, "Shall not exists before finalize");
AtomicFile::finalize_all ();
t.is (firstFilename.exists (), true, "Shall exists after finalize");
File::read(firstFilename, contents);
t.is (contents == goldenText, true, "Shall have the correct data");
tempDir.clear ();
{
AtomicFile first (firstFilename);
AtomicFile second (secondFilename);
first.write_raw ("first\n");
second.write_raw ("second\n");
first.close ();
second.close ();
}
t.is (firstFilename.exists () || secondFilename.exists (), false, "Neither shall exist before finalize");
AtomicFile::finalize_all ();
t.is (firstFilename.exists () && secondFilename.exists (), true, "Both shall exists after finalize");
File::read(firstFilename, contents);
test_name = "First file shall contain the correct data";
expected = "first\n";
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
File::read(secondFilename, contents);
test_name = "Second file shall contain the correct data";
if (contents == std::string("second\n"))
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected 'second\n' read '" + contents + "'"));
}
// Make sure appending works
test_name = "Appending does not update original before finalize";
expected = "first\n";
{
AtomicFile first (firstFilename);
first.append("append 1\n");
first.append("append 2\n");
first.close ();
}
File::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
test_name = "Finalizing updates the appended data";
expected = "first\nappend 1\nappend 2\n";
AtomicFile::finalize_all ();
File::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
test_name = "Read from Atomicfile";
// We do not want to update the expected
{
AtomicFile::read (firstFilename, contents);
}
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
// If we read from an atomic file before finalizing, we should get the data
// that was written to the temporary file and not the 'real' file.
test_name = "Read from Atomicfile should read unfinalized data";
expected += "expected\n";
AtomicFile::write (firstFilename, expected);
AtomicFile::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
AtomicFile::finalize_all ();
{
AtomicFile first (firstFilename);
AtomicFile second (firstFilename);
first.write_raw("first\n");
second.append ("second\n");
AtomicFile::finalize_all ();
first.append ("third\n");
second.append ("fourth\n");
first.append ("fifth\n");
}
test_name = "Two AtomicFiles should access same temp file (part 1)";
// The atomic files, which were closed above should return all the strings
// written since it was not yet finalized.
expected = "first\nsecond\nthird\nfourth\nfifth\n";
AtomicFile::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
// But since the file was not yet finalized, the "real" file should only
// contain the data present since before the finalize_all () call.
test_name = "Two AtomicFiles should access same temp file (part 2)";
expected = "first\nsecond\n";
File::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
// After we finalize the data, the AtomicFile and the File should now both
// return the same information
test_name = "Two AtomicFiles should access same temp file (part 3)";
AtomicFile::finalize_all ();
File::read (firstFilename, expected);
AtomicFile::read (firstFilename, contents);
if (contents == expected)
{
t.pass (test_name);
}
else
{
t.fail (test_name);
t.diag (std::string ("Expected '" + expected + "' read '" + contents + "'"));
}
AtomicFile::reset ();
{
tempDir.clear ();
Path test("test");
AtomicFile file(test);
file.truncate ();
assert (! test.exists ());
AtomicFile::finalize_all ();
assert (test.exists ());
file.remove ();
t.is (test.exists (), true, "File not removed before finalize");
AtomicFile::finalize_all ();
t.is (test.exists (), false, "File is removed after finalize");
}
return 0;
}
int main (int, char**)
{
UnitTest t (22);
try
{
int ret = test (t);
int fiu_ret = fiu_test (t);
return (ret == 0) ? fiu_ret : ret;
}
catch (const std::string& error)
{
std::cerr << "Test threw exception: " << error << '\n';
}
catch (...)
{
std::cerr << "Uncaught exception.\n";
}
}
////////////////////////////////////////////////////////////////////////////////
|