summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/input.output/filesystems/class.directory_entry/directory_entry.mods/refresh.pass.cpp
blob: 575f0d59b4cd559020b73ccdcbee02153d92fdd5 (plain)
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
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <filesystem>

// class directory_entry

// directory_entry& operator=(directory_entry const&) = default;
// directory_entry& operator=(directory_entry&&) noexcept = default;
// void assign(path const&);
// void replace_filename(path const&);

#include "filesystem_include.hpp"
#include <type_traits>
#include <cassert>

#include "test_macros.h"
#include "rapid-cxx-test.hpp"
#include "filesystem_test_helper.hpp"

TEST_SUITE(directory_entry_mods_suite)

TEST_CASE(test_refresh_method) {
  using namespace fs;
  {
    directory_entry e;
    static_assert(noexcept(e.refresh()) == false,
                  "operation cannot be noexcept");
    static_assert(std::is_same<decltype(e.refresh()), void>::value,
                  "operation must return void");
  }
  {
    directory_entry e;
    e.refresh();
    TEST_CHECK(!e.exists());
  }
}

TEST_CASE(test_refresh_ec_method) {
  using namespace fs;
  {
    directory_entry e;
    std::error_code ec;
    static_assert(noexcept(e.refresh(ec)), "operation should be noexcept");
    static_assert(std::is_same<decltype(e.refresh(ec)), void>::value,
                  "operation must return void");
  }
  {
    directory_entry e;
    std::error_code ec = GetTestEC();
    e.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
  }
}

TEST_CASE(refresh_on_file_dne) {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);

  const perms old_perms = status(dir).permissions();

  // test file doesn't exist
  {
    directory_entry ent(file);
    remove(file);
    TEST_CHECK(ent.exists());

    ent.refresh();

    permissions(dir, perms::none);
    TEST_CHECK(!ent.exists());
  }
  permissions(dir, old_perms);
  env.create_file("dir/file", 101);
  {
    directory_entry ent(file);
    remove(file);
    TEST_CHECK(ent.exists());

    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));

    permissions(dir, perms::none);
    TEST_CHECK(!ent.exists());
  }
}

void remove_if_exists(const fs::path& p) {
  std::error_code ec;
  remove(p, ec);
}

TEST_CASE(refresh_on_bad_symlink) {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path sym = env.create_symlink("dir/file", "sym");

  const perms old_perms = status(dir).permissions();

  // test file doesn't exist
  {
    directory_entry ent(sym);
    LIBCPP_ONLY(remove(file));
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.exists());

    remove_if_exists(file);
    ent.refresh();

    LIBCPP_ONLY(permissions(dir, perms::none));
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(!ent.is_regular_file());
    TEST_CHECK(!ent.exists());
  }
  permissions(dir, old_perms);
  env.create_file("dir/file", 101);
  {
    directory_entry ent(sym);
    LIBCPP_ONLY(remove(file));
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.exists());

    remove_if_exists(file);

    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(!ec); // we don't report bad symlinks as an error.

    LIBCPP_ONLY(permissions(dir, perms::none));
    TEST_CHECK(!ent.exists());
  }
}

