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.
4
5#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
6
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"
15
16using storage::ExternalMountPoints;
17using storage::FileSystemURL;
18
19namespace sync_file_system {
20
21namespace {
22
23const char kSyncableFileSystemRootURI[] =
24    "filesystem:http://www.example.com/external/syncfs/";
25const char kNonRegisteredFileSystemRootURI[] =
26    "filesystem:http://www.example.com/external/non_registered/";
27const char kNonSyncableFileSystemRootURI[] =
28    "filesystem:http://www.example.com/temporary/";
29
30const char kOrigin[] = "http://www.example.com/";
31const base::FilePath::CharType kPath[] = FILE_PATH_LITERAL("dir/file");
32
33FileSystemURL CreateFileSystemURL(const std::string& url) {
34  return ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(url));
35}
36
37base::FilePath CreateNormalizedFilePath(const base::FilePath::CharType* path) {
38  return base::FilePath(path).NormalizePathSeparators();
39}
40
41}  // namespace
42
43TEST(SyncableFileSystemUtilTest, GetSyncableFileSystemRootURI) {
44  const GURL root = GetSyncableFileSystemRootURI(GURL(kOrigin));
45  EXPECT_TRUE(root.is_valid());
46  EXPECT_EQ(GURL(kSyncableFileSystemRootURI), root);
47}
48
49TEST(SyncableFileSystemUtilTest, CreateSyncableFileSystemURL) {
50  RegisterSyncableFileSystem();
51
52  const base::FilePath path(kPath);
53  const FileSystemURL expected_url =
54      CreateFileSystemURL(kSyncableFileSystemRootURI + path.AsUTF8Unsafe());
55  const FileSystemURL url = CreateSyncableFileSystemURL(GURL(kOrigin), path);
56
57  EXPECT_TRUE(url.is_valid());
58  EXPECT_EQ(expected_url, url);
59
60  RevokeSyncableFileSystem();
61}
62
63TEST(SyncableFileSystemUtilTest,
64       SerializeAndDesirializeSyncableFileSystemURL) {
65  RegisterSyncableFileSystem();
66
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));
72
73  std::string serialized;
74  EXPECT_TRUE(SerializeSyncableFileSystemURL(url, &serialized));
75  EXPECT_EQ(expected_url_str, serialized);
76
77  FileSystemURL deserialized;
78  EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized, &deserialized));
79  EXPECT_TRUE(deserialized.is_valid());
80  EXPECT_EQ(expected_url, deserialized);
81
82  RevokeSyncableFileSystem();
83}
84
85TEST(SyncableFileSystemUtilTest,
86     FailInSerializingAndDeserializingSyncableFileSystemURL) {
87  RegisterSyncableFileSystem();
88
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();
94
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));
102
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));
110
111  RevokeSyncableFileSystem();
112}
113
114TEST(SyncableFileSystemUtilTest, SyncableFileSystemURL_IsParent) {
115  RegisterSyncableFileSystem();
116
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();
121
122  const std::string parent("dir");
123  const std::string child("dir/child");
124
125  // True case.
126  EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent(
127      CreateFileSystemURL(root1 + child)));
128  EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent(
129      CreateFileSystemURL(root2 + child)));
130
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)));
136
137  RevokeSyncableFileSystem();
138}
139
140}  // namespace sync_file_system
141