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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 | // Copyright (C) 2015-2025 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <exceptions/exceptions.h>
#include <testutils/gtest_utils.h>
#include <util/filesystem.h>
#include <sys/stat.h>
#include <fstream>
#include <list>
#include <string>
#include <cstdlib>
#include <gtest/gtest.h>
using namespace isc;
using namespace isc::util::file;
using namespace std;
namespace {
/// @brief Test fixture class for testing operations on files.
struct FileUtilTest : ::testing::Test {
/// @brief Destructor.
///
/// Deletes the test file if any.
virtual ~FileUtilTest() {<--- Destructor in derived class
string test_file_name(TEST_DATA_BUILDDIR "/fu.test");
static_cast<void>(remove(test_file_name.c_str()));
}
};
/// @brief Check that an error is returned by getContent on non-existent file.
TEST_F(FileUtilTest, notExist) {
EXPECT_THROW_MSG(getContent("/does/not/exist"), BadValue,
"Expected a file at path '/does/not/exist'");
}
/// @brief Check that an error is returned by getContent on not regular file.
TEST_F(FileUtilTest, notRegular) {
EXPECT_THROW_MSG(getContent("/"), BadValue, "Expected '/' to be a regular file");
}
/// @brief Check getContent.
TEST_F(FileUtilTest, getContent) {
string file_name(TEST_DATA_BUILDDIR "/fu.test");
ofstream fs(file_name.c_str(), ofstream::out | ofstream::trunc);
ASSERT_TRUE(fs.is_open());
fs << "abdc";
fs.close();
string content;
EXPECT_NO_THROW_LOG(content = getContent(file_name));
EXPECT_EQ("abdc", content);
}
/// @brief Check isDir.
TEST_F(FileUtilTest, isDir) {
EXPECT_TRUE(isDir("/dev"));
EXPECT_FALSE(isDir("/dev/null"));
EXPECT_FALSE(isDir("/this/does/not/exist"));
EXPECT_FALSE(isDir("/etc/hosts"));
}
/// @brief Check isFile.
TEST_F(FileUtilTest, isFile) {
EXPECT_TRUE(isFile(ABS_SRCDIR "/filesystem_unittests.cc"));
EXPECT_FALSE(isFile(TEST_DATA_BUILDDIR));
}
/// @brief Check hasPermissions.
TEST_F(FileUtilTest, hasPermissions) {
const std::string path = ABS_SRCDIR "/filesystem_unittests.cc";
ASSERT_TRUE(isFile(path));
mode_t current_permissions = getPermissions(path);
EXPECT_TRUE(hasPermissions(path, current_permissions));
current_permissions = ~current_permissions;
EXPECT_FALSE(hasPermissions(path, current_permissions));
}
/// @brief Test fixture class for testing operations on umask.
struct UMaskUtilTest : ::testing::Test {
/// @brief Constructor.
///
/// Cache the original umask value.
UMaskUtilTest() : orig_umask_(umask(S_IWGRP | S_IWOTH)) { }
/// @brief Destructor.
///
/// Restore the original umask value.
virtual ~UMaskUtilTest() {<--- Destructor in derived class
static_cast<void>(umask(orig_umask_));
}
private:
/// @brief Original umask.
mode_t orig_umask_;
};
/// @brief Check setUmask from 0000.
TEST_F(UMaskUtilTest, umask0) {
static_cast<void>(umask(0));
ASSERT_NO_THROW(setUmask());
EXPECT_EQ(S_IWGRP | S_IRWXO, umask(0));
}
/// @brief Check setUmask from no group access.
TEST_F(UMaskUtilTest, umask077) {
static_cast<void>(umask(S_IRWXG | S_IRWXO));
ASSERT_NO_THROW(setUmask());
EXPECT_EQ(S_IRWXG | S_IRWXO, umask(0));
}
/// @brief Check that the components are split correctly.
TEST(PathTest, components) {
// Complete name
Path fname("/alpha/beta/gamma.delta");
EXPECT_EQ("/alpha/beta/gamma.delta", fname.str());
EXPECT_EQ("/alpha/beta", fname.parentPath());
EXPECT_EQ("/alpha/beta/", fname.parentDirectory());
EXPECT_EQ("gamma", fname.stem());
EXPECT_EQ(".delta", fname.extension());
EXPECT_EQ("gamma.delta", fname.filename());
// The root.
Path root("/");
EXPECT_EQ("/", root.str());
EXPECT_EQ("", root.parentPath());
EXPECT_EQ("/", root.parentDirectory());
EXPECT_EQ("", root.stem());
EXPECT_EQ("", root.extension());
EXPECT_EQ("", root.filename());
// In the root directory.
Path inroot("/gamma.delta");
EXPECT_EQ("/gamma.delta", inroot.str());
EXPECT_EQ("", inroot.parentPath());
EXPECT_EQ("/", inroot.parentDirectory());
EXPECT_EQ("gamma", inroot.stem());
EXPECT_EQ(".delta", inroot.extension());
EXPECT_EQ("gamma.delta", inroot.filename());
// No directory.
Path nodir("gamma.delta");
EXPECT_EQ("gamma.delta", nodir.str());
EXPECT_EQ("", nodir.parentPath());
EXPECT_EQ("", nodir.parentDirectory());
EXPECT_EQ("gamma", nodir.stem());
EXPECT_EQ(".delta", nodir.extension());
EXPECT_EQ("gamma.delta", nodir.filename());
// Relative name.
Path relative("../alpha/gamma.delta");
EXPECT_EQ("../alpha/gamma.delta", relative.str());
EXPECT_EQ("../alpha", relative.parentPath());
EXPECT_EQ("../alpha/", relative.parentDirectory());
EXPECT_EQ("gamma", relative.stem());
EXPECT_EQ(".delta", relative.extension());
EXPECT_EQ("gamma.delta", relative.filename());
// Multiple extensions.
Path extensions("/alpha/beta/gamma.delta.epsilon");
EXPECT_EQ("/alpha/beta/gamma.delta.epsilon", extensions.str());
EXPECT_EQ("/alpha/beta", extensions.parentPath());
EXPECT_EQ("/alpha/beta/", extensions.parentDirectory());
EXPECT_EQ("gamma.delta", extensions.stem());
EXPECT_EQ(".epsilon", extensions.extension());
EXPECT_EQ("gamma.delta.epsilon", extensions.filename());
}
/// @brief Check replaceExtension.
TEST(PathTest, replaceExtension) {
Path fname("a.b");
EXPECT_EQ("a.b", fname.str());
EXPECT_EQ("a", fname.replaceExtension("").str());
EXPECT_EQ("a.f", fname.replaceExtension(".f").str());
EXPECT_EQ("a.f", fname.replaceExtension("f").str());
EXPECT_EQ("a./c/d/", fname.replaceExtension(" /c/d/ ").str());
EXPECT_EQ("a.f", fname.replaceExtension("/c/d/e.f").str());
EXPECT_EQ("a.f", fname.replaceExtension("e.f").str());
}
/// @brief Check replaceParentPath.
TEST(PathTest, replaceParentPath) {
Path fname("a.b");
EXPECT_EQ("", fname.parentPath());
EXPECT_EQ("", fname.parentDirectory());
EXPECT_EQ("a.b", fname.str());
fname.replaceParentPath("/just/some/dir/");
EXPECT_EQ("/just/some/dir", fname.parentPath());
EXPECT_EQ("/just/some/dir/", fname.parentDirectory());
EXPECT_EQ("/just/some/dir/a.b", fname.str());
fname.replaceParentPath("/just/some/dir");
EXPECT_EQ("/just/some/dir", fname.parentPath());
EXPECT_EQ("/just/some/dir/", fname.parentDirectory());
EXPECT_EQ("/just/some/dir/a.b", fname.str());
fname.replaceParentPath("/");
EXPECT_EQ("", fname.parentPath());
EXPECT_EQ("/", fname.parentDirectory());
EXPECT_EQ("/a.b", fname.str());
fname.replaceParentPath("");
EXPECT_EQ("", fname.parentPath());
EXPECT_EQ("", fname.parentDirectory());
EXPECT_EQ("a.b", fname.str());
fname = Path("/first/a.b");
EXPECT_EQ("/first", fname.parentPath());
EXPECT_EQ("/first/", fname.parentDirectory());
EXPECT_EQ("/first/a.b", fname.str());
fname.replaceParentPath("/just/some/dir");
EXPECT_EQ("/just/some/dir", fname.parentPath());
EXPECT_EQ("/just/some/dir/", fname.parentDirectory());
EXPECT_EQ("/just/some/dir/a.b", fname.str());
}
/// @brief Test fixture for testing PathChecker.
class PathCheckerTest : public ::testing::Test {
public:
/// @brief Constructor
PathCheckerTest() {
env_name_ = "KEA_PATCHECKER_TEST_PATH";<--- Variable 'env_name_' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'env_name_' a value by passing the value to the constructor in the initialization list.
// Save current value of the environment path.
char* env_path = std::getenv(env_name_.c_str());<--- Variable 'env_path' can be declared as pointer to const
if (env_path) {
original_path_ = std::string(env_name_.c_str());
}
// Clear the environment path.
unsetenv(env_name_.c_str());
PathChecker::enableEnforcement(true);
}
/// @brief Destructor
~PathCheckerTest() {<--- Destructor in derived class
// Restore the original environment path.
if (!original_path_.empty()) {
setenv(env_name_.c_str(), original_path_.c_str(), 1);
} else {
unsetenv(env_name_.c_str());
}
PathChecker::enableEnforcement(true);
}
/// @brief Name of env variable for overriding default path.
std::string env_name_;
/// @brief Retains the environment variable's original value.
std::string original_path_;
};
TEST_F(PathCheckerTest, getPathDefault) {
// No environment variable or explicit path should
// return the default path after construction.
ASSERT_FALSE(std::getenv(env_name_.c_str()));
PathChecker checker("/tmp/def_path", env_name_.c_str());
ASSERT_EQ(checker.getPath(), checker.getDefaultPath());
// A subsequent call with reset=true should do the same.
EXPECT_EQ(checker.getPath(true), checker.getDefaultPath());
}
TEST_F(PathCheckerTest, getPathEnvVariable) {
// Override default with an env variable upon construction.
setenv(env_name_.c_str(), "/tmp/override", 1);
ASSERT_TRUE(std::getenv(env_name_.c_str()));
PathChecker checker("/tmp/def_path", env_name_.c_str());
ASSERT_EQ(checker.getPath(), "/tmp/override");
// A subsequent call with reset=true should do the same.
EXPECT_EQ(checker.getPath(true), "/tmp/override");
}
TEST_F(PathCheckerTest, getPathExplicit) {
// Default construction with no env variable.
ASSERT_FALSE(std::getenv(env_name_.c_str()));
PathChecker checker("/tmp/def_path", env_name_.c_str());
ASSERT_EQ(checker.getPath(), checker.getDefaultPath());
// A subsequent call with reset=true and an explicit path
// should return the explicit path.
ASSERT_EQ(checker.getPath(true, "/tmp/explicit"),
"/tmp/explicit");
// A subsequent call with no arguments should do the same.
EXPECT_EQ(checker.getPath(), "/tmp/explicit");
}
// Verifies PathChecker::validatePath() when enforce_path is true.
TEST_F(PathCheckerTest, validatePathEnforcePath) {
std::string def_path(TEST_DATA_BUILDDIR);
struct Scenario {
int line_;
std::string lib_path_;
std::string exp_path_;
std::string exp_error_;
bool exp_security_err_;
};
std::list<Scenario> scenarios = {
{
// Invalid root parent path.
__LINE__,
"/mylib.so",
"",
string("invalid path specified: '/', supported path is '" + def_path + "'"),
true
},
{
// Invalid parent path.
__LINE__,
"/var/lib/bs/mylib.so",
"",
string("invalid path specified: '/var/lib/bs', supported path is '" + def_path + "'"),
true
},
{
// No file name.
__LINE__,
def_path + "/",
"",
string ("path: '" + def_path + "/' has no filename"),
false
},
{
// File name only is valid.
__LINE__,
"mylib.so",
def_path + "/mylib.so",
"",
false
},
{
// Valid full path.
__LINE__,
def_path + "/mylib.so",
def_path + "/mylib.so",
"",
false
},
{
// White space for file name.
__LINE__,
" ",
"",
string("path: '' has no filename"),
false
},
{
// Invalid relative path.
__LINE__,
"../kea/mylib.so",
"",
string("invalid path specified: '../kea', supported path is '" +
def_path + "'"),
true
}
};
// Create a PathChecker with a supported path of def_path.
PathChecker checker(def_path);
for (auto scenario : scenarios) {<--- Range variable 'scenario' should be declared as const reference.
std::ostringstream oss;
oss << " Scenario at line: " << scenario.line_;
SCOPED_TRACE(oss.str());
std::string validated_path;
if (scenario.exp_error_.empty()) {
ASSERT_NO_THROW_LOG(validated_path =
checker.validatePath(scenario.lib_path_));
EXPECT_EQ(validated_path, scenario.exp_path_);
} else {
if (scenario.exp_security_err_) {
ASSERT_THROW_MSG(validated_path =
checker.validatePath(scenario.lib_path_),
SecurityError, scenario.exp_error_);
} else {
ASSERT_THROW_MSG(validated_path =
checker.validatePath(scenario.lib_path_),
BadValue, scenario.exp_error_);
}
}
}
}
// Verifies PathChecker::validatePath() when enforce_path is false.
TEST_F(PathCheckerTest, validatePathEnforcePathFalse) {
std::string def_path(TEST_DATA_BUILDDIR);
struct Scenario {
int line_;
std::string lib_path_;
std::string exp_path_;
std::string exp_error_;
bool exp_security_warn_;
};
std::list<Scenario> scenarios = {
{
// Invalid root parent path.
__LINE__,
"/mylib.so",
"/mylib.so",
string("invalid path specified: '/', supported path is '" + def_path + "'"),
true
},
{
// Invalid parent path.
__LINE__,
"/var/lib/bs/mylib.so",
"/var/lib/bs/mylib.so",
string("invalid path specified: '/var/lib/bs', supported path is '"
+ def_path + "'"),
true
},
{
// No file name.
__LINE__,
def_path + "/",
"",
string ("path: '" + def_path + "/' has no filename"),
false
},
{
// File name only is valid.
__LINE__,
"mylib.so",
def_path + "/mylib.so",
"",
false
},
{
// Valid full path.
__LINE__,
def_path + "/mylib.so",
def_path + "/mylib.so",
"",
false
},
{
// White space for file name.
__LINE__,
" ",
"",
string("path: '' has no filename"),
false
}
};
ASSERT_TRUE(PathChecker::shouldEnforceSecurity());
PathChecker::enableEnforcement(false);
ASSERT_FALSE(PathChecker::shouldEnforceSecurity());
// Create a PathChecker with a supported path of def_path.
PathChecker checker(def_path);
for (auto scenario : scenarios) {<--- Range variable 'scenario' should be declared as const reference.
std::ostringstream oss;
oss << " Scenario at line: " << scenario.line_;
SCOPED_TRACE(oss.str());
std::string validated_path;
if (scenario.exp_error_.empty()) {
ASSERT_NO_THROW_LOG(validated_path =
checker.validatePath(scenario.lib_path_));
EXPECT_EQ(validated_path, scenario.exp_path_);
} else {
if (scenario.exp_security_warn_) {
ASSERT_THROW_MSG(validated_path =
checker.validatePath(scenario.lib_path_),
SecurityWarn, scenario.exp_error_);
} else {
ASSERT_THROW_MSG(validated_path =
checker.validatePath(scenario.lib_path_),
BadValue, scenario.exp_error_);
}
}
}
}
// Verifies PathChecker::validateDirectory() when enforce_path is true.
TEST_F(PathCheckerTest, validateDirectoryEnforcePath) {
std::string def_path(TEST_DATA_BUILDDIR);
struct Scenario {
int line_;
std::string lib_path_;
std::string exp_path_;
std::string exp_error_;
};
std::list<Scenario> scenarios = {
{
// Invalid path with slash.
__LINE__,
"/var/lib/bs/",
"",
string("invalid path specified: '/var/lib/bs/', supported path is '" + def_path + "'")
},
{
// Invalid path without slash.
__LINE__,
"/var/lib/bs",
"",
string("invalid path specified: '/var/lib/bs', supported path is '" + def_path + "'")
},
{
// File name is just another bad path.
__LINE__,
"mylib.so",
"",
string("invalid path specified: 'mylib.so', supported path is '" + def_path + "'")
},
{
// Valid full path with slash.
__LINE__,
def_path + "/",
def_path,
""
},
{
// Valid full path without slash.
__LINE__,
def_path,
def_path,
""
},
{
// Invalid relative path.
__LINE__,
"../kea/",
"",
string("invalid path specified: '../kea/', supported path is '" +
def_path + "'")
}
};
// Create a PathChecker with a supported path of def_path.
PathChecker checker(def_path);
for (auto scenario : scenarios) {<--- Range variable 'scenario' should be declared as const reference.
std::ostringstream oss;
oss << " Scenario at line: " << scenario.line_;
SCOPED_TRACE(oss.str());
std::string validated_path;
if (scenario.exp_error_.empty()) {
ASSERT_NO_THROW_LOG(validated_path =
checker.validateDirectory(scenario.lib_path_));
EXPECT_EQ(validated_path, scenario.exp_path_);
} else {
ASSERT_THROW_MSG(validated_path =
checker.validateDirectory(scenario.lib_path_),
SecurityError, scenario.exp_error_);
}
}
}
// Verifies PathChecker::validateDirectory() when enforce_path is false.
TEST_F(PathCheckerTest, validateDirectoryEnforcePathFalse) {
std::string def_path(TEST_DATA_BUILDDIR);
struct Scenario {
int line_;
std::string lib_path_;
std::string exp_path_;
std::string exp_error_;
};
std::list<Scenario> scenarios = {
{
// Invalid parent path but shouldn't care.
__LINE__,
"/var/lib/bs/",
"",
string("invalid path specified: '/var/lib/bs/', supported path is '" + def_path + "'")
},
{
// File name only is invalid.
__LINE__,
"mylib.so",
"",
string("invalid path specified: 'mylib.so', supported path is '" + def_path + "'")
},
{
// Valid full path.
__LINE__,
def_path + "/",
def_path,
""
},
{
// White space for file name.
__LINE__,
" ",
"",
string("invalid path specified: ' ', supported path is '" + def_path + "'")
}
};
ASSERT_TRUE(PathChecker::shouldEnforceSecurity());
PathChecker::enableEnforcement(false);
ASSERT_FALSE(PathChecker::shouldEnforceSecurity());
// Create a PathChecker with a supported path of def_path.
PathChecker checker(def_path);
for (auto scenario : scenarios) {<--- Range variable 'scenario' should be declared as const reference.
std::ostringstream oss;
oss << " Scenario at line: " << scenario.line_;
SCOPED_TRACE(oss.str());
std::string validated_path;
if (scenario.exp_error_.empty()) {
ASSERT_NO_THROW_LOG(validated_path =
checker.validateDirectory(scenario.lib_path_));
EXPECT_EQ(validated_path, scenario.exp_path_);
} else {
ASSERT_THROW_MSG(validated_path =
checker.validateDirectory(scenario.lib_path_),
SecurityWarn, scenario.exp_error_);
}
}
}
/// @brief Check pathHasPermissions.
TEST_F(PathCheckerTest, pathHasPermissions) {
PathChecker checker(TEST_DATA_BUILDDIR);
ASSERT_TRUE(PathChecker::shouldEnforceSecurity());
mode_t current_permissions = getPermissions(TEST_DATA_BUILDDIR);
EXPECT_TRUE(checker.pathHasPermissions(current_permissions));
current_permissions = ~current_permissions;
EXPECT_FALSE(checker.pathHasPermissions(current_permissions));
PathChecker::enableEnforcement(false);
ASSERT_FALSE(PathChecker::shouldEnforceSecurity());
EXPECT_TRUE(checker.pathHasPermissions(current_permissions));
}
} // namespace
|