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