4
4
5
5
#include " brave/components/brave_webtorrent/browser/net/brave_torrent_redirect_network_delegate_helper.h"
6
6
7
+ #include " base/files/file_path.h"
8
+ #include " base/files/scoped_temp_dir.h"
7
9
#include " base/strings/strcat.h"
8
10
#include " brave/browser/net/url_context.h"
11
+ #include " brave/browser/profiles/brave_profile_manager.h"
12
+ #include " brave/browser/profiles/tor_unittest_profile_manager.h"
13
+ #include " brave/browser/renderer_host/brave_navigation_ui_data.h"
14
+ #include " brave/browser/tor/mock_tor_profile_service_factory.h"
9
15
#include " brave/common/network_constants.h"
16
+ #include " chrome/test/base/scoped_testing_local_state.h"
17
+ #include " chrome/test/base/testing_browser_process.h"
18
+ #include " content/public/browser/resource_request_info.h"
19
+ #include " content/public/test/mock_resource_context.h"
10
20
#include " content/public/test/test_browser_thread_bundle.h"
11
21
#include " net/traffic_annotation/network_traffic_annotation_test_helper.h"
12
22
#include " net/url_request/url_request_test_util.h"
13
23
#include " testing/gtest/include/gtest/gtest.h"
14
24
15
25
namespace {
16
26
27
+ int kRenderProcessId = 1 ;
28
+ int kRenderFrameId = 2 ;
29
+
30
+ } // namespace
31
+
17
32
class BraveTorrentRedirectNetworkDelegateHelperTest : public testing ::Test {
18
33
public:
19
34
BraveTorrentRedirectNetworkDelegateHelperTest ()
20
- : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
35
+ : local_state_(TestingBrowserProcess::GetGlobal()),
36
+ thread_bundle_ (content::TestBrowserThreadBundle::IO_MAINLOOP),
21
37
context_(new net::TestURLRequestContext(true )) {
22
38
}
23
39
24
40
~BraveTorrentRedirectNetworkDelegateHelperTest () override {}
25
41
26
42
void SetUp () override {
43
+ // Create a new temporary directory, and store the path
44
+ ASSERT_TRUE (temp_dir_.CreateUniqueTempDir ());
45
+ TestingBrowserProcess::GetGlobal ()->SetProfileManager (
46
+ new TorUnittestProfileManager (temp_dir_.GetPath ()));
27
47
context_->Init ();
48
+
28
49
torrent_url_ = GURL (" https://webtorrent.io/torrents/sintel.torrent" );
29
50
non_torrent_url_ = GURL (" https://webtorrent.io/torrents/sintel" );
30
51
extension_url_ = GURL (" chrome-extension://lgjmpdmojkpocjcopdikifhejkkjglho/extension/brave_webtorrent.html?https://webtorrent.io/torrents/sintel.torrent" );
31
52
non_torrent_extension_url_ = GURL (" chrome-extension://lgjmpdmojkpocjcopdikifhejkkjglho/extension/brave_webtorrent.html?https://webtorrent.io/torrents/sintel" );
32
53
}
33
54
55
+ void TearDown () override {
56
+ TestingBrowserProcess::GetGlobal ()->SetProfileManager (nullptr );
57
+ }
58
+
34
59
net::TestURLRequestContext* context () { return context_.get (); }
35
60
61
+ content::MockResourceContext* resource_context () {
62
+ return resource_context_.get ();
63
+ }
64
+
36
65
const GURL& torrent_url () {
37
66
return torrent_url_;
38
67
}
@@ -49,13 +78,19 @@ class BraveTorrentRedirectNetworkDelegateHelperTest: public testing::Test {
49
78
return non_torrent_extension_url_;
50
79
}
51
80
81
+ protected:
82
+ // The path to temporary directory used to contain the test operations.
83
+ base::ScopedTempDir temp_dir_;
84
+ ScopedTestingLocalState local_state_;
85
+
52
86
private:
53
87
GURL torrent_url_;
54
88
GURL non_torrent_url_;
55
89
GURL extension_url_;
56
90
GURL non_torrent_extension_url_;
57
91
content::TestBrowserThreadBundle thread_bundle_;
58
92
std::unique_ptr<net::TestURLRequestContext> context_;
93
+ std::unique_ptr<content::MockResourceContext> resource_context_;
59
94
};
60
95
61
96
TEST_F (BraveTorrentRedirectNetworkDelegateHelperTest, NoRedirectWithoutMimeType) {
@@ -283,4 +318,52 @@ TEST_F(BraveTorrentRedirectNetworkDelegateHelperTest, WebtorrentInitiatedNoRedir
283
318
EXPECT_EQ (ret, net::OK);
284
319
}
285
320
286
- } // namespace
321
+ TEST_F (BraveTorrentRedirectNetworkDelegateHelperTest, NoRedirectTorProfile) {
322
+ ProfileManager* profile_manager = g_browser_process->profile_manager ();
323
+ base::FilePath tor_path = BraveProfileManager::GetTorProfilePath ();
324
+ Profile* profile = profile_manager->GetProfile (tor_path);
325
+ ASSERT_TRUE (profile);
326
+
327
+ net::TestDelegate test_delegate;
328
+ std::unique_ptr<net::URLRequest> request =
329
+ context ()->CreateRequest (torrent_url (), net::IDLE, &test_delegate,
330
+ TRAFFIC_ANNOTATION_FOR_TESTS);
331
+
332
+ scoped_refptr<net::HttpResponseHeaders> orig_response_headers =
333
+ new net::HttpResponseHeaders (std::string ());
334
+ orig_response_headers->AddHeader (
335
+ base::StrCat ({" Content-Type: " , kBittorrentMimeType }));
336
+ std::string mimeType;
337
+ ASSERT_TRUE (orig_response_headers->GetMimeType (&mimeType));
338
+ ASSERT_EQ (mimeType, kBittorrentMimeType );
339
+
340
+ scoped_refptr<net::HttpResponseHeaders> overwrite_response_headers =
341
+ new net::HttpResponseHeaders (std::string ());
342
+ GURL allowed_unsafe_redirect_url = GURL::EmptyGURL ();
343
+ std::shared_ptr<brave::BraveRequestInfo>
344
+ brave_request_info (new brave::BraveRequestInfo ());
345
+ brave::ResponseCallback callback;
346
+
347
+ std::unique_ptr<BraveNavigationUIData> navigation_ui_data =
348
+ std::make_unique<BraveNavigationUIData>();
349
+ BraveNavigationUIData* navigation_ui_data_ptr = navigation_ui_data.get ();
350
+
351
+ content::ResourceRequestInfo::AllocateForTesting (
352
+ request.get (), content::RESOURCE_TYPE_MAIN_FRAME, resource_context (),
353
+ kRenderProcessId , /* render_view_id=*/ -1 , kRenderFrameId ,
354
+ /* is_main_frame=*/ true , /* allow_download=*/ false , /* is_async=*/ true ,
355
+ content::PREVIEWS_OFF, std::move (navigation_ui_data));
356
+
357
+ MockTorProfileServiceFactory::SetTorNavigationUIData (profile,
358
+ navigation_ui_data_ptr);
359
+
360
+ int ret = webtorrent::OnHeadersReceived_TorrentRedirectWork (request.get (),
361
+ orig_response_headers.get (), &overwrite_response_headers,
362
+ &allowed_unsafe_redirect_url, callback, brave_request_info);
363
+
364
+ EXPECT_EQ (overwrite_response_headers->GetStatusLine (), " HTTP/1.0 200 OK" );
365
+ std::string location;
366
+ EXPECT_FALSE (overwrite_response_headers->EnumerateHeader (nullptr , " Location" , &location));
367
+ EXPECT_EQ (allowed_unsafe_redirect_url, GURL::EmptyGURL ());
368
+ EXPECT_EQ (ret, net::OK);
369
+ }
0 commit comments