Home | History | Annotate | Download | only in filesystems
      1 // Copyright 2014 The Android Open Source Project
      2 //
      3 // This software is licensed under the terms of the GNU General Public
      4 // License version 2, as published by the Free Software Foundation, and
      5 // may be copied, distributed, and modified under those terms.
      6 //
      7 // This program is distributed in the hope that it will be useful,
      8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 // GNU General Public License for more details.
     11 
     12 #include "android/filesystems/ramdisk_extractor.h"
     13 
     14 #include "android/base/EintrWrapper.h"
     15 #include "android/filesystems/testing/TestSupport.h"
     16 
     17 #include <gtest/gtest.h>
     18 
     19 #include <stdio.h>
     20 
     21 namespace {
     22 
     23 #include "android/filesystems/testing/TestRamdiskImage.h"
     24 
     25 class RamdiskExtractorTest : public ::testing::Test {
     26 public:
     27     RamdiskExtractorTest() :
     28         mTempFilePath(android::testing::CreateTempFilePath()) {}
     29 
     30     bool fillData(const void* data, size_t dataSize) {
     31         FILE* file = ::fopen(mTempFilePath.c_str(), "wb");
     32         if (!file) {
     33             return false;
     34         }
     35 
     36         bool result = (fwrite(data, dataSize, 1, file) == 1);
     37         fclose(file);
     38         return result;
     39     }
     40 
     41     ~RamdiskExtractorTest() {
     42         if (!mTempFilePath.empty()) {
     43             HANDLE_EINTR(unlink(mTempFilePath.c_str()));
     44         }
     45     }
     46 
     47     const char* path() const { return mTempFilePath.c_str(); }
     48 
     49 private:
     50     std::string mTempFilePath;
     51 };
     52 
     53 }  // namespace
     54 
     55 TEST_F(RamdiskExtractorTest, FindFoo) {
     56     static const char kExpected[] = "Hello World!\n";
     57     static const size_t kExpectedSize = sizeof(kExpected) - 1U;
     58     char* out = NULL;
     59     size_t outSize = 0;
     60 
     61     EXPECT_TRUE(fillData(kTestRamdiskImage, kTestRamdiskImageSize));
     62     EXPECT_TRUE(android_extractRamdiskFile(path(), "foo", &out, &outSize));
     63     EXPECT_EQ(kExpectedSize, outSize);
     64     EXPECT_TRUE(out);
     65     EXPECT_TRUE(!memcmp(out, kExpected, outSize));
     66     free(out);
     67 }
     68 
     69 TEST_F(RamdiskExtractorTest, FindBar2) {
     70     static const char kExpected[] = "La vie est un long fleuve tranquille\n";
     71     static const size_t kExpectedSize = sizeof(kExpected) - 1U;
     72     char* out = NULL;
     73     size_t outSize = 0;
     74 
     75     EXPECT_TRUE(fillData(kTestRamdiskImage, kTestRamdiskImageSize));
     76     EXPECT_TRUE(android_extractRamdiskFile(path(), "bar2", &out, &outSize));
     77     EXPECT_EQ(kExpectedSize, outSize);
     78     EXPECT_TRUE(out);
     79     EXPECT_TRUE(!memcmp(out, kExpected, outSize));
     80     free(out);
     81 }
     82 
     83 TEST_F(RamdiskExtractorTest, MissingFile) {
     84     char* out = NULL;
     85     size_t outSize = 0;
     86     EXPECT_TRUE(fillData(kTestRamdiskImage, kTestRamdiskImageSize));
     87     EXPECT_FALSE(android_extractRamdiskFile(path(), "zoolander", &out, &outSize));
     88 }
     89