1 // Copyright 2013 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 "chrome/browser/sync_file_system/syncable_file_system_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
11 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
12 #include "storage/browser/fileapi/external_mount_points.h"
13 #include "storage/common/fileapi/file_system_types.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using storage::ExternalMountPoints
;
17 using storage::FileSystemURL
;
19 namespace sync_file_system
{
23 const char kSyncableFileSystemRootURI
[] =
24 "filesystem:http://www.example.com/external/syncfs/";
25 const char kNonRegisteredFileSystemRootURI
[] =
26 "filesystem:http://www.example.com/external/non_registered/";
27 const char kNonSyncableFileSystemRootURI
[] =
28 "filesystem:http://www.example.com/temporary/";
30 const char kOrigin
[] = "http://www.example.com/";
31 const base::FilePath::CharType kPath
[] = FILE_PATH_LITERAL("dir/file");
33 FileSystemURL
CreateFileSystemURL(const std::string
& url
) {
34 return ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(url
));
37 base::FilePath
CreateNormalizedFilePath(const base::FilePath::CharType
* path
) {
38 return base::FilePath(path
).NormalizePathSeparators();
43 TEST(SyncableFileSystemUtilTest
, GetSyncableFileSystemRootURI
) {
44 const GURL root
= GetSyncableFileSystemRootURI(GURL(kOrigin
));
45 EXPECT_TRUE(root
.is_valid());
46 EXPECT_EQ(GURL(kSyncableFileSystemRootURI
), root
);
49 TEST(SyncableFileSystemUtilTest
, CreateSyncableFileSystemURL
) {
50 RegisterSyncableFileSystem();
52 const base::FilePath
path(kPath
);
53 const FileSystemURL expected_url
=
54 CreateFileSystemURL(kSyncableFileSystemRootURI
+ path
.AsUTF8Unsafe());
55 const FileSystemURL url
= CreateSyncableFileSystemURL(GURL(kOrigin
), path
);
57 EXPECT_TRUE(url
.is_valid());
58 EXPECT_EQ(expected_url
, url
);
60 RevokeSyncableFileSystem();
63 TEST(SyncableFileSystemUtilTest
,
64 SerializeAndDesirializeSyncableFileSystemURL
) {
65 RegisterSyncableFileSystem();
67 const std::string expected_url_str
= kSyncableFileSystemRootURI
+
68 CreateNormalizedFilePath(kPath
).AsUTF8Unsafe();
69 const FileSystemURL expected_url
= CreateFileSystemURL(expected_url_str
);
70 const FileSystemURL url
= CreateSyncableFileSystemURL(
71 GURL(kOrigin
), base::FilePath(kPath
));
73 std::string serialized
;
74 EXPECT_TRUE(SerializeSyncableFileSystemURL(url
, &serialized
));
75 EXPECT_EQ(expected_url_str
, serialized
);
77 FileSystemURL deserialized
;
78 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized
, &deserialized
));
79 EXPECT_TRUE(deserialized
.is_valid());
80 EXPECT_EQ(expected_url
, deserialized
);
82 RevokeSyncableFileSystem();
85 TEST(SyncableFileSystemUtilTest
,
86 FailInSerializingAndDeserializingSyncableFileSystemURL
) {
87 RegisterSyncableFileSystem();
89 const base::FilePath normalized_path
= CreateNormalizedFilePath(kPath
);
90 const std::string non_registered_url
=
91 kNonRegisteredFileSystemRootURI
+ normalized_path
.AsUTF8Unsafe();
92 const std::string non_syncable_url
=
93 kNonSyncableFileSystemRootURI
+ normalized_path
.AsUTF8Unsafe();
95 // Expected to fail in serializing URLs of non-registered filesystem and
96 // non-syncable filesystem.
97 std::string serialized
;
98 EXPECT_FALSE(SerializeSyncableFileSystemURL(
99 CreateFileSystemURL(non_registered_url
), &serialized
));
100 EXPECT_FALSE(SerializeSyncableFileSystemURL(
101 CreateFileSystemURL(non_syncable_url
), &serialized
));
103 // Expected to fail in deserializing a string that represents URLs of
104 // non-registered filesystem and non-syncable filesystem.
105 FileSystemURL deserialized
;
106 EXPECT_FALSE(DeserializeSyncableFileSystemURL(
107 non_registered_url
, &deserialized
));
108 EXPECT_FALSE(DeserializeSyncableFileSystemURL(
109 non_syncable_url
, &deserialized
));
111 RevokeSyncableFileSystem();
114 TEST(SyncableFileSystemUtilTest
, SyncableFileSystemURL_IsParent
) {
115 RegisterSyncableFileSystem();
117 const std::string root1
= sync_file_system::GetSyncableFileSystemRootURI(
118 GURL("http://foo.com")).spec();
119 const std::string root2
= sync_file_system::GetSyncableFileSystemRootURI(
120 GURL("http://bar.com")).spec();
122 const std::string
parent("dir");
123 const std::string
child("dir/child");
126 EXPECT_TRUE(CreateFileSystemURL(root1
+ parent
).IsParent(
127 CreateFileSystemURL(root1
+ child
)));
128 EXPECT_TRUE(CreateFileSystemURL(root2
+ parent
).IsParent(
129 CreateFileSystemURL(root2
+ child
)));
131 // False case: different origin.
132 EXPECT_FALSE(CreateFileSystemURL(root1
+ parent
).IsParent(
133 CreateFileSystemURL(root2
+ child
)));
134 EXPECT_FALSE(CreateFileSystemURL(root2
+ parent
).IsParent(
135 CreateFileSystemURL(root1
+ child
)));
137 RevokeSyncableFileSystem();
140 } // namespace sync_file_system