| Index: chrome/browser/nacl_host/pnacl_file_host_unittest.cc
|
| diff --git a/chrome/browser/nacl_host/pnacl_file_host_unittest.cc b/chrome/browser/nacl_host/pnacl_file_host_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..949e7daff1b47baa8462883e348688e063c889ae
|
| --- /dev/null
|
| +++ b/chrome/browser/nacl_host/pnacl_file_host_unittest.cc
|
| @@ -0,0 +1,96 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/nacl_host/pnacl_file_host.h"
|
| +
|
| +#include "base/basictypes.h"
|
| +#include "base/file_path.h"
|
| +#include "base/file_util.h"
|
| +#include "base/path_service.h"
|
| +#include "base/scoped_temp_dir.h"
|
| +#include "base/synchronization/lock.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using pnacl_file_host::PnaclCanOpenFile;
|
| +
|
| +namespace {
|
| +
|
| +class PnaclFileHostTest : public ::testing::Test {
|
| + public:
|
| + PnaclFileHostTest() {
|
| + }
|
| + virtual ~PnaclFileHostTest() {
|
| + }
|
| + // NOTE: because we override the DIR_PNACL_COMPONENT in both
|
| + // tests, these tests cannot be run concurrently.
|
| + base::Lock test_lock_;
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(PnaclFileHostTest);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +// Try to pass a few funny filenames with a dummy pnacl directory set.
|
| +TEST_F(PnaclFileHostTest, TestFilenamesWithPnaclPath) {
|
| + base::AutoLock auto_lock(test_lock_);
|
| + FilePath kDummyPnaclPath("dummy_pnacl_path");
|
| + ASSERT_TRUE(PathService::Override(chrome::DIR_PNACL_COMPONENT,
|
| + kDummyPnaclPath));
|
| + ASSERT_TRUE(PathService::Get(chrome::DIR_PNACL_COMPONENT,
|
| + &kDummyPnaclPath));
|
| + FilePath out_path;
|
| + EXPECT_TRUE(PnaclCanOpenFile("manifest.json", &out_path));
|
| + FilePath expected_path = kDummyPnaclPath.Append("manifest.json");
|
| + EXPECT_EQ(out_path, expected_path);
|
| +
|
| + EXPECT_TRUE(PnaclCanOpenFile("x86-32/llc", &out_path));
|
| + expected_path = kDummyPnaclPath.Append("x86-32/llc");
|
| + EXPECT_EQ(out_path, expected_path);
|
| +
|
| + EXPECT_FALSE(PnaclCanOpenFile("", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile(".", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("..", &out_path));
|
| +#if defined(OS_WIN)
|
| + EXPECT_FALSE(PnaclCanOpenFile("..\\llc", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("%SystemRoot%\\explorer.exe", &out_path));
|
| +#else
|
| + EXPECT_FALSE(PnaclCanOpenFile("../llc", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("/bin/sh", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path));
|
| + EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path));
|
| +#endif
|
| +
|
| + const char non_ascii[] = "\xff\xfe";
|
| + EXPECT_FALSE(PnaclCanOpenFile(non_ascii, &out_path));
|
| +}
|
| +
|
| +// Place a dummy file, open it, and make sure we can still delete it while
|
| +// it is open (in case we need the Pnacl upgrader to GC old versions
|
| +// of Pnacl).
|
| +TEST_F(PnaclFileHostTest, TestOpenAndDelete) {
|
| + base::AutoLock auto_lock(test_lock_);
|
| +
|
| + // Write something to a temporary directory, and try to open it,
|
| + // and delete while still open.
|
| +
|
| + ScopedTempDir temp_directory;
|
| + ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
|
| + FilePath temp_file;
|
| + ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
|
| + &temp_file));
|
| + ASSERT_TRUE(PathService::Override(chrome::DIR_PNACL_COMPONENT,
|
| + temp_directory.path()));
|
| + FilePath full_path;
|
| + EXPECT_TRUE(PnaclCanOpenFile(temp_file.BaseName().MaybeAsASCII(),
|
| + &full_path));
|
| + EXPECT_EQ(full_path, temp_directory.path().Append(temp_file.BaseName()));
|
| +
|
| + base::PlatformFile open_temp;
|
| + EXPECT_TRUE(pnacl_file_host::PnaclDoOpenFile(full_path, &open_temp));
|
| + EXPECT_TRUE(file_util::Delete(full_path, false));
|
| + EXPECT_TRUE(base::ClosePlatformFile(open_temp));
|
| +}
|
|
|