Home | History | Annotate | Download | only in utils
      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/utils/file_data.h"
     13 
     14 #include <gtest/gtest.h>
     15 
     16 class ScopedFileData {
     17 public:
     18     ScopedFileData() : mStatus(0) { fileData_initEmpty(&mFileData); }
     19 
     20     ScopedFileData(const void* buff, size_t length) {
     21         mStatus = fileData_initFromMemory(&mFileData, buff, length);
     22     }
     23 
     24     explicit ScopedFileData(const ScopedFileData& other) {
     25         mStatus = fileData_initFrom(&mFileData, other.ptr());
     26     }
     27 
     28     explicit ScopedFileData(const FileData* other) {
     29         mStatus = fileData_initFrom(&mFileData, other);
     30     }
     31 
     32     ~ScopedFileData() { fileData_done(&mFileData); }
     33 
     34     int status() const { return mStatus; }
     35     FileData* ptr() { return &mFileData; }
     36     const FileData* ptr() const { return &mFileData; }
     37     FileData& operator*() { return mFileData; }
     38     FileData* operator->() { return &mFileData; }
     39 private:
     40     FileData mFileData;
     41     int mStatus;
     42 };
     43 
     44 TEST(FileData, IsValid) {
     45     EXPECT_FALSE(fileData_isValid(NULL));
     46 
     47     FileData fakeData = { (uint8_t*)0x012345678, 12345, 23983 };
     48     EXPECT_FALSE(fileData_isValid(&fakeData));
     49 }
     50 
     51 TEST(FileData, InitializerConstant) {
     52     FileData data = FILE_DATA_INIT;
     53     EXPECT_TRUE(fileData_isValid(&data));
     54     EXPECT_TRUE(fileData_isEmpty(&data));
     55 }
     56 
     57 TEST(FileData, InitializerIsFullOfZeroes) {
     58     FileData data;
     59     memset(&data, 0, sizeof data);
     60     EXPECT_TRUE(fileData_isEmpty(&data));
     61     EXPECT_TRUE(fileData_isValid(&data));
     62 }
     63 
     64 TEST(FileData, EmptyInitializer) {
     65     ScopedFileData data;
     66     EXPECT_EQ(0, data.status());
     67     EXPECT_TRUE(fileData_isEmpty(data.ptr()));
     68 }
     69 
     70 TEST(FileData, InitEmpty) {
     71     ScopedFileData data("", 0U);
     72     EXPECT_EQ(0, data.status());
     73     EXPECT_TRUE(fileData_isEmpty(data.ptr()));
     74     EXPECT_FALSE(data->data);
     75     EXPECT_EQ(0U, data->size);
     76 }
     77 
     78 TEST(FileData, InitFromMemory) {
     79     static const char kData[] = "Hello World!";
     80     ScopedFileData data(kData, sizeof kData);
     81     EXPECT_EQ(0, data.status());
     82     EXPECT_NE(kData, (const char*)data->data);
     83     for (size_t n = 0; n < sizeof kData; ++n) {
     84         EXPECT_EQ(kData[n], data->data[n]) << "index " << n;
     85     }
     86 }
     87 
     88 TEST(FileData, InitFromOther) {
     89     static const char kData[] = "Hello World!";
     90     ScopedFileData data1(kData, sizeof kData);
     91     EXPECT_EQ(0, data1.status());
     92 
     93     ScopedFileData data2(data1.ptr());
     94     EXPECT_EQ(0, data2.status());
     95 
     96     EXPECT_EQ(data1->size, data2->size);
     97     EXPECT_NE(data1->data, data2->data);
     98     for (size_t n = 0; n < data1->size; ++n) {
     99         EXPECT_EQ(kData[n], data1->data[n]) << "index " << n;
    100         EXPECT_EQ(kData[n], data2->data[n]) << "index " << n;
    101     }
    102 }
    103 
    104 TEST(FileData, Swap) {
    105     static const char kData[] = "Hello World!";
    106     ScopedFileData data1(kData, sizeof kData);
    107     EXPECT_EQ(0, data1.status());
    108 
    109     ScopedFileData data2;
    110     EXPECT_EQ(0, data2.status());
    111     fileData_swap(data1.ptr(), data2.ptr());
    112 
    113     EXPECT_TRUE(fileData_isEmpty(data1.ptr()));
    114     EXPECT_FALSE(fileData_isEmpty(data2.ptr()));
    115     EXPECT_EQ(sizeof kData, data2->size);
    116     for (size_t n = 0; n < data2->size; ++n) {
    117         EXPECT_EQ(kData[n], data2->data[n]) << "index " << n;
    118     }
    119 }
    120