/src/mozilla-central/dom/quota/test/gtest/TestQuotaManager.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "gtest/gtest.h" |
8 | | #include "mozilla/dom/quota/OriginScope.h" |
9 | | |
10 | | using namespace mozilla; |
11 | | using namespace mozilla::dom::quota; |
12 | | |
13 | | namespace { |
14 | | |
15 | | struct OriginTest { |
16 | | const char* mOrigin; |
17 | | bool mMatch; |
18 | | }; |
19 | | |
20 | | void |
21 | | CheckOriginScopeMatchesOrigin(const OriginScope& aOriginScope, |
22 | | const char* aOrigin, |
23 | | bool aMatch) |
24 | 0 | { |
25 | 0 | bool result = |
26 | 0 | aOriginScope.Matches(OriginScope::FromOrigin(nsDependentCString(aOrigin))); |
27 | 0 |
|
28 | 0 | EXPECT_TRUE(result == aMatch); |
29 | 0 | } |
30 | | |
31 | | } // namespace |
32 | | |
33 | | TEST(QuotaManager, OriginScope) |
34 | 0 | { |
35 | 0 | OriginScope originScope; |
36 | 0 |
|
37 | 0 | // Sanity checks. |
38 | 0 |
|
39 | 0 | { |
40 | 0 | NS_NAMED_LITERAL_CSTRING(origin, "http://www.mozilla.org"); |
41 | 0 | originScope.SetFromOrigin(origin); |
42 | 0 | EXPECT_TRUE(originScope.IsOrigin()); |
43 | 0 | EXPECT_TRUE(originScope.GetOrigin().Equals(origin)); |
44 | 0 | EXPECT_TRUE(originScope.GetOriginNoSuffix().Equals(origin)); |
45 | 0 | } |
46 | 0 |
|
47 | 0 | { |
48 | 0 | NS_NAMED_LITERAL_CSTRING(prefix, "http://www.mozilla.org"); |
49 | 0 | originScope.SetFromPrefix(prefix); |
50 | 0 | EXPECT_TRUE(originScope.IsPrefix()); |
51 | 0 | EXPECT_TRUE(originScope.GetOriginNoSuffix().Equals(prefix)); |
52 | 0 | } |
53 | 0 |
|
54 | 0 | { |
55 | 0 | NS_NAMED_LITERAL_STRING(pattern, "{\"appId\":1007}"); |
56 | 0 | originScope.SetFromJSONPattern(pattern); |
57 | 0 | EXPECT_TRUE(originScope.IsPattern()); |
58 | 0 | EXPECT_TRUE(originScope.GetJSONPattern().Equals(pattern)); |
59 | 0 | } |
60 | 0 |
|
61 | 0 | { |
62 | 0 | originScope.SetFromNull(); |
63 | 0 | EXPECT_TRUE(originScope.IsNull()); |
64 | 0 | } |
65 | 0 |
|
66 | 0 | // Test each origin scope type against particular origins. |
67 | 0 |
|
68 | 0 | { |
69 | 0 | originScope.SetFromOrigin(NS_LITERAL_CSTRING("http://www.mozilla.org")); |
70 | 0 |
|
71 | 0 | static const OriginTest tests[] = { |
72 | 0 | { "http://www.mozilla.org", true }, |
73 | 0 | { "http://www.example.org", false }, |
74 | 0 | }; |
75 | 0 |
|
76 | 0 | for (const auto& test : tests) { |
77 | 0 | CheckOriginScopeMatchesOrigin(originScope, test.mOrigin, test.mMatch); |
78 | 0 | } |
79 | 0 | } |
80 | 0 |
|
81 | 0 | { |
82 | 0 | originScope.SetFromPrefix(NS_LITERAL_CSTRING("http://www.mozilla.org")); |
83 | 0 |
|
84 | 0 | static const OriginTest tests[] = { |
85 | 0 | { "http://www.mozilla.org", true }, |
86 | 0 | { "http://www.mozilla.org^userContextId=1", true }, |
87 | 0 | { "http://www.example.org^userContextId=1", false }, |
88 | 0 | }; |
89 | 0 |
|
90 | 0 | for (const auto& test : tests) { |
91 | 0 | CheckOriginScopeMatchesOrigin(originScope, test.mOrigin, test.mMatch); |
92 | 0 | } |
93 | 0 | } |
94 | 0 |
|
95 | 0 | { |
96 | 0 | originScope.SetFromJSONPattern(NS_LITERAL_STRING("{\"appId\":1007}")); |
97 | 0 |
|
98 | 0 | static const OriginTest tests[] = { |
99 | 0 | { "http+++www.mozilla.org^appId=1007", true }, |
100 | 0 | { "http+++www.example.org^appId=1007", true }, |
101 | 0 | { "http+++www.example.org^appId=1008", false }, |
102 | 0 | }; |
103 | 0 |
|
104 | 0 | for (const auto& test : tests) { |
105 | 0 | CheckOriginScopeMatchesOrigin(originScope, test.mOrigin, test.mMatch); |
106 | 0 | } |
107 | 0 | } |
108 | 0 |
|
109 | 0 | { |
110 | 0 | originScope.SetFromNull(); |
111 | 0 |
|
112 | 0 | static const OriginTest tests[] = { |
113 | 0 | { "http://www.mozilla.org", true }, |
114 | 0 | { "http://www.mozilla.org^userContextId=1", true }, |
115 | 0 | { "http://www.example.org^userContextId=1", true }, |
116 | 0 | { "http+++www.mozilla.org^appId=1007", true }, |
117 | 0 | { "http+++www.example.org^appId=1007", true }, |
118 | 0 | { "http+++www.example.org^appId=1008", true }, |
119 | 0 | }; |
120 | 0 |
|
121 | 0 | for (const auto& test : tests) { |
122 | 0 | CheckOriginScopeMatchesOrigin(originScope, test.mOrigin, test.mMatch); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |