Pull Chrome_Elf out of the renderer process
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_util_unittest.cc
blob98e1b766133d9a73abd6fdfee7f24ee2700fce32
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "google_apis/gaia/gaia_auth_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "url/gurl.h"
10 namespace gaia {
12 TEST(GaiaAuthUtilTest, EmailAddressNoOp) {
13 const char lower_case[] = "user@what.com";
14 EXPECT_EQ(lower_case, CanonicalizeEmail(lower_case));
17 TEST(GaiaAuthUtilTest, EmailAddressIgnoreCaps) {
18 EXPECT_EQ(CanonicalizeEmail("user@what.com"),
19 CanonicalizeEmail("UsEr@what.com"));
22 TEST(GaiaAuthUtilTest, EmailAddressIgnoreDomainCaps) {
23 EXPECT_EQ(CanonicalizeEmail("user@what.com"),
24 CanonicalizeEmail("UsEr@what.COM"));
27 TEST(GaiaAuthUtilTest, EmailAddressRejectOneUsernameDot) {
28 EXPECT_NE(CanonicalizeEmail("u.ser@what.com"),
29 CanonicalizeEmail("UsEr@what.com"));
32 TEST(GaiaAuthUtilTest, EmailAddressMatchWithOneUsernameDot) {
33 EXPECT_EQ(CanonicalizeEmail("u.ser@what.com"),
34 CanonicalizeEmail("U.sEr@what.com"));
37 TEST(GaiaAuthUtilTest, EmailAddressIgnoreOneUsernameDot) {
38 EXPECT_EQ(CanonicalizeEmail("us.er@gmail.com"),
39 CanonicalizeEmail("UsEr@gmail.com"));
42 TEST(GaiaAuthUtilTest, EmailAddressIgnoreManyUsernameDots) {
43 EXPECT_EQ(CanonicalizeEmail("u.ser@gmail.com"),
44 CanonicalizeEmail("Us.E.r@gmail.com"));
47 TEST(GaiaAuthUtilTest, EmailAddressIgnoreConsecutiveUsernameDots) {
48 EXPECT_EQ(CanonicalizeEmail("use.r@gmail.com"),
49 CanonicalizeEmail("Us....E.r@gmail.com"));
52 TEST(GaiaAuthUtilTest, EmailAddressDifferentOnesRejected) {
53 EXPECT_NE(CanonicalizeEmail("who@what.com"),
54 CanonicalizeEmail("Us....E.r@what.com"));
57 TEST(GaiaAuthUtilTest, GooglemailNotCanonicalizedToGmail) {
58 const char googlemail[] = "user@googlemail.com";
59 EXPECT_EQ(googlemail, CanonicalizeEmail(googlemail));
62 TEST(GaiaAuthUtilTest, CanonicalizeDomain) {
63 const char domain[] = "example.com";
64 EXPECT_EQ(domain, CanonicalizeDomain("example.com"));
65 EXPECT_EQ(domain, CanonicalizeDomain("EXAMPLE.cOm"));
68 TEST(GaiaAuthUtilTest, ExtractDomainName) {
69 const char domain[] = "example.com";
70 EXPECT_EQ(domain, ExtractDomainName("who@example.com"));
71 EXPECT_EQ(domain, ExtractDomainName("who@EXAMPLE.cOm"));
74 TEST(GaiaAuthUtilTest, SanitizeMissingDomain) {
75 EXPECT_EQ("nodomain@gmail.com", SanitizeEmail("nodomain"));
78 TEST(GaiaAuthUtilTest, SanitizeExistingDomain) {
79 const char existing[] = "test@example.com";
80 EXPECT_EQ(existing, SanitizeEmail(existing));
83 TEST(GaiaAuthUtilTest, AreEmailsSame) {
84 EXPECT_TRUE(AreEmailsSame("foo", "foo"));
85 EXPECT_TRUE(AreEmailsSame("foo", "foo@gmail.com"));
86 EXPECT_TRUE(AreEmailsSame("foo@gmail.com", "Foo@Gmail.com"));
87 EXPECT_FALSE(AreEmailsSame("foo@gmail.com", "foo@othermail.com"));
88 EXPECT_FALSE(AreEmailsSame("user@gmail.com", "foo@gmail.com"));
91 TEST(GaiaAuthUtilTest, GmailAndGooglemailAreSame) {
92 EXPECT_TRUE(AreEmailsSame("foo@gmail.com", "foo@googlemail.com"));
93 EXPECT_FALSE(AreEmailsSame("bar@gmail.com", "foo@googlemail.com"));
96 TEST(GaiaAuthUtilTest, IsGaiaSignonRealm) {
97 // Only https versions of Gaia URLs should be considered valid.
98 EXPECT_TRUE(IsGaiaSignonRealm(GURL("https://accounts.google.com/")));
99 EXPECT_FALSE(IsGaiaSignonRealm(GURL("http://accounts.google.com/")));
101 // Other Google URLs are not valid.
102 EXPECT_FALSE(IsGaiaSignonRealm(GURL("https://www.google.com/")));
103 EXPECT_FALSE(IsGaiaSignonRealm(GURL("http://www.google.com/")));
104 EXPECT_FALSE(IsGaiaSignonRealm(GURL("https://google.com/")));
105 EXPECT_FALSE(IsGaiaSignonRealm(GURL("https://mail.google.com/")));
107 // Other https URLs are not valid.
108 EXPECT_FALSE(IsGaiaSignonRealm(GURL("https://www.example.com/")));
111 TEST(GaiaAuthUtilTest, ParseListAccountsData) {
112 std::vector<ListedAccount> accounts;
113 ASSERT_FALSE(ParseListAccountsData("", &accounts));
114 ASSERT_EQ(0u, accounts.size());
116 ASSERT_FALSE(ParseListAccountsData("1", &accounts));
117 ASSERT_EQ(0u, accounts.size());
119 ASSERT_FALSE(ParseListAccountsData("[]", &accounts));
120 ASSERT_EQ(0u, accounts.size());
122 ASSERT_FALSE(ParseListAccountsData("[\"foo\", \"bar\"]", &accounts));
123 ASSERT_EQ(0u, accounts.size());
125 ASSERT_TRUE(ParseListAccountsData("[\"foo\", []]", &accounts));
126 ASSERT_EQ(0u, accounts.size());
128 ASSERT_TRUE(ParseListAccountsData(
129 "[\"foo\", [[\"bar\", 0, \"name\", 0, \"photo\", 0, 0, 0]]]", &accounts));
130 ASSERT_EQ(0u, accounts.size());
132 ASSERT_TRUE(ParseListAccountsData(
133 "[\"foo\", "
134 "[[\"bar\", 0, \"name\", \"u@g.c\", \"p\", 0, 0, 0, 0, 1, \"45\"]]]",
135 &accounts));
136 ASSERT_EQ(1u, accounts.size());
137 ASSERT_EQ("u@g.c", accounts[0].email);
138 ASSERT_TRUE(accounts[0].valid);
140 ASSERT_TRUE(ParseListAccountsData(
141 "[\"foo\", "
142 "[[\"bar1\",0,\"name1\",\"u1@g.c\",\"photo1\",0,0,0,0,1,\"45\"], "
143 "[\"bar2\",0,\"name2\",\"u2@g.c\",\"photo2\",0,0,0,0,1,\"6\"]]]",
144 &accounts));
145 ASSERT_EQ(2u, accounts.size());
146 ASSERT_EQ("u1@g.c", accounts[0].email);
147 ASSERT_TRUE(accounts[0].valid);
148 ASSERT_EQ("u2@g.c", accounts[1].email);
149 ASSERT_TRUE(accounts[1].valid);
151 ASSERT_TRUE(ParseListAccountsData(
152 "[\"foo\", "
153 "[[\"b1\", 0,\"name1\",\"U1@g.c\",\"photo1\",0,0,0,0,1,\"45\"], "
154 "[\"b2\",0,\"name2\",\"u.2@g.c\",\"photo2\",0,0,0,0,1,\"46\"]]]",
155 &accounts));
156 ASSERT_EQ(2u, accounts.size());
157 ASSERT_EQ(CanonicalizeEmail("U1@g.c"), accounts[0].email);
158 ASSERT_TRUE(accounts[0].valid);
159 ASSERT_EQ(CanonicalizeEmail("u.2@g.c"), accounts[1].email);
160 ASSERT_TRUE(accounts[1].valid);
163 TEST(GaiaAuthUtilTest, ParseListAccountsDataValidSession) {
164 std::vector<ListedAccount> accounts;
166 // Valid session is true means: return account.
167 ASSERT_TRUE(ParseListAccountsData(
168 "[\"foo\", [[\"b\",0,\"n\",\"u@g.c\",\"photo\",0,0,0,0,1,\"45\"]]]",
169 &accounts));
170 ASSERT_EQ(1u, accounts.size());
171 ASSERT_EQ("u@g.c", accounts[0].email);
172 ASSERT_TRUE(accounts[0].valid);
174 // Valid session is false means: return account with valid bit false.
175 ASSERT_TRUE(ParseListAccountsData(
176 "[\"foo\", [[\"b\",0,\"n\",\"u@g.c\",\"photo\",0,0,0,0,0,\"45\"]]]",
177 &accounts));
178 ASSERT_EQ(1u, accounts.size());
179 ASSERT_FALSE(accounts[0].valid);
182 TEST(GaiaAuthUtilTest, ParseListAccountsDataGaiaId) {
183 std::vector<ListedAccount> accounts;
185 // Missing gaia id means: do not return account.
186 ASSERT_TRUE(ParseListAccountsData(
187 "[\"foo\", [[\"b\", 0, \"n\", \"u@g.c\", \"photo\", 0, 0, 0, 0, 1]]]",
188 &accounts));
189 ASSERT_EQ(0u, accounts.size());
191 // Valid gaia session means: return gaia session
192 ASSERT_TRUE(ParseListAccountsData(
193 "[\"foo\", "
194 "[[\"b\",0,\"n\",\"u@g.c\",\"photo\",0,0,0,0,1,\"9863\"]]]",
195 &accounts));
196 ASSERT_EQ(1u, accounts.size());
197 ASSERT_EQ("u@g.c", accounts[0].email);
198 ASSERT_TRUE(accounts[0].valid);
199 ASSERT_EQ("9863", accounts[0].gaia_id);
202 } // namespace gaia