summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/sandbox_mount_point_provider_unittest.cc
blob: 8bf1feff60cc7d9438ff973a063082741cf8d61e (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
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/fileapi/sandbox_mount_point_provider.h"

#include <set>
#include <string>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/platform_file.h"
#include "base/scoped_temp_dir.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_mount_point_provider.h"
#include "webkit/fileapi/file_system_operation_context.h"
#include "webkit/fileapi/file_system_util.h"
#include "webkit/fileapi/mock_file_system_options.h"
#include "webkit/quota/mock_special_storage_policy.h"

namespace fileapi {

class SandboxMountPointProviderOriginEnumeratorTest : public testing::Test {
 public:
  void SetUp() {
    ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
    sandbox_provider_.reset(
        new SandboxMountPointProvider(
          base::MessageLoopProxy::current(),
          data_dir_.path(),
          CreateAllowFileAccessOptions()));
  }

  SandboxMountPointProvider::OriginEnumerator* CreateEnumerator() const {
    return sandbox_provider_->CreateOriginEnumerator();
  }

 protected:
  void CreateOriginTypeDirectory(const GURL& origin,
                                 fileapi::FileSystemType type) {
    FilePath target = sandbox_provider_->
        GetBaseDirectoryForOriginAndType(origin, type, true);
    ASSERT_TRUE(!target.empty());
    ASSERT_TRUE(file_util::DirectoryExists(target));
  }

  ScopedTempDir data_dir_;
  MessageLoop message_loop_;
  scoped_ptr<SandboxMountPointProvider> sandbox_provider_;
};

TEST_F(SandboxMountPointProviderOriginEnumeratorTest, Empty) {
  scoped_ptr<SandboxMountPointProvider::OriginEnumerator> enumerator(
      CreateEnumerator());
  ASSERT_TRUE(enumerator->Next().is_empty());
}

TEST_F(SandboxMountPointProviderOriginEnumeratorTest, EnumerateOrigins) {
  const char* temporary_origins[] = {
    "http://www.bar.com/",
    "http://www.foo.com/",
    "http://www.foo.com:1/",
    "http://www.example.com:8080/",
    "http://www.google.com:80/",
  };
  const char* persistent_origins[] = {
    "http://www.bar.com/",
    "http://www.foo.com:8080/",
    "http://www.foo.com:80/",
  };
  size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins);
  size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins);
  std::set<GURL> temporary_set, persistent_set;
  for (size_t i = 0; i < temporary_size; ++i) {
    CreateOriginTypeDirectory(GURL(temporary_origins[i]),
        fileapi::kFileSystemTypeTemporary);
    temporary_set.insert(GURL(temporary_origins[i]));
  }
  for (size_t i = 0; i < persistent_size; ++i) {
    CreateOriginTypeDirectory(GURL(persistent_origins[i]),
        kFileSystemTypePersistent);
    persistent_set.insert(GURL(persistent_origins[i]));
  }

  scoped_ptr<SandboxMountPointProvider::OriginEnumerator> enumerator(
      CreateEnumerator());
  size_t temporary_actual_size = 0;
  size_t persistent_actual_size = 0;
  GURL current;
  while (!(current = enumerator->Next()).is_empty()) {
    SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
    if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) {
      ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
      ++temporary_actual_size;
    }
    if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) {
      ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
      ++persistent_actual_size;
    }
  }

  EXPECT_EQ(temporary_size, temporary_actual_size);
  EXPECT_EQ(persistent_size, persistent_actual_size);
}

namespace {

struct MigrationTestCaseRecord {
  GURL origin;
  bool has_temporary;
  bool has_persistent;
};

const MigrationTestCaseRecord kMigrationTestRecords[] = {
  { GURL("http://www.example.com"), true, false },
  { GURL("http://example.com"), false, true },
  { GURL("http://www.google.com"), false, true },
  { GURL("http://www.another.origin.com"), true, true },
  { GURL("http://www.yet.another.origin.com"), true, true },
  { GURL("file:///"), false, true },
};

}  // anonymous namespace

