| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/setup/oauth_helper.h" | 5 #include "remoting/host/setup/oauth_helper.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 std::string Replace(const std::string& s, const std::string& old_substr, | 11 std::string Replace(const std::string& s, const std::string& old_substr, |
| 12 const std::string& new_substr) { | 12 const std::string& new_substr) { |
| 13 size_t pos = s.find(old_substr); | 13 size_t pos = s.find(old_substr); |
| 14 if (pos == std::string::npos) { | 14 if (pos == std::string::npos) { |
| 15 return s; | 15 return s; |
| 16 } | 16 } |
| 17 return s.substr(0, pos) + new_substr + | 17 return s.substr(0, pos) + new_substr + |
| 18 s.substr(pos + old_substr.length(), std::string::npos); | 18 s.substr(pos + old_substr.length(), std::string::npos); |
| 19 } | 19 } |
| 20 | 20 |
| 21 std::string GetTestRedirectUrl() { |
| 22 return std::string("https://google.com/redirect"); |
| 23 } |
| 24 |
| 21 } // namespace | 25 } // namespace |
| 22 | 26 |
| 23 namespace remoting { | 27 namespace remoting { |
| 24 | 28 |
| 25 TEST(OauthHelperTest, TestNotCode) { | 29 TEST(OauthHelperTest, TestNotCode) { |
| 26 ASSERT_EQ("", GetOauthCodeInUrl("notURL")); | 30 ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl())); |
| 27 } | 31 } |
| 28 | 32 |
| 29 TEST(OauthHelperTest, TestVeryShort) { | 33 TEST(OauthHelperTest, TestVeryShort) { |
| 30 ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl())); | 34 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl())); |
| 31 } | 35 } |
| 32 | 36 |
| 33 TEST(OauthHelperTest, TestEmptyQuery) { | 37 TEST(OauthHelperTest, TestEmptyQuery) { |
| 34 ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?")); | 38 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?", |
| 39 GetTestRedirectUrl())); |
| 35 } | 40 } |
| 36 | 41 |
| 37 TEST(OauthHelperTest, TestNoQueryValue) { | 42 TEST(OauthHelperTest, TestNoQueryValue) { |
| 38 ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code")); | 43 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code", |
| 44 GetTestRedirectUrl())); |
| 39 } | 45 } |
| 40 | 46 |
| 41 TEST(OauthHelperTest, TestEmptyCode) { | 47 TEST(OauthHelperTest, TestEmptyCode) { |
| 42 ASSERT_EQ("", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code=")); | 48 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=", |
| 49 GetTestRedirectUrl())); |
| 43 } | 50 } |
| 44 | 51 |
| 45 TEST(OauthHelperTest, TestCode) { | 52 TEST(OauthHelperTest, TestCode) { |
| 46 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetOauthRedirectUrl() + "?code=Dummy")); | 53 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy", |
| 54 GetTestRedirectUrl())); |
| 47 } | 55 } |
| 48 | 56 |
| 49 TEST(OauthHelperTest, TestCodeInLongQuery) { | 57 TEST(OauthHelperTest, TestCodeInLongQuery) { |
| 50 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetOauthRedirectUrl() + | 58 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + |
| 51 "?x=1&code=Dummy&y=2")); | 59 "?x=1&code=Dummy&y=2", |
| 60 GetTestRedirectUrl())); |
| 52 } | 61 } |
| 53 | 62 |
| 54 TEST(OauthHelperTest, TestBadScheme) { | 63 TEST(OauthHelperTest, TestBadScheme) { |
| 55 std::string url = GetOauthRedirectUrl() + "?code=Dummy"; | 64 std::string url = GetTestRedirectUrl() + "?code=Dummy"; |
| 56 url = Replace(url, "https:", "http"); | 65 url = Replace(url, "https:", "http"); |
| 57 ASSERT_EQ("", GetOauthCodeInUrl(url)); | 66 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); |
| 58 } | 67 } |
| 59 | 68 |
| 60 TEST(OauthHelperTest, TestBadHost) { | 69 TEST(OauthHelperTest, TestBadHost) { |
| 61 std::string url = GetOauthRedirectUrl() + "?code=Dummy"; | 70 std::string url = GetTestRedirectUrl() + "?code=Dummy"; |
| 62 url = Replace(url, "google", "goggle"); | 71 url = Replace(url, "google", "goggle"); |
| 63 ASSERT_EQ("", GetOauthCodeInUrl(url)); | 72 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); |
| 64 } | 73 } |
| 65 | 74 |
| 66 } // namespace remoting | 75 } // namespace remoting |
| OLD | NEW |