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.
5 #include "remoting/host/setup/oauth_helper.h"
7 #include "testing/gtest/include/gtest/gtest.h"
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
) {
17 return s
.substr(0, pos
) + new_substr
+
18 s
.substr(pos
+ old_substr
.length(), std::string::npos
);
21 std::string
GetTestRedirectUrl() {
22 return std::string("https://google.com/redirect");
29 TEST(OauthHelperTest
, TestNotCode
) {
30 ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl()));
33 TEST(OauthHelperTest
, TestVeryShort
) {
34 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl()));
37 TEST(OauthHelperTest
, TestEmptyQuery
) {
38 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?",
39 GetTestRedirectUrl()));
42 TEST(OauthHelperTest
, TestNoQueryValue
) {
43 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code",
44 GetTestRedirectUrl()));
47 TEST(OauthHelperTest
, TestEmptyCode
) {
48 ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=",
49 GetTestRedirectUrl()));
52 TEST(OauthHelperTest
, TestCode
) {
53 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy",
54 GetTestRedirectUrl()));
57 TEST(OauthHelperTest
, TestCodeInLongQuery
) {
58 ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() +
59 "?x=1&code=Dummy&y=2",
60 GetTestRedirectUrl()));
63 TEST(OauthHelperTest
, TestBadScheme
) {
64 std::string url
= GetTestRedirectUrl() + "?code=Dummy";
65 url
= Replace(url
, "https:", "http");
66 ASSERT_EQ("", GetOauthCodeInUrl(url
, GetTestRedirectUrl()));
69 TEST(OauthHelperTest
, TestBadHost
) {
70 std::string url
= GetTestRedirectUrl() + "?code=Dummy";
71 url
= Replace(url
, "google", "goggle");
72 ASSERT_EQ("", GetOauthCodeInUrl(url
, GetTestRedirectUrl()));
75 } // namespace remoting