1 /*
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 <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <memory>
24 #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 
31 static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt";
32 static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n";
33 
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 
__pmsg_fn(log_id_t logId,char prio,const char * filename,const char * buf,size_t len,void * arg)38 static 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);
42   EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename));
43   EXPECT_EQ(kInjectTxtContent, buf);
44   EXPECT_EQ(kInjectTxtContent.size(), len);
45   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.
TEST(recovery,refresh)51 TEST(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) {
57     EXPECT_LT(0, __android_log_pmsg_file_write(
58                      LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
59                      kInjectTxtContent.c_str(), kInjectTxtContent.size()));
60 
61     fprintf(stderr,
62             "injected test data, requires two intervening reboots to check for replication\n");
63   }
64   EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret);
65 }
66 
67 // recovery.persist - Requires recovery.inject, then a reboot, then
68 //                    expect success after for this test on that boot.
TEST(recovery,persist)69 TEST(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) {
75     EXPECT_LT(0, __android_log_pmsg_file_write(
76                      LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
77                      kInjectTxtContent.c_str(), kInjectTxtContent.size()));
78 
79     fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n");
80   }
81 
82   std::string buf;
83   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");
87   }
88   EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str()));
89 }
90