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/partition_types.h" 13 14#include "android/base/EintrWrapper.h" 15#include "android/base/files/ScopedStdioFile.h" 16#include "android/filesystems/ext4_utils.h" 17#include "android/filesystems/testing/TestExt4ImageHeader.h" 18#include "android/filesystems/testing/TestSupport.h" 19 20#include "android/utils/path.h" 21 22#include <gtest/gtest.h> 23 24#include <stdio.h> 25#include <string> 26#include <unistd.h> 27 28namespace { 29 30using android::base::ScopedStdioFile; 31 32class TempPartition { 33public: 34 TempPartition() { 35 mPath = android::testing::CreateTempFilePath(); 36 } 37 38 ~TempPartition() { 39 if (!mPath.empty()) { 40 HANDLE_EINTR(::unlink(mPath.c_str())); 41 } 42 } 43 44 const char* GetPath() const { 45 return mPath.c_str(); 46 } 47 48protected: 49 std::string mPath; 50}; 51 52} // namespace 53 54TEST(AndroidPartitionType, ToString) { 55 EXPECT_STREQ( 56 "unknown", 57 androidPartitionType_toString(ANDROID_PARTITION_TYPE_UNKNOWN)); 58 EXPECT_STREQ( 59 "yaffs2", 60 androidPartitionType_toString(ANDROID_PARTITION_TYPE_YAFFS2)); 61 EXPECT_STREQ( 62 "ext4", 63 androidPartitionType_toString(ANDROID_PARTITION_TYPE_EXT4)); 64} 65 66TEST(AndroidPartitionType, FromString) { 67 EXPECT_EQ(ANDROID_PARTITION_TYPE_YAFFS2, 68 androidPartitionType_fromString("yaffs2")); 69 EXPECT_EQ(ANDROID_PARTITION_TYPE_EXT4, 70 androidPartitionType_fromString("ext4")); 71 EXPECT_EQ(ANDROID_PARTITION_TYPE_UNKNOWN, 72 androidPartitionType_fromString("unknown")); 73 EXPECT_EQ(ANDROID_PARTITION_TYPE_UNKNOWN, 74 androidPartitionType_fromString("foobar")); 75} 76 77TEST(AndroidPartitionType, ProbeFileYaffs2) { 78 TempPartition part; 79 80 // An empty partition is a valid YAFFS2 one. 81 ::path_empty_file(part.GetPath()); 82 83 EXPECT_EQ(ANDROID_PARTITION_TYPE_YAFFS2, 84 androidPartitionType_probeFile(part.GetPath())); 85} 86 87 88TEST(AndroidPartitionType, ProbeFileExt4) { 89 TempPartition part; 90 91 android_createEmptyExt4Image(part.GetPath(), 16*1024*1024, "cache"); 92 93 EXPECT_EQ(ANDROID_PARTITION_TYPE_EXT4, 94 androidPartitionType_probeFile(part.GetPath())); 95} 96 97TEST(AndroidPartitionType, MakeEmptyFileYaffs2) { 98 TempPartition part; 99 100 EXPECT_EQ(0, androidPartitionType_makeEmptyFile( 101 ANDROID_PARTITION_TYPE_YAFFS2, 102 8 * 1024 * 1024, 103 part.GetPath())) << "Could not create Yaffs2 partition image"; 104 105 EXPECT_EQ(ANDROID_PARTITION_TYPE_YAFFS2, 106 androidPartitionType_probeFile(part.GetPath())); 107} 108 109TEST(AndroidPartitionType, MakeEmptyFileExt4) { 110 TempPartition part; 111 112 EXPECT_EQ(0, androidPartitionType_makeEmptyFile( 113 ANDROID_PARTITION_TYPE_EXT4, 114 8 * 1024 * 1024, 115 part.GetPath())) << "Could not create EXT4 partition image"; 116 117 EXPECT_EQ(ANDROID_PARTITION_TYPE_EXT4, 118 androidPartitionType_probeFile(part.GetPath())); 119} 120