• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "components/nacl/browser/nacl_file_host.h"
6 
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"
15 
16 using nacl_file_host::PnaclCanOpenFile;
17 
18 class FileHostTestNaClBrowserDelegate : public TestNaClBrowserDelegate {
19  public:
FileHostTestNaClBrowserDelegate()20   FileHostTestNaClBrowserDelegate() {}
21 
GetPnaclDirectory(base::FilePath * pnacl_dir)22   virtual bool GetPnaclDirectory(base::FilePath* pnacl_dir) OVERRIDE {
23     *pnacl_dir = pnacl_path_;
24     return true;
25   }
26 
SetPnaclDirectory(const base::FilePath & pnacl_dir)27   void SetPnaclDirectory(const base::FilePath& pnacl_dir) {
28     pnacl_path_ = pnacl_dir;
29   }
30 
31  private:
32   base::FilePath pnacl_path_;
33 };
34 
35 class NaClFileHostTest : public testing::Test {
36  protected:
37   NaClFileHostTest();
38   virtual ~NaClFileHostTest();
39 
SetUp()40   virtual void SetUp() OVERRIDE {
41     nacl_browser_delegate_ = new FileHostTestNaClBrowserDelegate;
42     nacl::NaClBrowser::SetDelegate(nacl_browser_delegate_);
43   }
44 
TearDown()45   virtual void TearDown() OVERRIDE {
46     // This deletes nacl_browser_delegate_.
47     nacl::NaClBrowser::SetDelegate(NULL);
48   }
49 
nacl_browser_delegate()50   FileHostTestNaClBrowserDelegate* nacl_browser_delegate() {
51     return nacl_browser_delegate_;
52   }
53 
54  private:
55   FileHostTestNaClBrowserDelegate* nacl_browser_delegate_;
56   DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest);
57 };
58 
NaClFileHostTest()59 NaClFileHostTest::NaClFileHostTest() : nacl_browser_delegate_(NULL) {}
60 
~NaClFileHostTest()61 NaClFileHostTest::~NaClFileHostTest() {
62 }
63 
64 // Try to pass a few funny filenames with a dummy PNaCl directory set.
TEST_F(NaClFileHostTest,TestFilenamesWithPnaclPath)65 TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) {
66   base::ScopedTempDir scoped_tmp_dir;
67   ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir());
68 
69   base::FilePath kTestPnaclPath = scoped_tmp_dir.path();
70 
71   nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath);
72   ASSERT_TRUE(nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(
73       &kTestPnaclPath));
74 
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);
81 
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);
86 
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));
98 
99   // Check file length restriction.
100   EXPECT_FALSE(PnaclCanOpenFile("thisstringisactuallywaaaaaaaaaaaaaaaaaaaaaaaa"
101                                 "toolongwaytoolongwaaaaayyyyytoooooooooooooooo"
102                                 "looooooooong",
103                                 &out_path));
104 
105   // Other bad files.
106   EXPECT_FALSE(PnaclCanOpenFile(std::string(), &out_path));
107   EXPECT_FALSE(PnaclCanOpenFile(".", &out_path));
108   EXPECT_FALSE(PnaclCanOpenFile("..", &out_path));
109 #if defined(OS_WIN)
110   EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path));
111   EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path));
112   EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path));
113 #else
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));
118 #endif
119 }
120