1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "base/unix_file/random_access_file_utils.h" 18 #include "base/unix_file/fd_file.h" 19 #include "base/unix_file/string_file.h" 20 #include "gtest/gtest.h" 21 22 namespace unix_file { 23 24 class RandomAccessFileUtilsTest : public testing::Test { }; 25 26 TEST_F(RandomAccessFileUtilsTest, CopyFile) { 27 StringFile src; 28 StringFile dst; 29 30 const std::string content("hello"); 31 src.Assign(content); 32 ASSERT_EQ(src.ToStringPiece(), content); 33 ASSERT_EQ(dst.ToStringPiece(), ""); 34 35 ASSERT_TRUE(CopyFile(src, &dst)); 36 ASSERT_EQ(src.ToStringPiece(), dst.ToStringPiece()); 37 } 38 39 TEST_F(RandomAccessFileUtilsTest, BadSrc) { 40 FdFile src(-1, false); 41 StringFile dst; 42 ASSERT_FALSE(CopyFile(src, &dst)); 43 } 44 45 TEST_F(RandomAccessFileUtilsTest, BadDst) { 46 StringFile src; 47 FdFile dst(-1, false); 48 49 // We need some source content to trigger a write. 50 // Copying an empty file is a no-op. 51 src.Assign("hello"); 52 53 ASSERT_FALSE(CopyFile(src, &dst)); 54 } 55 56 } // namespace unix_file 57