blob: f397f258e4169d507af1fae66cfd5093df287e8a [file] [log] [blame]
Mark Salyzyna4f701a2016-03-09 14:58:16 -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 <android/log.h>
23#include <gtest/gtest.h>
24#include <log/logger.h>
25#include <private/android_logger.h>
26
27static const char myFilename[] = "/data/misc/recovery/inject.txt";
28static const char myContent[] = "Hello World\nWelcome to my recovery\n";
29
30// Failure is expected on systems that do not deliver either the
31// recovery-persist or recovery-refresh executables. Tests also require
32// a reboot sequence of test to truly verify.
33
34static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename,
35 const char *buf, size_t len, void *arg) {
36 EXPECT_EQ(LOG_ID_SYSTEM, logId);
37 EXPECT_EQ(ANDROID_LOG_INFO, prio);
38 EXPECT_EQ(0, NULL == strstr(myFilename,filename));
39 EXPECT_EQ(0, strcmp(myContent, buf));
40 EXPECT_EQ(sizeof(myContent), len);
41 EXPECT_EQ(0, NULL != arg);
42 return len;
43}
44
45// recovery.refresh - May fail. Requires recovery.inject, two reboots,
46// then expect success after second reboot.
47TEST(recovery, refresh) {
48 EXPECT_EQ(0, access("/system/bin/recovery-refresh", F_OK));
49
50 ssize_t ret = __android_log_pmsg_file_read(
51 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, NULL);
52 if (ret == -ENOENT) {
53 EXPECT_LT(0, __android_log_pmsg_file_write(
54 LOG_ID_SYSTEM, ANDROID_LOG_INFO,
55 myFilename, myContent, sizeof(myContent)));
56 fprintf(stderr, "injected test data, "
57 "requires two intervening reboots "
58 "to check for replication\n");
59 }
60 EXPECT_EQ((ssize_t)sizeof(myContent), 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, NULL);
70 if (ret == -ENOENT) {
71 EXPECT_LT(0, __android_log_pmsg_file_write(
72 LOG_ID_SYSTEM, ANDROID_LOG_INFO,
73 myFilename, myContent, sizeof(myContent)));
74 fprintf(stderr, "injected test data, "
75 "requires intervening reboot "
76 "to check for storage\n");
77 }
78
79 int fd = open(myFilename, O_RDONLY);
80 EXPECT_LE(0, fd);
81
82 char buf[sizeof(myContent) + 32];
83 ret = read(fd, buf, sizeof(buf));
84 close(fd);
85 EXPECT_EQ(ret, (ssize_t)sizeof(myContent));
86 EXPECT_EQ(0, strcmp(myContent, buf));
87 if (fd >= 0) {
88 fprintf(stderr, "Removing persistent test data, "
89 "check if reconstructed on reboot\n");
90 }
91 EXPECT_EQ(0, unlink(myFilename));
92}