adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | #include <gtest/gtest.h> |
| 11 | #include "update_engine/set_bootable_flag_action.h" |
| 12 | #include "update_engine/test_utils.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | using std::vector; |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | class SetBootableFlagActionProcessorDelegate : public ActionProcessorDelegate { |
| 21 | public: |
| 22 | SetBootableFlagActionProcessorDelegate() |
| 23 | : success_(false), success_set_(false) {} |
| 24 | void ActionCompleted(ActionProcessor* processor, |
| 25 | AbstractAction* action, |
| 26 | bool success) { |
| 27 | if (action->Type() == SetBootableFlagAction::StaticType()) { |
| 28 | success_ = success; |
| 29 | success_set_ = true; |
| 30 | } |
| 31 | } |
| 32 | bool success_; |
| 33 | bool success_set_; |
| 34 | }; |
| 35 | |
| 36 | class SetBootableFlagActionTest : public ::testing::Test { |
| 37 | public: |
| 38 | SetBootableFlagActionTest(); |
| 39 | protected: |
| 40 | enum TestFlags { |
| 41 | EXPECT_SUCCESS = 0x01, |
| 42 | WRITE_FILE = 0x02, |
| 43 | SKIP_INPUT_OBJECT = 0x04 |
| 44 | }; |
| 45 | static const char* kTestDir; |
| 46 | virtual void SetUp() { |
| 47 | EXPECT_EQ(0, mkdir(kTestDir, 0755)); |
| 48 | } |
| 49 | virtual void TearDown() { |
| 50 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 51 | } |
| 52 | vector<char> DoTest(vector<char> mbr_in, |
| 53 | const string& filename, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 54 | uint32_t test_flags); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | // first partition bootable, no others bootable |
| 56 | const vector<char> sample_mbr_; |
| 57 | }; |
| 58 | |
| 59 | const char* SetBootableFlagActionTest::kTestDir = |
| 60 | "SetBootableFlagActionTestDir"; |
| 61 | |
| 62 | SetBootableFlagActionTest::SetBootableFlagActionTest() |
| 63 | : sample_mbr_(GenerateSampleMbr()) {} |
| 64 | |
| 65 | vector<char> SetBootableFlagActionTest::DoTest(vector<char> mbr_in, |
| 66 | const string& filename, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 67 | uint32_t flags) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | CHECK(!filename.empty()); |
| 69 | const string root_filename(filename.begin(), --filename.end()); |
| 70 | if (flags & WRITE_FILE) |
| 71 | EXPECT_TRUE(WriteFileVector(root_filename, mbr_in)); |
| 72 | |
| 73 | ActionProcessor processor; |
| 74 | SetBootableFlagActionProcessorDelegate delegate; |
| 75 | processor.set_delegate(&delegate); |
| 76 | |
| 77 | ObjectFeederAction<string> feeder_action; |
| 78 | feeder_action.set_obj(filename); |
| 79 | SetBootableFlagAction set_bootable_action; |
| 80 | if (!(flags & SKIP_INPUT_OBJECT)) |
| 81 | BondActions(&feeder_action, &set_bootable_action); |
| 82 | ObjectCollectorAction<string> collector_action; |
| 83 | BondActions(&set_bootable_action, &collector_action); |
| 84 | processor.EnqueueAction(&feeder_action); |
| 85 | processor.EnqueueAction(&set_bootable_action); |
| 86 | processor.EnqueueAction(&collector_action); |
| 87 | processor.StartProcessing(); |
| 88 | EXPECT_TRUE(!processor.IsRunning()) |
| 89 | << "Update test to handle non-asynch actions"; |
| 90 | |
| 91 | EXPECT_TRUE(delegate.success_set_); |
| 92 | EXPECT_EQ(flags & EXPECT_SUCCESS, delegate.success_); |
| 93 | |
| 94 | vector<char> new_mbr; |
| 95 | if (flags & WRITE_FILE) |
| 96 | utils::ReadFile(root_filename, &new_mbr); |
| 97 | |
| 98 | unlink(root_filename.c_str()); |
| 99 | return new_mbr; |
| 100 | } |
| 101 | |
| 102 | TEST_F(SetBootableFlagActionTest, SimpleTest) { |
| 103 | for (int i = 0; i < 4; i++) { |
| 104 | vector<char> expected_new_mbr = sample_mbr_; |
| 105 | for (int j = 0; j < 4; j++) |
| 106 | expected_new_mbr[446 + 16 * j] = '\0'; // mark non-bootable |
| 107 | expected_new_mbr[446 + 16 * i] = 0x80; // mark bootable |
| 108 | |
| 109 | string filename(string(kTestDir) + "/SetBootableFlagActionTest.devX"); |
| 110 | filename[filename.size() - 1] = '1' + i; |
| 111 | |
| 112 | vector<char> actual_new_mbr = DoTest(expected_new_mbr, filename, |
| 113 | EXPECT_SUCCESS | WRITE_FILE); |
| 114 | |
| 115 | ExpectVectorsEq(expected_new_mbr, actual_new_mbr); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | TEST_F(SetBootableFlagActionTest, BadDeviceTest) { |
| 120 | vector<char> actual_new_mbr = DoTest(sample_mbr_, |
| 121 | string(kTestDir) + |
| 122 | "SetBootableFlagActionTest.dev5", |
| 123 | WRITE_FILE); |
| 124 | ExpectVectorsEq(sample_mbr_, actual_new_mbr); // no change |
| 125 | |
| 126 | actual_new_mbr = DoTest(sample_mbr_, |
| 127 | string(kTestDir) + "SetBootableFlagActionTest.dev13", |
| 128 | WRITE_FILE); |
| 129 | ExpectVectorsEq(sample_mbr_, actual_new_mbr); // no change |
| 130 | |
| 131 | actual_new_mbr = DoTest(sample_mbr_, |
| 132 | "/some/nonexistent/file3", |
| 133 | 0); |
| 134 | EXPECT_TRUE(actual_new_mbr.empty()); |
| 135 | |
| 136 | vector<char> bad_mbr = sample_mbr_; |
| 137 | bad_mbr[510] = 'x'; // makes signature invalid |
| 138 | |
| 139 | actual_new_mbr = DoTest(bad_mbr, |
| 140 | string(kTestDir) + "SetBootableFlagActionTest.dev2", |
| 141 | WRITE_FILE); |
| 142 | ExpectVectorsEq(bad_mbr, actual_new_mbr); // no change |
| 143 | } |
| 144 | |
| 145 | TEST_F(SetBootableFlagActionTest, NoInputObjectTest) { |
| 146 | vector<char> actual_new_mbr = DoTest(sample_mbr_, |
| 147 | string(kTestDir) + |
| 148 | "SetBootableFlagActionTest.dev5", |
| 149 | WRITE_FILE | SKIP_INPUT_OBJECT); |
| 150 | ExpectVectorsEq(sample_mbr_, actual_new_mbr); // no change |
| 151 | } |
| 152 | |
| 153 | } // namespace chromeos_update_engine |