Home | History | Annotate | Download | only in unix_file
      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/null_file.h"
     18 
     19 #include <errno.h>
     20 
     21 #include "gtest/gtest.h"
     22 
     23 namespace unix_file {
     24 
     25 class NullFileTest : public testing::Test { };
     26 
     27 TEST_F(NullFileTest, Read) {
     28   NullFile f;
     29   char buf[256];
     30   // You can't read a negative number of bytes...
     31   ASSERT_EQ(-EINVAL, f.Read(buf, 0, -1));
     32   // ...but everything else is fine (though you'll get no data).
     33   ASSERT_EQ(0, f.Read(buf, 128, 0));
     34   ASSERT_EQ(0, f.Read(buf, 128, 128));
     35 }
     36 
     37 TEST_F(NullFileTest, SetLength) {
     38   NullFile f;
     39   // You can't set a negative length...
     40   ASSERT_EQ(-EINVAL, f.SetLength(-1));
     41   // ...but everything else is fine.
     42   ASSERT_EQ(0, f.SetLength(0));
     43   ASSERT_EQ(0, f.SetLength(128));
     44 }
     45 
     46 TEST_F(NullFileTest, GetLength) {
     47   const std::string content("hello");
     48   NullFile f;
     49   // The length is always 0.
     50   ASSERT_EQ(0, f.GetLength());
     51   ASSERT_EQ(content.size(), static_cast<uint64_t>(f.Write(content.data(), content.size(), 0)));
     52   ASSERT_EQ(0, f.GetLength());
     53 }
     54 
     55 TEST_F(NullFileTest, Write) {
     56   const std::string content("hello");
     57   NullFile f;
     58   // You can't write at a negative offset...
     59   ASSERT_EQ(-EINVAL, f.Write(content.data(), content.size(), -128));
     60   // But you can write anywhere else...
     61   ASSERT_EQ(content.size(), static_cast<uint64_t>(f.Write(content.data(), content.size(), 0)));
     62   ASSERT_EQ(content.size(), static_cast<uint64_t>(f.Write(content.data(), content.size(), 128)));
     63   // ...though the file will remain empty.
     64   ASSERT_EQ(0, f.GetLength());
     65 }
     66 
     67 }  // namespace unix_file
     68