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"
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
<std::pair
<std::string
, bool> > 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\", [[\"bar\", 0, \"name\", \"u@g.c\", \"photo\", 0, 0, 0]]]",
135 ASSERT_EQ(1u, accounts
.size());
136 ASSERT_EQ("u@g.c", accounts
[0].first
);
137 ASSERT_TRUE(accounts
[0].second
);
139 ASSERT_TRUE(ParseListAccountsData(
140 "[\"foo\", [[\"bar1\", 0, \"name1\", \"u1@g.c\", \"photo1\", 0, 0, 0], "
141 "[\"bar2\", 0, \"name2\", \"u2@g.c\", \"photo2\", 0, 0, 0]]]",
143 ASSERT_EQ(2u, accounts
.size());
144 ASSERT_EQ("u1@g.c", accounts
[0].first
);
145 ASSERT_TRUE(accounts
[0].second
);
146 ASSERT_EQ("u2@g.c", accounts
[1].first
);
147 ASSERT_TRUE(accounts
[1].second
);
149 ASSERT_TRUE(ParseListAccountsData(
150 "[\"foo\", [[\"b1\", 0, \"name1\", \"U1@g.c\", \"photo1\", 0, 0, 0], "
151 "[\"b2\", 0, \"name2\", \"u.2@g.c\", \"photo2\", 0, 0, 0]]]",
153 ASSERT_EQ(2u, accounts
.size());
154 ASSERT_EQ(CanonicalizeEmail("U1@g.c"), accounts
[0].first
);
155 ASSERT_TRUE(accounts
[0].second
);
156 ASSERT_EQ(CanonicalizeEmail("u.2@g.c"), accounts
[1].first
);
157 ASSERT_TRUE(accounts
[1].second
);
160 TEST(GaiaAuthUtilTest
, ParseListAccountsDataValidSession
) {
161 std::vector
<std::pair
<std::string
, bool> > accounts
;
163 // Missing valid session means: return account.
164 ASSERT_TRUE(ParseListAccountsData(
165 "[\"foo\", [[\"b\", 0, \"n\", \"u@g.c\", \"p\", 0, 0, 0]]]",
167 ASSERT_EQ(1u, accounts
.size());
168 ASSERT_EQ("u@g.c", accounts
[0].first
);
169 ASSERT_TRUE(accounts
[0].second
);
171 // Valid session is true means: return account.
172 ASSERT_TRUE(ParseListAccountsData(
173 "[\"foo\", [[\"b\", 0, \"n\", \"u@g.c\", \"p\", 0, 0, 0, 0, 1]]]",
175 ASSERT_EQ(1u, accounts
.size());
176 ASSERT_EQ("u@g.c", accounts
[0].first
);
177 ASSERT_TRUE(accounts
[0].second
);
179 // Valid session is false means: return account with valid bit false.
180 ASSERT_TRUE(ParseListAccountsData(
181 "[\"foo\", [[\"b\", 0, \"n\", \"u@g.c\", \"p\", 0, 0, 0, 0, 0]]]",
183 ASSERT_EQ(1u, accounts
.size());
184 ASSERT_FALSE(accounts
[0].second
);