Lumiera  0.pre.03
»edit your freedom«
file-support-test.cpp
Go to the documentation of this file.
1 /*
2  FileSupport(Test) - verify additional filesystem helpers
3 
4  Copyright (C) Lumiera.org
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/test/temp-dir.hpp"
31 #include "lib/stat/file.hpp"
32 
33 #include <fstream>
34 
35 
36 namespace lib {
37 namespace stat{
38 namespace test{
39 
40  using lib::test::TempDir;
41 
42 
43 
44 
45  /********************************************************************/
51  class FileSupport_test : public Test
52  {
53  void
54  run (Arg)
55  {
56  simplifiedPermissionAccess();
58  }
59 
60 
61  void
62  simplifiedPermissionAccess()
63  {
64  TempDir temp;
65  fs::path f = temp.makeFile("Lumiera.nix");
66  CHECK (fs::exists(f));
67  CHECK (f.filename() == "Lumiera.nix");
68  CHECK (f.parent_path() == temp);
69 
70  // enforce specific permissions...
71  fs::permissions(f, fs::perms::owner_read | fs::perms::group_all | fs::perms::others_exec);
72 
73  CHECK ( fs::has_perm(f, fs::perms::owner_read));
74  CHECK (not fs::has_perm(f, fs::perms::owner_write));
75  CHECK (not fs::has_perm(f, fs::perms::owner_exec));
76  CHECK (not fs::has_perm(f, fs::perms::owner_all));
77  CHECK ( fs::has_perm(f, fs::perms::group_read));
78  CHECK ( fs::has_perm(f, fs::perms::group_write));
79  CHECK ( fs::has_perm(f, fs::perms::group_exec));
80  CHECK ( fs::has_perm(f, fs::perms::group_all));
81  CHECK (not fs::has_perm(f, fs::perms::others_read));
82  CHECK (not fs::has_perm(f, fs::perms::others_write));
83  CHECK ( fs::has_perm(f, fs::perms::others_exec));
84  CHECK (not fs::has_perm(f, fs::perms::others_all));
85  CHECK (not fs::has_perm(f, fs::perms::all));
86  CHECK ( fs::can_read(f));
87  CHECK (not fs::can_write(f));
88  CHECK (not fs::can_exec(f));
89 
90  // and indeed: we can not write
91  std::ofstream out{f};
92  out << "outch";
93  out.close();
94  CHECK (not out.good());
95  CHECK (0 == fs::file_size(f));
96  }
97 
98 
99 
104  void
106  {
107  fs::path sweetHome{"~"};
108  CHECK ("~" == sweetHome.generic_string());
109  CHECK (not sweetHome.empty());
110  CHECK (not sweetHome.has_parent_path());
111  CHECK (not sweetHome.is_absolute());
112 
113  sweetHome = fs::consolidated (sweetHome);
114  CHECK (not util::startsWith (sweetHome.generic_string(), "~"));
115  CHECK ( util::startsWith (sweetHome.generic_string(), "/"));
116  CHECK (not sweetHome.empty());
117  CHECK ( sweetHome.has_parent_path());
118  CHECK ( sweetHome.is_absolute());
119  CHECK (fs::is_directory(sweetHome));
120 
121  fs::path itFollows = fs::consolidated ("~/it/follows");
122  CHECK (util::startsWith (itFollows.generic_string(), "/"));
123  CHECK (util::endsWith (itFollows.generic_string(), "follows"));
124  CHECK (itFollows.filename() == "follows");
125  CHECK (itFollows.is_absolute());
126 
127  CHECK (fs::relative (itFollows, sweetHome) == "it/follows");
128  }
129  };
130 
131  LAUNCHER (FileSupport_test, "unit common");
132 
133 
134 }}} // namespace lib::stat::test
135 
Definition: run.hpp:49
Includes the C++ Filesystem library and provides some convenience helpers.
Manage a temporary directory for storage, with automated clean-up.
Implementation namespace for support and library code.
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
A RAII style temporary directory.
Definition: temp-dir.hpp:63