1/*
2 * Copyright (C) 2015 Canon Inc. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28
29#include "Test.h"
30#include <wtf/FileMetadata.h>
31#include <wtf/FileSystem.h>
32#include <wtf/MainThread.h>
33#include <wtf/StringExtras.h>
34
35namespace TestWebKitAPI {
36
37const char* FileSystemTestData = "This is a test";
38
39// FIXME: Refactor FileSystemTest and SharedBufferTest as a single class.
40class FileSystemTest : public testing::Test {
41public:
42 void SetUp() override
43 {
44 WTF::initializeMainThread();
45
46 // create temp file
47 FileSystem::PlatformFileHandle handle;
48 m_tempFilePath = FileSystem::openTemporaryFile("tempTestFile", handle);
49 FileSystem::writeToFile(handle, FileSystemTestData, strlen(FileSystemTestData));
50 FileSystem::closeFile(handle);
51
52 m_tempFileSymlinkPath = "tempTestFile-symlink";
53 FileSystem::createSymbolicLink(m_tempFilePath, m_tempFileSymlinkPath);
54
55 m_tempEmptyFilePath = FileSystem::openTemporaryFile("tempEmptyTestFile", handle);
56 FileSystem::closeFile(handle);
57
58 m_spaceContainingFilePath = FileSystem::openTemporaryFile("temp Empty Test File", handle);
59 FileSystem::closeFile(handle);
60
61 m_bangContainingFilePath = FileSystem::openTemporaryFile("temp!Empty!Test!File", handle);
62 FileSystem::closeFile(handle);
63
64 m_quoteContainingFilePath = FileSystem::openTemporaryFile("temp\"Empty\"TestFile", handle);
65 FileSystem::closeFile(handle);
66 }
67
68 void TearDown() override
69 {
70 FileSystem::deleteFile(m_tempFilePath);
71 FileSystem::deleteFile(m_tempFileSymlinkPath);
72 FileSystem::deleteFile(m_tempEmptyFilePath);
73 FileSystem::deleteFile(m_spaceContainingFilePath);
74 FileSystem::deleteFile(m_bangContainingFilePath);
75 FileSystem::deleteFile(m_quoteContainingFilePath);
76 }
77
78 const String& tempFilePath() { return m_tempFilePath; }
79 const String& tempFileSymlinkPath() { return m_tempFileSymlinkPath; }
80 const String& tempEmptyFilePath() { return m_tempEmptyFilePath; }
81 const String& spaceContainingFilePath() { return m_spaceContainingFilePath; }
82 const String& bangContainingFilePath() { return m_bangContainingFilePath; }
83 const String& quoteContainingFilePath() { return m_quoteContainingFilePath; }
84
85private:
86 String m_tempFilePath;
87 String m_tempFileSymlinkPath;
88 String m_tempEmptyFilePath;
89 String m_spaceContainingFilePath;
90 String m_bangContainingFilePath;
91 String m_quoteContainingFilePath;
92};
93
94TEST_F(FileSystemTest, MappingMissingFile)
95{
96 bool success;
97 FileSystem::MappedFileData mappedFileData(String("not_existing_file"), success);
98 EXPECT_FALSE(success);
99 EXPECT_TRUE(!mappedFileData);
100}
101
102TEST_F(FileSystemTest, MappingExistingFile)
103{
104 bool success;
105 FileSystem::MappedFileData mappedFileData(tempFilePath(), success);
106 EXPECT_TRUE(success);
107 EXPECT_TRUE(!!mappedFileData);
108 EXPECT_TRUE(mappedFileData.size() == strlen(FileSystemTestData));
109 EXPECT_TRUE(strnstr(FileSystemTestData, static_cast<const char*>(mappedFileData.data()), mappedFileData.size()));
110}
111
112TEST_F(FileSystemTest, MappingExistingEmptyFile)
113{
114 bool success;
115 FileSystem::MappedFileData mappedFileData(tempEmptyFilePath(), success);
116 EXPECT_TRUE(success);
117 EXPECT_TRUE(!mappedFileData);
118}
119
120TEST_F(FileSystemTest, FilesHaveSameVolume)
121{
122 EXPECT_TRUE(FileSystem::filesHaveSameVolume(tempFilePath(), spaceContainingFilePath()));
123 EXPECT_TRUE(FileSystem::filesHaveSameVolume(spaceContainingFilePath(), bangContainingFilePath()));
124 EXPECT_TRUE(FileSystem::filesHaveSameVolume(bangContainingFilePath(), quoteContainingFilePath()));
125}
126
127TEST_F(FileSystemTest, GetFileMetadataSymlink)
128{
129 auto symlinkMetadata = FileSystem::fileMetadata(tempFileSymlinkPath());
130 ASSERT_TRUE(symlinkMetadata.hasValue());
131 EXPECT_TRUE(symlinkMetadata.value().type == FileMetadata::Type::SymbolicLink);
132 EXPECT_FALSE(static_cast<size_t>(symlinkMetadata.value().length) == strlen(FileSystemTestData));
133
134 auto targetMetadata = FileSystem::fileMetadataFollowingSymlinks(tempFileSymlinkPath());
135 ASSERT_TRUE(targetMetadata.hasValue());
136 EXPECT_TRUE(targetMetadata.value().type == FileMetadata::Type::File);
137 EXPECT_EQ(strlen(FileSystemTestData), static_cast<size_t>(targetMetadata.value().length));
138}
139
140TEST_F(FileSystemTest, UnicodeDirectoryName)
141{
142 String path = String::fromUTF8("/test/a\u0308lo/test.txt");
143 String directoryName = FileSystem::directoryName(path);
144 String expectedDirectoryName = String::fromUTF8("/test/a\u0308lo");
145 EXPECT_TRUE(expectedDirectoryName == directoryName);
146}
147
148}
149