1761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes/*
2761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes *
4761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * you may not use this file except in compliance with the License.
6761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * You may obtain a copy of the License at
7761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes *
8761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes *
10761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * See the License for the specific language governing permissions and
14761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * limitations under the License.
15761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes */
16761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
17761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/random_access_file_utils.h"
18761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
19761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/string_file.h"
20761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "gtest/gtest.h"
21761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
22761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesnamespace unix_file {
23761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
24761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesclass RandomAccessFileUtilsTest : public testing::Test { };
25761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
26761600567d73b23324ae0251e871c15d6849ffd8Elliott HughesTEST_F(RandomAccessFileUtilsTest, CopyFile) {
27761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  StringFile src;
28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  StringFile dst;
29761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  const std::string content("hello");
31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  src.Assign(content);
32761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_EQ(src.ToStringPiece(), content);
33761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_EQ(dst.ToStringPiece(), "");
34761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
35761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_TRUE(CopyFile(src, &dst));
36761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_EQ(src.ToStringPiece(), dst.ToStringPiece());
37761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}
38761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
39761600567d73b23324ae0251e871c15d6849ffd8Elliott HughesTEST_F(RandomAccessFileUtilsTest, BadSrc) {
409433ec60b325b708b9fa87e699ab4a6565741494Andreas Gampe  FdFile src(-1, false);
41761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  StringFile dst;
42761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_FALSE(CopyFile(src, &dst));
43761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}
44761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
45761600567d73b23324ae0251e871c15d6849ffd8Elliott HughesTEST_F(RandomAccessFileUtilsTest, BadDst) {
46761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  StringFile src;
479433ec60b325b708b9fa87e699ab4a6565741494Andreas Gampe  FdFile dst(-1, false);
48761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
49761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // We need some source content to trigger a write.
50761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Copying an empty file is a no-op.
51761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  src.Assign("hello");
52761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
53761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  ASSERT_FALSE(CopyFile(src, &dst));
54761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}
55761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
56761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}  // namespace unix_file
57