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> |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 25 | #include <memory> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 26 | #include <string> |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 27 | #include <vector> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 28 | |
| 29 | #include <android-base/file.h> |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 31 | #include <android/log.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <private/android_logger.h> |
| 34 | |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 35 | #include "minui/minui.h" |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 36 | #include "private/resources.h" |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 37 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 38 | static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt"; |
| 39 | static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n"; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 40 | static const std::string kLocale = "zu"; |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 41 | |
| 42 | // Failure is expected on systems that do not deliver either the |
| 43 | // recovery-persist or recovery-refresh executables. Tests also require |
| 44 | // a reboot sequence of test to truly verify. |
| 45 | |
| 46 | static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename, |
| 47 | const char *buf, size_t len, void *arg) { |
| 48 | EXPECT_EQ(LOG_ID_SYSTEM, logId); |
| 49 | EXPECT_EQ(ANDROID_LOG_INFO, prio); |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 50 | EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename)); |
| 51 | EXPECT_EQ(kInjectTxtContent, buf); |
| 52 | EXPECT_EQ(kInjectTxtContent.size(), len); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 53 | EXPECT_EQ(nullptr, arg); |
| 54 | return len; |
| 55 | } |
| 56 | |
| 57 | // recovery.refresh - May fail. Requires recovery.inject, two reboots, |
| 58 | // then expect success after second reboot. |
| 59 | TEST(recovery, refresh) { |
| 60 | EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK)); |
| 61 | |
| 62 | ssize_t ret = __android_log_pmsg_file_read( |
| 63 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); |
| 64 | if (ret == -ENOENT) { |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 65 | EXPECT_LT(0, __android_log_pmsg_file_write( |
| 66 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(), |
| 67 | kInjectTxtContent.c_str(), kInjectTxtContent.size())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 68 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 69 | fprintf(stderr, |
| 70 | "injected test data, requires two intervening reboots to check for replication\n"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 71 | } |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 72 | EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // recovery.persist - Requires recovery.inject, then a reboot, then |
| 76 | // expect success after for this test on that boot. |
| 77 | TEST(recovery, persist) { |
| 78 | EXPECT_EQ(0, access("/system/bin/recovery-persist", F_OK)); |
| 79 | |
| 80 | ssize_t ret = __android_log_pmsg_file_read( |
| 81 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); |
| 82 | if (ret == -ENOENT) { |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 83 | EXPECT_LT(0, __android_log_pmsg_file_write( |
| 84 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(), |
| 85 | kInjectTxtContent.c_str(), kInjectTxtContent.size())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 86 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 87 | fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | std::string buf; |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(android::base::ReadFileToString(kInjectTxtFilename, &buf)); |
| 92 | EXPECT_EQ(kInjectTxtContent, buf); |
| 93 | if (access(kInjectTxtFilename.c_str(), F_OK) == 0) { |
| 94 | fprintf(stderr, "Removing persistent test data, check if reconstructed on reboot\n"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 95 | } |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 96 | EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 97 | } |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 98 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 99 | static const std::vector<std::string> kResourceImagesDirs{ "res-mdpi/images/", "res-hdpi/images/", |
| 100 | "res-xhdpi/images/", |
| 101 | "res-xxhdpi/images/", |
| 102 | "res-xxxhdpi/images/" }; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 103 | |
| 104 | static int png_filter(const dirent* de) { |
| 105 | if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { |
| 106 | return 0; |
| 107 | } |
| 108 | return 1; |
| 109 | } |
| 110 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 111 | // Finds 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] | 112 | static std::vector<std::string> add_files() { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 113 | std::string exec_dir = android::base::GetExecutableDirectory(); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 114 | std::vector<std::string> files; |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 115 | for (const std::string& images_dir : kResourceImagesDirs) { |
| 116 | std::string dir_path = exec_dir + "/" + images_dir; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 117 | dirent** namelist; |
| 118 | int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); |
| 119 | if (n == -1) { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 120 | 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] | 121 | return files; |
| 122 | } |
| 123 | if (n == 0) { |
Tao Bao | 9d91e89 | 2018-03-30 20:43:45 -0700 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | |
| 127 | while (n--) { |
| 128 | std::string file_path = dir_path + namelist[n]->d_name; |
| 129 | files.push_back(file_path); |
| 130 | free(namelist[n]); |
| 131 | } |
| 132 | free(namelist); |
| 133 | } |
| 134 | return files; |
| 135 | } |
| 136 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 137 | class ResourcesTest : public testing::TestWithParam<std::string> { |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 138 | public: |
| 139 | static std::vector<std::string> png_list; |
| 140 | |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 141 | protected: |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 142 | void SetUp() override { |
| 143 | png_ = std::make_unique<PngHandler>(GetParam()); |
| 144 | ASSERT_TRUE(png_); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 145 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 146 | ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file."; |
| 147 | ASSERT_LT(static_cast<png_uint_32>(5), png_->width()); |
| 148 | ASSERT_LT(static_cast<png_uint_32>(0), png_->height()); |
| 149 | ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file."; |
| 150 | } |
| 151 | |
| 152 | std::unique_ptr<PngHandler> png_{ nullptr }; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 155 | // Parses a png file and tests if it's qualified for the background text image under recovery. |
| 156 | TEST_P(ResourcesTest, ValidateLocale) { |
| 157 | std::vector<unsigned char> row(png_->width()); |
| 158 | for (png_uint_32 y = 0; y < png_->height(); ++y) { |
| 159 | png_read_row(png_->png_ptr(), row.data(), nullptr); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 160 | int w = (row[1] << 8) | row[0]; |
| 161 | int h = (row[3] << 8) | row[2]; |
| 162 | int len = row[4]; |
| 163 | EXPECT_LT(0, w); |
| 164 | EXPECT_LT(0, h); |
| 165 | EXPECT_LT(0, len) << "Locale string should be non-empty."; |
| 166 | EXPECT_NE(0, row[5]) << "Locale string is missing."; |
| 167 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 168 | ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 169 | char* loc = reinterpret_cast<char*>(&row[5]); |
| 170 | if (matches_locale(loc, kLocale.c_str())) { |
Elliott Hughes | 1d65c95 | 2017-12-20 12:36:31 -0800 | [diff] [blame] | 171 | EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 172 | break; |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 173 | } |
| 174 | for (int i = 0; i < h; ++i, ++y) { |
| 175 | png_read_row(png_->png_ptr(), row.data(), nullptr); |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 180 | std::vector<std::string> ResourcesTest::png_list = add_files(); |
| 181 | |
| 182 | INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest, |
| 183 | ::testing::ValuesIn(ResourcesTest::png_list.cbegin(), |
| 184 | ResourcesTest::png_list.cend())); |