diff options
author | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-25 12:36:05 +0000 |
---|---|---|
committer | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-25 12:36:05 +0000 |
commit | 9008b35a1a6c49905694a93215148ec6d86696e5 (patch) | |
tree | d0a0acb45fa9c752c8dcceb10e9510721034f206 /chrome/common/net | |
parent | b4c71c1b9c5daa9afd39a121ca8e68eb576acbf9 (diff) | |
download | chromium_src-9008b35a1a6c49905694a93215148ec6d86696e5.zip chromium_src-9008b35a1a6c49905694a93215148ec6d86696e5.tar.gz chromium_src-9008b35a1a6c49905694a93215148ec6d86696e5.tar.bz2 |
Move email and domain canonicalization helper to common/net/gaia
This is in preparation for a CL that uses this functionality in non-chromeos code.
BUG=None
TEST=Compiles and passes tests, no functional changes.
R=nkostylev@chromium.org
TBR=arv@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10584039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@143899 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/net')
-rw-r--r-- | chrome/common/net/gaia/gaia_auth_util.cc | 61 | ||||
-rw-r--r-- | chrome/common/net/gaia/gaia_auth_util.h | 32 | ||||
-rw-r--r-- | chrome/common/net/gaia/gaia_auth_util_unittest.cc | 87 |
3 files changed, 180 insertions, 0 deletions
diff --git a/chrome/common/net/gaia/gaia_auth_util.cc b/chrome/common/net/gaia/gaia_auth_util.cc new file mode 100644 index 0000000..ec9f421 --- /dev/null +++ b/chrome/common/net/gaia/gaia_auth_util.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/net/gaia/gaia_auth_util.h" + +#include <vector> + +#include "base/logging.h" +#include "base/string_split.h" +#include "base/string_util.h" + +namespace gaia { + +namespace { +const char kGmailDomain[] = "gmail.com"; +} + +std::string CanonicalizeEmail(const std::string& email_address) { + std::vector<std::string> parts; + char at = '@'; + base::SplitString(email_address, at, &parts); + if (parts.size() != 2U) + NOTREACHED() << "expecting exactly one @, but got " << parts.size(); + else if (parts[1] == kGmailDomain) // only strip '.' for gmail accounts. + RemoveChars(parts[0], ".", &parts[0]); + std::string new_email = StringToLowerASCII(JoinString(parts, at)); + VLOG(1) << "Canonicalized " << email_address << " to " << new_email; + return new_email; +} + +std::string CanonicalizeDomain(const std::string& domain) { + // Canonicalization of domain names means lower-casing them. Make sure to + // update this function in sync with Canonicalize if this ever changes. + return StringToLowerASCII(domain); +} + +std::string SanitizeEmail(const std::string& email_address) { + std::string sanitized(email_address); + + // Apply a default domain if necessary. + if (sanitized.find('@') == std::string::npos) { + sanitized += '@'; + sanitized += kGmailDomain; + } + + return sanitized; +} + +std::string ExtractDomainName(const std::string& email_address) { + // First canonicalize which will also verify we have proper domain part. + std::string email = CanonicalizeEmail(email_address); + size_t separator_pos = email.find('@'); + if (separator_pos != email.npos && separator_pos < email.length() - 1) + return email.substr(separator_pos + 1); + else + NOTREACHED() << "Not a proper email address: " << email; + return std::string(); +} + +} // namespace gaia diff --git a/chrome/common/net/gaia/gaia_auth_util.h b/chrome/common/net/gaia/gaia_auth_util.h new file mode 100644 index 0000000..2d4ab00 --- /dev/null +++ b/chrome/common/net/gaia/gaia_auth_util.h @@ -0,0 +1,32 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTH_UTIL_H_ +#define CHROME_COMMON_NET_GAIA_GAIA_AUTH_UTIL_H_ +#pragma once + +#include <string> + +namespace gaia { + +// Perform basic canonicalization of |email_address|, taking into account that +// gmail does not consider '.' or caps inside a username to matter. It also +// ignores everything after a '+'. For example, c.masone+abc@gmail.com == +// cMaSone@gmail.com, per +// http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313# +std::string CanonicalizeEmail(const std::string& email_address); + +// Returns the canonical form of the given domain. +std::string CanonicalizeDomain(const std::string& domain); + +// Sanitize emails. Currently, it only ensures all emails have a domain by +// adding gmail.com if no domain is present. +std::string SanitizeEmail(const std::string& email_address); + +// Extract the domain part from the canonical form of the given email. +std::string ExtractDomainName(const std::string& email); + +} // namespace gaia + +#endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_UTIL_H_ diff --git a/chrome/common/net/gaia/gaia_auth_util_unittest.cc b/chrome/common/net/gaia/gaia_auth_util_unittest.cc new file mode 100644 index 0000000..0f7c702 --- /dev/null +++ b/chrome/common/net/gaia/gaia_auth_util_unittest.cc @@ -0,0 +1,87 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/net/gaia/gaia_auth_util.h" + +#include "testing/gtest/include/gtest/gtest.h" + +namespace gaia { + +TEST(GaiaAuthUtilTest, EmailAddressNoOp) { + const char lower_case[] = "user@what.com"; + EXPECT_EQ(lower_case, CanonicalizeEmail(lower_case)); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreCaps) { + EXPECT_EQ(CanonicalizeEmail("user@what.com"), + CanonicalizeEmail("UsEr@what.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreDomainCaps) { + EXPECT_EQ(CanonicalizeEmail("user@what.com"), + CanonicalizeEmail("UsEr@what.COM")); +} + +TEST(GaiaAuthUtilTest, EmailAddressRejectOneUsernameDot) { + EXPECT_NE(CanonicalizeEmail("u.ser@what.com"), + CanonicalizeEmail("UsEr@what.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressMatchWithOneUsernameDot) { + EXPECT_EQ(CanonicalizeEmail("u.ser@what.com"), + CanonicalizeEmail("U.sEr@what.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreOneUsernameDot) { + EXPECT_EQ(CanonicalizeEmail("us.er@gmail.com"), + CanonicalizeEmail("UsEr@gmail.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreManyUsernameDots) { + EXPECT_EQ(CanonicalizeEmail("u.ser@gmail.com"), + CanonicalizeEmail("Us.E.r@gmail.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreConsecutiveUsernameDots) { + EXPECT_EQ(CanonicalizeEmail("use.r@gmail.com"), + CanonicalizeEmail("Us....E.r@gmail.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressDifferentOnesRejected) { + EXPECT_NE(CanonicalizeEmail("who@what.com"), + CanonicalizeEmail("Us....E.r@what.com")); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnorePlusSuffix) { + const char with_plus[] = "user+cc@what.com"; + EXPECT_EQ(with_plus, CanonicalizeEmail(with_plus)); +} + +TEST(GaiaAuthUtilTest, EmailAddressIgnoreMultiPlusSuffix) { + const char multi_plus[] = "user+cc+bcc@what.com"; + EXPECT_EQ(multi_plus, CanonicalizeEmail(multi_plus)); +} + +TEST(GaiaAuthUtilTest, CanonicalizeDomain) { + const char domain[] = "example.com"; + EXPECT_EQ(domain, CanonicalizeDomain("example.com")); + EXPECT_EQ(domain, CanonicalizeDomain("EXAMPLE.cOm")); +} + +TEST(GaiaAuthUtilTest, ExtractDomainName) { + const char domain[] = "example.com"; + EXPECT_EQ(domain, ExtractDomainName("who@example.com")); + EXPECT_EQ(domain, ExtractDomainName("who@EXAMPLE.cOm")); +} + +TEST(GaiaAuthUtilTest, SanitizeMissingDomain) { + EXPECT_EQ("nodomain@gmail.com", SanitizeEmail("nodomain")); +} + +TEST(GaiaAuthUtilTest, SanitizeExistingDomain) { + const char existing[] = "test@example.com"; + EXPECT_EQ(existing, SanitizeEmail(existing)); +} + +} // namespace gaia |