blob: e1d0771e791b275e873bc0cb14f1ef5e5a8c1e47 [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
Tao Bao3d0cd1d2018-04-13 13:41:58 -070017#include <errno.h>
18#include <stdio.h>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080019#include <string.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Tao Bao3d0cd1d2018-04-13 13:41:58 -070023#include <memory>
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080024#include <string>
25
26#include <android-base/file.h>
27#include <android/log.h>
28#include <gtest/gtest.h>
29#include <private/android_logger.h>
30
Tao Bao3d0cd1d2018-04-13 13:41:58 -070031static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt";
32static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n";
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080033
34// Failure is expected on systems that do not deliver either the
35// recovery-persist or recovery-refresh executables. Tests also require
36// a reboot sequence of test to truly verify.
37
38static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename,
39 const char *buf, size_t len, void *arg) {
40 EXPECT_EQ(LOG_ID_SYSTEM, logId);
41 EXPECT_EQ(ANDROID_LOG_INFO, prio);
Tao Bao3d0cd1d2018-04-13 13:41:58 -070042 EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename));
43 EXPECT_EQ(kInjectTxtContent, buf);
44 EXPECT_EQ(kInjectTxtContent.size(), len);
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080045 EXPECT_EQ(nullptr, arg);
46 return len;
47}
48
49// recovery.refresh - May fail. Requires recovery.inject, two reboots,
50// then expect success after second reboot.
51TEST(recovery, refresh) {
52 EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK));
53
54 ssize_t ret = __android_log_pmsg_file_read(
55 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
56 if (ret == -ENOENT) {
Tao Bao3d0cd1d2018-04-13 13:41:58 -070057 EXPECT_LT(0, __android_log_pmsg_file_write(
58 LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
59 kInjectTxtContent.c_str(), kInjectTxtContent.size()));
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080060
Tao Bao3d0cd1d2018-04-13 13:41:58 -070061 fprintf(stderr,
62 "injected test data, requires two intervening reboots to check for replication\n");
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080063 }
Tao Bao3d0cd1d2018-04-13 13:41:58 -070064 EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret);
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080065}
66
67// recovery.persist - Requires recovery.inject, then a reboot, then
68// expect success after for this test on that boot.
69TEST(recovery, persist) {
70 EXPECT_EQ(0, access("/system/bin/recovery-persist", F_OK));
71
72 ssize_t ret = __android_log_pmsg_file_read(
73 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
74 if (ret == -ENOENT) {
Tao Bao3d0cd1d2018-04-13 13:41:58 -070075 EXPECT_LT(0, __android_log_pmsg_file_write(
76 LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
77 kInjectTxtContent.c_str(), kInjectTxtContent.size()));
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080078
Tao Bao3d0cd1d2018-04-13 13:41:58 -070079 fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n");
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080080 }
81
82 std::string buf;
Tao Bao3d0cd1d2018-04-13 13:41:58 -070083 EXPECT_TRUE(android::base::ReadFileToString(kInjectTxtFilename, &buf));
84 EXPECT_EQ(kInjectTxtContent, buf);
85 if (access(kInjectTxtFilename.c_str(), F_OK) == 0) {
86 fprintf(stderr, "Removing persistent test data, check if reconstructed on reboot\n");
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080087 }
Tao Bao3d0cd1d2018-04-13 13:41:58 -070088 EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str()));
Tianjie Xu5d8b53b2016-11-07 14:45:59 -080089}