1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/common/extensions/permissions/chrome_permission_message_rules.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
19 std::string
PermissionIDsToString(const std::set
<APIPermission::ID
>& ids
) {
20 std::vector
<std::string
> strs
;
22 strs
.push_back(base::IntToString(id
));
23 return base::JoinString(strs
, " ");
26 std::string
RuleToString(const ChromePermissionMessageRule
& rule
) {
27 return base::StringPrintf(
29 PermissionIDsToString(rule
.required_permissions()).c_str(),
30 PermissionIDsToString(rule
.optional_permissions()).c_str());
33 bool MakesRedundant(const ChromePermissionMessageRule
& first_rule
,
34 const ChromePermissionMessageRule
& second_rule
) {
35 // The second rule is redundant if the first rule has a (non-strict) subset
36 // of its required permissions - the first rule will always "steal" those
37 // permissions, so the second rule can never apply.
38 // Example: Say rule 1 has required permissions A, B, and rule 2 has A, B,
39 // and C. So 1 is a subset of 2. If the requirements for 2 are satisfied
40 // (i.e., A, B, and C are all there), then the requirements for 1 are also
41 // satisfied. Since 1 comes first, it will always take A and B, and so the
42 // requirements for 2 can never be satisfied by the time it's applied.
43 return base::STLIncludes(second_rule
.required_permissions(),
44 first_rule
.required_permissions());
49 TEST(ChromePermissionMessageRulesTest
, NoRedundantRules
) {
50 std::vector
<ChromePermissionMessageRule
> rules
=
51 ChromePermissionMessageRule::GetAllRules();
52 for (size_t i
= 1; i
< rules
.size(); i
++) {
53 for (size_t j
= 0; j
< i
; j
++) {
54 EXPECT_FALSE(MakesRedundant(rules
[j
], rules
[i
]))
55 << "Rule at index " << i
<< " " << RuleToString(rules
[i
])
56 << " is redundant because of previous rule at index " << j
<< " "
57 << RuleToString(rules
[j
]);
62 } // namespace extensions