blob: 43d6be49765d1380539c65eb76f096798369fe27 [file] [log] [blame]
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "starboard/directory.h"
#include "starboard/file.h"
#include "starboard/nplb/file_helpers.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
const char* kManyFileSeparators =
SB_FILE_SEP_STRING SB_FILE_SEP_STRING SB_FILE_SEP_STRING SB_FILE_SEP_STRING;
// NOTE: There is a test missing here, for creating a directory right off of the
// root. But, this is likely to fail due to permissions, so we can't make a
// reliable test for this.
TEST(SbDirectoryCreateTest, SunnyDay) {
ScopedRandomFile dir(ScopedRandomFile::kDontCreate);
const std::string& path = dir.filename();
EXPECT_FALSE(SbDirectoryCanOpen(path.c_str()));
EXPECT_TRUE(SbDirectoryCreate(path.c_str()));
EXPECT_TRUE(SbDirectoryCanOpen(path.c_str()));
// Should return true if called redundantly.
EXPECT_TRUE(SbDirectoryCreate(path.c_str()));
EXPECT_TRUE(SbDirectoryCanOpen(path.c_str()));
}
TEST(SbDirectoryCreateTest, SunnyDayTrailingSeparators) {
ScopedRandomFile dir(ScopedRandomFile::kDontCreate);
std::string path = dir.filename() + kManyFileSeparators;
EXPECT_FALSE(SbDirectoryCanOpen(path.c_str()));
EXPECT_TRUE(SbDirectoryCreate(path.c_str()));
EXPECT_TRUE(SbDirectoryCanOpen(path.c_str()));
}
TEST(SbDirectoryCreateTest, SunnyDayTempDirectory) {
const int kMaxFilePath = SB_FILE_MAX_PATH;
char temp_path[kMaxFilePath];
bool system_path_success =
SbSystemGetPath(kSbSystemPathTempDirectory, temp_path, kMaxFilePath);
ASSERT_TRUE(system_path_success);
EXPECT_TRUE(SbDirectoryCanOpen(temp_path));
EXPECT_TRUE(SbDirectoryCreate(temp_path));
EXPECT_TRUE(SbDirectoryCanOpen(temp_path));
}
TEST(SbDirectoryCreateTest, SunnyDayTempDirectoryManySeparators) {
const int kMaxFilePath = SB_FILE_MAX_PATH;
char temp_path[kMaxFilePath];
bool system_path_success =
SbSystemGetPath(kSbSystemPathTempDirectory, temp_path, kMaxFilePath);
ASSERT_TRUE(system_path_success);
const int new_size =
SbStringConcat(temp_path, kManyFileSeparators, kMaxFilePath);
ASSERT_LT(new_size, kMaxFilePath);
EXPECT_TRUE(SbDirectoryCanOpen(temp_path));
EXPECT_TRUE(SbDirectoryCreate(temp_path));
EXPECT_TRUE(SbDirectoryCanOpen(temp_path));
}
TEST(SbDirectoryCreateTest, FailureNullPath) {
EXPECT_FALSE(SbDirectoryCreate(NULL));
}
TEST(SbDirectoryCreateTest, FailureEmptyPath) {
EXPECT_FALSE(SbDirectoryCreate(""));
}
TEST(SbDirectoryCreateTest, FailureNonexistentParent) {
ScopedRandomFile dir(ScopedRandomFile::kDontCreate);
std::string path = dir.filename() + SB_FILE_SEP_STRING "test";
EXPECT_FALSE(SbDirectoryCanOpen(path.c_str()));
EXPECT_FALSE(SbDirectoryCreate(path.c_str()));
EXPECT_FALSE(SbDirectoryCanOpen(path.c_str()));
}
TEST(SbDirectoryCreateTest, FailureNotAbsolute) {
const char* kPath = "hallo";
EXPECT_FALSE(SbDirectoryCanOpen(kPath));
EXPECT_FALSE(SbDirectoryCreate(kPath));
EXPECT_FALSE(SbDirectoryCanOpen(kPath));
}
} // namespace
} // namespace nplb
} // namespace starboard