Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #define LOG_TAG "incidentd" |
| 16 | |
| 17 | #include "Section.h" |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/test_utils.h> |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <string.h> |
| 24 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 25 | const int QUICK_TIMEOUT_MS = 100; |
| 26 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 27 | using namespace android::base; |
| 28 | using namespace std; |
| 29 | using ::testing::StrEq; |
| 30 | using ::testing::internal::CaptureStdout; |
| 31 | using ::testing::internal::GetCapturedStdout; |
| 32 | |
| 33 | // NOTICE: this test requires /system/bin/incident_helper is installed. |
| 34 | TEST(SectionTest, WriteHeader) { |
| 35 | int id = 13; // expect output is 13 << 3 & 2 = 106 --> \x6a in ASCII |
| 36 | FileSection s(id, ""); // ignore the path, just used to test the header |
| 37 | ReportRequestSet requests; |
| 38 | |
| 39 | requests.setMainFd(STDOUT_FILENO); |
| 40 | |
| 41 | CaptureStdout(); |
| 42 | ASSERT_EQ(NO_ERROR, s.WriteHeader(&requests, 300)); |
| 43 | // According to protobuf encoding, 300 is "1010 1100 0000 0010" -> \xac \x02 |
| 44 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x6a\xac\x02")); |
| 45 | } |
| 46 | |
| 47 | TEST(SectionTest, FileSection) { |
| 48 | TemporaryFile tf; |
| 49 | FileSection fs(0, tf.path); |
| 50 | ReportRequestSet requests; |
| 51 | |
| 52 | ASSERT_TRUE(tf.fd != -1); |
| 53 | ASSERT_TRUE(WriteStringToFile("iamtestdata", tf.path, false)); |
| 54 | |
| 55 | requests.setMainFd(STDOUT_FILENO); |
| 56 | |
| 57 | CaptureStdout(); |
| 58 | ASSERT_EQ(NO_ERROR, fs.Execute(&requests)); |
| 59 | // The input string is reversed in incident helper |
| 60 | // The length is 11, in 128Varint it is "0000 1011" -> \v |
| 61 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\vatadtsetmai")); |
| 62 | } |
| 63 | |
| 64 | TEST(SectionTest, FileSectionTimeout) { |
| 65 | TemporaryFile tf; |
| 66 | // id -1 is timeout parser |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 67 | FileSection fs(-1, tf.path, QUICK_TIMEOUT_MS); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 68 | ReportRequestSet requests; |
| 69 | ASSERT_EQ(NO_ERROR, fs.Execute(&requests)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST(SectionTest, CommandSectionConstructor) { |
| 73 | CommandSection cs1(1, "echo", "\"this is a test\"", "ooo", NULL); |
| 74 | CommandSection cs2(2, "single_command", NULL); |
| 75 | CommandSection cs3(1, 3123, "echo", "\"this is a test\"", "ooo", NULL); |
| 76 | CommandSection cs4(2, 43214, "single_command", NULL); |
| 77 | |
| 78 | EXPECT_THAT(cs1.name.string(), StrEq("echo \"this is a test\" ooo")); |
| 79 | EXPECT_THAT(cs2.name.string(), StrEq("single_command")); |
| 80 | EXPECT_EQ(3123, cs3.timeoutMs); |
| 81 | EXPECT_EQ(43214, cs4.timeoutMs); |
| 82 | EXPECT_THAT(cs3.name.string(), StrEq("echo \"this is a test\" ooo")); |
| 83 | EXPECT_THAT(cs4.name.string(), StrEq("single_command")); |
| 84 | } |
| 85 | |
| 86 | TEST(SectionTest, CommandSectionEcho) { |
| 87 | CommandSection cs(0, "/system/bin/echo", "about", NULL); |
| 88 | ReportRequestSet requests; |
| 89 | requests.setMainFd(STDOUT_FILENO); |
| 90 | CaptureStdout(); |
| 91 | ASSERT_EQ(NO_ERROR, cs.Execute(&requests)); |
| 92 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\x06\ntuoba")); |
| 93 | } |
| 94 | |
| 95 | TEST(SectionTest, CommandSectionCommandTimeout) { |
| 96 | CommandSection cs(0, QUICK_TIMEOUT_MS, "/system/bin/yes", NULL); |
| 97 | ReportRequestSet requests; |
| 98 | ASSERT_EQ(NO_ERROR, cs.Execute(&requests)); |
| 99 | } |
| 100 | |
| 101 | TEST(SectionTest, CommandSectionIncidentHelperTimeout) { |
| 102 | CommandSection cs(-1, QUICK_TIMEOUT_MS, "/system/bin/echo", "about", NULL); |
| 103 | ReportRequestSet requests; |
| 104 | requests.setMainFd(STDOUT_FILENO); |
| 105 | ASSERT_EQ(NO_ERROR, cs.Execute(&requests)); |
| 106 | } |
| 107 | |
| 108 | TEST(SectionTest, CommandSectionBadCommand) { |
| 109 | CommandSection cs(0, "echo", "about", NULL); |
| 110 | ReportRequestSet requests; |
| 111 | ASSERT_EQ(NAME_NOT_FOUND, cs.Execute(&requests)); |
| 112 | } |
| 113 | |
| 114 | TEST(SectionTest, CommandSectionBadCommandAndTimeout) { |
| 115 | CommandSection cs(-1, QUICK_TIMEOUT_MS, "nonexistcommand", "-opt", NULL); |
| 116 | ReportRequestSet requests; |
| 117 | // timeout will return first |
| 118 | ASSERT_EQ(NO_ERROR, cs.Execute(&requests)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 119 | } |