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