From f7feb041e8d8dac5fac3c6cd44c8108e12ea4cd6 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 11 Apr 2014 11:29:31 +0200 Subject: extend FileFd test behond basic permission tests We now do Open, Write and Read (the later multiple ways) for each permission and each compressor we have configured to cover more cases and especially ensure that compressors do not change our premissions. This test is also to be credited for discovering the skippos-fix. Git-Dch: Ignore --- test/libapt/fileutl_test.cc | 109 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 10 deletions(-) diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 1d1a1a1b8..3a354814d 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -2,29 +2,93 @@ #include #include +#include #include #include #include #include +#include #include "assert.h" -// regression test for permission bug LP: #1304657 +static void assertStringEquals(char const * const expect, char const * const got, unsigned long const line) { + if (strncmp(expect, got, strlen(expect)) == 0) + return; + OutputAssertEqual(expect, "==", got, line); +} +#define strequals(x,y) assertStringEquals(x, y, __LINE__) + static bool -TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission) +TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission, unsigned int const filemode, APT::Configuration::Compressor const &compressor) { FileFd f; struct stat buf; static const char* fname = "test.txt"; umask(a_umask); - f.Open(fname, FileFd::ReadWrite|FileFd::Atomic); + equals(f.Open(fname, filemode, compressor), true); + equals(f.IsOpen(), true); + equals(f.Failed(), false); + equals(umask(a_umask), a_umask); + + std::string test = "This is a test!"; + equals(f.Write(test.c_str(), test.size()), true); + equals(f.IsOpen(), true); + equals(f.Failed(), false); + f.Close(); + equals(f.IsOpen(), false); + equals(f.Failed(), false); + + equals(f.Open(fname, FileFd::ReadOnly, compressor), true); + equalsNot(f.FileSize(), 0); + equals(f.IsOpen(), true); + equals(f.Failed(), false); + + char readback[20]; + { + char const * const expect = "This"; + equals(f.Read(readback, strlen(expect)), true); + equals(f.Failed(), false); + equals(f.Eof(), false); + strequals(expect, readback); + equals(strlen(expect), f.Tell()); + } + { + char const * const expect = "test!"; + equals(f.Skip((test.size() - f.Tell()) - strlen(expect)), true); + equals(f.Read(readback, strlen(expect)), true); + equals(f.Failed(), false); + equals(f.Eof(), false); + strequals(expect, readback); + equals(test.size(), f.Tell()); + } + + equals(f.Seek(0), true); + equals(f.Read(readback, 20, true), true); + equals(f.Failed(), false); + equals(f.Eof(), true); + equals(test, readback); + equals(test.size(), strlen(readback)); + equals(f.Size(), f.Tell()); + + equals(f.Seek(0), true); + f.ReadLine(readback, 20); + equals(f.Failed(), false); + equals(f.Eof(), true); + equals(test, readback); + equals(test.size(), strlen(readback)); + equals(f.Size(), f.Tell()); + + f.Close(); + equals(f.IsOpen(), false); + equals(f.Failed(), false); + + // regression test for permission bug LP: #1304657 if (stat(fname, &buf) < 0) { _error->Errno("stat", "failed to stat"); - _error->DumpErrors(); return false; } unlink(fname); @@ -32,18 +96,43 @@ TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission) return true; } -int main() +static bool TestFileFd(unsigned int const filemode) { - std::vector files; + std::vector const compressors = APT::Configuration::getCompressors(); + for (std::vector::const_iterator c = compressors.begin(); c != compressors.end(); ++c) + { + if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite && + (c->Name.empty() != true && c->Binary.empty() != true)) + continue; + if (TestFileFd(0002, 0664, filemode, *c) == false || + TestFileFd(0022, 0644, filemode, *c) == false || + TestFileFd(0077, 0600, filemode, *c) == false || + TestFileFd(0026, 0640, filemode, *c) == false) + { + _error->DumpErrors(); + return false; + } + } + return true; +} - if (TestFileFdOpenPermissions(0002, 0664) == false || - TestFileFdOpenPermissions(0022, 0644) == false || - TestFileFdOpenPermissions(0077, 0600) == false || - TestFileFdOpenPermissions(0026, 0640) == false) +int main() +{ + if (TestFileFd(FileFd::WriteOnly | FileFd::Create) == false || + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty) == false || + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive) == false || + TestFileFd(FileFd::WriteOnly | FileFd::Atomic) == false || + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic) == false || + // short-hands for ReadWrite with these modes + TestFileFd(FileFd::WriteEmpty) == false || + TestFileFd(FileFd::WriteAny) == false || + TestFileFd(FileFd::WriteTemp) == false || + TestFileFd(FileFd::WriteAtomic) == false) { return 1; } + std::vector files; // normal match files = Glob("*.lst"); if (files.size() != 1) -- cgit v1.2.3