OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #import "ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.h" |
| 6 |
| 7 #include "base/ios/ios_util.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/gtest_mac.h" |
| 13 #include "testing/platform_test.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 |
| 17 using namespace ios_internal::nsurlprotectionspace_util; |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Test hostnames and URL origins. |
| 22 NSString* const kTestHost = @"chromium.org"; |
| 23 NSString* const kTestHttpOrigin = @"http://chromium.org"; |
| 24 NSString* const kTestHttpsOrigin = @"https://chromium.org:80"; |
| 25 |
| 26 // Returns protection space for the given |host|, |protocol| and |port|. |
| 27 NSURLProtectionSpace* GetProtectionSpaceForHost(NSString* host, |
| 28 NSString* protocol, |
| 29 NSInteger port) { |
| 30 return [[[NSURLProtectionSpace alloc] initWithHost:host |
| 31 port:port |
| 32 protocol:protocol |
| 33 realm:nil |
| 34 authenticationMethod:nil] autorelease]; |
| 35 } |
| 36 |
| 37 // Returns protection space for the given |host| and |protocol| and port 80. |
| 38 NSURLProtectionSpace* GetProtectionSpaceForHost(NSString* host, |
| 39 NSString* protocol) { |
| 40 return GetProtectionSpaceForHost(host, protocol, 80); |
| 41 } |
| 42 |
| 43 // Returns protection space for the given proxy |host| and |protocol|. |
| 44 NSURLProtectionSpace* GetProtectionSpaceForProxyHost(NSString* host, |
| 45 NSString* type) { |
| 46 return [[[NSURLProtectionSpace alloc] initWithProxyHost:host |
| 47 port:80 |
| 48 type:type |
| 49 realm:nil |
| 50 authenticationMethod:nil] autorelease]; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 // Tests that dialog can not be shown without valid host. |
| 56 TEST(NSURLProtectionSpaceUtilTest, CantShowWithoutValidHost) { |
| 57 NSURLProtectionSpace* protectionSpace = |
| 58 GetProtectionSpaceForHost(@"", NSURLProtectionSpaceHTTPS); |
| 59 |
| 60 EXPECT_FALSE(CanShow(protectionSpace)); |
| 61 } |
| 62 |
| 63 // Tests that dialog can not be shown with invalid port. |
| 64 TEST(NSURLProtectionSpaceUtilTest, CantShowWithoutValidPort) { |
| 65 NSURLProtectionSpace* protectionSpace = |
| 66 GetProtectionSpaceForHost(kTestHost, NSURLProtectionSpaceHTTPS, INT_MAX); |
| 67 |
| 68 EXPECT_FALSE(CanShow(protectionSpace)); |
| 69 } |
| 70 |
| 71 // Tests showing the dialog for SOCKS proxy server. |
| 72 TEST(NSURLProtectionSpaceUtilTest, ShowForSocksProxy) { |
| 73 NSURLProtectionSpace* protectionSpace = |
| 74 GetProtectionSpaceForProxyHost(kTestHost, NSURLProtectionSpaceSOCKSProxy); |
| 75 |
| 76 ASSERT_TRUE(CanShow(protectionSpace)); |
| 77 |
| 78 // Expecting the following text: |
| 79 // The proxy chromium.org requires a username and password. |
| 80 // Your connection to this site is not private. |
| 81 NSString* expectedText = [NSString |
| 82 stringWithFormat:@"%@ %@", l10n_util::GetNSStringF( |
| 83 IDS_LOGIN_DIALOG_PROXY_AUTHORITY, |
| 84 base::SysNSStringToUTF16(kTestHost)), |
| 85 l10n_util::GetNSString( |
| 86 IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT)]; |
| 87 |
| 88 EXPECT_NSEQ(expectedText, MessageForHTTPAuth(protectionSpace)); |
| 89 } |
| 90 |
| 91 // Tests showing the dialog for http proxy server. |
| 92 TEST(NSURLProtectionSpaceUtilTest, ShowForHttpProxy) { |
| 93 NSURLProtectionSpace* protectionSpace = |
| 94 GetProtectionSpaceForProxyHost(kTestHost, NSURLProtectionSpaceHTTPProxy); |
| 95 |
| 96 ASSERT_TRUE(CanShow(protectionSpace)); |
| 97 |
| 98 // Expecting the following text: |
| 99 // The proxy http://chromium.org requires a username and password. |
| 100 // Your connection to this site is not private. |
| 101 NSString* expectedText = [NSString |
| 102 stringWithFormat:@"%@ %@", l10n_util::GetNSStringF( |
| 103 IDS_LOGIN_DIALOG_PROXY_AUTHORITY, |
| 104 base::SysNSStringToUTF16(kTestHttpOrigin)), |
| 105 l10n_util::GetNSString( |
| 106 IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT)]; |
| 107 EXPECT_NSEQ(expectedText, MessageForHTTPAuth(protectionSpace)); |
| 108 } |
| 109 |
| 110 // Tests showing the dialog for https proxy server. |
| 111 TEST(NSURLProtectionSpaceUtilTest, ShowForHttpsProxy) { |
| 112 NSURLProtectionSpace* protectionSpace = |
| 113 GetProtectionSpaceForProxyHost(kTestHost, NSURLProtectionSpaceHTTPSProxy); |
| 114 |
| 115 ASSERT_TRUE(CanShow(protectionSpace)); |
| 116 |
| 117 NSString* expectedText = nil; |
| 118 // On iOS 10, HTTPS Proxy protection space reports itself as unsecure |
| 119 // (crbug.com/629570). |
| 120 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 121 // Expecting the following text: |
| 122 // The proxy https://chromium.org requires a username and password. |
| 123 // Your connection to this site is not private. |
| 124 expectedText = [NSString |
| 125 stringWithFormat:@"%@ %@", |
| 126 l10n_util::GetNSStringF( |
| 127 IDS_LOGIN_DIALOG_PROXY_AUTHORITY, |
| 128 base::SysNSStringToUTF16(kTestHttpsOrigin)), |
| 129 l10n_util::GetNSString( |
| 130 IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT)]; |
| 131 } else { |
| 132 // Expecting the following text: |
| 133 // The proxy https://chromium.org:80 requires a username and password. |
| 134 expectedText = |
| 135 l10n_util::GetNSStringF(IDS_LOGIN_DIALOG_PROXY_AUTHORITY, |
| 136 base::SysNSStringToUTF16(kTestHttpsOrigin)); |
| 137 } |
| 138 EXPECT_NSEQ(expectedText, MessageForHTTPAuth(protectionSpace)); |
| 139 } |
| 140 |
| 141 // Tests showing the dialog for http server. |
| 142 TEST(NSURLProtectionSpaceUtilTest, ShowForHttpServer) { |
| 143 NSURLProtectionSpace* protectionSpace = |
| 144 GetProtectionSpaceForHost(kTestHost, NSURLProtectionSpaceHTTP); |
| 145 |
| 146 ASSERT_TRUE(CanShow(protectionSpace)); |
| 147 |
| 148 // Expecting the following text: |
| 149 // http://chromium.org requires a username and password. |
| 150 NSString* expectedText = [NSString |
| 151 stringWithFormat:@"%@ %@", l10n_util::GetNSStringF( |
| 152 IDS_LOGIN_DIALOG_AUTHORITY, |
| 153 base::SysNSStringToUTF16(kTestHttpOrigin)), |
| 154 l10n_util::GetNSString( |
| 155 IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT)]; |
| 156 EXPECT_NSEQ(expectedText, MessageForHTTPAuth(protectionSpace)); |
| 157 } |
| 158 |
| 159 // Tests showing the dialog for https server. |
| 160 TEST(NSURLProtectionSpaceUtilTest, ShowForHttpsServer) { |
| 161 NSURLProtectionSpace* protectionSpace = |
| 162 GetProtectionSpaceForHost(kTestHost, NSURLProtectionSpaceHTTPS); |
| 163 |
| 164 ASSERT_TRUE(CanShow(protectionSpace)); |
| 165 |
| 166 // Expecting the following text: |
| 167 // https://chromium.org:80 requires a username and password. |
| 168 NSString* expectedText = l10n_util::GetNSStringF( |
| 169 IDS_LOGIN_DIALOG_AUTHORITY, base::SysNSStringToUTF16(kTestHttpsOrigin)); |
| 170 EXPECT_NSEQ(expectedText, MessageForHTTPAuth(protectionSpace)); |
| 171 } |
OLD | NEW |