summaryrefslogtreecommitdiffstats
path: root/remoting/host/third_party_auth_config_unittest.cc
blob: 002fd81b2950cd329f58d11b7937428209b7b734 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2015 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 "remoting/host/third_party_auth_config.h"

#include <sstream>

#include "base/values.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

namespace key = ::policy::key;

TEST(ThirdPartyAuthConfig, ParseEmpty) {
  ThirdPartyAuthConfig third_party_auth_config;

  EXPECT_TRUE(
      ThirdPartyAuthConfig::ParseStrings("", "", "", &third_party_auth_config));
  EXPECT_TRUE(third_party_auth_config.is_null());
}

TEST(ThirdPartyAuthConfig, ParseValidAll) {
  ThirdPartyAuthConfig third_party_auth_config;

  EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
      "https://token.com/", "https://validation.com/", "certificate subject",
      &third_party_auth_config));
  EXPECT_FALSE(third_party_auth_config.is_null());
  EXPECT_EQ("https://token.com/", third_party_auth_config.token_url.spec());
  EXPECT_EQ("https://validation.com/",
            third_party_auth_config.token_validation_url.spec());
  EXPECT_EQ("certificate subject",
            third_party_auth_config.token_validation_cert_issuer);
}

TEST(ThirdPartyAuthConfig, ParseValidNoCert) {
  ThirdPartyAuthConfig third_party_auth_config;

  EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings("https://token.com/",
                                                 "https://validation.com/", "",
                                                 &third_party_auth_config));
  EXPECT_FALSE(third_party_auth_config.is_null());
  EXPECT_EQ("https://token.com/", third_party_auth_config.token_url.spec());
  EXPECT_EQ("https://validation.com/",
            third_party_auth_config.token_validation_url.spec());
  EXPECT_EQ("", third_party_auth_config.token_validation_cert_issuer);
}

// We validate https-vs-http only on Release builds to help with manual testing.
#if !defined(NDEBUG)
TEST(ThirdPartyAuthConfig, ParseHttp) {
  ThirdPartyAuthConfig third_party_auth_config;

  EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings("http://token.com/",
                                                 "http://validation.com/", "",
                                                 &third_party_auth_config));
  EXPECT_FALSE(third_party_auth_config.is_null());
  EXPECT_EQ("http://token.com/", third_party_auth_config.token_url.spec());
  EXPECT_EQ("http://validation.com/",
            third_party_auth_config.token_validation_url.spec());
}
#endif

namespace {

const std::string valid_url("https://valid.com");
const std::string valid_cert("certificate subject");

}  // namespace

class InvalidUrlTest : public ::testing::TestWithParam<const char*> {};

TEST_P(InvalidUrlTest, ParseInvalidUrl) {
  const std::string& invalid_url(GetParam());

  // Populate |config| with some known data - we will use this for validating
  // that |config| doesn't get modified by ParseStrings during subsequent
  // parsing
  // failure tests.
  ThirdPartyAuthConfig config;
  EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
      "https://token.com/", "https://validation.com/", "certificate subject",
      &config));

  // Test for parsing failure.
  EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings(invalid_url, valid_url,
                                                  valid_cert, &config));
  EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings(valid_url, invalid_url,
                                                  valid_cert, &config));
  EXPECT_FALSE(
      ThirdPartyAuthConfig::ParseStrings(invalid_url, "", "", &config));
  EXPECT_FALSE(
      ThirdPartyAuthConfig::ParseStrings("", invalid_url, "", &config));

  // Validate that ParseStrings doesn't modify its output upon parsing failure.
  EXPECT_EQ("https://token.com/", config.token_url.spec());
  EXPECT_EQ("https://validation.com/", config.token_validation_url.spec());
  EXPECT_EQ("certificate subject", config.token_validation_cert_issuer);
}

// We validate https-vs-http only on Release builds to help with manual testing.
#if defined(NDEBUG)
INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig,
                        InvalidUrlTest,
                        ::testing::Values("http://insecure.com",
                                          "I am not a URL"));
#else
INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig,
                        InvalidUrlTest,
                        ::testing::Values("I am not a URL"));
#endif

TEST(ThirdPartyAuthConfig, ParseInvalidCombination) {
  // Populate |config| with some known data - we will use this for validating
  // that |config| doesn't get modified by ParseStrings during subsequent
  // parsing
  // failure tests.
  ThirdPartyAuthConfig config;
  EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
      "https://token.com/", "https://validation.com/", "certificate subject",
      &config));

  // Only one of TokenUrl and TokenValidationUrl
  EXPECT_FALSE(
      ThirdPartyAuthConfig::ParseStrings("", valid_url, valid_cert, &config));
  EXPECT_FALSE(
      ThirdPartyAuthConfig::ParseStrings(valid_url, "", valid_cert, &config));

  // CertSubject when no TokenUrl and TokenValidationUrl
  EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings("", "", valid_cert, &config));

  // Validate that ParseStrings doesn't modify its output upon parsing failure.
  EXPECT_EQ("https://token.com/", config.token_url.spec());
  EXPECT_EQ("https://validation.com/", config.token_validation_url.spec());
  EXPECT_EQ("certificate subject", config.token_validation_cert_issuer);
}

TEST(ThirdPartyAuthConfig, ExtractEmpty) {
  base::DictionaryValue dict;
  std::string url1, url2, cert;
  EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert));
}

TEST(ThirdPartyAuthConfig, ExtractUnknown) {
  base::DictionaryValue dict;
  dict.SetString("unknownName", "someValue");

  std::string url1, url2, cert;
  EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert));
}

TEST(ThirdPartyAuthConfig, ExtractAll) {
  base::DictionaryValue dict;
  dict.SetString(key::kRemoteAccessHostTokenUrl, "test1");
  dict.SetString(key::kRemoteAccessHostTokenValidationUrl, "test2");
  dict.SetString(key::kRemoteAccessHostTokenValidationCertificateIssuer, "t3");

  std::string url1, url2, cert;
  EXPECT_TRUE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert));
  EXPECT_EQ("test1", url1);
  EXPECT_EQ("test2", url2);
  EXPECT_EQ("t3", cert);
}

TEST(ThirdPartyAuthConfig, ExtractPartial) {
  base::DictionaryValue dict;
  dict.SetString(key::kRemoteAccessHostTokenValidationUrl, "test2");

  std::string url1, url2, cert;
  EXPECT_TRUE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert));
  EXPECT_EQ("", url1);
  EXPECT_EQ("test2", url2);
  EXPECT_EQ("", cert);
}

TEST(ThirdPartyAuthConfig, Output) {
  ThirdPartyAuthConfig third_party_auth_config;
  third_party_auth_config.token_url = GURL("https://token.com");
  third_party_auth_config.token_validation_url = GURL("https://validation.com");
  third_party_auth_config.token_validation_cert_issuer = "certificate subject";

  std::ostringstream str;
  str << third_party_auth_config;

  EXPECT_THAT(str.str(), testing::HasSubstr("token"));
  EXPECT_THAT(str.str(), testing::HasSubstr("validation"));
  EXPECT_THAT(str.str(), testing::HasSubstr("certificate subject"));
}

}  // namespace remoting