| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "ios/web/public/web_state/js/credential_util.h" | 5 #include "ios/web/public/web_state/js/credential_util.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 11 #include "base/values.h" |
| 10 #include "ios/web/public/web_state/credential.h" | 12 #include "ios/web/public/web_state/credential.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 #include "url/origin.h" | 18 #include "url/origin.h" |
| 17 | 19 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 credential.id = base::ASCIIToUTF16(kTestCredentialID); | 59 credential.id = base::ASCIIToUTF16(kTestCredentialID); |
| 58 credential.name = base::ASCIIToUTF16(kTestCredentialName); | 60 credential.name = base::ASCIIToUTF16(kTestCredentialName); |
| 59 credential.avatar_url = GURL(kTestCredentialAvatarURL); | 61 credential.avatar_url = GURL(kTestCredentialAvatarURL); |
| 60 credential.federation_origin = | 62 credential.federation_origin = |
| 61 url::Origin(GURL(kTestCredentialFederationURL)); | 63 url::Origin(GURL(kTestCredentialFederationURL)); |
| 62 return credential; | 64 return credential; |
| 63 } | 65 } |
| 64 | 66 |
| 65 // Returns a value representing the credential returned by | 67 // Returns a value representing the credential returned by |
| 66 // |GetTestPasswordCredential()|. | 68 // |GetTestPasswordCredential()|. |
| 67 scoped_ptr<base::DictionaryValue> GetTestPasswordCredentialDictionaryValue() { | 69 std::unique_ptr<base::DictionaryValue> |
| 68 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 70 GetTestPasswordCredentialDictionaryValue() { |
| 71 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 69 value->SetString("type", kTestCredentialTypePassword); | 72 value->SetString("type", kTestCredentialTypePassword); |
| 70 value->SetString("id", kTestCredentialID); | 73 value->SetString("id", kTestCredentialID); |
| 71 value->SetString("name", kTestCredentialName); | 74 value->SetString("name", kTestCredentialName); |
| 72 value->SetString("avatarURL", kTestCredentialAvatarURL); | 75 value->SetString("avatarURL", kTestCredentialAvatarURL); |
| 73 value->SetString("password", kTestCredentialPassword); | 76 value->SetString("password", kTestCredentialPassword); |
| 74 return value; | 77 return value; |
| 75 } | 78 } |
| 76 | 79 |
| 77 // Returns a value representing the credential returned by | 80 // Returns a value representing the credential returned by |
| 78 // |GetTestFederatedCredentialDictionaryValue()|. | 81 // |GetTestFederatedCredentialDictionaryValue()|. |
| 79 scoped_ptr<base::DictionaryValue> GetTestFederatedCredentialDictionaryValue() { | 82 std::unique_ptr<base::DictionaryValue> |
| 80 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 83 GetTestFederatedCredentialDictionaryValue() { |
| 84 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 81 value->SetString("type", kTestCredentialTypeFederated); | 85 value->SetString("type", kTestCredentialTypeFederated); |
| 82 value->SetString("id", kTestCredentialID); | 86 value->SetString("id", kTestCredentialID); |
| 83 value->SetString("name", kTestCredentialName); | 87 value->SetString("name", kTestCredentialName); |
| 84 value->SetString("avatarURL", kTestCredentialAvatarURL); | 88 value->SetString("avatarURL", kTestCredentialAvatarURL); |
| 85 value->SetString("federation", | 89 value->SetString("federation", |
| 86 url::Origin(GURL(kTestCredentialFederationURL)).Serialize()); | 90 url::Origin(GURL(kTestCredentialFederationURL)).Serialize()); |
| 87 return value; | 91 return value; |
| 88 } | 92 } |
| 89 | 93 |
| 90 // Tests that parsing an empty value fails. | 94 // Tests that parsing an empty value fails. |
| 91 TEST(CredentialUtilTest, ParsingEmptyValueFails) { | 95 TEST(CredentialUtilTest, ParsingEmptyValueFails) { |
| 92 base::DictionaryValue value; | 96 base::DictionaryValue value; |
| 93 Credential credential; | 97 Credential credential; |
| 94 EXPECT_FALSE(DictionaryValueToCredential(value, &credential)); | 98 EXPECT_FALSE(DictionaryValueToCredential(value, &credential)); |
| 95 } | 99 } |
| 96 | 100 |
| 97 // Tests that parsing a value with a bad type fails. | 101 // Tests that parsing a value with a bad type fails. |
| 98 TEST(CredentialUtilTest, ParsingValueWithBadTypeFails) { | 102 TEST(CredentialUtilTest, ParsingValueWithBadTypeFails) { |
| 99 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 103 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 100 value->SetString("type", "FooCredential"); | 104 value->SetString("type", "FooCredential"); |
| 101 Credential credential; | 105 Credential credential; |
| 102 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 106 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 103 } | 107 } |
| 104 | 108 |
| 105 // Tests that parsing a correctly-formed value representing a PasswordCredential | 109 // Tests that parsing a correctly-formed value representing a PasswordCredential |
| 106 // succeeds. | 110 // succeeds. |
| 107 TEST(CredentialUtilTest, ParsingPasswordCredentialSucceeds) { | 111 TEST(CredentialUtilTest, ParsingPasswordCredentialSucceeds) { |
| 108 Credential credential; | 112 Credential credential; |
| 109 EXPECT_TRUE(DictionaryValueToCredential( | 113 EXPECT_TRUE(DictionaryValueToCredential( |
| 110 *GetTestPasswordCredentialDictionaryValue(), &credential)); | 114 *GetTestPasswordCredentialDictionaryValue(), &credential)); |
| 111 EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential)); | 115 EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential)); |
| 112 } | 116 } |
| 113 | 117 |
| 114 // Tests that parsing a value representing a PasswordCredential but with no ID | 118 // Tests that parsing a value representing a PasswordCredential but with no ID |
| 115 // specified fails. | 119 // specified fails. |
| 116 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) { | 120 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) { |
| 117 scoped_ptr<base::DictionaryValue> value( | 121 std::unique_ptr<base::DictionaryValue> value( |
| 118 GetTestPasswordCredentialDictionaryValue()); | 122 GetTestPasswordCredentialDictionaryValue()); |
| 119 value->RemoveWithoutPathExpansion("id", nullptr); | 123 value->RemoveWithoutPathExpansion("id", nullptr); |
| 120 Credential credential; | 124 Credential credential; |
| 121 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 125 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 122 } | 126 } |
| 123 | 127 |
| 124 // Tests that parsing a value representing a PasswordCredential with a badly- | 128 // Tests that parsing a value representing a PasswordCredential with a badly- |
| 125 // formed avatarURL fails. | 129 // formed avatarURL fails. |
| 126 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) { | 130 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) { |
| 127 scoped_ptr<base::DictionaryValue> value( | 131 std::unique_ptr<base::DictionaryValue> value( |
| 128 GetTestPasswordCredentialDictionaryValue()); | 132 GetTestPasswordCredentialDictionaryValue()); |
| 129 value->SetString("avatarURL", "foo"); | 133 value->SetString("avatarURL", "foo"); |
| 130 Credential credential; | 134 Credential credential; |
| 131 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 135 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 132 } | 136 } |
| 133 | 137 |
| 134 // Tests that parsing a value representing a PasswordCredential with no password | 138 // Tests that parsing a value representing a PasswordCredential with no password |
| 135 // specified fails. | 139 // specified fails. |
| 136 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) { | 140 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) { |
| 137 scoped_ptr<base::DictionaryValue> value( | 141 std::unique_ptr<base::DictionaryValue> value( |
| 138 GetTestPasswordCredentialDictionaryValue()); | 142 GetTestPasswordCredentialDictionaryValue()); |
| 139 value->Remove("password", nullptr); | 143 value->Remove("password", nullptr); |
| 140 Credential credential; | 144 Credential credential; |
| 141 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 145 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 142 } | 146 } |
| 143 | 147 |
| 144 // Tests that parsing a correctly-formed value representing a | 148 // Tests that parsing a correctly-formed value representing a |
| 145 // FederatedCredential succeeds. | 149 // FederatedCredential succeeds. |
| 146 TEST(CredentialUtilTest, ParsingFederatedCredentialSucceeds) { | 150 TEST(CredentialUtilTest, ParsingFederatedCredentialSucceeds) { |
| 147 Credential credential; | 151 Credential credential; |
| 148 EXPECT_TRUE(DictionaryValueToCredential( | 152 EXPECT_TRUE(DictionaryValueToCredential( |
| 149 *GetTestFederatedCredentialDictionaryValue(), &credential)); | 153 *GetTestFederatedCredentialDictionaryValue(), &credential)); |
| 150 EXPECT_TRUE(CredentialsEqual(GetTestFederatedCredential(), credential)); | 154 EXPECT_TRUE(CredentialsEqual(GetTestFederatedCredential(), credential)); |
| 151 } | 155 } |
| 152 | 156 |
| 153 // Tests that parsing a value representing a FederatedCredential with no ID | 157 // Tests that parsing a value representing a FederatedCredential with no ID |
| 154 // fails. | 158 // fails. |
| 155 TEST(CredentialUtilTest, ParsingFederatedCredentialWithNoIDFails) { | 159 TEST(CredentialUtilTest, ParsingFederatedCredentialWithNoIDFails) { |
| 156 scoped_ptr<base::DictionaryValue> value( | 160 std::unique_ptr<base::DictionaryValue> value( |
| 157 GetTestFederatedCredentialDictionaryValue()); | 161 GetTestFederatedCredentialDictionaryValue()); |
| 158 value->RemoveWithoutPathExpansion("id", nullptr); | 162 value->RemoveWithoutPathExpansion("id", nullptr); |
| 159 Credential credential; | 163 Credential credential; |
| 160 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 164 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 161 } | 165 } |
| 162 | 166 |
| 163 // Tests that parsing a value representing a FederatedCredential with a badly- | 167 // Tests that parsing a value representing a FederatedCredential with a badly- |
| 164 // formed avatarURL fails. | 168 // formed avatarURL fails. |
| 165 TEST(CredentialUtilTest, ParsingFederatedCredentialWithBadAvatarURLFails) { | 169 TEST(CredentialUtilTest, ParsingFederatedCredentialWithBadAvatarURLFails) { |
| 166 scoped_ptr<base::DictionaryValue> value( | 170 std::unique_ptr<base::DictionaryValue> value( |
| 167 GetTestFederatedCredentialDictionaryValue()); | 171 GetTestFederatedCredentialDictionaryValue()); |
| 168 value->SetString("avatarURL", "foo"); | 172 value->SetString("avatarURL", "foo"); |
| 169 Credential credential; | 173 Credential credential; |
| 170 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 174 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 171 } | 175 } |
| 172 | 176 |
| 173 // Tests that parsing a value representing a FederatedCredential with no | 177 // Tests that parsing a value representing a FederatedCredential with no |
| 174 // federation URL fails. | 178 // federation URL fails. |
| 175 TEST(CredentialUtilTest, ParsingFederatedValueWithNoFederationURLFails) { | 179 TEST(CredentialUtilTest, ParsingFederatedValueWithNoFederationURLFails) { |
| 176 scoped_ptr<base::DictionaryValue> value( | 180 std::unique_ptr<base::DictionaryValue> value( |
| 177 GetTestFederatedCredentialDictionaryValue()); | 181 GetTestFederatedCredentialDictionaryValue()); |
| 178 value->Remove("federation", nullptr); | 182 value->Remove("federation", nullptr); |
| 179 Credential credential; | 183 Credential credential; |
| 180 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 184 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 181 } | 185 } |
| 182 | 186 |
| 183 // Tests that parsing a value representing a FederatedCredential with a badly- | 187 // Tests that parsing a value representing a FederatedCredential with a badly- |
| 184 // formed federationURL fails. | 188 // formed federationURL fails. |
| 185 TEST(CredentialUtilTest, ParsingFederatedValueWithBadFederationURLFails) { | 189 TEST(CredentialUtilTest, ParsingFederatedValueWithBadFederationURLFails) { |
| 186 scoped_ptr<base::DictionaryValue> value( | 190 std::unique_ptr<base::DictionaryValue> value( |
| 187 GetTestFederatedCredentialDictionaryValue()); | 191 GetTestFederatedCredentialDictionaryValue()); |
| 188 value->SetString("federation", "bar"); | 192 value->SetString("federation", "bar"); |
| 189 Credential credential; | 193 Credential credential; |
| 190 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 194 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 191 } | 195 } |
| 192 | 196 |
| 193 // Tests that serializing a FederatedCredential to a DictionaryValue results | 197 // Tests that serializing a FederatedCredential to a DictionaryValue results |
| 194 // in the expected structure. | 198 // in the expected structure. |
| 195 TEST(CredentialUtilTest, SerializeFederatedCredential) { | 199 TEST(CredentialUtilTest, SerializeFederatedCredential) { |
| 196 base::DictionaryValue value; | 200 base::DictionaryValue value; |
| 197 Credential credential(GetTestFederatedCredential()); | 201 Credential credential(GetTestFederatedCredential()); |
| 198 CredentialToDictionaryValue(credential, &value); | 202 CredentialToDictionaryValue(credential, &value); |
| 199 EXPECT_TRUE(GetTestFederatedCredentialDictionaryValue()->Equals(&value)); | 203 EXPECT_TRUE(GetTestFederatedCredentialDictionaryValue()->Equals(&value)); |
| 200 } | 204 } |
| 201 | 205 |
| 202 // Tests that serializing a PasswordCredential to a DictionaryValue results in | 206 // Tests that serializing a PasswordCredential to a DictionaryValue results in |
| 203 // the | 207 // the |
| 204 // expected structure. | 208 // expected structure. |
| 205 TEST(CredentialUtilTest, SerializePasswordCredential) { | 209 TEST(CredentialUtilTest, SerializePasswordCredential) { |
| 206 base::DictionaryValue value; | 210 base::DictionaryValue value; |
| 207 Credential credential(GetTestPasswordCredential()); | 211 Credential credential(GetTestPasswordCredential()); |
| 208 CredentialToDictionaryValue(credential, &value); | 212 CredentialToDictionaryValue(credential, &value); |
| 209 EXPECT_TRUE(GetTestPasswordCredentialDictionaryValue()->Equals(&value)); | 213 EXPECT_TRUE(GetTestPasswordCredentialDictionaryValue()->Equals(&value)); |
| 210 } | 214 } |
| 211 | 215 |
| 212 TEST(CredentialUtilTest, SerializeEmptyCredential) { | 216 TEST(CredentialUtilTest, SerializeEmptyCredential) { |
| 213 base::DictionaryValue value; | 217 base::DictionaryValue value; |
| 214 Credential credential; | 218 Credential credential; |
| 215 CredentialToDictionaryValue(credential, &value); | 219 CredentialToDictionaryValue(credential, &value); |
| 216 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); | 220 EXPECT_TRUE(base::WrapUnique(new base::DictionaryValue)->Equals(&value)); |
| 217 } | 221 } |
| 218 | 222 |
| 219 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { | 223 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { |
| 220 base::DictionaryValue value; | 224 base::DictionaryValue value; |
| 221 value.SetString("foo", "bar"); | 225 value.SetString("foo", "bar"); |
| 222 Credential credential; | 226 Credential credential; |
| 223 CredentialToDictionaryValue(credential, &value); | 227 CredentialToDictionaryValue(credential, &value); |
| 224 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); | 228 EXPECT_TRUE(base::WrapUnique(new base::DictionaryValue)->Equals(&value)); |
| 225 } | 229 } |
| 226 | 230 |
| 227 } // namespace | 231 } // namespace |
| 228 } // namespace web | 232 } // namespace web |
| OLD | NEW |