| 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 "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "ios/web/public/web_state/credential.h" | 10 #include "ios/web/public/web_state/credential.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 // Returns a value representing the credential returned by | 63 // Returns a value representing the credential returned by |
| 64 // |GetTestPasswordCredential()|. | 64 // |GetTestPasswordCredential()|. |
| 65 scoped_ptr<base::DictionaryValue> GetTestPasswordCredentialDictionaryValue() { | 65 scoped_ptr<base::DictionaryValue> GetTestPasswordCredentialDictionaryValue() { |
| 66 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 66 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 67 value->SetString("type", kTestCredentialTypePassword); | 67 value->SetString("type", kTestCredentialTypePassword); |
| 68 value->SetString("id", kTestCredentialID); | 68 value->SetString("id", kTestCredentialID); |
| 69 value->SetString("name", kTestCredentialName); | 69 value->SetString("name", kTestCredentialName); |
| 70 value->SetString("avatarURL", kTestCredentialAvatarURL); | 70 value->SetString("avatarURL", kTestCredentialAvatarURL); |
| 71 value->SetString("password", kTestCredentialPassword); | 71 value->SetString("password", kTestCredentialPassword); |
| 72 return value.Pass(); | 72 return value; |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Returns a value representing the credential returned by | 75 // Returns a value representing the credential returned by |
| 76 // |GetTestFederatedCredentialDictionaryValue()|. | 76 // |GetTestFederatedCredentialDictionaryValue()|. |
| 77 scoped_ptr<base::DictionaryValue> GetTestFederatedCredentialDictionaryValue() { | 77 scoped_ptr<base::DictionaryValue> GetTestFederatedCredentialDictionaryValue() { |
| 78 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 78 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 79 value->SetString("type", kTestCredentialTypeFederated); | 79 value->SetString("type", kTestCredentialTypeFederated); |
| 80 value->SetString("id", kTestCredentialID); | 80 value->SetString("id", kTestCredentialID); |
| 81 value->SetString("name", kTestCredentialName); | 81 value->SetString("name", kTestCredentialName); |
| 82 value->SetString("avatarURL", kTestCredentialAvatarURL); | 82 value->SetString("avatarURL", kTestCredentialAvatarURL); |
| 83 value->SetString("federation", kTestCredentialFederationURL); | 83 value->SetString("federation", kTestCredentialFederationURL); |
| 84 return value.Pass(); | 84 return value; |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Tests that parsing an empty value fails. | 87 // Tests that parsing an empty value fails. |
| 88 TEST(CredentialUtilTest, ParsingEmptyValueFails) { | 88 TEST(CredentialUtilTest, ParsingEmptyValueFails) { |
| 89 base::DictionaryValue value; | 89 base::DictionaryValue value; |
| 90 Credential credential; | 90 Credential credential; |
| 91 EXPECT_FALSE(DictionaryValueToCredential(value, &credential)); | 91 EXPECT_FALSE(DictionaryValueToCredential(value, &credential)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Tests that parsing a value with a bad type fails. | 94 // Tests that parsing a value with a bad type fails. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 Credential credential; | 105 Credential credential; |
| 106 EXPECT_TRUE(DictionaryValueToCredential( | 106 EXPECT_TRUE(DictionaryValueToCredential( |
| 107 *GetTestPasswordCredentialDictionaryValue(), &credential)); | 107 *GetTestPasswordCredentialDictionaryValue(), &credential)); |
| 108 EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential)); | 108 EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Tests that parsing a value representing a PasswordCredential but with no ID | 111 // Tests that parsing a value representing a PasswordCredential but with no ID |
| 112 // specified fails. | 112 // specified fails. |
| 113 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) { | 113 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) { |
| 114 scoped_ptr<base::DictionaryValue> value( | 114 scoped_ptr<base::DictionaryValue> value( |
| 115 GetTestPasswordCredentialDictionaryValue().Pass()); | 115 GetTestPasswordCredentialDictionaryValue()); |
| 116 value->RemoveWithoutPathExpansion("id", nullptr); | 116 value->RemoveWithoutPathExpansion("id", nullptr); |
| 117 Credential credential; | 117 Credential credential; |
| 118 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 118 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // Tests that parsing a value representing a PasswordCredential with a badly- | 121 // Tests that parsing a value representing a PasswordCredential with a badly- |
| 122 // formed avatarURL fails. | 122 // formed avatarURL fails. |
| 123 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) { | 123 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) { |
| 124 scoped_ptr<base::DictionaryValue> value( | 124 scoped_ptr<base::DictionaryValue> value( |
| 125 GetTestPasswordCredentialDictionaryValue().Pass()); | 125 GetTestPasswordCredentialDictionaryValue()); |
| 126 value->SetString("avatarURL", "foo"); | 126 value->SetString("avatarURL", "foo"); |
| 127 Credential credential; | 127 Credential credential; |
| 128 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 128 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Tests that parsing a value representing a PasswordCredential with no password | 131 // Tests that parsing a value representing a PasswordCredential with no password |
| 132 // specified fails. | 132 // specified fails. |
| 133 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) { | 133 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) { |
| 134 scoped_ptr<base::DictionaryValue> value( | 134 scoped_ptr<base::DictionaryValue> value( |
| 135 GetTestPasswordCredentialDictionaryValue().Pass()); | 135 GetTestPasswordCredentialDictionaryValue()); |
| 136 value->Remove("password", nullptr); | 136 value->Remove("password", nullptr); |
| 137 Credential credential; | 137 Credential credential; |
| 138 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 138 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Tests that parsing a correctly-formed value representing a | 141 // Tests that parsing a correctly-formed value representing a |
| 142 // FederatedCredential succeeds. | 142 // FederatedCredential succeeds. |
| 143 TEST(CredentialUtilTest, ParsingFederatedCredentialSucceeds) { | 143 TEST(CredentialUtilTest, ParsingFederatedCredentialSucceeds) { |
| 144 Credential credential; | 144 Credential credential; |
| 145 EXPECT_TRUE(DictionaryValueToCredential( | 145 EXPECT_TRUE(DictionaryValueToCredential( |
| 146 *GetTestFederatedCredentialDictionaryValue(), &credential)); | 146 *GetTestFederatedCredentialDictionaryValue(), &credential)); |
| 147 EXPECT_TRUE(CredentialsEqual(GetTestFederatedCredential(), credential)); | 147 EXPECT_TRUE(CredentialsEqual(GetTestFederatedCredential(), credential)); |
| 148 } | 148 } |
| 149 | 149 |
| 150 // Tests that parsing a value representing a FederatedCredential with no ID | 150 // Tests that parsing a value representing a FederatedCredential with no ID |
| 151 // fails. | 151 // fails. |
| 152 TEST(CredentialUtilTest, ParsingFederatedCredentialWithNoIDFails) { | 152 TEST(CredentialUtilTest, ParsingFederatedCredentialWithNoIDFails) { |
| 153 scoped_ptr<base::DictionaryValue> value( | 153 scoped_ptr<base::DictionaryValue> value( |
| 154 GetTestFederatedCredentialDictionaryValue().Pass()); | 154 GetTestFederatedCredentialDictionaryValue()); |
| 155 value->RemoveWithoutPathExpansion("id", nullptr); | 155 value->RemoveWithoutPathExpansion("id", nullptr); |
| 156 Credential credential; | 156 Credential credential; |
| 157 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 157 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Tests that parsing a value representing a FederatedCredential with a badly- | 160 // Tests that parsing a value representing a FederatedCredential with a badly- |
| 161 // formed avatarURL fails. | 161 // formed avatarURL fails. |
| 162 TEST(CredentialUtilTest, ParsingFederatedCredentialWithBadAvatarURLFails) { | 162 TEST(CredentialUtilTest, ParsingFederatedCredentialWithBadAvatarURLFails) { |
| 163 scoped_ptr<base::DictionaryValue> value( | 163 scoped_ptr<base::DictionaryValue> value( |
| 164 GetTestFederatedCredentialDictionaryValue().Pass()); | 164 GetTestFederatedCredentialDictionaryValue()); |
| 165 value->SetString("avatarURL", "foo"); | 165 value->SetString("avatarURL", "foo"); |
| 166 Credential credential; | 166 Credential credential; |
| 167 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 167 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Tests that parsing a value representing a FederatedCredential with no | 170 // Tests that parsing a value representing a FederatedCredential with no |
| 171 // federation URL fails. | 171 // federation URL fails. |
| 172 TEST(CredentialUtilTest, ParsingFederatedValueWithNoFederationURLFails) { | 172 TEST(CredentialUtilTest, ParsingFederatedValueWithNoFederationURLFails) { |
| 173 scoped_ptr<base::DictionaryValue> value( | 173 scoped_ptr<base::DictionaryValue> value( |
| 174 GetTestFederatedCredentialDictionaryValue().Pass()); | 174 GetTestFederatedCredentialDictionaryValue()); |
| 175 value->Remove("federation", nullptr); | 175 value->Remove("federation", nullptr); |
| 176 Credential credential; | 176 Credential credential; |
| 177 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 177 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 // Tests that parsing a value representing a FederatedCredential with a badly- | 180 // Tests that parsing a value representing a FederatedCredential with a badly- |
| 181 // formed federationURL fails. | 181 // formed federationURL fails. |
| 182 TEST(CredentialUtilTest, ParsingFederatedValueWithBadFederationURLFails) { | 182 TEST(CredentialUtilTest, ParsingFederatedValueWithBadFederationURLFails) { |
| 183 scoped_ptr<base::DictionaryValue> value( | 183 scoped_ptr<base::DictionaryValue> value( |
| 184 GetTestFederatedCredentialDictionaryValue().Pass()); | 184 GetTestFederatedCredentialDictionaryValue()); |
| 185 value->SetString("federation", "bar"); | 185 value->SetString("federation", "bar"); |
| 186 Credential credential; | 186 Credential credential; |
| 187 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 187 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Tests that serializing a FederatedCredential to a DictionaryValue results | 190 // Tests that serializing a FederatedCredential to a DictionaryValue results |
| 191 // in the expected structure. | 191 // in the expected structure. |
| 192 TEST(CredentialUtilTest, SerializeFederatedCredential) { | 192 TEST(CredentialUtilTest, SerializeFederatedCredential) { |
| 193 base::DictionaryValue value; | 193 base::DictionaryValue value; |
| 194 Credential credential(GetTestFederatedCredential()); | 194 Credential credential(GetTestFederatedCredential()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 216 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { | 216 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { |
| 217 base::DictionaryValue value; | 217 base::DictionaryValue value; |
| 218 value.SetString("foo", "bar"); | 218 value.SetString("foo", "bar"); |
| 219 Credential credential; | 219 Credential credential; |
| 220 CredentialToDictionaryValue(credential, &value); | 220 CredentialToDictionaryValue(credential, &value); |
| 221 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); | 221 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); |
| 222 } | 222 } |
| 223 | 223 |
| 224 } // namespace | 224 } // namespace |
| 225 } // namespace web | 225 } // namespace web |
| OLD | NEW |