| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/manifest/manifest_parser.h" | 5 #include "content/renderer/manifest/manifest_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "content/public/common/manifest.h" | 8 #include "content/public/common/manifest.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 errors()[4]); | 120 errors()[4]); |
| 121 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " | 121 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " |
| 122 "type array expected.", | 122 "type array expected.", |
| 123 errors()[5]); | 123 errors()[5]); |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST_F(ManifestParserTest, NameParseRules) { | 126 TEST_F(ManifestParserTest, NameParseRules) { |
| 127 // Smoke test. | 127 // Smoke test. |
| 128 { | 128 { |
| 129 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); | 129 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); |
| 130 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); | 130 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); |
| 131 ASSERT_FALSE(manifest.IsEmpty()); | 131 ASSERT_FALSE(manifest.IsEmpty()); |
| 132 EXPECT_EQ(0u, GetErrorCount()); | 132 EXPECT_EQ(0u, GetErrorCount()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Trim whitespaces. | 135 // Trim whitespaces. |
| 136 { | 136 { |
| 137 Manifest manifest = ParseManifest("{ \"name\": \" foo \" }"); | 137 Manifest manifest = ParseManifest("{ \"name\": \" foo \" }"); |
| 138 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); | 138 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); |
| 139 EXPECT_EQ(0u, GetErrorCount()); | 139 EXPECT_EQ(0u, GetErrorCount()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Don't parse if name isn't a string. | 142 // Don't parse if name isn't a string. |
| 143 { | 143 { |
| 144 Manifest manifest = ParseManifest("{ \"name\": {} }"); | 144 Manifest manifest = ParseManifest("{ \"name\": {} }"); |
| 145 ASSERT_TRUE(manifest.name.is_null()); | 145 ASSERT_TRUE(manifest.name.is_null()); |
| 146 EXPECT_EQ(1u, GetErrorCount()); | 146 EXPECT_EQ(1u, GetErrorCount()); |
| 147 EXPECT_EQ("Manifest parsing error: property 'name' ignored," | 147 EXPECT_EQ("Manifest parsing error: property 'name' ignored," |
| 148 " type string expected.", | 148 " type string expected.", |
| 149 errors()[0]); | 149 errors()[0]); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Don't parse if name isn't a string. | 152 // Don't parse if name isn't a string. |
| 153 { | 153 { |
| 154 Manifest manifest = ParseManifest("{ \"name\": 42 }"); | 154 Manifest manifest = ParseManifest("{ \"name\": 42 }"); |
| 155 ASSERT_TRUE(manifest.name.is_null()); | 155 ASSERT_TRUE(manifest.name.is_null()); |
| 156 EXPECT_EQ(1u, GetErrorCount()); | 156 EXPECT_EQ(1u, GetErrorCount()); |
| 157 EXPECT_EQ("Manifest parsing error: property 'name' ignored," | 157 EXPECT_EQ("Manifest parsing error: property 'name' ignored," |
| 158 " type string expected.", | 158 " type string expected.", |
| 159 errors()[0]); | 159 errors()[0]); |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 TEST_F(ManifestParserTest, ShortNameParseRules) { | 163 TEST_F(ManifestParserTest, ShortNameParseRules) { |
| 164 // Smoke test. | 164 // Smoke test. |
| 165 { | 165 { |
| 166 Manifest manifest = ParseManifest("{ \"short_name\": \"foo\" }"); | 166 Manifest manifest = ParseManifest("{ \"short_name\": \"foo\" }"); |
| 167 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); | 167 ASSERT_TRUE(base::EqualsASCII(manifest.short_name.string(), "foo")); |
| 168 ASSERT_FALSE(manifest.IsEmpty()); | 168 ASSERT_FALSE(manifest.IsEmpty()); |
| 169 EXPECT_EQ(0u, GetErrorCount()); | 169 EXPECT_EQ(0u, GetErrorCount()); |
| 170 } | 170 } |
| 171 | 171 |
| 172 // Trim whitespaces. | 172 // Trim whitespaces. |
| 173 { | 173 { |
| 174 Manifest manifest = ParseManifest("{ \"short_name\": \" foo \" }"); | 174 Manifest manifest = ParseManifest("{ \"short_name\": \" foo \" }"); |
| 175 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); | 175 ASSERT_TRUE(base::EqualsASCII(manifest.short_name.string(), "foo")); |
| 176 EXPECT_EQ(0u, GetErrorCount()); | 176 EXPECT_EQ(0u, GetErrorCount()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Don't parse if name isn't a string. | 179 // Don't parse if name isn't a string. |
| 180 { | 180 { |
| 181 Manifest manifest = ParseManifest("{ \"short_name\": {} }"); | 181 Manifest manifest = ParseManifest("{ \"short_name\": {} }"); |
| 182 ASSERT_TRUE(manifest.short_name.is_null()); | 182 ASSERT_TRUE(manifest.short_name.is_null()); |
| 183 EXPECT_EQ(1u, GetErrorCount()); | 183 EXPECT_EQ(1u, GetErrorCount()); |
| 184 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," | 184 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," |
| 185 " type string expected.", | 185 " type string expected.", |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 "http://foo.com/landing/icons/foo.png"); | 561 "http://foo.com/landing/icons/foo.png"); |
| 562 EXPECT_EQ(0u, GetErrorCount()); | 562 EXPECT_EQ(0u, GetErrorCount()); |
| 563 } | 563 } |
| 564 } | 564 } |
| 565 | 565 |
| 566 TEST_F(ManifestParserTest, IconTypeParseRules) { | 566 TEST_F(ManifestParserTest, IconTypeParseRules) { |
| 567 // Smoke test. | 567 // Smoke test. |
| 568 { | 568 { |
| 569 Manifest manifest = | 569 Manifest manifest = |
| 570 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": \"foo\" } ] }"); | 570 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": \"foo\" } ] }"); |
| 571 EXPECT_TRUE(EqualsASCII(manifest.icons[0].type.string(), "foo")); | 571 EXPECT_TRUE(base::EqualsASCII(manifest.icons[0].type.string(), "foo")); |
| 572 EXPECT_EQ(0u, GetErrorCount()); | 572 EXPECT_EQ(0u, GetErrorCount()); |
| 573 } | 573 } |
| 574 | 574 |
| 575 // Trim whitespaces. | 575 // Trim whitespaces. |
| 576 { | 576 { |
| 577 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," | 577 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," |
| 578 " \"type\": \" foo \" } ] }"); | 578 " \"type\": \" foo \" } ] }"); |
| 579 EXPECT_TRUE(EqualsASCII(manifest.icons[0].type.string(), "foo")); | 579 EXPECT_TRUE(base::EqualsASCII(manifest.icons[0].type.string(), "foo")); |
| 580 EXPECT_EQ(0u, GetErrorCount()); | 580 EXPECT_EQ(0u, GetErrorCount()); |
| 581 } | 581 } |
| 582 | 582 |
| 583 // Don't parse if property isn't a string. | 583 // Don't parse if property isn't a string. |
| 584 { | 584 { |
| 585 Manifest manifest = | 585 Manifest manifest = |
| 586 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": {} } ] }"); | 586 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": {} } ] }"); |
| 587 EXPECT_TRUE(manifest.icons[0].type.is_null()); | 587 EXPECT_TRUE(manifest.icons[0].type.is_null()); |
| 588 EXPECT_EQ(1u, GetErrorCount()); | 588 EXPECT_EQ(1u, GetErrorCount()); |
| 589 EXPECT_EQ("Manifest parsing error: property 'type' ignored," | 589 EXPECT_EQ("Manifest parsing error: property 'type' ignored," |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 "related application ignored.", | 853 "related application ignored.", |
| 854 errors()[0]); | 854 errors()[0]); |
| 855 } | 855 } |
| 856 | 856 |
| 857 // Valid application, with url. | 857 // Valid application, with url. |
| 858 { | 858 { |
| 859 Manifest manifest = ParseManifest( | 859 Manifest manifest = ParseManifest( |
| 860 "{ \"related_applications\": [" | 860 "{ \"related_applications\": [" |
| 861 "{\"platform\": \"play\", \"url\": \"http://www.foo.com\"}]}"); | 861 "{\"platform\": \"play\", \"url\": \"http://www.foo.com\"}]}"); |
| 862 EXPECT_EQ(manifest.related_applications.size(), 1u); | 862 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 863 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), | 863 EXPECT_TRUE(base::EqualsASCII( |
| 864 "play")); | 864 manifest.related_applications[0].platform.string(), |
| 865 "play")); |
| 865 EXPECT_EQ(manifest.related_applications[0].url.spec(), | 866 EXPECT_EQ(manifest.related_applications[0].url.spec(), |
| 866 "http://www.foo.com/"); | 867 "http://www.foo.com/"); |
| 867 EXPECT_FALSE(manifest.IsEmpty()); | 868 EXPECT_FALSE(manifest.IsEmpty()); |
| 868 EXPECT_EQ(0u, GetErrorCount()); | 869 EXPECT_EQ(0u, GetErrorCount()); |
| 869 } | 870 } |
| 870 | 871 |
| 871 // Valid application, with id. | 872 // Valid application, with id. |
| 872 { | 873 { |
| 873 Manifest manifest = ParseManifest( | 874 Manifest manifest = ParseManifest( |
| 874 "{ \"related_applications\": [" | 875 "{ \"related_applications\": [" |
| 875 "{\"platform\": \"itunes\", \"id\": \"foo\"}]}"); | 876 "{\"platform\": \"itunes\", \"id\": \"foo\"}]}"); |
| 876 EXPECT_EQ(manifest.related_applications.size(), 1u); | 877 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 877 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), | 878 EXPECT_TRUE(base::EqualsASCII( |
| 878 "itunes")); | 879 manifest.related_applications[0].platform.string(), |
| 879 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), | 880 "itunes")); |
| 880 "foo")); | 881 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(), |
| 882 "foo")); |
| 881 EXPECT_FALSE(manifest.IsEmpty()); | 883 EXPECT_FALSE(manifest.IsEmpty()); |
| 882 EXPECT_EQ(0u, GetErrorCount()); | 884 EXPECT_EQ(0u, GetErrorCount()); |
| 883 } | 885 } |
| 884 | 886 |
| 885 // All valid applications are in list. | 887 // All valid applications are in list. |
| 886 { | 888 { |
| 887 Manifest manifest = ParseManifest( | 889 Manifest manifest = ParseManifest( |
| 888 "{ \"related_applications\": [" | 890 "{ \"related_applications\": [" |
| 889 "{\"platform\": \"play\", \"id\": \"foo\"}," | 891 "{\"platform\": \"play\", \"id\": \"foo\"}," |
| 890 "{\"platform\": \"itunes\", \"id\": \"bar\"}]}"); | 892 "{\"platform\": \"itunes\", \"id\": \"bar\"}]}"); |
| 891 EXPECT_EQ(manifest.related_applications.size(), 2u); | 893 EXPECT_EQ(manifest.related_applications.size(), 2u); |
| 892 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), | 894 EXPECT_TRUE(base::EqualsASCII( |
| 893 "play")); | 895 manifest.related_applications[0].platform.string(), |
| 894 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), | 896 "play")); |
| 895 "foo")); | 897 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(), |
| 896 EXPECT_TRUE(EqualsASCII(manifest.related_applications[1].platform.string(), | 898 "foo")); |
| 897 "itunes")); | 899 EXPECT_TRUE(base::EqualsASCII( |
| 898 EXPECT_TRUE(EqualsASCII(manifest.related_applications[1].id.string(), | 900 manifest.related_applications[1].platform.string(), |
| 899 "bar")); | 901 "itunes")); |
| 902 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[1].id.string(), |
| 903 "bar")); |
| 900 EXPECT_FALSE(manifest.IsEmpty()); | 904 EXPECT_FALSE(manifest.IsEmpty()); |
| 901 EXPECT_EQ(0u, GetErrorCount()); | 905 EXPECT_EQ(0u, GetErrorCount()); |
| 902 } | 906 } |
| 903 | 907 |
| 904 // Two invalid applications and one valid. Only the valid application should | 908 // Two invalid applications and one valid. Only the valid application should |
| 905 // be in the list. | 909 // be in the list. |
| 906 { | 910 { |
| 907 Manifest manifest = ParseManifest( | 911 Manifest manifest = ParseManifest( |
| 908 "{ \"related_applications\": [" | 912 "{ \"related_applications\": [" |
| 909 "{\"platform\": \"itunes\"}," | 913 "{\"platform\": \"itunes\"}," |
| 910 "{\"platform\": \"play\", \"id\": \"foo\"}," | 914 "{\"platform\": \"play\", \"id\": \"foo\"}," |
| 911 "{}]}"); | 915 "{}]}"); |
| 912 EXPECT_EQ(manifest.related_applications.size(), 1u); | 916 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 913 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), | 917 EXPECT_TRUE(base::EqualsASCII( |
| 914 "play")); | 918 manifest.related_applications[0].platform.string(), |
| 915 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), | 919 "play")); |
| 916 "foo")); | 920 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(), |
| 921 "foo")); |
| 917 EXPECT_FALSE(manifest.IsEmpty()); | 922 EXPECT_FALSE(manifest.IsEmpty()); |
| 918 EXPECT_EQ(2u, GetErrorCount()); | 923 EXPECT_EQ(2u, GetErrorCount()); |
| 919 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, " | 924 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, " |
| 920 "related application ignored.", | 925 "related application ignored.", |
| 921 errors()[0]); | 926 errors()[0]); |
| 922 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " | 927 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " |
| 923 "related application ignored.", | 928 "related application ignored.", |
| 924 errors()[1]); | 929 errors()[1]); |
| 925 } | 930 } |
| 926 } | 931 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 ParseManifest("{ \"prefer_related_applications\": false }"); | 976 ParseManifest("{ \"prefer_related_applications\": false }"); |
| 972 EXPECT_FALSE(manifest.prefer_related_applications); | 977 EXPECT_FALSE(manifest.prefer_related_applications); |
| 973 EXPECT_EQ(0u, GetErrorCount()); | 978 EXPECT_EQ(0u, GetErrorCount()); |
| 974 } | 979 } |
| 975 } | 980 } |
| 976 | 981 |
| 977 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { | 982 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { |
| 978 // Smoke test. | 983 // Smoke test. |
| 979 { | 984 { |
| 980 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); | 985 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); |
| 981 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); | 986 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
| 982 EXPECT_EQ(0u, GetErrorCount()); | 987 EXPECT_EQ(0u, GetErrorCount()); |
| 983 } | 988 } |
| 984 | 989 |
| 985 // Trim whitespaces. | 990 // Trim whitespaces. |
| 986 { | 991 { |
| 987 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }"); | 992 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }"); |
| 988 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); | 993 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
| 989 EXPECT_EQ(0u, GetErrorCount()); | 994 EXPECT_EQ(0u, GetErrorCount()); |
| 990 } | 995 } |
| 991 | 996 |
| 992 // Don't parse if the property isn't a string. | 997 // Don't parse if the property isn't a string. |
| 993 { | 998 { |
| 994 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }"); | 999 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }"); |
| 995 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); | 1000 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 996 EXPECT_EQ(1u, GetErrorCount()); | 1001 EXPECT_EQ(1u, GetErrorCount()); |
| 997 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," | 1002 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
| 998 " type string expected.", | 1003 " type string expected.", |
| 999 errors()[0]); | 1004 errors()[0]); |
| 1000 } | 1005 } |
| 1001 { | 1006 { |
| 1002 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); | 1007 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
| 1003 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); | 1008 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 1004 EXPECT_EQ(1u, GetErrorCount()); | 1009 EXPECT_EQ(1u, GetErrorCount()); |
| 1005 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," | 1010 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
| 1006 " type string expected.", | 1011 " type string expected.", |
| 1007 errors()[0]); | 1012 errors()[0]); |
| 1008 } | 1013 } |
| 1009 } | 1014 } |
| 1010 | 1015 |
| 1011 } // namespace content | 1016 } // namespace content |
| OLD | NEW |