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

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: Created 4 years, 3 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;
50 run_loop_closure.Run(); 51 run_loop_closure.Run();
51 } 52 }
52 53
53 void StatusCallback(base::Closure run_loop_closure, bool success) { 54 void StatusCallback(base::Closure run_loop_closure, bool success) {
54 success_ = success; 55 success_ = success;
(...skipping 19 matching lines...) Expand all
74 run_loop.Run(); 75 run_loop.Run();
75 return success_; 76 return success_;
76 } 77 }
77 78
78 // Members for callbacks to set. 79 // Members for callbacks to set.
79 bool success_; 80 bool success_;
80 81
81 private: 82 private:
82 content::TestBrowserThreadBundle thread_bundle_; 83 content::TestBrowserThreadBundle thread_bundle_;
83 TestingProfile profile_; 84 TestingProfile profile_;
84 const url::Origin origin_; 85 url::Origin origin_;
85 }; 86 };
86 87
87 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { 88 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) {
88 // Set initial SES. The first time we try to spend budget, the 89 // Set initial SES. The first time we try to spend budget, the
89 // engagement award will be granted which is 48.0. 90 // engagement award will be granted which is 48.0.
90 SetSiteEngagementScore(kTestSES); 91 SetSiteEngagementScore(kTestSES);
91 const blink::mojom::BudgetOperationType type = 92 const blink::mojom::BudgetOperationType type =
92 blink::mojom::BudgetOperationType::SILENT_PUSH; 93 blink::mojom::BudgetOperationType::SILENT_PUSH;
93 94
94 // Spend for 24 silent push messages. This should consume all the original 95 // Spend for 24 silent push messages. This should consume all the original
95 // budget grant. 96 // budget grant.
96 for (int i = 0; i < 24; i++) 97 for (int i = 0; i < 24; i++)
97 ASSERT_TRUE(ReserveBudget(type)); 98 ASSERT_TRUE(ReserveBudget(type));
98 99
99 // Try to send one final silent push. The origin should be out of budget. 100 // Try to send one final silent push. The origin should be out of budget.
100 ASSERT_FALSE(ReserveBudget(type)); 101 ASSERT_FALSE(ReserveBudget(type));
101 102
102 // Try to consume for the 24 messages reserved. 103 // Try to consume for the 24 messages reserved.
103 for (int i = 0; i < 24; i++) 104 for (int i = 0; i < 24; i++)
104 ASSERT_TRUE(ConsumeBudget(type)); 105 ASSERT_TRUE(ConsumeBudget(type));
105 106
106 // The next consume should fail, since there is no reservation or budget 107 // The next consume should fail, since there is no reservation or budget
107 // available. 108 // available.
108 ASSERT_FALSE(ConsumeBudget(type)); 109 ASSERT_FALSE(ConsumeBudget(type));
109 } 110 }
111
112 TEST_F(BudgetManagerTest, TestInsecureOrigins) {
113 const blink::mojom::BudgetOperationType type =
114 blink::mojom::BudgetOperationType::SILENT_PUSH;
115 SetOrigin(url::Origin(GURL("http://example.com")));
116 SetSiteEngagementScore(kTestSES);
117
118 // Methods on the BudgetManager should only be allowed for secure origins.
119 ASSERT_FALSE(ReserveBudget(type));
120 ASSERT_FALSE(ConsumeBudget(type));
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698