lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "remoting/host/third_party_auth_config.h" |
| 6 | |
| 7 | #include <sstream> |
| 8 | |
| 9 | #include "base/values.h" |
brettw | 39d6ba4 | 2016-08-24 16:56:38 | [diff] [blame^] | 10 | #include "components/policy/policy_constants.h" |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 11 | #include "testing/gmock/include/gmock/gmock.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace remoting { |
| 15 | |
| 16 | namespace key = ::policy::key; |
| 17 | |
| 18 | TEST(ThirdPartyAuthConfig, ParseEmpty) { |
| 19 | ThirdPartyAuthConfig third_party_auth_config; |
| 20 | |
| 21 | EXPECT_TRUE( |
| 22 | ThirdPartyAuthConfig::ParseStrings("", "", "", &third_party_auth_config)); |
| 23 | EXPECT_TRUE(third_party_auth_config.is_null()); |
| 24 | } |
| 25 | |
| 26 | TEST(ThirdPartyAuthConfig, ParseValidAll) { |
| 27 | ThirdPartyAuthConfig third_party_auth_config; |
| 28 | |
| 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); |
| 38 | } |
| 39 | |
| 40 | TEST(ThirdPartyAuthConfig, ParseValidNoCert) { |
| 41 | ThirdPartyAuthConfig third_party_auth_config; |
| 42 | |
| 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); |
| 51 | } |
| 52 | |
| 53 | // We validate https-vs-http only on Release builds to help with manual testing. |
| 54 | #if !defined(NDEBUG) |
| 55 | TEST(ThirdPartyAuthConfig, ParseHttp) { |
| 56 | ThirdPartyAuthConfig third_party_auth_config; |
| 57 | |
| 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()); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | namespace { |
| 69 | |
| 70 | const std::string valid_url("https://valid.com"); |
| 71 | const std::string valid_cert("certificate subject"); |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | class InvalidUrlTest : public ::testing::TestWithParam<const char*> {}; |
| 76 | |
| 77 | TEST_P(InvalidUrlTest, ParseInvalidUrl) { |
| 78 | const std::string& invalid_url(GetParam()); |
| 79 | |
| 80 | // Populate |config| with some known data - we will use this for validating |
| 81 | // that |config| doesn't get modified by ParseStrings during subsequent |
| 82 | // parsing |
| 83 | // failure tests. |
| 84 | ThirdPartyAuthConfig config; |
| 85 | EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings( |
| 86 | "https://token.com/", "https://validation.com/", "certificate subject", |
| 87 | &config)); |
| 88 | |
| 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)); |
| 94 | EXPECT_FALSE( |
| 95 | ThirdPartyAuthConfig::ParseStrings(invalid_url, "", "", &config)); |
| 96 | EXPECT_FALSE( |
| 97 | ThirdPartyAuthConfig::ParseStrings("", invalid_url, "", &config)); |
| 98 | |
| 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); |
| 103 | } |
| 104 | |
| 105 | // We validate https-vs-http only on Release builds to help with manual testing. |
| 106 | #if defined(NDEBUG) |
| 107 | INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig, |
| 108 | InvalidUrlTest, |
| 109 | ::testing::Values("http://insecure.com", |
| 110 | "I am not a URL")); |
| 111 | #else |
| 112 | INSTANTIATE_TEST_CASE_P(ThirdPartyAuthConfig, |
| 113 | InvalidUrlTest, |
| 114 | ::testing::Values("I am not a URL")); |
| 115 | #endif |
| 116 | |
| 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 |
| 120 | // parsing |
| 121 | // failure tests. |
| 122 | ThirdPartyAuthConfig config; |
| 123 | EXPECT_TRUE(ThirdPartyAuthConfig::ParseStrings( |
| 124 | "https://token.com/", "https://validation.com/", "certificate subject", |
| 125 | &config)); |
| 126 | |
| 127 | // Only one of TokenUrl and TokenValidationUrl |
| 128 | EXPECT_FALSE( |
| 129 | ThirdPartyAuthConfig::ParseStrings("", valid_url, valid_cert, &config)); |
| 130 | EXPECT_FALSE( |
| 131 | ThirdPartyAuthConfig::ParseStrings(valid_url, "", valid_cert, &config)); |
| 132 | |
| 133 | // CertSubject when no TokenUrl and TokenValidationUrl |
| 134 | EXPECT_FALSE(ThirdPartyAuthConfig::ParseStrings("", "", valid_cert, &config)); |
| 135 | |
| 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); |
| 140 | } |
| 141 | |
| 142 | TEST(ThirdPartyAuthConfig, ExtractEmpty) { |
| 143 | base::DictionaryValue dict; |
| 144 | std::string url1, url2, cert; |
| 145 | EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert)); |
| 146 | } |
| 147 | |
| 148 | TEST(ThirdPartyAuthConfig, ExtractUnknown) { |
| 149 | base::DictionaryValue dict; |
| 150 | dict.SetString("unknownName", "someValue"); |
| 151 | |
| 152 | std::string url1, url2, cert; |
| 153 | EXPECT_FALSE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert)); |
| 154 | } |
| 155 | |
| 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"); |
| 161 | |
| 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); |
| 167 | } |
| 168 | |
| 169 | TEST(ThirdPartyAuthConfig, ExtractPartial) { |
| 170 | base::DictionaryValue dict; |
| 171 | dict.SetString(key::kRemoteAccessHostTokenValidationUrl, "test2"); |
| 172 | |
| 173 | std::string url1, url2, cert; |
| 174 | EXPECT_TRUE(ThirdPartyAuthConfig::ExtractStrings(dict, &url1, &url2, &cert)); |
| 175 | EXPECT_EQ("", url1); |
| 176 | EXPECT_EQ("test2", url2); |
| 177 | EXPECT_EQ("", cert); |
| 178 | } |
| 179 | |
| 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"; |
| 185 | |
| 186 | std::ostringstream str; |
| 187 | str << third_party_auth_config; |
| 188 | |
| 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")); |
| 192 | } |
| 193 | |
| 194 | } // namespace remoting |