summaryrefslogtreecommitdiffstats
path: root/components/auto_login_parser/auto_login_parser_unittest.cc
blob: 442784c734f0153fb3b85499d89d885fe4b6aad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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 "components/auto_login_parser/auto_login_parser.h"

#include <string>

#include "testing/gtest/include/gtest/gtest.h"

namespace auto_login_parser {

class AutoLoginParserTest : public testing::Test {
 protected:
  static bool IsHeaderDataEmpty(const HeaderData& header) {
    return header.realm.empty() && header.account.empty() &&
        header.args.empty();
  }
};

TEST_F(AutoLoginParserTest, ParseHeader) {
  std::string header =
      "realm=com.google&"
      "account=fred.example%40gmail.com&"
      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";

  HeaderData header_data;
  EXPECT_TRUE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));

  ASSERT_EQ("com.google", header_data.realm);
  ASSERT_EQ("fred.example@gmail.com", header_data.account);
  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
}

TEST_F(AutoLoginParserTest, ParseHeaderOnlySupportsComGoogle) {
  std::string header =
      "realm=com.microsoft&"
      "account=fred.example%40gmail.com&"
      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";

  HeaderData header_data;
  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
  // |header| should not be touched when parsing fails.
  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
}

TEST_F(AutoLoginParserTest, ParseHeaderWithMissingRealm) {
  std::string header =
      "account=fred.example%40gmail.com&"
      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";

  HeaderData header_data;
  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
}

TEST_F(AutoLoginParserTest, ParseHeaderWithMissingArgs) {
  std::string header =
      "realm=com.google&"
      "account=fred.example%40gmail.com&";

  HeaderData header_data;
  EXPECT_FALSE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
  EXPECT_TRUE(IsHeaderDataEmpty(header_data));
}

TEST_F(AutoLoginParserTest, ParseHeaderWithoutOptionalAccount) {
  std::string header =
      "realm=com.google&"
      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";

  HeaderData header_data;
  EXPECT_TRUE(ParseHeader(header, ONLY_GOOGLE_COM, &header_data));
  ASSERT_EQ("com.google", header_data.realm);
  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
}

TEST_F(AutoLoginParserTest, ParseHeaderAllowsAnyRealmWithOption) {
  std::string header =
      "realm=com.microsoft&"
      "account=fred.example%40gmail.com&"
      "args=kfdshfwoeriudslkfsdjfhdskjfhsdkr";

  HeaderData header_data;
  EXPECT_TRUE(ParseHeader(header, ALLOW_ANY_REALM, &header_data));

  ASSERT_EQ("com.microsoft", header_data.realm);
  ASSERT_EQ("fred.example@gmail.com", header_data.account);
  ASSERT_EQ("kfdshfwoeriudslkfsdjfhdskjfhsdkr", header_data.args);
}

}  // namespace auto_login_parser