1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "webkit/chromeos/fileapi/file_access_permissions.h"
6 #include "testing/gtest/include/gtest/gtest.h"
9 class FileAccessPermissionsTest
: public testing::Test
{
12 TEST_F(FileAccessPermissionsTest
, FileAccessChecks
) {
14 base::FilePath
good_dir(FILE_PATH_LITERAL("c:\\root\\dir"));
15 base::FilePath
bad_dir(FILE_PATH_LITERAL("c:\\root"));
16 base::FilePath
good_file(FILE_PATH_LITERAL("c:\\root\\dir\\good_file.txt"));
17 base::FilePath
bad_file(FILE_PATH_LITERAL("c:\\root\\dir\\bad_file.txt"));
18 #elif defined(OS_POSIX)
19 base::FilePath
good_dir(FILE_PATH_LITERAL("/root/dir"));
20 base::FilePath
bad_dir(FILE_PATH_LITERAL("/root"));
21 base::FilePath
good_file(FILE_PATH_LITERAL("/root/dir/good_file.txt"));
22 base::FilePath
bad_file(FILE_PATH_LITERAL("/root/dir/bad_file.txt"));
24 std::string
extension1("ddammdhioacbehjngdmkjcjbnfginlla");
25 std::string
extension2("jkhdjkhkhsdkfhsdkhrterwmtermeter");
27 chromeos::FileAccessPermissions permissions
;
28 // By default extension have no access to any local file.
29 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_dir
));
30 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_file
));
31 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, bad_file
));
32 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_dir
));
33 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_file
));
34 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, bad_file
));
36 // After granting file access to the handler extension for a given file, it
37 // can only access that file an nothing else.
38 permissions
.GrantAccessPermission(extension1
, good_file
);
39 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_dir
));
40 EXPECT_TRUE(permissions
.HasAccessPermission(extension1
, good_file
));
41 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, bad_file
));
42 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_dir
));
43 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_file
));
44 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, bad_file
));
47 // After granting file access to the handler extension for a given directory,
48 // it can access that directory and all files within it.
49 permissions
.GrantAccessPermission(extension2
, good_dir
);
50 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_dir
));
51 EXPECT_TRUE(permissions
.HasAccessPermission(extension1
, good_file
));
52 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, bad_file
));
53 EXPECT_TRUE(permissions
.HasAccessPermission(extension2
, good_dir
));
54 EXPECT_TRUE(permissions
.HasAccessPermission(extension2
, good_file
));
55 EXPECT_TRUE(permissions
.HasAccessPermission(extension2
, bad_file
));
57 // After revoking rights for extensions, they should not be able to access
58 // any file system element anymore.
59 permissions
.RevokePermissions(extension1
);
60 permissions
.RevokePermissions(extension2
);
61 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_dir
));
62 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, good_file
));
63 EXPECT_FALSE(permissions
.HasAccessPermission(extension1
, bad_file
));
64 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_dir
));
65 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, good_file
));
66 EXPECT_FALSE(permissions
.HasAccessPermission(extension2
, bad_file
));