TEST_CASE(refresh_cannot_resolve) {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path file_out_of_dir = env.create_file("file1", 99);
  const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
  const path sym_in_dir = env.create_symlink("file1", "dir/sym1");
  perms old_perms = status(dir).permissions();

  {
    directory_entry ent(file);
    permissions(dir, perms::none);

    TEST_CHECK(ent.is_regular_file());

    std::error_code ec = GetTestEC();
    ent.refresh(ec);

    TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    TEST_CHECK(ent.path() == file);

    ExceptionChecker Checker(file, std::errc::permission_denied,
                             "directory_entry::refresh");
    TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.refresh());
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym_in_dir);
    permissions(dir, perms::none);
    TEST_CHECK(ent.is_symlink());

    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    TEST_CHECK(ent.path() == sym_in_dir);

    ExceptionChecker Checker(sym_in_dir, std::errc::permission_denied,
                             "directory_entry::refresh");
    TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.refresh());
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym_out_of_dir);
    permissions(dir, perms::none);
    TEST_CHECK(ent.is_symlink());

    // Failure to resolve the linked entity due to permissions is not
    // reported as an error.
    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(!ec);
    TEST_CHECK(ent.is_symlink());

    ec = GetTestEC();
    TEST_CHECK(ent.exists(ec) == false);
    TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    TEST_CHECK(ent.path() == sym_out_of_dir);
  }
  permissions(dir, old_perms);
  {
    directory_entry ent_file(file);
    directory_entry ent_sym(sym_in_dir);
    directory_entry ent_sym2(sym_out_of_dir);
    permissions(dir, perms::none);
    ((void)ent_file);
    ((void)ent_sym);

    TEST_CHECK_THROW(filesystem_error, ent_file.refresh());
    TEST_CHECK_THROW(filesystem_error, ent_sym.refresh());
    TEST_CHECK_NO_THROW(ent_sym2);
  }
}

TEST_CASE(refresh_doesnt_throw_on_dne_but_reports_it) {
  using namespace fs;
  scoped_test_env env;

  const path file = env.create_file("file1", 42);
  const path sym = env.create_symlink("file1", "sym");

  {
    directory_entry ent(file);
    TEST_CHECK(ent.file_size() == 42);

    remove(file);

    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
    TEST_CHECK_NO_THROW(ent.refresh());

    ec = GetTestEC();
    TEST_CHECK(ent.file_size(ec) == uintmax_t(-1));
    TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));

    // doesn't throw!
    TEST_CHECK_THROW(filesystem_error, ent.file_size());
  }
  env.create_file("file1", 99);
  {
    directory_entry ent(sym);
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.file_size() == 99);

    remove(file);

    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(!ec);

    ec = GetTestEC();
    TEST_CHECK(ent.file_size(ec) == uintmax_t(-1));
    TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));

    TEST_CHECK_THROW(filesystem_error, ent.file_size());
  }
}

TEST_CASE(access_cache_after_refresh_fails) {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path file_out_of_dir = env.create_file("file1", 101);
  const path sym = env.create_symlink("dir/file", "sym");
  const path sym_in_dir = env.create_symlink("dir/file", "dir/sym2");

  const perms old_perms = status(dir).permissions();

#define CHECK_ACCESS(func, expect)                                             \
  ec = GetTestEC();                                                            \
  TEST_CHECK(ent.func(ec) == expect);                                          \
  TEST_CHECK(ErrorIs(ec, std::errc::permission_denied))

  // test file doesn't exist
  {
    directory_entry ent(file);

    TEST_CHECK(!ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.exists());

    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));

    CHECK_ACCESS(exists, false);
    CHECK_ACCESS(is_symlink, false);
    CHECK_ACCESS(last_write_time, file_time_type::min());
    CHECK_ACCESS(hard_link_count, uintmax_t(-1));
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym_in_dir);
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.exists());

    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));

    CHECK_ACCESS(exists, false);
    CHECK_ACCESS(is_symlink, false);
    CHECK_ACCESS(last_write_time, file_time_type::min());
    CHECK_ACCESS(hard_link_count, uintmax_t(-1));
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym);
    TEST_CHECK(ent.is_symlink());
    TEST_CHECK(ent.is_regular_file());
    TEST_CHECK(ent.exists());

    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.refresh(ec);
    TEST_CHECK(!ec);
    TEST_CHECK(ent.is_symlink());

    CHECK_ACCESS(exists, false);
    CHECK_ACCESS(is_regular_file, false);
    CHECK_ACCESS(last_write_time, file_time_type::min());
    CHECK_ACCESS(hard_link_count, uintmax_t(-1));
  }
#undef CHECK_ACCESS
}

TEST_SUITE_END()
OpenPOWER on IntegriCloud