1 // Copyright 2012 The Chromium Authors
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 "components/nacl/browser/nacl_file_host.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/memory/raw_ptr.h"
11 #include "base/test/scoped_path_override.h"
12 #include "build/build_config.h"
13 #include "components/nacl/browser/nacl_browser.h"
14 #include "components/nacl/browser/nacl_browser_delegate.h"
15 #include "components/nacl/browser/test_nacl_browser_delegate.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using nacl_file_host::PnaclCanOpenFile;
19
20 class FileHostTestNaClBrowserDelegate : public TestNaClBrowserDelegate {
21 public:
FileHostTestNaClBrowserDelegate()22 FileHostTestNaClBrowserDelegate() {}
23
GetPnaclDirectory(base::FilePath * pnacl_dir)24 bool GetPnaclDirectory(base::FilePath* pnacl_dir) override {
25 *pnacl_dir = pnacl_path_;
26 return true;
27 }
28
SetPnaclDirectory(const base::FilePath & pnacl_dir)29 void SetPnaclDirectory(const base::FilePath& pnacl_dir) {
30 pnacl_path_ = pnacl_dir;
31 }
32
33 private:
34 base::FilePath pnacl_path_;
35 };
36
37 class NaClFileHostTest : public testing::Test {
38 public:
39 NaClFileHostTest(const NaClFileHostTest&) = delete;
40 NaClFileHostTest& operator=(const NaClFileHostTest&) = delete;
41
42 protected:
43 NaClFileHostTest();
44 ~NaClFileHostTest() override;
45
SetUp()46 void SetUp() override {
47 nacl_browser_delegate_ = new FileHostTestNaClBrowserDelegate;
48 nacl::NaClBrowser::SetDelegate(
49 base::WrapUnique(nacl_browser_delegate_.get()));
50 }
51
TearDown()52 void TearDown() override {
53 nacl_browser_delegate_ = nullptr;
54 nacl::NaClBrowser::ClearAndDeleteDelegate();
55 }
56
nacl_browser_delegate()57 FileHostTestNaClBrowserDelegate* nacl_browser_delegate() {
58 return nacl_browser_delegate_;
59 }
60
61 private:
62 raw_ptr<FileHostTestNaClBrowserDelegate> nacl_browser_delegate_;
63 };
64
NaClFileHostTest()65 NaClFileHostTest::NaClFileHostTest() : nacl_browser_delegate_(nullptr) {}
66
~NaClFileHostTest()67 NaClFileHostTest::~NaClFileHostTest() {
68 }
69
70 // Try to pass a few funny filenames with a dummy PNaCl directory set.
TEST_F(NaClFileHostTest,TestFilenamesWithPnaclPath)71 TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) {
72 base::ScopedTempDir scoped_tmp_dir;
73 ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir());
74
75 base::FilePath kTestPnaclPath = scoped_tmp_dir.GetPath();
76
77 nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath);
78 ASSERT_TRUE(nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(
79 &kTestPnaclPath));
80
81 // Check allowed strings, and check that the expected prefix is added.
82 base::FilePath out_path;
83 EXPECT_TRUE(PnaclCanOpenFile("pnacl_json", &out_path));
84 base::FilePath expected_path = kTestPnaclPath.Append(
85 FILE_PATH_LITERAL("pnacl_public_pnacl_json"));
86 EXPECT_EQ(expected_path, out_path);
87
88 EXPECT_TRUE(PnaclCanOpenFile("x86_32_llc", &out_path));
89 expected_path = kTestPnaclPath.Append(
90 FILE_PATH_LITERAL("pnacl_public_x86_32_llc"));
91 EXPECT_EQ(expected_path, out_path);
92
93 // Check character ranges.
94 EXPECT_FALSE(PnaclCanOpenFile(".xchars", &out_path));
95 EXPECT_FALSE(PnaclCanOpenFile("/xchars", &out_path));
96 EXPECT_FALSE(PnaclCanOpenFile("x/chars", &out_path));
97 EXPECT_FALSE(PnaclCanOpenFile("\\xchars", &out_path));
98 EXPECT_FALSE(PnaclCanOpenFile("x\\chars", &out_path));
99 EXPECT_FALSE(PnaclCanOpenFile("$xchars", &out_path));
100 EXPECT_FALSE(PnaclCanOpenFile("%xchars", &out_path));
101 EXPECT_FALSE(PnaclCanOpenFile("CAPS", &out_path));
102 const char non_ascii[] = "\xff\xfe";
103 EXPECT_FALSE(PnaclCanOpenFile(non_ascii, &out_path));
104
105 // Check file length restriction.
106 EXPECT_FALSE(PnaclCanOpenFile("thisstringisactuallywaaaaaaaaaaaaaaaaaaaaaaaa"
107 "toolongwaytoolongwaaaaayyyyytoooooooooooooooo"
108 "looooooooong",
109 &out_path));
110
111 // Other bad files.
112 EXPECT_FALSE(PnaclCanOpenFile(std::string(), &out_path));
113 EXPECT_FALSE(PnaclCanOpenFile(".", &out_path));
114 EXPECT_FALSE(PnaclCanOpenFile("..", &out_path));
115 EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path));
116 EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path));
117 EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path));
118 EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path));
119 }
120