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