OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
6 | 6 |
7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 820 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
821 } | 821 } |
822 | 822 |
823 TEST(ExtensionTest, IsPrivilegeIncrease) { | 823 TEST(ExtensionTest, IsPrivilegeIncrease) { |
824 const struct { | 824 const struct { |
825 const char* base_name; | 825 const char* base_name; |
826 // Increase these sizes if you have more than 10. | 826 // Increase these sizes if you have more than 10. |
827 const char* granted_apis[10]; | 827 const char* granted_apis[10]; |
828 const char* granted_hosts[10]; | 828 const char* granted_hosts[10]; |
829 bool full_access; | 829 bool full_access; |
830 bool expect_success; | 830 bool expect_increase; |
831 } kTests[] = { | 831 } kTests[] = { |
832 { "allhosts1", {NULL}, {"http://*/", NULL}, false, | 832 { "allhosts1", {NULL}, {"http://*/", NULL}, false, |
833 false }, // all -> all | 833 false }, // all -> all |
834 { "allhosts2", {NULL}, {"http://*/", NULL}, false, | 834 { "allhosts2", {NULL}, {"http://*/", NULL}, false, |
835 false }, // all -> one | 835 false }, // all -> one |
836 { "allhosts3", {NULL}, {NULL}, false, true }, // one -> all | 836 { "allhosts3", {NULL}, {NULL}, false, true }, // one -> all |
837 { "hosts1", {NULL}, | 837 { "hosts1", {NULL}, |
838 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, | 838 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
839 false }, // http://a,http://b -> http://a,http://b | 839 false }, // http://a,http://b -> http://a,http://b |
840 { "hosts2", {NULL}, | 840 { "hosts2", {NULL}, |
841 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, | 841 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
842 false }, // http://a,http://b -> https://a,http://*.b | 842 true }, // http://a,http://b -> https://a,http://*.b |
843 { "hosts3", {NULL}, | 843 { "hosts3", {NULL}, |
844 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, | 844 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
845 false }, // http://a,http://b -> http://a | 845 false }, // http://a,http://b -> http://a |
846 { "hosts4", {NULL}, | 846 { "hosts4", {NULL}, |
847 {"http://www.google.com/", NULL}, false, | 847 {"http://www.google.com/", NULL}, false, |
848 true }, // http://a -> http://a,http://b | 848 true }, // http://a -> http://a,http://b |
849 { "hosts5", {"tabs", "notifications", NULL}, | 849 { "hosts5", {"tabs", "notifications", NULL}, |
850 {"http://*.example.com/", "http://*.example.com/*", | 850 {"http://*.example.com/", "http://*.example.com/*", |
851 "http://*.example.co.uk/*", "http://*.example.com.au/*", | 851 "http://*.example.co.uk/*", "http://*.example.com.au/*", |
852 NULL}, false, | 852 NULL}, false, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 granted_apis.insert(kTests[i].granted_apis[j]); | 892 granted_apis.insert(kTests[i].granted_apis[j]); |
893 | 893 |
894 ExtensionExtent granted_hosts; | 894 ExtensionExtent granted_hosts; |
895 for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j) | 895 for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j) |
896 AddPattern(&granted_hosts, kTests[i].granted_hosts[j]); | 896 AddPattern(&granted_hosts, kTests[i].granted_hosts[j]); |
897 | 897 |
898 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json"; | 898 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json"; |
899 if (!new_extension.get()) | 899 if (!new_extension.get()) |
900 continue; | 900 continue; |
901 | 901 |
902 EXPECT_EQ(kTests[i].expect_success, | 902 EXPECT_EQ(kTests[i].expect_increase, |
903 Extension::IsPrivilegeIncrease(kTests[i].full_access, | 903 Extension::IsPrivilegeIncrease(kTests[i].full_access, |
904 granted_apis, | 904 granted_apis, |
905 granted_hosts, | 905 granted_hosts, |
906 new_extension.get())) | 906 new_extension.get())) |
907 << kTests[i].base_name; | 907 << kTests[i].base_name; |
908 } | 908 } |
909 } | 909 } |
910 | 910 |
911 TEST(ExtensionTest, PermissionMessages) { | 911 TEST(ExtensionTest, PermissionMessages) { |
912 // Ensure that all permissions that needs to show install UI actually have | 912 // Ensure that all permissions that needs to show install UI actually have |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1096 scoped_refptr<Extension> extension; | 1096 scoped_refptr<Extension> extension; |
1097 extension = LoadManifest("empty_manifest", "empty.json"); | 1097 extension = LoadManifest("empty_manifest", "empty.json"); |
1098 | 1098 |
1099 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 1099 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
1100 EXPECT_EQ(kTests[i].expect_success, | 1100 EXPECT_EQ(kTests[i].expect_success, |
1101 extension->HasApiPermission(kTests[i].permission_name)) | 1101 extension->HasApiPermission(kTests[i].permission_name)) |
1102 << "Permission being tested: " << kTests[i].permission_name; | 1102 << "Permission being tested: " << kTests[i].permission_name; |
1103 } | 1103 } |
1104 } | 1104 } |
1105 | 1105 |
1106 TEST(ExtensionTest, GetDistinctHosts) { | 1106 TEST(ExtensionTest, GetDistinctHostsForDisplay) { |
1107 std::vector<std::string> expected; | 1107 std::vector<std::string> expected; |
1108 expected.push_back("www.foo.com"); | 1108 expected.push_back("www.foo.com"); |
1109 expected.push_back("www.bar.com"); | 1109 expected.push_back("www.bar.com"); |
1110 expected.push_back("www.baz.com"); | 1110 expected.push_back("www.baz.com"); |
1111 URLPatternList actual; | 1111 URLPatternList actual; |
1112 | 1112 |
1113 { | 1113 { |
1114 SCOPED_TRACE("no dupes"); | 1114 SCOPED_TRACE("no dupes"); |
1115 | 1115 |
1116 // Simple list with no dupes. | 1116 // Simple list with no dupes. |
1117 actual.push_back( | 1117 actual.push_back( |
1118 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); | 1118 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); |
1119 actual.push_back( | 1119 actual.push_back( |
1120 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path")); | 1120 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path")); |
1121 actual.push_back( | 1121 actual.push_back( |
1122 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); | 1122 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); |
1123 CompareLists(expected, | 1123 CompareLists(expected, |
1124 Extension::GetDistinctHosts(actual)); | 1124 Extension::GetDistinctHostsForDisplay(actual)); |
1125 } | 1125 } |
1126 | 1126 |
1127 { | 1127 { |
1128 SCOPED_TRACE("two dupes"); | 1128 SCOPED_TRACE("two dupes"); |
1129 | 1129 |
1130 // Add some dupes. | 1130 // Add some dupes. |
1131 actual.push_back( | 1131 actual.push_back( |
1132 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); | 1132 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); |
1133 actual.push_back( | 1133 actual.push_back( |
1134 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); | 1134 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); |
1135 CompareLists(expected, | 1135 CompareLists(expected, |
1136 Extension::GetDistinctHosts(actual)); | 1136 Extension::GetDistinctHostsForDisplay(actual)); |
1137 } | 1137 } |
1138 | 1138 |
1139 { | 1139 { |
1140 SCOPED_TRACE("schemes differ"); | 1140 SCOPED_TRACE("schemes differ"); |
1141 | 1141 |
1142 // Add a pattern that differs only by scheme. This should be filtered out. | 1142 // Add a pattern that differs only by scheme. This should be filtered out. |
1143 actual.push_back( | 1143 actual.push_back( |
1144 URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path")); | 1144 URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path")); |
1145 CompareLists(expected, | 1145 CompareLists(expected, |
1146 Extension::GetDistinctHosts(actual)); | 1146 Extension::GetDistinctHostsForDisplay(actual)); |
1147 } | 1147 } |
1148 | 1148 |
1149 { | 1149 { |
1150 SCOPED_TRACE("paths differ"); | 1150 SCOPED_TRACE("paths differ"); |
1151 | 1151 |
1152 // Add some dupes by path. | 1152 // Add some dupes by path. |
1153 actual.push_back( | 1153 actual.push_back( |
1154 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath")); | 1154 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath")); |
1155 CompareLists(expected, | 1155 CompareLists(expected, |
1156 Extension::GetDistinctHosts(actual)); | 1156 Extension::GetDistinctHostsForDisplay(actual)); |
1157 } | 1157 } |
1158 | 1158 |
1159 { | 1159 { |
1160 SCOPED_TRACE("subdomains differ"); | 1160 SCOPED_TRACE("subdomains differ"); |
1161 | 1161 |
1162 // We don't do anything special for subdomains. | 1162 // We don't do anything special for subdomains. |
1163 actual.push_back( | 1163 actual.push_back( |
1164 URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path")); | 1164 URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path")); |
1165 actual.push_back( | 1165 actual.push_back( |
1166 URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path")); | 1166 URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path")); |
1167 | 1167 |
1168 expected.push_back("monkey.www.bar.com"); | 1168 expected.push_back("monkey.www.bar.com"); |
1169 expected.push_back("bar.com"); | 1169 expected.push_back("bar.com"); |
1170 | 1170 |
1171 CompareLists(expected, | 1171 CompareLists(expected, |
1172 Extension::GetDistinctHosts(actual)); | 1172 Extension::GetDistinctHostsForDisplay(actual)); |
1173 } | 1173 } |
1174 | 1174 |
1175 { | 1175 { |
1176 SCOPED_TRACE("RCDs differ"); | 1176 SCOPED_TRACE("RCDs differ"); |
1177 | 1177 |
1178 // Now test for RCD uniquing. | 1178 // Now test for RCD uniquing. |
1179 actual.push_back( | 1179 actual.push_back( |
1180 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); | 1180 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); |
1181 actual.push_back( | 1181 actual.push_back( |
1182 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); | 1182 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); |
1183 actual.push_back( | 1183 actual.push_back( |
1184 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.de/path")); | 1184 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.de/path")); |
1185 actual.push_back( | 1185 actual.push_back( |
1186 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca.us/path")); | 1186 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca.us/path")); |
1187 actual.push_back( | 1187 actual.push_back( |
1188 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path")); | 1188 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path")); |
1189 actual.push_back( | 1189 actual.push_back( |
1190 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com.my/path")); | 1190 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com.my/path")); |
1191 | 1191 |
1192 // This is an unknown RCD, which shouldn't be uniqued out. | 1192 // This is an unknown RCD, which shouldn't be uniqued out. |
1193 actual.push_back( | 1193 actual.push_back( |
1194 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path")); | 1194 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path")); |
1195 | 1195 |
1196 expected.push_back("www.foo.xyzzy"); | 1196 expected.push_back("www.foo.xyzzy"); |
1197 | 1197 |
1198 CompareLists(expected, | 1198 CompareLists(expected, |
1199 Extension::GetDistinctHosts(actual)); | 1199 Extension::GetDistinctHostsForDisplay(actual)); |
1200 } | 1200 } |
| 1201 |
| 1202 { |
| 1203 SCOPED_TRACE("wildcards"); |
| 1204 |
| 1205 actual.push_back( |
| 1206 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*")); |
| 1207 |
| 1208 expected.push_back("*.google.com"); |
| 1209 |
| 1210 CompareLists(expected, |
| 1211 Extension::GetDistinctHostsForDisplay(actual)); |
| 1212 } |
| 1213 } |
| 1214 |
| 1215 TEST(ExtensionTest, IsElevatedHostList) { |
| 1216 URLPatternList list1; |
| 1217 URLPatternList list2; |
| 1218 |
| 1219 list1.push_back( |
| 1220 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path")); |
| 1221 list1.push_back( |
| 1222 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path")); |
| 1223 |
| 1224 // Test that the host order does not matter. |
| 1225 list2.push_back( |
| 1226 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path")); |
| 1227 list2.push_back( |
| 1228 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path")); |
| 1229 |
| 1230 EXPECT_FALSE(Extension::IsElevatedHostList(list1, list2)); |
| 1231 EXPECT_FALSE(Extension::IsElevatedHostList(list2, list1)); |
| 1232 |
| 1233 // Test that paths are ignored. |
| 1234 list2.clear(); |
| 1235 list2.push_back( |
| 1236 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/*")); |
| 1237 EXPECT_FALSE(Extension::IsElevatedHostList(list1, list2)); |
| 1238 EXPECT_FALSE(Extension::IsElevatedHostList(list2, list1)); |
| 1239 |
| 1240 // Test that RCDs are ignored. |
| 1241 list2.clear(); |
| 1242 list2.push_back( |
| 1243 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/*")); |
| 1244 EXPECT_FALSE(Extension::IsElevatedHostList(list1, list2)); |
| 1245 EXPECT_FALSE(Extension::IsElevatedHostList(list2, list1)); |
| 1246 |
| 1247 // Test that subdomain wildcards are handled properly. |
| 1248 list2.clear(); |
| 1249 list2.push_back( |
| 1250 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com.hk/*")); |
| 1251 EXPECT_TRUE(Extension::IsElevatedHostList(list1, list2)); |
| 1252 //TODO(jstritar): Does not match subdomains properly. http://crbug.com/65337 |
| 1253 //EXPECT_FALSE(Extension::IsElevatedHostList(list2, list1)); |
| 1254 |
| 1255 // Test that different domains count as different hosts. |
| 1256 list2.clear(); |
| 1257 list2.push_back( |
| 1258 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path")); |
| 1259 list2.push_back( |
| 1260 URLPattern(URLPattern::SCHEME_HTTP, "http://www.example.org/path")); |
| 1261 EXPECT_TRUE(Extension::IsElevatedHostList(list1, list2)); |
| 1262 EXPECT_FALSE(Extension::IsElevatedHostList(list2, list1)); |
| 1263 |
| 1264 // Test that different subdomains count as different hosts. |
| 1265 list2.clear(); |
| 1266 list2.push_back( |
| 1267 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*")); |
| 1268 EXPECT_TRUE(Extension::IsElevatedHostList(list1, list2)); |
| 1269 EXPECT_TRUE(Extension::IsElevatedHostList(list2, list1)); |
1201 } | 1270 } |
1202 | 1271 |
1203 TEST(ExtensionTest, GenerateId) { | 1272 TEST(ExtensionTest, GenerateId) { |
1204 std::string result; | 1273 std::string result; |
1205 EXPECT_FALSE(Extension::GenerateId("", &result)); | 1274 EXPECT_FALSE(Extension::GenerateId("", &result)); |
1206 | 1275 |
1207 EXPECT_TRUE(Extension::GenerateId("test", &result)); | 1276 EXPECT_TRUE(Extension::GenerateId("test", &result)); |
1208 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); | 1277 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); |
1209 | 1278 |
1210 EXPECT_TRUE(Extension::GenerateId("_", &result)); | 1279 EXPECT_TRUE(Extension::GenerateId("_", &result)); |
1211 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); | 1280 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); |
1212 | 1281 |
1213 EXPECT_TRUE(Extension::GenerateId( | 1282 EXPECT_TRUE(Extension::GenerateId( |
1214 "this_string_is_longer_than_a_single_sha256_hash_digest", &result)); | 1283 "this_string_is_longer_than_a_single_sha256_hash_digest", &result)); |
1215 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi"); | 1284 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi"); |
1216 } | 1285 } |
OLD | NEW |