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.
4
5#include "components/auto_login_parser/auto_login_parser.h"
6
7#include <string>
8
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace auto_login_parser {
12
13class AutoLoginParserTest : public testing::Test {
14 protected:
15  static bool IsHeaderDataEmpty(const HeaderData& header) {
16    return header.realm.empty() && header.account.empty() &&
17        header.args.empty();
18  }
19};
20
21TEST_F(AutoLoginParserTest, ParseHeader) {
22  std::string header =
23      "realm=com.google&"
24      "account=fred.example%40gmail.com&"
25      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";
26
27  HeaderData header_data;
28  EXPECT_TRUE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
29
30  ASSERT_EQ("com.google", header_data.realm);
31  ASSERT_EQ("fred.example@gmail.com", header_data.account);
32  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
33}
34
35TEST_F(AutoLoginParserTest, ParseHeaderOnlySupportsComGoogle) {
36  std::string header =
37      "realm=com.microsoft&"
38      "account=fred.example%40gmail.com&"
39      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";
40
41  HeaderData header_data;
42  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
43  // |header| should not be touched when parsing fails.
44  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
45}
46
47TEST_F(AutoLoginParserTest, ParseHeaderWithMissingRealm) {
48  std::string header =
49      "account=fred.example%40gmail.com&"
50      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";
51
52  HeaderData header_data;
53  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
54  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
55}
56
57TEST_F(AutoLoginParserTest, ParseHeaderWithMissingArgs) {
58  std::string header =
59      "realm=com.google&"
60      "account=fred.example%40gmail.com&";
61
62  HeaderData header_data;
63  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
64  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
65}
66
67TEST_F(AutoLoginParserTest, ParseHeaderWithoutOptionalAccount) {
68  std::string header =
69      "realm=com.google&"
70      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";
71
72  HeaderData header_data;
73  EXPECT_TRUE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
74  ASSERT_EQ("com.google", header_data.realm);
75  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
76}
77
78TEST_F(AutoLoginParserTest, ParseHeaderAllowsAnyRealmWithOption) {
79  std::string header =
80      "realm=com.microsoft&"
81      "account=fred.example%40gmail.com&"
82      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";
83
84  HeaderData header_data;
85  EXPECT_TRUE(ParseHeader(header, ALLOW_ANY_REALM, &header_data));
86
87  ASSERT_EQ("com.microsoft", header_data.realm);
88  ASSERT_EQ("fred.example@gmail.com", header_data.account);
89  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
90}
91
92}  // namespace auto_login_parser
93