| 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/web_state/js/credential_util.h" | 5 #include "ios/web/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" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" | 13 #include "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 namespace web { | 17 namespace web { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // "type" value for a DictionaryValue representation of LocalCredential. | 20 // "type" value for a DictionaryValue representation of PasswordCredential. |
| 21 const char* kTestCredentialTypeLocal = "LocalCredential"; | 21 const char* kTestCredentialTypePassword = "PasswordCredential"; |
| 22 | 22 |
| 23 // "type" value for a DictionaryValue representation of FederatedCredential. | 23 // "type" value for a DictionaryValue representation of FederatedCredential. |
| 24 const char* kTestCredentialTypeFederated = "FederatedCredential"; | 24 const char* kTestCredentialTypeFederated = "FederatedCredential"; |
| 25 | 25 |
| 26 // "id" value for a DictionaryValue representation of a credential. | 26 // "id" value for a DictionaryValue representation of a credential. |
| 27 const char* kTestCredentialID = "foo"; | 27 const char* kTestCredentialID = "foo"; |
| 28 | 28 |
| 29 // "name" value for a DictionaryValue representation of a credential. | 29 // "name" value for a DictionaryValue representation of a credential. |
| 30 const char* kTestCredentialName = "Foo Bar"; | 30 const char* kTestCredentialName = "Foo Bar"; |
| 31 | 31 |
| 32 // "avatarURL" value for a DictionaryValue representation of a credential. | 32 // "avatarURL" value for a DictionaryValue representation of a credential. |
| 33 const char* kTestCredentialAvatarURL = "https://foo.com/bar.jpg"; | 33 const char* kTestCredentialAvatarURL = "https://foo.com/bar.jpg"; |
| 34 | 34 |
| 35 // "password" value for a DictionaryValue representation of a credential. | 35 // "password" value for a DictionaryValue representation of a credential. |
| 36 const char* kTestCredentialPassword = "baz"; | 36 const char* kTestCredentialPassword = "baz"; |
| 37 | 37 |
| 38 // "federationURL" value for a DictionaryValue representation of a credential. | 38 // "federationURL" value for a DictionaryValue representation of a credential. |
| 39 const char* kTestCredentialFederationURL = "https://foo.com/"; | 39 const char* kTestCredentialFederationURL = "https://foo.com/"; |
| 40 | 40 |
| 41 // Returns a Credential with Local type. | 41 // Returns a Credential with Password type. |
| 42 Credential GetTestLocalCredential() { | 42 Credential GetTestPasswordCredential() { |
| 43 Credential credential; | 43 Credential credential; |
| 44 credential.type = CredentialType::CREDENTIAL_TYPE_LOCAL; | 44 credential.type = CredentialType::CREDENTIAL_TYPE_PASSWORD; |
| 45 credential.id = base::ASCIIToUTF16(kTestCredentialID); | 45 credential.id = base::ASCIIToUTF16(kTestCredentialID); |
| 46 credential.name = base::ASCIIToUTF16(kTestCredentialName); | 46 credential.name = base::ASCIIToUTF16(kTestCredentialName); |
| 47 credential.avatar_url = GURL(kTestCredentialAvatarURL); | 47 credential.avatar_url = GURL(kTestCredentialAvatarURL); |
| 48 credential.password = base::ASCIIToUTF16(kTestCredentialPassword); | 48 credential.password = base::ASCIIToUTF16(kTestCredentialPassword); |
| 49 return credential; | 49 return credential; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Returns a credential with Federated type. | 52 // Returns a credential with Federated type. |
| 53 Credential GetTestFederatedCredential() { | 53 Credential GetTestFederatedCredential() { |
| 54 Credential credential; | 54 Credential credential; |
| 55 credential.type = CredentialType::CREDENTIAL_TYPE_FEDERATED; | 55 credential.type = CredentialType::CREDENTIAL_TYPE_FEDERATED; |
| 56 credential.id = base::ASCIIToUTF16(kTestCredentialID); | 56 credential.id = base::ASCIIToUTF16(kTestCredentialID); |
| 57 credential.name = base::ASCIIToUTF16(kTestCredentialName); | 57 credential.name = base::ASCIIToUTF16(kTestCredentialName); |
| 58 credential.avatar_url = GURL(kTestCredentialAvatarURL); | 58 credential.avatar_url = GURL(kTestCredentialAvatarURL); |
| 59 credential.federation_url = GURL(kTestCredentialFederationURL); | 59 credential.federation_url = GURL(kTestCredentialFederationURL); |
| 60 return credential; | 60 return credential; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Returns a value representing the credential returned by | 63 // Returns a value representing the credential returned by |
| 64 // |GetTestLocalCredential()|. | 64 // |GetTestPasswordCredential()|. |
| 65 scoped_ptr<base::DictionaryValue> GetTestLocalCredentialDictionaryValue() { | 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", kTestCredentialTypeLocal); | 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.Pass(); |
| 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() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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. |
| 95 TEST(CredentialUtilTest, ParsingValueWithBadTypeFails) { | 95 TEST(CredentialUtilTest, ParsingValueWithBadTypeFails) { |
| 96 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 96 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 97 value->SetString("type", "FooCredential"); | 97 value->SetString("type", "FooCredential"); |
| 98 Credential credential; | 98 Credential credential; |
| 99 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); | 99 EXPECT_FALSE(DictionaryValueToCredential(*value, &credential)); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Tests that parsing a correctly-formed value representing a LocalCredential | 102 // Tests that parsing a correctly-formed value representing a PasswordCredential |
| 103 // succeeds. | 103 // succeeds. |
| 104 TEST(CredentialUtilTest, ParsingLocalCredentialSucceeds) { | 104 TEST(CredentialUtilTest, ParsingPasswordCredentialSucceeds) { |
| 105 Credential credential; | 105 Credential credential; |
| 106 EXPECT_TRUE(DictionaryValueToCredential( | 106 EXPECT_TRUE(DictionaryValueToCredential( |
| 107 *GetTestLocalCredentialDictionaryValue(), &credential)); | 107 *GetTestPasswordCredentialDictionaryValue(), &credential)); |
| 108 EXPECT_TRUE(CredentialsEqual(GetTestLocalCredential(), credential)); | 108 EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Tests that parsing a value representing a LocalCredential 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, ParsingLocalCredentialWithNoIDFails) { | 113 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) { |
| 114 scoped_ptr<base::DictionaryValue> value( | 114 scoped_ptr<base::DictionaryValue> value( |
| 115 GetTestLocalCredentialDictionaryValue().Pass()); | 115 GetTestPasswordCredentialDictionaryValue().Pass()); |
| 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 LocalCredential 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, ParsingLocalCredentialWithBadAvatarURLFails) { | 123 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) { |
| 124 scoped_ptr<base::DictionaryValue> value( | 124 scoped_ptr<base::DictionaryValue> value( |
| 125 GetTestLocalCredentialDictionaryValue().Pass()); | 125 GetTestPasswordCredentialDictionaryValue().Pass()); |
| 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 LocalCredential with no password | 131 // Tests that parsing a value representing a PasswordCredential with no password |
| 132 // specified fails. | 132 // specified fails. |
| 133 TEST(CredentialUtilTest, ParsingLocalCredentialWithNoPasswordFails) { | 133 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) { |
| 134 scoped_ptr<base::DictionaryValue> value( | 134 scoped_ptr<base::DictionaryValue> value( |
| 135 GetTestLocalCredentialDictionaryValue().Pass()); | 135 GetTestPasswordCredentialDictionaryValue().Pass()); |
| 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( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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()); |
| 195 CredentialToDictionaryValue(credential, &value); | 195 CredentialToDictionaryValue(credential, &value); |
| 196 EXPECT_TRUE(GetTestFederatedCredentialDictionaryValue()->Equals(&value)); | 196 EXPECT_TRUE(GetTestFederatedCredentialDictionaryValue()->Equals(&value)); |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Tests that serializing a LocalCredential to a DictionaryValue results in the | 199 // Tests that serializing a PasswordCredential to a DictionaryValue results in |
| 200 // the |
| 200 // expected structure. | 201 // expected structure. |
| 201 TEST(CredentialUtilTest, SerializeLocalCredential) { | 202 TEST(CredentialUtilTest, SerializePasswordCredential) { |
| 202 base::DictionaryValue value; | 203 base::DictionaryValue value; |
| 203 Credential credential(GetTestLocalCredential()); | 204 Credential credential(GetTestPasswordCredential()); |
| 204 CredentialToDictionaryValue(credential, &value); | 205 CredentialToDictionaryValue(credential, &value); |
| 205 EXPECT_TRUE(GetTestLocalCredentialDictionaryValue()->Equals(&value)); | 206 EXPECT_TRUE(GetTestPasswordCredentialDictionaryValue()->Equals(&value)); |
| 206 } | 207 } |
| 207 | 208 |
| 208 TEST(CredentialUtilTest, SerializeEmptyCredential) { | 209 TEST(CredentialUtilTest, SerializeEmptyCredential) { |
| 209 base::DictionaryValue value; | 210 base::DictionaryValue value; |
| 210 Credential credential; | 211 Credential credential; |
| 211 CredentialToDictionaryValue(credential, &value); | 212 CredentialToDictionaryValue(credential, &value); |
| 212 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); | 213 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); |
| 213 } | 214 } |
| 214 | 215 |
| 215 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { | 216 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) { |
| 216 base::DictionaryValue value; | 217 base::DictionaryValue value; |
| 217 value.SetString("foo", "bar"); | 218 value.SetString("foo", "bar"); |
| 218 Credential credential; | 219 Credential credential; |
| 219 CredentialToDictionaryValue(credential, &value); | 220 CredentialToDictionaryValue(credential, &value); |
| 220 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); | 221 EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value)); |
| 221 } | 222 } |
| 222 | 223 |
| 223 } // namespace | 224 } // namespace |
| 224 } // namespace web | 225 } // namespace web |
| OLD | NEW |