Home | History | Annotate | Download | only in setup
      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 
     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 
     21 std::string GetTestRedirectUrl() {
     22   return std::string("https://google.com/redirect");
     23 }
     24 
     25 }  // namespace
     26 
     27 namespace remoting {
     28 
     29 TEST(OauthHelperTest, TestNotCode) {
     30   ASSERT_EQ("", GetOauthCodeInUrl("notURL", GetTestRedirectUrl()));
     31 }
     32 
     33 TEST(OauthHelperTest, TestVeryShort) {
     34   ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl(), GetTestRedirectUrl()));
     35 }
     36 
     37 TEST(OauthHelperTest, TestEmptyQuery) {
     38   ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?",
     39                                   GetTestRedirectUrl()));
     40 }
     41 
     42 TEST(OauthHelperTest, TestNoQueryValue) {
     43   ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code",
     44                                   GetTestRedirectUrl()));
     45 }
     46 
     47 TEST(OauthHelperTest, TestEmptyCode) {
     48   ASSERT_EQ("", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=",
     49                                   GetTestRedirectUrl()));
     50 }
     51 
     52 TEST(OauthHelperTest, TestCode) {
     53   ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() + "?code=Dummy",
     54                                        GetTestRedirectUrl()));
     55 }
     56 
     57 TEST(OauthHelperTest, TestCodeInLongQuery) {
     58   ASSERT_EQ("Dummy", GetOauthCodeInUrl(GetTestRedirectUrl() +
     59                                            "?x=1&code=Dummy&y=2",
     60                                        GetTestRedirectUrl()));
     61 }
     62 
     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 
     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