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/kernel/kernel_utils.h" 13 14#include "android/base/String.h" 15#include "android/kernel/kernel_utils_testing.h" 16 17#include <gtest/gtest.h> 18 19using android::base::String; 20 21namespace android { 22namespace kernel { 23 24namespace { 25 26class ScopedDescriptionFunc { 27public: 28 explicit ScopedDescriptionFunc(GetFileDescriptionFunction* file_func) { 29 setFileDescriptionFunction(file_func, NULL); 30 } 31 32 ScopedDescriptionFunc(GetFileDescriptionFunction* file_func, 33 void* file_opaque) { 34 setFileDescriptionFunction(file_func, file_opaque); 35 } 36 37 ~ScopedDescriptionFunc() { 38 setFileDescriptionFunction(NULL, NULL); 39 } 40}; 41 42} // namespace 43 44TEST(KernelUtils, GetKernelSerialDevicePrefix) { 45 EXPECT_STREQ("ttyS", 46 android_kernelSerialDevicePrefix(KERNEL_TYPE_LEGACY)); 47 EXPECT_STREQ("ttyGF", 48 android_kernelSerialDevicePrefix(KERNEL_TYPE_3_10_OR_ABOVE)); 49} 50 51static bool failFunc(void* opaque, const char* path, String* text) { 52 return false; 53} 54 55TEST(KernelUtils, ProbeKernelTypeWithNoKernelFile) { 56 ScopedDescriptionFunc func(&failFunc); 57 58 KernelType ktype; 59 EXPECT_FALSE(android_pathProbeKernelType("/tmp/kernel", &ktype)); 60} 61 62struct Expectation { 63 const char* description; 64 bool result; 65 KernelType ktype; 66 67 static bool getDescriptionFunc(void* opaque, 68 const char* path, 69 String* text) { 70 const Expectation* expectation = static_cast<const Expectation*>(opaque); 71 if (!expectation->result) 72 return false; 73 text->assign(expectation->description); 74 return true; 75 } 76}; 77 78#ifdef _WIN32 79#define DISABLED_ON_WIN32(x) DISABLED_ ## x 80#else 81#define DISABLED_ON_WIN32(x) x 82#endif 83 84TEST(KernelUtils, DISABLED_ON_WIN32(PathProbeKernelType)) { 85 static const Expectation kData[] = { 86 { NULL, false, KERNEL_TYPE_LEGACY }, 87 // Missing bzImage. 88 { "Linux kernel x86 boot executable raw image, version 3.10.0+", 89 false, KERNEL_TYPE_LEGACY }, 90 // Missing version 91 { "Linux kernel x86 boot executable bzImage, 3.10.0+", 92 false, KERNEL_TYPE_LEGACY }, 93 // Legacy 2.6.29 kernel 94 { "Linux kernel x86 boot executable bzImage, version 2.6.29 (foo...)", 95 true, 96 KERNEL_TYPE_LEGACY }, 97 // Legacy 3.4 98 { "Linux kernel x86 boot executable bzImage, version 3.4.1 (foo...)", 99 true, 100 KERNEL_TYPE_LEGACY }, 101 // 3.10 102 { "Linux kernel x86 boot executable bzImage, version 3.10.0+", 103 true, 104 KERNEL_TYPE_3_10_OR_ABOVE }, 105 // 3.40 106 { "Linux kernel x86 boot executable bzImage, version 3.40.0", 107 true, 108 KERNEL_TYPE_3_10_OR_ABOVE }, 109 // 4.0 110 { "Linux kernel x86 boot executable bzImage, version 4.0.9", 111 true, 112 KERNEL_TYPE_3_10_OR_ABOVE }, 113 }; 114 const size_t kDataSize = sizeof(kData) / sizeof(kData[0]); 115 static const char kKernelPath[] = "/tmp/kernel"; 116 for (size_t n = 0; n < kDataSize; ++n) { 117 KernelType kernelType; 118 const Expectation& expectation = kData[n]; 119 ScopedDescriptionFunc func(&Expectation::getDescriptionFunc, 120 (void*)&expectation); 121 EXPECT_EQ(expectation.result, 122 android_pathProbeKernelType(kKernelPath, &kernelType)) 123 << "For [" << expectation.description << "]"; 124 if (expectation.result) { 125 EXPECT_EQ(expectation.ktype, kernelType) << "For [" 126 << expectation.description << "]"; 127 } 128 } 129} 130 131} // namespace kernel 132} // namespace android 133