Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 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 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 23 | #include <memory> |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 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 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 31 | static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt"; |
| 32 | static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n"; |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 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 | |
| 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); |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 42 | EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename)); |
| 43 | EXPECT_EQ(kInjectTxtContent, buf); |
| 44 | EXPECT_EQ(kInjectTxtContent.size(), len); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 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. |
| 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) { |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 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())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 60 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 61 | fprintf(stderr, |
| 62 | "injected test data, requires two intervening reboots to check for replication\n"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 63 | } |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 64 | EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // recovery.persist - Requires recovery.inject, then a reboot, then |
| 68 | // expect success after for this test on that boot. |
| 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) { |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 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())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 78 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 79 | fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::string buf; |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 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"); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 87 | } |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 88 | EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str())); |
Tianjie Xu | 5d8b53b | 2016-11-07 14:45:59 -0800 | [diff] [blame] | 89 | } |