blob: c992b07e20ea255d1f176c26482bc2a738aa9fbf [file] [log] [blame]
Tianjie Xu5d8b53b2016-11-07 14:45:59 -08001/*
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 Xud17a6882017-01-09 11:18:29 -080017#include <dirent.h>
Tao Bao3d0cd1d2018-04-13 13:41:58 -070018#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080021#include <string.h>
22#include <sys/types.h>
23#include <unistd.h>
24
Tao Bao3d0cd1d2018-04-13 13:41:58 -070025#include <memory>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080026#include <string>
Tianjie Xud17a6882017-01-09 11:18:29 -080027#include <vector>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080028
29#include <android-base/file.h>
Tianjie Xud17a6882017-01-09 11:18:29 -080030#include <android-base/strings.h>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080031#include <android/log.h>
32#include <gtest/gtest.h>
33#include <private/android_logger.h>
34
Tianjie Xud17a6882017-01-09 11:18:29 -080035#include "minui/minui.h"
Tao Bao3d0cd1d2018-04-13 13:41:58 -070036#include "private/resources.h"
Tianjie Xud17a6882017-01-09 11:18:29 -080037
Tao Bao3d0cd1d2018-04-13 13:41:58 -070038static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt";
39static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n";
Tianjie Xud17a6882017-01-09 11:18:29 -080040static const std::string kLocale = "zu";
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080041
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
46static 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 Bao3d0cd1d2018-04-13 13:41:58 -070050 EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename));
51 EXPECT_EQ(kInjectTxtContent, buf);
52 EXPECT_EQ(kInjectTxtContent.size(), len);
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080053 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.
59TEST(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 Bao3d0cd1d2018-04-13 13:41:58 -070065 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 Xu5d8b53b2016-11-07 14:45:59 -080068
Tao Bao3d0cd1d2018-04-13 13:41:58 -070069 fprintf(stderr,
70 "injected test data, requires two intervening reboots to check for replication\n");
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080071 }
Tao Bao3d0cd1d2018-04-13 13:41:58 -070072 EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret);
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080073}
74
75// recovery.persist - Requires recovery.inject, then a reboot, then
76// expect success after for this test on that boot.
77TEST(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 Bao3d0cd1d2018-04-13 13:41:58 -070083 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 Xu5d8b53b2016-11-07 14:45:59 -080086
Tao Bao3d0cd1d2018-04-13 13:41:58 -070087 fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n");
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080088 }
89
90 std::string buf;
Tao Bao3d0cd1d2018-04-13 13:41:58 -070091 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 Xu5d8b53b2016-11-07 14:45:59 -080095 }
Tao Bao3d0cd1d2018-04-13 13:41:58 -070096 EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str()));
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080097}
Tianjie Xud17a6882017-01-09 11:18:29 -080098
Tao Bao3d0cd1d2018-04-13 13:41:58 -070099static 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 Xud17a6882017-01-09 11:18:29 -0800103
104static 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 Bao3d0cd1d2018-04-13 13:41:58 -0700111// Finds out all the PNG files to test, which stay under the same dir with the executable.
Tianjie Xud17a6882017-01-09 11:18:29 -0800112static std::vector<std::string> add_files() {
Tao Bao9d91e892018-03-30 20:43:45 -0700113 std::string exec_dir = android::base::GetExecutableDirectory();
Tianjie Xud17a6882017-01-09 11:18:29 -0800114 std::vector<std::string> files;
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700115 for (const std::string& images_dir : kResourceImagesDirs) {
116 std::string dir_path = exec_dir + "/" + images_dir;
Tianjie Xud17a6882017-01-09 11:18:29 -0800117 dirent** namelist;
118 int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
119 if (n == -1) {
Tao Bao9d91e892018-03-30 20:43:45 -0700120 printf("Failed to scan dir %s: %s\n", exec_dir.c_str(), strerror(errno));
Tianjie Xud17a6882017-01-09 11:18:29 -0800121 return files;
122 }
123 if (n == 0) {
Tao Bao9d91e892018-03-30 20:43:45 -0700124 printf("No file is added for test in %s\n", exec_dir.c_str());
Tianjie Xud17a6882017-01-09 11:18:29 -0800125 }
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 Bao3d0cd1d2018-04-13 13:41:58 -0700137class ResourcesTest : public testing::TestWithParam<std::string> {
Tianjie Xud17a6882017-01-09 11:18:29 -0800138 public:
139 static std::vector<std::string> png_list;
140
Tianjie Xud17a6882017-01-09 11:18:29 -0800141 protected:
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700142 void SetUp() override {
143 png_ = std::make_unique<PngHandler>(GetParam());
144 ASSERT_TRUE(png_);
Tianjie Xud17a6882017-01-09 11:18:29 -0800145
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700146 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 Xud17a6882017-01-09 11:18:29 -0800153};
154
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700155// Parses a png file and tests if it's qualified for the background text image under recovery.
156TEST_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 Xud17a6882017-01-09 11:18:29 -0800160 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 Bao3d0cd1d2018-04-13 13:41:58 -0700168 ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file.";
Tianjie Xud17a6882017-01-09 11:18:29 -0800169 char* loc = reinterpret_cast<char*>(&row[5]);
170 if (matches_locale(loc, kLocale.c_str())) {
Elliott Hughes1d65c952017-12-20 12:36:31 -0800171 EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
Tianjie Xud17a6882017-01-09 11:18:29 -0800172 break;
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700173 }
174 for (int i = 0; i < h; ++i, ++y) {
175 png_read_row(png_->png_ptr(), row.data(), nullptr);
Tianjie Xud17a6882017-01-09 11:18:29 -0800176 }
177 }
178}
179
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700180std::vector<std::string> ResourcesTest::png_list = add_files();
181
182INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
183 ::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
184 ResourcesTest::png_list.cend()));