summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/file_system_quota_manager_unittest.cc
blob: b22a33f9eb11ff24dec5ef4f529a629a02f8adce (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
// Copyright (c) 2010 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/file_system_quota_manager.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "base/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/quota/special_storage_policy.h"

using namespace fileapi;

namespace {

static const char* const kTestOrigins[] = {
  "https://a.com/",
  "http://b.com/",
  "http://c.com:1/",
  "file:///",
};

class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy {
 public:
  virtual bool IsStorageProtected(const GURL& origin) {
    return false;
  }

  virtual bool IsStorageUnlimited(const GURL& origin) {
    return origin == GURL(kTestOrigins[1]);
  }
};

}  // anonymous namespace

TEST(FileSystemQuotaManagerTest, CheckNotAllowed) {
  FileSystemQuotaManager quota(false, false, NULL);
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
    SCOPED_TRACE(testing::Message() << "CheckOriginQuotaNotAllowed #"
                 << i << " " << kTestOrigins[i]);
    // Should fail no matter how much size is requested.
    GURL origin(kTestOrigins[i]);
    EXPECT_FALSE(quota.CheckOriginQuota(origin, -1));
    EXPECT_FALSE(quota.CheckOriginQuota(origin, 0));
    EXPECT_FALSE(quota.CheckOriginQuota(origin, 100));
  }
}

TEST(FileSystemQuotaManagerTest, CheckUnlimitedFlag) {
  FileSystemQuotaManager quota(false, true, NULL);
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
    SCOPED_TRACE(testing::Message() << "CheckOriginQuotaNotAllowed #"
                 << i << " " << kTestOrigins[i]);
    // Should succeed no matter how much size is requested.
    GURL origin(kTestOrigins[i]);
    EXPECT_TRUE(quota.CheckOriginQuota(origin, -1));
    EXPECT_TRUE(quota.CheckOriginQuota(origin, 0));
    EXPECT_TRUE(quota.CheckOriginQuota(origin, 100));
  }
}

TEST(FileSystemQuotaManagerTest, CheckAllowFileFlag) {
  FileSystemQuotaManager quota(true, false, NULL);
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
    SCOPED_TRACE(testing::Message() << "CheckOriginQuotaNotAllowed #"
                 << i << " " << kTestOrigins[i]);
    // Should succeed only for file:// urls
    GURL origin(kTestOrigins[i]);
    if (origin.SchemeIsFile()) {
      EXPECT_TRUE(quota.CheckOriginQuota(origin, -1));
      EXPECT_TRUE(quota.CheckOriginQuota(origin, 0));
      EXPECT_TRUE(quota.CheckOriginQuota(origin, 100));
    } else {
      EXPECT_FALSE(quota.CheckOriginQuota(origin, -1));
      EXPECT_FALSE(quota.CheckOriginQuota(origin, 0));
      EXPECT_FALSE(quota.CheckOriginQuota(origin, 100));
    }
  }
}

TEST(FileSystemQuotaManagerTest, CheckSpecialPolicy) {
  scoped_refptr<TestSpecialStoragePolicy> policy(new TestSpecialStoragePolicy);

  FileSystemQuotaManager quota(false, false, policy);
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
    SCOPED_TRACE(testing::Message() << "CheckOriginQuotaNotAllowed #"
                 << i << " " << kTestOrigins[i]);
    // Should succeed only for unlimited origins according to the policy.
    GURL origin(kTestOrigins[i]);
    if (policy->IsStorageUnlimited(origin)) {
      EXPECT_TRUE(quota.CheckOriginQuota(origin, -1));
      EXPECT_TRUE(quota.CheckOriginQuota(origin, 0));
      EXPECT_TRUE(quota.CheckOriginQuota(origin, 100));
    } else {
      EXPECT_FALSE(quota.CheckOriginQuota(origin, -1));
      EXPECT_FALSE(quota.CheckOriginQuota(origin, 0));
      EXPECT_FALSE(quota.CheckOriginQuota(origin, 100));
    }
  }
}