Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 18 | #include <string.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <string> |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 23 | #include <vector> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 24 | |
| 25 | #include <android-base/file.h> |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 26 | #include <android-base/strings.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 27 | #include <android/log.h> |
| 28 | #include <gtest/gtest.h> |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 29 | #include <png.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 30 | #include <private/android_logger.h> |
| 31 | |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 32 | #include "minui/minui.h" |
| 33 | |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 34 | static const std::string myFilename = "/data/misc/recovery/inject.txt"; |
| 35 | static const std::string myContent = "Hello World\nWelcome to my recovery\n"; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 36 | static const std::string kLocale = "zu"; |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 37 | |
| 38 | // Failure is expected on systems that do not deliver either the |
| 39 | // recovery-persist or recovery-refresh executables. Tests also require |
| 40 | // a reboot sequence of test to truly verify. |
| 41 | |
| 42 | static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename, |
| 43 | const char *buf, size_t len, void *arg) { |
| 44 | EXPECT_EQ(LOG_ID_SYSTEM, logId); |
| 45 | EXPECT_EQ(ANDROID_LOG_INFO, prio); |
| 46 | EXPECT_NE(std::string::npos, myFilename.find(filename)); |
| 47 | EXPECT_EQ(myContent, buf); |
| 48 | EXPECT_EQ(myContent.size(), len); |
| 49 | EXPECT_EQ(nullptr, arg); |
| 50 | return len; |
| 51 | } |
| 52 | |
| 53 | // recovery.refresh - May fail. Requires recovery.inject, two reboots, |
| 54 | // then expect success after second reboot. |
| 55 | TEST(recovery, refresh) { |
| 56 | EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK)); |
| 57 | |
| 58 | ssize_t ret = __android_log_pmsg_file_read( |
| 59 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); |
| 60 | if (ret == -ENOENT) { |
| 61 | EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, |
| 62 | myFilename.c_str(), myContent.c_str(), myContent.size())); |
| 63 | |
| 64 | fprintf(stderr, "injected test data, requires two intervening reboots " |
| 65 | "to check for replication\n"); |
| 66 | } |
| 67 | EXPECT_EQ(static_cast<ssize_t>(myContent.size()), ret); |
| 68 | } |
| 69 | |
| 70 | // recovery.persist - Requires recovery.inject, then a reboot, then |
| 71 | // expect success after for this test on that boot. |
| 72 | TEST(recovery, persist) { |
| 73 | EXPECT_EQ(0, access("/system/bin/recovery-persist", F_OK)); |
| 74 | |
| 75 | ssize_t ret = __android_log_pmsg_file_read( |
| 76 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); |
| 77 | if (ret == -ENOENT) { |
| 78 | EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, |
| 79 | myFilename.c_str(), myContent.c_str(), myContent.size())); |
| 80 | |
| 81 | fprintf(stderr, "injected test data, requires intervening reboot " |
| 82 | "to check for storage\n"); |
| 83 | } |
| 84 | |
| 85 | std::string buf; |
| 86 | EXPECT_TRUE(android::base::ReadFileToString(myFilename, &buf)); |
| 87 | EXPECT_EQ(myContent, buf); |
Tao Bao | a3ece96 | 2016-12-21 18:44:20 -0800 | [diff] [blame] | 88 | if (access(myFilename.c_str(), F_OK) == 0) { |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 89 | fprintf(stderr, "Removing persistent test data, " |
| 90 | "check if reconstructed on reboot\n"); |
| 91 | } |
| 92 | EXPECT_EQ(0, unlink(myFilename.c_str())); |
| 93 | } |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 94 | |
| 95 | std::vector<std::string> image_dir { |
| 96 | "res-mdpi/images/", |
| 97 | "res-hdpi/images/", |
| 98 | "res-xhdpi/images/", |
| 99 | "res-xxhdpi/images/", |
| 100 | "res-xxxhdpi/images/" |
| 101 | }; |
| 102 | |
| 103 | static int png_filter(const dirent* de) { |
| 104 | if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { |
| 105 | return 0; |
| 106 | } |
| 107 | return 1; |
| 108 | } |
| 109 | |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 110 | // Find out all the PNG files to test, which stay under the same dir with the executable. |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 111 | static std::vector<std::string> add_files() { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 112 | std::string exec_dir = android::base::GetExecutableDirectory(); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 113 | std::vector<std::string> files; |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 114 | for (const std::string& image : image_dir) { |
| 115 | std::string dir_path = exec_dir + "/" + image; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 116 | dirent** namelist; |
| 117 | int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); |
| 118 | if (n == -1) { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 119 | printf("Failed to scan dir %s: %s\n", exec_dir.c_str(), strerror(errno)); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 120 | return files; |
| 121 | } |
| 122 | if (n == 0) { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 123 | printf("No file is added for test in %s\n", exec_dir.c_str()); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | while (n--) { |
| 127 | std::string file_path = dir_path + namelist[n]->d_name; |
| 128 | files.push_back(file_path); |
| 129 | free(namelist[n]); |
| 130 | } |
| 131 | free(namelist); |
| 132 | } |
| 133 | return files; |
| 134 | } |
| 135 | |
| 136 | class ResourceTest : public testing::TestWithParam<std::string> { |
| 137 | public: |
| 138 | static std::vector<std::string> png_list; |
| 139 | |
| 140 | // Parse a png file and test if it's qualified for the background text image |
| 141 | // under recovery. |
| 142 | void SetUp() override { |
| 143 | std::string file_path = GetParam(); |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 144 | fp = fopen(file_path.c_str(), "rbe"); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 145 | ASSERT_NE(nullptr, fp); |
| 146 | |
| 147 | unsigned char header[8]; |
| 148 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 149 | ASSERT_EQ(sizeof(header), bytesRead); |
| 150 | ASSERT_EQ(0, png_sig_cmp(header, 0, sizeof(header))); |
| 151 | |
| 152 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 153 | ASSERT_NE(nullptr, png_ptr); |
| 154 | |
| 155 | info_ptr = png_create_info_struct(png_ptr); |
| 156 | ASSERT_NE(nullptr, info_ptr); |
| 157 | |
| 158 | png_init_io(png_ptr, fp); |
| 159 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 160 | png_read_info(png_ptr, info_ptr); |
| 161 | |
| 162 | int color_type, bit_depth; |
| 163 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, nullptr, nullptr, |
| 164 | nullptr); |
| 165 | ASSERT_EQ(PNG_COLOR_TYPE_GRAY, color_type) << "Recovery expects grayscale PNG file."; |
| 166 | ASSERT_LT(static_cast<png_uint_32>(5), width); |
| 167 | ASSERT_LT(static_cast<png_uint_32>(0), height); |
| 168 | if (bit_depth <= 8) { |
| 169 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 170 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 171 | } |
| 172 | |
| 173 | png_byte channels = png_get_channels(png_ptr, info_ptr); |
| 174 | ASSERT_EQ(1, channels) << "Recovery background text images expects 1-channel PNG file."; |
| 175 | } |
| 176 | |
| 177 | void TearDown() override { |
| 178 | if (png_ptr != nullptr && info_ptr != nullptr) { |
| 179 | png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); |
| 180 | } |
| 181 | |
| 182 | if (fp != nullptr) { |
| 183 | fclose(fp); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | protected: |
| 188 | png_structp png_ptr; |
| 189 | png_infop info_ptr; |
| 190 | png_uint_32 width, height; |
| 191 | |
| 192 | FILE* fp; |
| 193 | }; |
| 194 | |
| 195 | std::vector<std::string> ResourceTest::png_list = add_files(); |
| 196 | |
| 197 | TEST_P(ResourceTest, ValidateLocale) { |
| 198 | std::vector<unsigned char> row(width); |
| 199 | for (png_uint_32 y = 0; y < height; ++y) { |
| 200 | png_read_row(png_ptr, row.data(), nullptr); |
| 201 | int w = (row[1] << 8) | row[0]; |
| 202 | int h = (row[3] << 8) | row[2]; |
| 203 | int len = row[4]; |
| 204 | EXPECT_LT(0, w); |
| 205 | EXPECT_LT(0, h); |
| 206 | EXPECT_LT(0, len) << "Locale string should be non-empty."; |
| 207 | EXPECT_NE(0, row[5]) << "Locale string is missing."; |
| 208 | |
| 209 | ASSERT_GT(height, y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; |
| 210 | char* loc = reinterpret_cast<char*>(&row[5]); |
| 211 | if (matches_locale(loc, kLocale.c_str())) { |
Elliott Hughes | 1d65c95 | 2017-12-20 12:36:31 -0800 | [diff] [blame] | 212 | EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 213 | break; |
| 214 | } else { |
| 215 | for (int i = 0; i < h; ++i, ++y) { |
| 216 | png_read_row(png_ptr, row.data(), nullptr); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourceTest, |
| 223 | ::testing::ValuesIn(ResourceTest::png_list.cbegin(), |
| 224 | ResourceTest::png_list.cend())); |