Files
Michael Pratt 4d52a55201 Change copyright notice to "The gVisor Authors"
Based on the guidelines at
https://opensource.google.com/docs/releasing/authors/.

1. $ rg -l "Google LLC" | xargs sed -i 's/Google LLC.*/The gVisor Authors./'
2. Manual fixup of "Google Inc" references.
3. Add AUTHORS file. Authors may request to be added to this file.
4. Point netstack AUTHORS to gVisor AUTHORS. Drop CONTRIBUTORS.

Fixes #209

PiperOrigin-RevId: 245823212
Change-Id: I64530b24ad021a7d683137459cafc510f5ee1de9
2019-04-29 14:26:23 -07:00

69 lines
2.1 KiB
C++

// Copyright 2018 The gVisor Authors.
//
// 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 <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include "gtest/gtest.h"
#include "test/util/fs_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
constexpr int kMode = 0666;
TEST(CreatTest, CreatCreatesNewFile) {
std::string const path = NewTempAbsPath();
struct stat buf;
int fd;
ASSERT_THAT(stat(path.c_str(), &buf), SyscallFailsWithErrno(ENOENT));
ASSERT_THAT(fd = creat(path.c_str(), kMode), SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(stat(path.c_str(), &buf), SyscallSucceeds());
}
TEST(CreatTest, CreatTruncatesExistingFile) {
auto temp_path = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
int fd;
ASSERT_NO_ERRNO(SetContents(temp_path.path(), "non-empty"));
ASSERT_THAT(fd = creat(temp_path.path().c_str(), kMode), SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
std::string new_contents;
ASSERT_NO_ERRNO(GetContents(temp_path.path(), &new_contents));
EXPECT_EQ("", new_contents);
}
TEST(CreatTest, CreatWithNameTooLong) {
// Start with a unique name, and pad it to NAME_MAX + 1;
std::string name = NewTempRelPath();
int padding = (NAME_MAX + 1) - name.size();
name.append(padding, 'x');
const std::string& path = JoinPath(GetAbsoluteTestTmpdir(), name);
// Creation should return ENAMETOOLONG.
ASSERT_THAT(creat(path.c_str(), kMode), SyscallFailsWithErrno(ENAMETOOLONG));
}
} // namespace
} // namespace testing
} // namespace gvisor