Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/budget_service/budget_manager_unittest.cc

Issue 2366533002: Budget API calls should only succeed on secure origins (Closed)
Patch Set: Expanded unique origin test. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 #include <string> 6 #include <string>
7 7
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "chrome/browser/budget_service/budget_manager.h" 10 #include "chrome/browser/budget_service/budget_manager.h"
(...skipping 19 matching lines...) Expand all
30 public: 30 public:
31 BudgetManagerTest() : origin_(url::Origin(GURL(kTestOrigin))) {} 31 BudgetManagerTest() : origin_(url::Origin(GURL(kTestOrigin))) {}
32 ~BudgetManagerTest() override {} 32 ~BudgetManagerTest() override {}
33 33
34 BudgetManager* GetManager() { 34 BudgetManager* GetManager() {
35 return BudgetManagerFactory::GetForProfile(&profile_); 35 return BudgetManagerFactory::GetForProfile(&profile_);
36 } 36 }
37 37
38 void SetSiteEngagementScore(double score) { 38 void SetSiteEngagementScore(double score) {
39 SiteEngagementService* service = SiteEngagementService::Get(&profile_); 39 SiteEngagementService* service = SiteEngagementService::Get(&profile_);
40 service->ResetScoreForURL(GURL(kTestOrigin), score); 40 service->ResetScoreForURL(GURL(origin().Serialize()), score);
41 } 41 }
42 42
43 Profile* profile() { return &profile_; } 43 Profile* profile() { return &profile_; }
44 const url::Origin origin() const { return origin_; } 44 const url::Origin origin() const { return origin_; }
45 void SetOrigin(const url::Origin& origin) { origin_ = origin; }
45 46
46 void ReserveCallback(base::Closure run_loop_closure, 47 void ReserveCallback(base::Closure run_loop_closure,
47 blink::mojom::BudgetServiceErrorType error, 48 blink::mojom::BudgetServiceErrorType error,
48 bool success) { 49 bool success) {
49 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE) && success; 50 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE) && success;
51 error_ = error;
50 run_loop_closure.Run(); 52 run_loop_closure.Run();
51 } 53 }
52 54
53 void StatusCallback(base::Closure run_loop_closure, bool success) { 55 void StatusCallback(base::Closure run_loop_closure, bool success) {
54 success_ = success; 56 success_ = success;
55 run_loop_closure.Run(); 57 run_loop_closure.Run();
56 } 58 }
57 59
58 bool ReserveBudget(blink::mojom::BudgetOperationType type) { 60 bool ReserveBudget(blink::mojom::BudgetOperationType type) {
59 base::RunLoop run_loop; 61 base::RunLoop run_loop;
(...skipping 10 matching lines...) Expand all
70 GetManager()->Consume( 72 GetManager()->Consume(
71 origin(), type, 73 origin(), type,
72 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), 74 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this),
73 run_loop.QuitClosure())); 75 run_loop.QuitClosure()));
74 run_loop.Run(); 76 run_loop.Run();
75 return success_; 77 return success_;
76 } 78 }
77 79
78 // Members for callbacks to set. 80 // Members for callbacks to set.
79 bool success_; 81 bool success_;
82 blink::mojom::BudgetServiceErrorType error_;
80 83
81 private: 84 private:
82 content::TestBrowserThreadBundle thread_bundle_; 85 content::TestBrowserThreadBundle thread_bundle_;
83 TestingProfile profile_; 86 TestingProfile profile_;
84 const url::Origin origin_; 87 url::Origin origin_;
85 }; 88 };
86 89
87 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { 90 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) {
88 // Set initial SES. The first time we try to spend budget, the 91 // Set initial SES. The first time we try to spend budget, the
89 // engagement award will be granted which is 48.0. 92 // engagement award will be granted which is 48.0.
90 SetSiteEngagementScore(kTestSES); 93 SetSiteEngagementScore(kTestSES);
91 const blink::mojom::BudgetOperationType type = 94 const blink::mojom::BudgetOperationType type =
92 blink::mojom::BudgetOperationType::SILENT_PUSH; 95 blink::mojom::BudgetOperationType::SILENT_PUSH;
93 96
94 // Spend for 24 silent push messages. This should consume all the original 97 // Spend for 24 silent push messages. This should consume all the original
95 // budget grant. 98 // budget grant.
96 for (int i = 0; i < 24; i++) 99 for (int i = 0; i < 24; i++)
97 ASSERT_TRUE(ReserveBudget(type)); 100 ASSERT_TRUE(ReserveBudget(type));
98 101
99 // Try to send one final silent push. The origin should be out of budget. 102 // Try to send one final silent push. The origin should be out of budget.
100 ASSERT_FALSE(ReserveBudget(type)); 103 ASSERT_FALSE(ReserveBudget(type));
101 104
102 // Try to consume for the 24 messages reserved. 105 // Try to consume for the 24 messages reserved.
103 for (int i = 0; i < 24; i++) 106 for (int i = 0; i < 24; i++)
104 ASSERT_TRUE(ConsumeBudget(type)); 107 ASSERT_TRUE(ConsumeBudget(type));
105 108
106 // The next consume should fail, since there is no reservation or budget 109 // The next consume should fail, since there is no reservation or budget
107 // available. 110 // available.
108 ASSERT_FALSE(ConsumeBudget(type)); 111 ASSERT_FALSE(ConsumeBudget(type));
109 } 112 }
113
114 TEST_F(BudgetManagerTest, TestInsecureOrigin) {
115 const blink::mojom::BudgetOperationType type =
116 blink::mojom::BudgetOperationType::SILENT_PUSH;
117 SetOrigin(url::Origin(GURL("http://example.com")));
118 SetSiteEngagementScore(kTestSES);
119
120 // Methods on the BudgetManager should only be allowed for secure origins.
121 ASSERT_FALSE(ReserveBudget(type));
122 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_);
123 ASSERT_FALSE(ConsumeBudget(type));
124 }
125
126 TEST_F(BudgetManagerTest, TestUniqueOrigin) {
127 const blink::mojom::BudgetOperationType type =
128 blink::mojom::BudgetOperationType::SILENT_PUSH;
129 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd")));
130
131 // Methods on the BudgetManager should not be allowed for unique origins.
132 ASSERT_FALSE(ReserveBudget(type));
133 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_);
134 ASSERT_FALSE(ConsumeBudget(type));
135 }
OLDNEW
« no previous file with comments | « chrome/browser/budget_service/budget_manager.cc ('k') | third_party/WebKit/Source/modules/budget/BudgetService.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698