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 #ifndef ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_TEST_H_
     18 #define ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_TEST_H_
     19 
     20 #include <errno.h>
     21 #include <memory>
     22 #include <string>
     23 
     24 #include "common_runtime_test.h"
     25 
     26 namespace unix_file {
     27 
     28 class RandomAccessFileTest : public testing::Test {
     29  protected:
     30   virtual ~RandomAccessFileTest() {
     31   }
     32 
     33   // Override this to return an instance of the subclass under test that's
     34   // backed by a temporary file.
     35   virtual RandomAccessFile* MakeTestFile() = 0;
     36 
     37   virtual void SetUp() {
     38     art::CommonRuntimeTest::SetUpAndroidData(android_data_);
     39   }
     40 
     41   virtual void TearDown() {
     42     art::CommonRuntimeTest::TearDownAndroidData(android_data_, true);
     43   }
     44 
     45   std::string GetTmpPath(const std::string& name) {
     46     std::string path;
     47     path = android_data_;
     48     path += "/";
     49     path += name;
     50     return path;
     51   }
     52 
     53   // TODO(enh): ReadString (and WriteString) might be generally useful.
     54   static bool ReadString(RandomAccessFile* f, std::string* s) {
     55     s->clear();
     56     char buf[256];
     57     int64_t n = 0;
     58     int64_t offset = 0;
     59     while ((n = f->Read(buf, sizeof(buf), offset)) > 0) {
     60       s->append(buf, n);
     61       offset += n;
     62     }
     63     return n != -1;
     64   }
     65 
     66   void TestRead() {
     67     char buf[256];
     68     std::unique_ptr<RandomAccessFile> file(MakeTestFile());
     69 
     70     // Reading from the start of an empty file gets you zero bytes, however many
     71     // you ask for.
     72     ASSERT_EQ(0, file->Read(buf, 0, 0));
     73     ASSERT_EQ(0, file->Read(buf, 123, 0));
     74 
     75     const std::string content("hello");
     76     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Write(content.data(), content.size(), 0)));
     77 
     78     TestReadContent(content, file.get());
     79   }
     80 
     81   void TestReadContent(const std::string& content, RandomAccessFile* file) {
     82     const int buf_size = content.size() + 10;
     83     std::unique_ptr<char> buf(new char[buf_size]);
     84     // Can't read from a negative offset.
     85     ASSERT_EQ(-EINVAL, file->Read(buf.get(), 0, -123));
     86 
     87     // Reading too much gets us just what's in the file.
     88     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Read(buf.get(), buf_size, 0)));
     89     ASSERT_EQ(std::string(buf.get(), content.size()), content);
     90 
     91     // We only get as much as we ask for.
     92     const size_t short_request = 2;
     93     ASSERT_LT(short_request, content.size());
     94     ASSERT_EQ(short_request, static_cast<uint64_t>(file->Read(buf.get(), short_request, 0)));
     95     ASSERT_EQ(std::string(buf.get(), short_request),
     96               content.substr(0, short_request));
     97 
     98     // We don't have to start at the beginning.
     99     const int non_zero_offset = 2;
    100     ASSERT_GT(non_zero_offset, 0);
    101     ASSERT_EQ(short_request, static_cast<uint64_t>(file->Read(buf.get(), short_request,
    102                                                               non_zero_offset)));
    103     ASSERT_EQ(std::string(buf.get(), short_request),
    104               content.substr(non_zero_offset, short_request));
    105 
    106     // Reading past the end gets us nothing.
    107     ASSERT_EQ(0, file->Read(buf.get(), buf_size, file->GetLength()));
    108     ASSERT_EQ(0, file->Read(buf.get(), buf_size, file->GetLength() + 1));
    109   }
    110 
    111   void TestSetLength() {
    112     const std::string content("hello");
    113     std::unique_ptr<RandomAccessFile> file(MakeTestFile());
    114     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Write(content.data(), content.size(), 0)));
    115     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->GetLength()));
    116 
    117     // Can't give a file a negative length.
    118     ASSERT_EQ(-EINVAL, file->SetLength(-123));
    119 
    120     // Can truncate the file.
    121     int64_t new_length = 2;
    122     ASSERT_EQ(0, file->SetLength(new_length));
    123     ASSERT_EQ(new_length, file->GetLength());
    124     std::string new_content;
    125     ASSERT_TRUE(ReadString(file.get(), &new_content));
    126     ASSERT_EQ(content.substr(0, 2), new_content);
    127 
    128     // Expanding the file appends zero bytes.
    129     new_length = file->GetLength() + 1;
    130     ASSERT_EQ(0, file->SetLength(new_length));
    131     ASSERT_EQ(new_length, file->GetLength());
    132     ASSERT_TRUE(ReadString(file.get(), &new_content));
    133     ASSERT_EQ('\0', new_content[new_length - 1]);
    134   }
    135 
    136   void TestWrite() {
    137     const std::string content("hello");
    138     std::unique_ptr<RandomAccessFile> file(MakeTestFile());
    139 
    140     // Can't write to a negative offset.
    141     ASSERT_EQ(-EINVAL, file->Write(content.data(), 0, -123));
    142 
    143     // Writing zero bytes of data is a no-op.
    144     ASSERT_EQ(0, file->Write(content.data(), 0, 0));
    145     ASSERT_EQ(0, file->GetLength());
    146 
    147     // We can write data.
    148     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Write(content.data(), content.size(), 0)));
    149     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->GetLength()));
    150     std::string new_content;
    151     ASSERT_TRUE(ReadString(file.get(), &new_content));
    152     ASSERT_EQ(new_content, content);
    153 
    154     // We can read it back.
    155     char buf[256];
    156     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Read(buf, sizeof(buf), 0)));
    157     ASSERT_EQ(std::string(buf, content.size()), content);
    158 
    159     // We can append data past the end.
    160     ASSERT_EQ(content.size(), static_cast<uint64_t>(file->Write(content.data(), content.size(),
    161                                                                 file->GetLength() + 1)));
    162     int64_t new_length = 2*content.size() + 1;
    163     ASSERT_EQ(file->GetLength(), new_length);
    164     ASSERT_TRUE(ReadString(file.get(), &new_content));
    165     ASSERT_EQ(std::string("hello\0hello", new_length), new_content);
    166   }
    167 
    168  protected:
    169   std::string android_data_;
    170 };
    171 
    172 }  // namespace unix_file
    173 
    174 #endif  // ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_TEST_H_
    175