Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: boot_mode_unittest.cc

Issue 3530001: first cut: establishes base helper classes and main (Closed) Base URL: http://git.chromium.org/git/cros_boot_mode.git
Patch Set: truncation is bad, m'kay. . . Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « boot_mode_testrunner.cc ('k') | bootloader_type.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium OS 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 // Tests for cros_boot_mode::BootMode
6
7 #include "boot_mode.h"
8
9 #include <base/basictypes.h>
10 #include <base/file_util.h>
11 #include <base/stringprintf.h>
12 #include <gtest/gtest.h>
13 #include <string>
14
15 class BootModeTest : public ::testing::Test {
16 public:
17 void SetUp() {
18 EXPECT_TRUE(file_util::CreateNewTempDirectory("", &temp_dir_));
19
20 developer_switch_path_ = temp_dir_.value();
21 developer_switch_path_.append("/CHSW");
22 developer_switch_.set_platform_file_path(developer_switch_path_.c_str());
23 boot_mode_.set_developer_switch(&developer_switch_);
24
25 bootloader_type_path_ = temp_dir_.value();
26 bootloader_type_path_.append("/cmdline");
27 bootloader_type_.set_platform_file_path(bootloader_type_path_.c_str());
28 boot_mode_.set_bootloader_type(&bootloader_type_);
29
30 firmware_path_ = temp_dir_.value();
31 firmware_path_.append("/BINF.1");
32 active_main_firmware_.set_platform_file_path(firmware_path_.c_str());
33 boot_mode_.set_active_main_firmware(&active_main_firmware_);
34 }
35
36 ~BootModeTest() {
37 TearDown();
38 }
39 void TearDown() {
40 if (!temp_dir_.empty())
41 file_util::Delete(temp_dir_, true);
42 temp_dir_.clear();
43 }
44
45 // In SetUp, the temporary files were created and the paths injected
46 // into the underlying classes. This helper function wraps updating
47 // each file. |chsw| contains the dev mode switch value. |fw| contains
48 // the active firmware number, and the |cmdline| is the /proc/cmdline
49 // that the "system" booted in.
50 void UpdateFiles(int chsw, int fw, const char *cmdline) {
51 std::string data = StringPrintf("%d", chsw);
52 EXPECT_EQ(
53 file_util::WriteFile(FilePath(developer_switch_.platform_file_path()),
54 data.c_str(),
55 data.length()), data.length());
56
57 data = StringPrintf("%d", fw);
58 EXPECT_EQ(
59 file_util::WriteFile(FilePath(active_main_firmware_.platform_file_path()),
60 data.c_str(),
61 data.length()), data.length());
62
63 data.assign(cmdline);
64 EXPECT_EQ(
65 file_util::WriteFile(FilePath(bootloader_type_.platform_file_path()),
66 data.c_str(),
67 data.length()), data.length());
68
69 }
70
71 static const char *kUnsupportedText;
72 static const char *kNormalRecoveryText;
73 static const char *kNormalText;
74 static const char *kDeveloperText;
75 static const char *kDeveloperRecoveryText;
76 static const int kNormal = 0x0;
77 static const int kDeveloper = 0x32;
78 static const int kFwRecovery = 0;
79 static const int kFwA = 1;
80 static const int kFwB = 2;
81 static const char *kCrosDebug;
82 static const char *kCrosEfi;
83 static const char *kCrosLegacy;
84 static const char *kCrosSecure;
85 protected:
86 FilePath temp_dir_;
87 cros_boot_mode::BootMode boot_mode_;
88 cros_boot_mode::DeveloperSwitch developer_switch_;
89 std::string developer_switch_path_;
90 cros_boot_mode::ActiveMainFirmware active_main_firmware_;
91 std::string firmware_path_;
92 cros_boot_mode::BootloaderType bootloader_type_;
93 std::string bootloader_type_path_;
94 };
95
96 const char *BootModeTest::kUnsupportedText = "unsupported";
97 const char *BootModeTest::kNormalText = "normal";
98 const char *BootModeTest::kNormalRecoveryText = "normal recovery";
99 const char *BootModeTest::kDeveloperText = "developer";
100 const char *BootModeTest::kDeveloperRecoveryText = "developer recovery";
101 const char *BootModeTest::kCrosSecure =
102 "some nonsense dm=\"dacros_securesd ada\" cros_secure";
103 const char *BootModeTest::kCrosLegacy =
104 "some nonsense dm=\"dacros_securesd ada\" cros_legacy";
105 const char *BootModeTest::kCrosEfi =
106 "some nonsense dm=\"dacros_securesd ada\" cros_efi";
107 const char *BootModeTest::kCrosDebug =
108 "some nonsense dm=\"dacros_securesd ada\" cros_debug BOOT=hello";
109
110 TEST_F(BootModeTest, NoFilesUseBootloader) {
111 boot_mode_.Initialize(false, true);
112 EXPECT_EQ(cros_boot_mode::BootMode::kUnsupported, boot_mode_.mode());
113 EXPECT_STREQ(kUnsupportedText, boot_mode_.mode_text());
114 }
115
116 TEST_F(BootModeTest, UnsupportedAsDeveloper) {
117 boot_mode_.Initialize(true, true);
118 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloper, boot_mode_.mode());
119 EXPECT_STREQ(kDeveloperText, boot_mode_.mode_text());
120 }
121
122 TEST_F(BootModeTest, NormalRecoveryNothingIgnoreBootloader) {
123 UpdateFiles(kNormal, kFwRecovery, " some kernel noise ");
124 boot_mode_.Initialize(false, false);
125 EXPECT_EQ(cros_boot_mode::BootMode::kNormalRecovery, boot_mode_.mode());
126 EXPECT_STREQ(kNormalRecoveryText, boot_mode_.mode_text());
127 }
128
129 TEST_F(BootModeTest, NormalFwASecureUseBootloader) {
130 UpdateFiles(kNormal, kFwA, kCrosSecure);
131 boot_mode_.Initialize(false, true);
132 EXPECT_EQ(cros_boot_mode::BootMode::kNormal, boot_mode_.mode());
133 EXPECT_STREQ(kNormalText, boot_mode_.mode_text());
134 }
135
136 TEST_F(BootModeTest, NormalFwBSecureUseBootloader) {
137 UpdateFiles(kNormal, kFwB, kCrosSecure);
138 boot_mode_.Initialize(false, true);
139 EXPECT_EQ(cros_boot_mode::BootMode::kNormal, boot_mode_.mode());
140 EXPECT_STREQ(kNormalText, boot_mode_.mode_text());
141 }
142
143 TEST_F(BootModeTest, NormalFwASecureIgnoreBootloader) {
144 UpdateFiles(kNormal, kFwA, kCrosSecure);
145 boot_mode_.Initialize(false, false);
146 EXPECT_EQ(cros_boot_mode::BootMode::kNormal, boot_mode_.mode());
147 EXPECT_STREQ(kNormalText, boot_mode_.mode_text());
148 }
149
150 TEST_F(BootModeTest, NormalFwBSecureIgnoreBootloader) {
151 UpdateFiles(kNormal, kFwB, kCrosSecure);
152 boot_mode_.Initialize(false, false);
153 EXPECT_EQ(cros_boot_mode::BootMode::kNormal, boot_mode_.mode());
154 EXPECT_STREQ(kNormalText, boot_mode_.mode_text());
155 }
156
157 TEST_F(BootModeTest, BootloaderPreemptsHardwareUseBootloader) {
158 UpdateFiles(kNormal, kFwA, " some kernel noise ");
159 boot_mode_.Initialize(false, true);
160 EXPECT_EQ(cros_boot_mode::BootMode::kUnsupported, boot_mode_.mode());
161 EXPECT_STREQ(kUnsupportedText, boot_mode_.mode_text());
162 }
163
164 TEST_F(BootModeTest, BootloaderPreemptsHardwareUseBootloaderFwB) {
165 UpdateFiles(kNormal, kFwB, " some kernel noise ");
166 boot_mode_.Initialize(false, true);
167 EXPECT_EQ(cros_boot_mode::BootMode::kUnsupported, boot_mode_.mode());
168 EXPECT_STREQ(kUnsupportedText, boot_mode_.mode_text());
169 }
170
171 TEST_F(BootModeTest, NormalFwADebugUseBootloader) {
172 UpdateFiles(kNormal, kFwA, kCrosDebug);
173 boot_mode_.Initialize(false, true);
174 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloper, boot_mode_.mode());
175 EXPECT_STREQ(kDeveloperText, boot_mode_.mode_text());
176 }
177
178 TEST_F(BootModeTest, NormalFwBDebugUseBootloader) {
179 UpdateFiles(kNormal, kFwB, kCrosDebug);
180 boot_mode_.Initialize(false, true);
181 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloper, boot_mode_.mode());
182 EXPECT_STREQ(kDeveloperText, boot_mode_.mode_text());
183 }
184
185 TEST_F(BootModeTest, UnsupportedDebugUseBootloader) {
186 UpdateFiles(-1, -1, kCrosDebug);
187 boot_mode_.Initialize(false, true);
188 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloper, boot_mode_.mode());
189 EXPECT_STREQ(kDeveloperText, boot_mode_.mode_text());
190 }
191
192 TEST_F(BootModeTest, EfiIsUnsuppportedUseBootloader) {
193 UpdateFiles(-1, -1, kCrosEfi);
194 boot_mode_.Initialize(false, true);
195 EXPECT_EQ(cros_boot_mode::BootMode::kUnsupported, boot_mode_.mode());
196 EXPECT_STREQ(kUnsupportedText, boot_mode_.mode_text());
197 }
198
199 TEST_F(BootModeTest, LegacyIsUnsuppportedUseBootloader) {
200 UpdateFiles(-1, -1, kCrosLegacy);
201 boot_mode_.Initialize(false, true);
202 EXPECT_EQ(cros_boot_mode::BootMode::kUnsupported, boot_mode_.mode());
203 EXPECT_STREQ(kUnsupportedText, boot_mode_.mode_text());
204 }
205
206 TEST_F(BootModeTest, DeveloperFwASecureUseBootloader) {
207 UpdateFiles(kDeveloper, kFwA, kCrosSecure);
208 boot_mode_.Initialize(false, true);
209 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloper, boot_mode_.mode());
210 EXPECT_STREQ(kDeveloperText, boot_mode_.mode_text());
211 }
212
213 TEST_F(BootModeTest, DeveloperRecoverySecureUseBootloader) {
214 UpdateFiles(kDeveloper, kFwRecovery, kCrosSecure);
215 boot_mode_.Initialize(false, true);
216 EXPECT_EQ(cros_boot_mode::BootMode::kDeveloperRecovery, boot_mode_.mode());
217 EXPECT_STREQ(kDeveloperRecoveryText, boot_mode_.mode_text());
218 }
OLDNEW
« no previous file with comments | « boot_mode_testrunner.cc ('k') | bootloader_type.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698