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 "components/nacl/browser/nacl_file_host.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/test/scoped_path_override.h"
11 #include "components/nacl/browser/nacl_browser.h"
12 #include "components/nacl/browser/nacl_browser_delegate.h"
13 #include "components/nacl/browser/test_nacl_browser_delegate.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using nacl_file_host::PnaclCanOpenFile
;
18 class FileHostTestNaClBrowserDelegate
: public TestNaClBrowserDelegate
{
20 FileHostTestNaClBrowserDelegate() {}
22 bool GetPnaclDirectory(base::FilePath
* pnacl_dir
) override
{
23 *pnacl_dir
= pnacl_path_
;
27 void SetPnaclDirectory(const base::FilePath
& pnacl_dir
) {
28 pnacl_path_
= pnacl_dir
;
32 base::FilePath pnacl_path_
;
35 class NaClFileHostTest
: public testing::Test
{
38 ~NaClFileHostTest() override
;
40 void SetUp() override
{
41 nacl_browser_delegate_
= new FileHostTestNaClBrowserDelegate
;
42 nacl::NaClBrowser::SetDelegate(nacl_browser_delegate_
);
45 void TearDown() override
{
46 // This deletes nacl_browser_delegate_.
47 nacl::NaClBrowser::SetDelegate(NULL
);
50 FileHostTestNaClBrowserDelegate
* nacl_browser_delegate() {
51 return nacl_browser_delegate_
;
55 FileHostTestNaClBrowserDelegate
* nacl_browser_delegate_
;
56 DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest
);
59 NaClFileHostTest::NaClFileHostTest() : nacl_browser_delegate_(NULL
) {}
61 NaClFileHostTest::~NaClFileHostTest() {
64 // Try to pass a few funny filenames with a dummy PNaCl directory set.
65 TEST_F(NaClFileHostTest
, TestFilenamesWithPnaclPath
) {
66 base::ScopedTempDir scoped_tmp_dir
;
67 ASSERT_TRUE(scoped_tmp_dir
.CreateUniqueTempDir());
69 base::FilePath kTestPnaclPath
= scoped_tmp_dir
.path();
71 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath
);
72 ASSERT_TRUE(nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(
75 // Check allowed strings, and check that the expected prefix is added.
76 base::FilePath out_path
;
77 EXPECT_TRUE(PnaclCanOpenFile("pnacl_json", &out_path
));
78 base::FilePath expected_path
= kTestPnaclPath
.Append(
79 FILE_PATH_LITERAL("pnacl_public_pnacl_json"));
80 EXPECT_EQ(expected_path
, out_path
);
82 EXPECT_TRUE(PnaclCanOpenFile("x86_32_llc", &out_path
));
83 expected_path
= kTestPnaclPath
.Append(
84 FILE_PATH_LITERAL("pnacl_public_x86_32_llc"));
85 EXPECT_EQ(expected_path
, out_path
);
87 // Check character ranges.
88 EXPECT_FALSE(PnaclCanOpenFile(".xchars", &out_path
));
89 EXPECT_FALSE(PnaclCanOpenFile("/xchars", &out_path
));
90 EXPECT_FALSE(PnaclCanOpenFile("x/chars", &out_path
));
91 EXPECT_FALSE(PnaclCanOpenFile("\\xchars", &out_path
));
92 EXPECT_FALSE(PnaclCanOpenFile("x\\chars", &out_path
));
93 EXPECT_FALSE(PnaclCanOpenFile("$xchars", &out_path
));
94 EXPECT_FALSE(PnaclCanOpenFile("%xchars", &out_path
));
95 EXPECT_FALSE(PnaclCanOpenFile("CAPS", &out_path
));
96 const char non_ascii
[] = "\xff\xfe";
97 EXPECT_FALSE(PnaclCanOpenFile(non_ascii
, &out_path
));
99 // Check file length restriction.
100 EXPECT_FALSE(PnaclCanOpenFile("thisstringisactuallywaaaaaaaaaaaaaaaaaaaaaaaa"
101 "toolongwaytoolongwaaaaayyyyytoooooooooooooooo"
106 EXPECT_FALSE(PnaclCanOpenFile(std::string(), &out_path
));
107 EXPECT_FALSE(PnaclCanOpenFile(".", &out_path
));
108 EXPECT_FALSE(PnaclCanOpenFile("..", &out_path
));
110 EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path
));
111 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path
));
112 EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path
));
114 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path
));
115 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path
));
116 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path
));
117 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path
));