class SandboxMountPointProviderMigrationTest : public testing::Test {
 public:
  SandboxMountPointProviderMigrationTest() :
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
  }

  void SetUp() {
    ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
    scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy =
        new quota::MockSpecialStoragePolicy;
    special_storage_policy->SetAllUnlimited(true);
    file_system_context_ = new FileSystemContext(
        base::MessageLoopProxy::current(),
        base::MessageLoopProxy::current(),
        special_storage_policy,
        NULL,
        data_dir_.path(),
        CreateAllowFileAccessOptions());
  }

  SandboxMountPointProvider* sandbox_provider() {
    return file_system_context_->sandbox_provider();
  }

  FileSystemFileUtil* file_util() {
    return sandbox_provider()->GetFileUtil();
  }

  void OnValidate(base::PlatformFileError result) {
    EXPECT_NE(base::PLATFORM_FILE_OK, result);  // We told it not to create.
  }

  FileSystemMountPointProvider::ValidateFileSystemCallback
  GetValidateCallback() {
    return base::Bind(&SandboxMountPointProviderMigrationTest::OnValidate,
                      weak_factory_.GetWeakPtr());
  }

  void EnsureFileExists(const FilePath& path) {
    bool created = false;
    PlatformFileError error_code = base::PLATFORM_FILE_OK;
    PlatformFile handle = base::CreatePlatformFile(
        path,
        base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
        &created, &error_code);
    ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
    ASSERT_TRUE(created);
    ASSERT_NE(base::kInvalidPlatformFileValue, handle);
    base::ClosePlatformFile(handle);
  }

  void CreateDataInDir(const FilePath& root, const std::string& seed) {
    ASSERT_TRUE(file_util::CreateDirectory(
        root.AppendASCII(seed)));
    ASSERT_TRUE(file_util::CreateDirectory(
        root.AppendASCII(seed).AppendASCII(seed)));
    ASSERT_TRUE(file_util::CreateDirectory(
        root.AppendASCII(seed).AppendASCII("d 0")));
    ASSERT_TRUE(file_util::CreateDirectory(
        root.AppendASCII(seed).AppendASCII("d 1")));
    EnsureFileExists(root.AppendASCII("file 0"));
    EnsureFileExists(
        root.AppendASCII(seed).AppendASCII("d 0").AppendASCII("file 1"));
    EnsureFileExists(
        root.AppendASCII(seed).AppendASCII("d 0").AppendASCII("file 2"));
  }

  FileSystemOperationContext* NewContext() {
    return new FileSystemOperationContext(file_system_context_);
  }

  std::string URLAndTypeToSeedString(const GURL& origin_url,
    fileapi::FileSystemType type) {
    return GetOriginIdentifierFromURL(origin_url) +
        GetFileSystemTypeString(type);
  }

  void ValidateDataInNewFileSystem(
      const GURL& origin_url, fileapi::FileSystemType type) {
    scoped_ptr<FileSystemOperationContext> context;
    FilePath seed_file_path = FilePath().AppendASCII(
        URLAndTypeToSeedString(origin_url, type));

    FileSystemPath root(origin_url, type, FilePath());
    FileSystemPath seed = root.Append(seed_file_path);

    context.reset(NewContext());
    EXPECT_TRUE(file_util()->DirectoryExists(
        context.get(), seed));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->DirectoryExists(
        context.get(), seed.Append(seed_file_path)));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->DirectoryExists(
        context.get(), seed.AppendASCII("d 0")));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->DirectoryExists(
        context.get(), seed.AppendASCII("d 1")));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->PathExists(
        context.get(), root.AppendASCII("file 0")));
    context.reset(NewContext());
    EXPECT_FALSE(file_util()->DirectoryExists(
        context.get(), seed.AppendASCII("file 0")));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->PathExists(
        context.get(), seed.AppendASCII("d 0").AppendASCII("file 1")));
    context.reset(NewContext());
    EXPECT_FALSE(file_util()->DirectoryExists(
        context.get(), seed.AppendASCII("d 0").AppendASCII("file 1")));
    context.reset(NewContext());
    EXPECT_TRUE(file_util()->PathExists(
        context.get(), seed.AppendASCII("d 0").AppendASCII("file 2")));
    context.reset(NewContext());
    EXPECT_FALSE(file_util()->DirectoryExists(
        context.get(), seed.AppendASCII("d 0").AppendASCII("file 2")));
  }

  void RunMigrationTest(int method) {
    for (size_t i = 0; i < arraysize(kMigrationTestRecords); ++i) {
      const MigrationTestCaseRecord& test_case = kMigrationTestRecords[i];
      const GURL& origin_url = test_case.origin;
      ASSERT_TRUE(test_case.has_temporary || test_case.has_persistent);
      if (test_case.has_temporary) {
        FilePath root = sandbox_provider()->OldCreateFileSystemRootPath(
            origin_url, kFileSystemTypeTemporary);
        ASSERT_FALSE(root.empty());
        CreateDataInDir(root, URLAndTypeToSeedString(origin_url,
            kFileSystemTypeTemporary));
      }
      if (test_case.has_persistent) {
        FilePath root = sandbox_provider()->OldCreateFileSystemRootPath(
            origin_url, kFileSystemTypePersistent);
        ASSERT_FALSE(root.empty());
        CreateDataInDir(root, URLAndTypeToSeedString(origin_url,
            kFileSystemTypePersistent));
      }
    }

    const GURL origin_url("http://not.in.the.test.cases");
    fileapi::FileSystemType type = kFileSystemTypeTemporary;
    bool create = false;
    std::set<GURL> origins;
    std::string host = "the host with the most";
    int64 delta = 0;

    // We want to make sure that all the public methods of
    // SandboxMountPointProvider which might access the filesystem will cause a
    // migration if one is needed.
    switch (method) {
    case 0:
      sandbox_provider()->ValidateFileSystemRoot(
          origin_url, type, create, GetValidateCallback());
      MessageLoop::current()->RunAllPending();
      break;
    case 1:
      sandbox_provider()->GetFileSystemRootPathOnFileThread(
          origin_url, type, FilePath(), create);
      break;
    case 2:
      sandbox_provider()->GetBaseDirectoryForOriginAndType(
          origin_url, type, create);
      break;
    case 3:
      sandbox_provider()->DeleteOriginDataOnFileThread(
          file_system_context_, NULL, origin_url, type);
      break;
    case 4:
      sandbox_provider()->GetOriginsForTypeOnFileThread(
          type, &origins);
      break;
    case 5:
      sandbox_provider()->GetOriginsForHostOnFileThread(
          type, host, &origins);
      break;
    case 6:
      sandbox_provider()->GetOriginUsageOnFileThread(
          file_system_context_, origin_url, type);
      break;
    case 7:
      // This case has to use an origin that already exists in the
      // migrated data.
      sandbox_provider()->UpdateOriginUsageOnFileThread(
          NULL, kMigrationTestRecords[0].origin,
          kFileSystemTypeTemporary, delta);
      break;
    case 8:
      // This case has to use a filesystem that already exists in the
      // migrated data.
      sandbox_provider()->StartUpdateOriginOnFileThread(
          kMigrationTestRecords[0].origin, kFileSystemTypeTemporary);
      break;
    case 9:
      // This case has to use a filesystem that already exists in the
      // migrated data.
      sandbox_provider()->EndUpdateOriginOnFileThread(
          kMigrationTestRecords[0].origin, kFileSystemTypeTemporary);
      break;
    default:
      FAIL();
      break;
    }
    for (size_t i = 0; i < arraysize(kMigrationTestRecords); ++i) {
      const MigrationTestCaseRecord& test_case = kMigrationTestRecords[i];
      const GURL& origin_url = test_case.origin;
      ASSERT_TRUE(test_case.has_temporary || test_case.has_persistent);
      if (test_case.has_temporary)
        ValidateDataInNewFileSystem(origin_url, kFileSystemTypeTemporary);
      if (test_case.has_persistent)
        ValidateDataInNewFileSystem(origin_url, kFileSystemTypePersistent);
    }
  }

 protected:
  ScopedTempDir data_dir_;
  MessageLoop message_loop_;
  scoped_refptr<FileSystemContext> file_system_context_;
  base::WeakPtrFactory<SandboxMountPointProviderMigrationTest> weak_factory_;
};

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod0) {
  RunMigrationTest(0);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod1) {
  RunMigrationTest(1);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod2) {
  RunMigrationTest(2);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod3) {
  RunMigrationTest(3);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod4) {
  RunMigrationTest(4);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod5) {
  RunMigrationTest(5);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod6) {
  RunMigrationTest(6);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod7) {
  RunMigrationTest(7);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod8) {
  RunMigrationTest(8);
}

TEST_F(SandboxMountPointProviderMigrationTest, TestMigrateViaMethod9) {
  RunMigrationTest(9);
}

}  // namespace fileapi