blob: d36dd331ed778d93770b79aaf2486383d9358c06 [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";
37static const std::string kResourceTestDir = "/data/nativetest/recovery/";
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080038
39// Failure is expected on systems that do not deliver either the
40// recovery-persist or recovery-refresh executables. Tests also require
41// a reboot sequence of test to truly verify.
42
43static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename,
44 const char *buf, size_t len, void *arg) {
45 EXPECT_EQ(LOG_ID_SYSTEM, logId);
46 EXPECT_EQ(ANDROID_LOG_INFO, prio);
47 EXPECT_NE(std::string::npos, myFilename.find(filename));
48 EXPECT_EQ(myContent, buf);
49 EXPECT_EQ(myContent.size(), len);
50 EXPECT_EQ(nullptr, arg);
51 return len;
52}
53
54// recovery.refresh - May fail. Requires recovery.inject, two reboots,
55// then expect success after second reboot.
56TEST(recovery, refresh) {
57 EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK));
58
59 ssize_t ret = __android_log_pmsg_file_read(
60 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
61 if (ret == -ENOENT) {
62 EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
63 myFilename.c_str(), myContent.c_str(), myContent.size()));
64
65 fprintf(stderr, "injected test data, requires two intervening reboots "
66 "to check for replication\n");
67 }
68 EXPECT_EQ(static_cast<ssize_t>(myContent.size()), ret);
69}
70
71// recovery.persist - Requires recovery.inject, then a reboot, then
72// expect success after for this test on that boot.
73TEST(recovery, persist) {
74 EXPECT_EQ(0, access("/system/bin/recovery-persist", F_OK));
75
76 ssize_t ret = __android_log_pmsg_file_read(
77 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
78 if (ret == -ENOENT) {
79 EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
80 myFilename.c_str(), myContent.c_str(), myContent.size()));
81
82 fprintf(stderr, "injected test data, requires intervening reboot "
83 "to check for storage\n");
84 }
85
86 std::string buf;
87 EXPECT_TRUE(android::base::ReadFileToString(myFilename, &buf));
88 EXPECT_EQ(myContent, buf);
Tao Baoa3ece962016-12-21 18:44:20 -080089 if (access(myFilename.c_str(), F_OK) == 0) {
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080090 fprintf(stderr, "Removing persistent test data, "
91 "check if reconstructed on reboot\n");
92 }
93 EXPECT_EQ(0, unlink(myFilename.c_str()));
94}
Tianjie Xud17a6882017-01-09 11:18:29 -080095
96std::vector<std::string> image_dir {
97 "res-mdpi/images/",
98 "res-hdpi/images/",
99 "res-xhdpi/images/",
100 "res-xxhdpi/images/",
101 "res-xxxhdpi/images/"
102};
103
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
111// Find out all png files to test under /data/nativetest/recovery/.
112static std::vector<std::string> add_files() {
113 std::vector<std::string> files;
114 for (const std::string& str : image_dir) {
115 std::string dir_path = kResourceTestDir + str;
116 dirent** namelist;
117 int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
118 if (n == -1) {
119 printf("Failed to scan dir %s: %s\n", kResourceTestDir.c_str(), strerror(errno));
120 return files;
121 }
122 if (n == 0) {
123 printf("No file is added for test in %s\n", kResourceTestDir.c_str());
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
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();
144 fp = fopen(file_path.c_str(), "rb");
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
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())) {
212 EXPECT_TRUE(android::base::StartsWith(loc, kLocale.c_str()));
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
222INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourceTest,
223 ::testing::ValuesIn(ResourceTest::png_list.cbegin(),
224 ResourceTest::png_list.cend()));