blob: e73cb1509964a4f585fcf1158faca76a855533c5 [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 Xu5d8b53b2016-11-07 14:45:59 -080017#include <string.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <string>
22
23#include <android-base/file.h>
24#include <android/log.h>
25#include <gtest/gtest.h>
26#include <private/android_logger.h>
27
28static const std::string myFilename = "/data/misc/recovery/inject.txt";
29static const std::string myContent = "Hello World\nWelcome to my recovery\n";
30
31// Failure is expected on systems that do not deliver either the
32// recovery-persist or recovery-refresh executables. Tests also require
33// a reboot sequence of test to truly verify.
34
35static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename,
36 const char *buf, size_t len, void *arg) {
37 EXPECT_EQ(LOG_ID_SYSTEM, logId);
38 EXPECT_EQ(ANDROID_LOG_INFO, prio);
39 EXPECT_NE(std::string::npos, myFilename.find(filename));
40 EXPECT_EQ(myContent, buf);
41 EXPECT_EQ(myContent.size(), len);
42 EXPECT_EQ(nullptr, arg);
43 return len;
44}
45
46// recovery.refresh - May fail. Requires recovery.inject, two reboots,
47// then expect success after second reboot.
48TEST(recovery, refresh) {
49 EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK));
50
51 ssize_t ret = __android_log_pmsg_file_read(
52 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
53 if (ret == -ENOENT) {
54 EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
55 myFilename.c_str(), myContent.c_str(), myContent.size()));
56
57 fprintf(stderr, "injected test data, requires two intervening reboots "
58 "to check for replication\n");
59 }
60 EXPECT_EQ(static_cast<ssize_t>(myContent.size()), ret);
61}
62
63// recovery.persist - Requires recovery.inject, then a reboot, then
64// expect success after for this test on that boot.
65TEST(recovery, persist) {
66 EXPECT_EQ(0, access("/system/bin/recovery-persist", F_OK));
67
68 ssize_t ret = __android_log_pmsg_file_read(
69 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
70 if (ret == -ENOENT) {
71 EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
72 myFilename.c_str(), myContent.c_str(), myContent.size()));
73
74 fprintf(stderr, "injected test data, requires intervening reboot "
75 "to check for storage\n");
76 }
77
78 std::string buf;
79 EXPECT_TRUE(android::base::ReadFileToString(myFilename, &buf));
80 EXPECT_EQ(myContent, buf);
Tao Baoa3ece962016-12-21 18:44:20 -080081 if (access(myFilename.c_str(), F_OK) == 0) {
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080082 fprintf(stderr, "Removing persistent test data, "
83 "check if reconstructed on reboot\n");
84 }
85 EXPECT_EQ(0, unlink(myFilename.c_str()));
86}