Chromium Code Reviews| 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/base/oauth_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 std::string Replace(const std::string& s, const std::string& old_substr, | 13 std::string Replace(const std::string& s, |
|
nicholss
2017/01/31 16:44:58
git cl format did these changes.
| |
| 14 const std::string& old_substr, | |
| 14 const std::string& new_substr) { | 15 const std::string& new_substr) { |
| 15 size_t pos = s.find(old_substr); | 16 size_t pos = s.find(old_substr); |
| 16 if (pos == std::string::npos) { | 17 if (pos == std::string::npos) { |
| 17 return s; | 18 return s; |
| 18 } | 19 } |
| 19 return s.substr(0, pos) + new_substr + | 20 return s.substr(0, pos) + new_substr + |
| 20 s.substr(pos + old_substr.length(), std::string::npos); | 21 s.substr(pos + old_substr.length(), std::string::npos); |
| 21 } | 22 } |
| 22 | 23 |
| 23 std::string GetTestRedirectUrl() { | 24 std::string GetTestRedirectUrl() { |
| 24 return std::string("https://google.com/redirect"); | 25 return std::string("https://google.com/redirect"); |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace remoting { | 30 namespace remoting { |
| 30 | 31 |
| 31 TEST(OauthHelperTest, TestNotCode) { | 32 TEST(OauthHelperTest, TestNotCode) { |
| 32 ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl())); | 33 ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl())); |
| 33 } | 34 } |
| 34 | 35 |
| 35 TEST(OauthHelperTest, TestVeryShort) { | 36 TEST(OauthHelperTest, TestVeryShort) { |
| 36 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl())); | 37 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl())); |
| 37 } | 38 } |
| 38 | 39 |
| 39 TEST(OauthHelperTest, TestEmptyQuery) { | 40 TEST(OauthHelperTest, TestEmptyQuery) { |
| 40 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?", | 41 ASSERT_EQ( |
| 41 GetTestRedirectUrl())); | 42 "", GetOauthCodeInUrl(GetTestRedirectUrl() + "?", GetTestRedirectUrl())); |
| 42 } | 43 } |
| 43 | 44 |
| 44 TEST(OauthHelperTest, TestNoQueryValue) { | 45 TEST(OauthHelperTest, TestNoQueryValue) { |
| 45 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code", | 46 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code", |
| 46 GetTestRedirectUrl())); | 47 GetTestRedirectUrl())); |
| 47 } | 48 } |
| 48 | 49 |
| 49 TEST(OauthHelperTest, TestEmptyCode) { | 50 TEST(OauthHelperTest, TestEmptyCode) { |
| 50 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=", | 51 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=", |
| 51 GetTestRedirectUrl())); | 52 GetTestRedirectUrl())); |
| 52 } | 53 } |
| 53 | 54 |
| 54 TEST(OauthHelperTest, TestCode) { | 55 TEST(OauthHelperTest, TestCode) { |
| 55 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy", | 56 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy", |
| 56 GetTestRedirectUrl())); | 57 GetTestRedirectUrl())); |
| 57 } | 58 } |
| 58 | 59 |
| 59 TEST(OauthHelperTest, TestCodeInLongQuery) { | 60 TEST(OauthHelperTest, TestCodeInLongQuery) { |
| 60 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + | 61 ASSERT_EQ("Dummy", |
| 61 "?x=1&code=Dummy&y=2", | 62 GetOauthCodeInUrl(GetTestRedirectUrl() + "?x=1&code=Dummy&y=2", |
| 62 GetTestRedirectUrl())); | 63 GetTestRedirectUrl())); |
| 63 } | 64 } |
| 64 | 65 |
| 65 TEST(OauthHelperTest, TestBadScheme) { | 66 TEST(OauthHelperTest, TestBadScheme) { |
| 66 std::string url = GetTestRedirectUrl() + "?code=Dummy"; | 67 std::string url = GetTestRedirectUrl() + "?code=Dummy"; |
| 67 url = Replace(url, "https:", "http"); | 68 url = Replace(url, "https:", "http"); |
| 68 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); | 69 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); |
| 69 } | 70 } |
| 70 | 71 |
| 71 TEST(OauthHelperTest, TestBadHost) { | 72 TEST(OauthHelperTest, TestBadHost) { |
| 72 std::string url = GetTestRedirectUrl() + "?code=Dummy"; | 73 std::string url = GetTestRedirectUrl() + "?code=Dummy"; |
| 73 url = Replace(url, "google", "goggle"); | 74 url = Replace(url, "google", "goggle"); |
| 74 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); | 75 ASSERT_EQ("", GetOauthCodeInUrl(url, GetTestRedirectUrl())); |
| 75 } | 76 } |
| 76 | 77 |
| 77 } // namespace remoting | 78 } // namespace remoting |
| OLD | NEW |