blob: 224ed5b683c3e8372607bfe59a92d9e84ac0b23f [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>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080018#include <string.h>
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <string>
Tianjie Xud17a6882017-01-09 11:18:29 -080023#include <vector>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080024
25#include <android-base/file.h>
Tianjie Xud17a6882017-01-09 11:18:29 -080026#include <android-base/strings.h>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080027#include <android/log.h>
28#include <gtest/gtest.h>
Tianjie Xud17a6882017-01-09 11:18:29 -080029#include <png.h>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080030#include <private/android_logger.h>
31
Tianjie Xud17a6882017-01-09 11:18:29 -080032#include "minui/minui.h"
33
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080034static const std::string myFilename = "/data/misc/recovery/inject.txt";
35static const std::string myContent = "Hello World\nWelcome to my recovery\n";
Tianjie Xud17a6882017-01-09 11:18:29 -080036static const std::string kLocale = "zu";
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080037
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
42static 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.
55TEST(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.
72TEST(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 Baoa3ece962016-12-21 18:44:20 -080088 if (access(myFilename.c_str(), F_OK) == 0) {
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080089 fprintf(stderr, "Removing persistent test data, "
90 "check if reconstructed on reboot\n");
91 }
92 EXPECT_EQ(0, unlink(myFilename.c_str()));
93}
Tianjie Xud17a6882017-01-09 11:18:29 -080094
95std::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
103static 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 Bao9d91e892018-03-30 20:43:45 -0700110// Find out all the PNG files to test, which stay under the same dir with the executable.
Tianjie Xud17a6882017-01-09 11:18:29 -0800111static std::vector<std::string> add_files() {
Tao Bao9d91e892018-03-30 20:43:45 -0700112 std::string exec_dir = android::base::GetExecutableDirectory();
Tianjie Xud17a6882017-01-09 11:18:29 -0800113 std::vector<std::string> files;
Tao Bao9d91e892018-03-30 20:43:45 -0700114 for (const std::string& image : image_dir) {
115 std::string dir_path = exec_dir + "/" + image;
Tianjie Xud17a6882017-01-09 11:18:29 -0800116 dirent** namelist;
117 int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
118 if (n == -1) {
Tao Bao9d91e892018-03-30 20:43:45 -0700119 printf("Failed to scan dir %s: %s\n", exec_dir.c_str(), strerror(errno));
Tianjie Xud17a6882017-01-09 11:18:29 -0800120 return files;
121 }
122 if (n == 0) {
Tao Bao9d91e892018-03-30 20:43:45 -0700123 printf("No file is added for test in %s\n", exec_dir.c_str());
Tianjie Xud17a6882017-01-09 11:18:29 -0800124 }
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
136class 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 Xude6735e2017-07-10 15:13:33 -0700144 fp = fopen(file_path.c_str(), "rbe");
Tianjie Xud17a6882017-01-09 11:18:29 -0800145 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
195std::vector<std::string> ResourceTest::png_list = add_files();
196
197TEST_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 Hughes1d65c952017-12-20 12:36:31 -0800212 EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
Tianjie Xud17a6882017-01-09 11:18:29 -0800213 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
222INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourceTest,
223 ::testing::ValuesIn(ResourceTest::png_list.cbegin(),
224 ResourceTest::png_list.cend()));