1 // Copyright 2015 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 "remoting/host/third_party_auth_config.h"
9 #include "base/values.h"
10 #include "policy/policy_constants.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 namespace key
= ::policy::key
;
18 TEST(ThirdPartyAuthConfig
, ParseEmpty
) {
19 ThirdPartyAuthConfig third_party_auth_config
;
22 ThirdPartyAuthConfig::ParseStrings("", "", "", &third_party_auth_config
));
23 EXPECT_TRUE(third_party_auth_config
.is_null());
26 TEST(ThirdPartyAuthConfig
, ParseValidAll
) {
27 ThirdPartyAuthConfig third_party_auth_config
;
29 EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
30 "https://token.com/", "https://validation.com/", "certificate subject",
31 &third_party_auth_config
));
32 EXPECT_FALSE(third_party_auth_config
.is_null());
33 EXPECT_EQ("https://token.com/", third_party_auth_config
.token_url
.spec());
34 EXPECT_EQ("https://validation.com/",
35 third_party_auth_config
.token_validation_url
.spec());
36 EXPECT_EQ("certificate subject",
37 third_party_auth_config
.token_validation_cert_issuer
);
40 TEST(ThirdPartyAuthConfig
, ParseValidNoCert
) {
41 ThirdPartyAuthConfig third_party_auth_config
;
43 EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings("https://token.com/",
44 "https://validation.com/", "",
45 &third_party_auth_config
));
46 EXPECT_FALSE(third_party_auth_config
.is_null());
47 EXPECT_EQ("https://token.com/", third_party_auth_config
.token_url
.spec());
48 EXPECT_EQ("https://validation.com/",
49 third_party_auth_config
.token_validation_url
.spec());
50 EXPECT_EQ("", third_party_auth_config
.token_validation_cert_issuer
);
53 // We validate https-vs-http only on Release builds to help with manual testing.
55 TEST(ThirdPartyAuthConfig
, ParseHttp
) {
56 ThirdPartyAuthConfig third_party_auth_config
;
58 EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings("http://token.com/",
59 "http://validation.com/", "",
60 &third_party_auth_config
));
61 EXPECT_FALSE(third_party_auth_config
.is_null());
62 EXPECT_EQ("http://token.com/", third_party_auth_config
.token_url
.spec());
63 EXPECT_EQ("http://validation.com/",
64 third_party_auth_config
.token_validation_url
.spec());
70 const std::string
valid_url("https://valid.com");
71 const std::string
valid_cert("certificate subject");
75 class InvalidUrlTest
: public ::testing::TestWithParam
<const char*> {};
77 TEST_P(InvalidUrlTest
, ParseInvalidUrl
) {
78 const std::string
& invalid_url(GetParam());
80 // Populate |config| with some known data - we will use this for validating
81 // that |config| doesn't get modified by ParseStrings during subsequent
84 ThirdPartyAuthConfig config
;
85 EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
86 "https://token.com/", "https://validation.com/", "certificate subject",
89 // Test for parsing failure.
90 EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings(invalid_url
, valid_url
,
91 valid_cert
, &config
));
92 EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings(valid_url
, invalid_url
,
93 valid_cert
, &config
));
95 ThirdPartyAuthConfig::ParseStrings(invalid_url
, "", "", &config
));
97 ThirdPartyAuthConfig::ParseStrings("", invalid_url
, "", &config
));
99 // Validate that ParseStrings doesn't modify its output upon parsing failure.
100 EXPECT_EQ("https://token.com/", config
.token_url
.spec());
101 EXPECT_EQ("https://validation.com/", config
.token_validation_url
.spec());
102 EXPECT_EQ("certificate subject", config
.token_validation_cert_issuer
);
105 // We validate https-vs-http only on Release builds to help with manual testing.
107 INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig
,
109 ::testing::Values("http://insecure.com",
112 INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig
,
114 ::testing::Values("I am not a URL"));
117 TEST(ThirdPartyAuthConfig
, ParseInvalidCombination
) {
118 // Populate |config| with some known data - we will use this for validating
119 // that |config| doesn't get modified by ParseStrings during subsequent
122 ThirdPartyAuthConfig config
;
123 EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings(
124 "https://token.com/", "https://validation.com/", "certificate subject",
127 // Only one of TokenUrl and TokenValidationUrl
129 ThirdPartyAuthConfig::ParseStrings("", valid_url
, valid_cert
, &config
));
131 ThirdPartyAuthConfig::ParseStrings(valid_url
, "", valid_cert
, &config
));
133 // CertSubject when no TokenUrl and TokenValidationUrl
134 EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings("", "", valid_cert
, &config
));
136 // Validate that ParseStrings doesn't modify its output upon parsing failure.
137 EXPECT_EQ("https://token.com/", config
.token_url
.spec());
138 EXPECT_EQ("https://validation.com/", config
.token_validation_url
.spec());
139 EXPECT_EQ("certificate subject", config
.token_validation_cert_issuer
);
142 TEST(ThirdPartyAuthConfig
, ExtractEmpty
) {
143 base::DictionaryValue dict
;
144 std::string url1
, url2
, cert
;
145 EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict
, &url1
, &url2
, &cert
));
148 TEST(ThirdPartyAuthConfig
, ExtractUnknown
) {
149 base::DictionaryValue dict
;
150 dict
.SetString("unknownName", "someValue");
152 std::string url1
, url2
, cert
;
153 EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict
, &url1
, &url2
, &cert
));
156 TEST(ThirdPartyAuthConfig
, ExtractAll
) {
157 base::DictionaryValue dict
;
158 dict
.SetString(key::kRemoteAccessHostTokenUrl
, "test1");
159 dict
.SetString(key::kRemoteAccessHostTokenValidationUrl
, "test2");
160 dict
.SetString(key::kRemoteAccessHostTokenValidationCertificateIssuer
, "t3");
162 std::string url1
, url2
, cert
;
163 EXPECT_TRUE(ThirdPartyAuthConfig::ExtractStrings(dict
, &url1
, &url2
, &cert
));
164 EXPECT_EQ("test1", url1
);
165 EXPECT_EQ("test2", url2
);
166 EXPECT_EQ("t3", cert
);
169 TEST(ThirdPartyAuthConfig
, ExtractPartial
) {
170 base::DictionaryValue dict
;
171 dict
.SetString(key::kRemoteAccessHostTokenValidationUrl
, "test2");
173 std::string url1
, url2
, cert
;
174 EXPECT_TRUE(ThirdPartyAuthConfig::ExtractStrings(dict
, &url1
, &url2
, &cert
));
176 EXPECT_EQ("test2", url2
);
180 TEST(ThirdPartyAuthConfig
, Output
) {
181 ThirdPartyAuthConfig third_party_auth_config
;
182 third_party_auth_config
.token_url
= GURL("https://token.com");
183 third_party_auth_config
.token_validation_url
= GURL("https://validation.com");
184 third_party_auth_config
.token_validation_cert_issuer
= "certificate subject";
186 std::ostringstream str
;
187 str
<< third_party_auth_config
;
189 EXPECT_THAT(str
.str(), testing::HasSubstr("token"));
190 EXPECT_THAT(str
.str(), testing::HasSubstr("validation"));
191 EXPECT_THAT(str
.str(), testing::HasSubstr("certificate subject"));
194 } // namespace remoting