David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 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 | * https://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 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 17 | #include <array> |
| 18 | #include <cstdio> |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 19 | #include <span> |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 20 | #include <sstream> |
| 21 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | |
| 24 | extern "C" { |
| 25 | #include "hf/manifest.h" |
| 26 | } |
| 27 | |
| 28 | namespace |
| 29 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 30 | using ::testing::ElementsAre; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 31 | using ::testing::Eq; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 32 | using ::testing::IsEmpty; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 33 | using ::testing::NotNull; |
| 34 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 35 | template <typename T> |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 36 | void exec(const char *program, const char *args[], const T &stdin, |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 37 | std::vector<char> *stdout) |
| 38 | { |
| 39 | /* Create two pipes, one for stdin and one for stdout. */ |
| 40 | int pipes[2][2]; |
| 41 | pipe(pipes[0]); |
| 42 | pipe(pipes[1]); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 43 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 44 | /* Assign FDs for reading/writing by the parent/child. */ |
| 45 | int parent_read_fd = pipes[1][0]; /* stdout pipe, read FD */ |
| 46 | int parent_write_fd = pipes[0][1]; /* stdin pipe, write FD */ |
| 47 | int child_read_fd = pipes[0][0]; /* stdin pipe, read FD */ |
| 48 | int child_write_fd = pipes[1][1]; /* stdout pipe, write FD */ |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 49 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 50 | if (fork()) { |
| 51 | /* Parent process. */ |
| 52 | std::array<char, 128> buf; |
| 53 | ssize_t res; |
| 54 | |
| 55 | /* Close child FDs which won't be used. */ |
| 56 | close(child_read_fd); |
| 57 | close(child_write_fd); |
| 58 | |
| 59 | /* Write to stdin. */ |
| 60 | for (size_t count = 0; count < stdin.size();) { |
| 61 | res = write(parent_write_fd, stdin.data() + count, |
| 62 | stdin.size() - count); |
| 63 | if (res < 0) { |
| 64 | std::cerr << "IO error" << std::endl; |
| 65 | exit(1); |
| 66 | } |
| 67 | count += res; |
| 68 | } |
| 69 | close(parent_write_fd); |
| 70 | |
| 71 | /* Read from stdout. */ |
| 72 | while (true) { |
| 73 | res = read(parent_read_fd, buf.data(), buf.size()); |
| 74 | if (res == 0) { |
| 75 | /* EOF */ |
| 76 | break; |
| 77 | } else if (res < 0) { |
| 78 | std::cerr << "IO error" << std::endl; |
| 79 | exit(1); |
| 80 | } |
| 81 | stdout->insert(stdout->end(), buf.begin(), |
| 82 | buf.begin() + res); |
| 83 | } |
| 84 | close(parent_read_fd); |
| 85 | } else { |
| 86 | /* Child process. */ |
| 87 | |
| 88 | /* Redirect stdin/stdout to read/write FDs. */ |
| 89 | dup2(child_read_fd, STDIN_FILENO); |
| 90 | dup2(child_write_fd, STDOUT_FILENO); |
| 91 | |
| 92 | /* Close all FDs which are now unused. */ |
| 93 | close(child_read_fd); |
| 94 | close(child_write_fd); |
| 95 | close(parent_read_fd); |
| 96 | close(parent_write_fd); |
| 97 | |
| 98 | /* Execute the given program. */ |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 99 | execv(program, const_cast<char *const *>(args)); |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Class for programatically building a Device Tree. |
| 105 | * |
| 106 | * Usage: |
| 107 | * std::vector<char> dtb = ManifestDtBuilder() |
| 108 | * .Command1() |
| 109 | * .Command2() |
| 110 | * ... |
| 111 | * .CommandN() |
| 112 | * .Build(); |
| 113 | */ |
| 114 | class ManifestDtBuilder |
| 115 | { |
| 116 | public: |
| 117 | ManifestDtBuilder() |
| 118 | { |
| 119 | dts_ << "/dts-v1/;" << std::endl; |
| 120 | dts_ << std::endl; |
| 121 | |
| 122 | /* Start root node. */ |
| 123 | StartChild("/"); |
| 124 | } |
| 125 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 126 | std::vector<char> Build(bool dump = false) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 127 | { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 128 | const char *program = "./build/image/dtc.py"; |
| 129 | const char *dtc_args[] = {program, "compile", NULL}; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 130 | std::vector<char> dtc_stdout; |
| 131 | |
| 132 | /* Finish root node. */ |
| 133 | EndChild(); |
| 134 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 135 | if (dump) { |
| 136 | Dump(); |
| 137 | } |
| 138 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 139 | exec(program, dtc_args, dts_.str(), &dtc_stdout); |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 140 | return dtc_stdout; |
| 141 | } |
| 142 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 143 | void Dump() |
| 144 | { |
| 145 | std::cerr << dts_.str() << std::endl; |
| 146 | } |
| 147 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 148 | ManifestDtBuilder &StartChild(const std::string_view &name) |
| 149 | { |
| 150 | dts_ << name << " {" << std::endl; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | ManifestDtBuilder &EndChild() |
| 155 | { |
| 156 | dts_ << "};" << std::endl; |
| 157 | return *this; |
| 158 | } |
| 159 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 160 | ManifestDtBuilder &Compatible(const std::vector<std::string_view> |
| 161 | &value = {"hafnium,hafnium"}) |
| 162 | { |
| 163 | return StringListProperty("compatible", value); |
| 164 | } |
| 165 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 166 | ManifestDtBuilder &DebugName(const std::string_view &value) |
| 167 | { |
| 168 | return StringProperty("debug_name", value); |
| 169 | } |
| 170 | |
| 171 | ManifestDtBuilder &KernelFilename(const std::string_view &value) |
| 172 | { |
| 173 | return StringProperty("kernel_filename", value); |
| 174 | } |
| 175 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 176 | ManifestDtBuilder &RamdiskFilename(const std::string_view &value) |
| 177 | { |
| 178 | return StringProperty("ramdisk_filename", value); |
| 179 | } |
| 180 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 181 | ManifestDtBuilder &VcpuCount(uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 182 | { |
| 183 | return IntegerProperty("vcpu_count", value); |
| 184 | } |
| 185 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 186 | ManifestDtBuilder &MemSize(uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 187 | { |
| 188 | return IntegerProperty("mem_size", value); |
| 189 | } |
| 190 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 191 | ManifestDtBuilder &SmcWhitelist(const std::vector<uint32_t> &value) |
| 192 | { |
| 193 | return IntegerListProperty("smc_whitelist", value); |
| 194 | } |
| 195 | |
| 196 | ManifestDtBuilder &SmcWhitelistPermissive() |
| 197 | { |
| 198 | return BooleanProperty("smc_whitelist_permissive"); |
| 199 | } |
| 200 | |
| 201 | ManifestDtBuilder &Property(const std::string_view &name, |
| 202 | const std::string_view &value) |
| 203 | { |
| 204 | dts_ << name << " = " << value << ";" << std::endl; |
| 205 | return *this; |
| 206 | } |
| 207 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 208 | private: |
| 209 | ManifestDtBuilder &StringProperty(const std::string_view &name, |
| 210 | const std::string_view &value) |
| 211 | { |
| 212 | dts_ << name << " = \"" << value << "\";" << std::endl; |
| 213 | return *this; |
| 214 | } |
| 215 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 216 | ManifestDtBuilder &StringListProperty( |
| 217 | const std::string_view &name, |
| 218 | const std::vector<std::string_view> &value) |
| 219 | { |
| 220 | bool is_first = true; |
| 221 | |
| 222 | dts_ << name << " = "; |
| 223 | for (const std::string_view &entry : value) { |
| 224 | if (is_first) { |
| 225 | is_first = false; |
| 226 | } else { |
| 227 | dts_ << ", "; |
| 228 | } |
| 229 | dts_ << "\"" << entry << "\""; |
| 230 | } |
| 231 | dts_ << ";" << std::endl; |
| 232 | return *this; |
| 233 | } |
| 234 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 235 | ManifestDtBuilder &IntegerProperty(const std::string_view &name, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 236 | uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 237 | { |
| 238 | dts_ << name << " = <" << value << ">;" << std::endl; |
| 239 | return *this; |
| 240 | } |
| 241 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 242 | ManifestDtBuilder &IntegerListProperty( |
| 243 | const std::string_view &name, |
| 244 | const std::vector<uint32_t> &value) |
| 245 | { |
| 246 | dts_ << name << " = < "; |
| 247 | for (const uint32_t entry : value) { |
| 248 | dts_ << entry << " "; |
| 249 | } |
| 250 | dts_ << ">;" << std::endl; |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | ManifestDtBuilder &BooleanProperty(const std::string_view &name) |
| 255 | { |
Andrew Scull | 5dc089e | 2019-11-04 13:21:03 +0000 | [diff] [blame] | 256 | dts_ << name << ";" << std::endl; |
| 257 | return *this; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 258 | } |
| 259 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 260 | std::stringstream dts_; |
| 261 | }; |
| 262 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 263 | static enum manifest_return_code manifest_from_vec(struct manifest *m, |
| 264 | const std::vector<char> &vec) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 265 | { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 266 | struct memiter it; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 267 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 268 | memiter_init(&it, vec.data(), vec.size()); |
| 269 | return manifest_init(m, &it); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 270 | } |
| 271 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 272 | TEST(manifest, no_hypervisor_node) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 273 | { |
| 274 | struct manifest m; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 275 | std::vector<char> dtb = ManifestDtBuilder().Build(); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 276 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 277 | ASSERT_EQ(manifest_from_vec(&m, dtb), |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 278 | MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE); |
| 279 | } |
| 280 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 281 | TEST(manifest, no_compatible_property) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 282 | { |
| 283 | struct manifest m; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 284 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 285 | /* clang-format off */ |
| 286 | std::vector<char> dtb = ManifestDtBuilder() |
| 287 | .StartChild("hypervisor") |
| 288 | .EndChild() |
| 289 | .Build(); |
| 290 | /* clang-format on */ |
| 291 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 292 | ASSERT_EQ(manifest_from_vec(&m, dtb), |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 293 | MANIFEST_ERROR_PROPERTY_NOT_FOUND); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 294 | } |
| 295 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 296 | TEST(manifest, not_compatible) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 297 | { |
| 298 | struct manifest m; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 299 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 300 | /* clang-format off */ |
| 301 | std::vector<char> dtb = ManifestDtBuilder() |
| 302 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 303 | .Compatible({ "foo,bar" }) |
| 304 | .EndChild() |
| 305 | .Build(); |
| 306 | /* clang-format on */ |
| 307 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 308 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_NOT_COMPATIBLE); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(manifest, compatible_one_of_many) |
| 312 | { |
| 313 | struct manifest m; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 314 | |
| 315 | /* clang-format off */ |
| 316 | std::vector<char> dtb = ManifestDtBuilder() |
| 317 | .StartChild("hypervisor") |
| 318 | .Compatible({ "foo,bar", "hafnium,hafnium" }) |
| 319 | .StartChild("vm1") |
| 320 | .DebugName("primary") |
| 321 | .EndChild() |
| 322 | .EndChild() |
| 323 | .Build(); |
| 324 | /* clang-format on */ |
| 325 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 326 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | TEST(manifest, no_vm_nodes) |
| 330 | { |
| 331 | struct manifest m; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 332 | |
| 333 | /* clang-format off */ |
| 334 | std::vector<char> dtb = ManifestDtBuilder() |
| 335 | .StartChild("hypervisor") |
| 336 | .Compatible() |
| 337 | .EndChild() |
| 338 | .Build(); |
| 339 | /* clang-format on */ |
| 340 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 341 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_NO_PRIMARY_VM); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static std::vector<char> gen_long_string_dtb(bool valid) |
| 345 | { |
| 346 | const char last_valid[] = "1234567890123456789012345678901"; |
| 347 | const char first_invalid[] = "12345678901234567890123456789012"; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 348 | static_assert(sizeof(last_valid) == STRING_MAX_SIZE); |
| 349 | static_assert(sizeof(first_invalid) == STRING_MAX_SIZE + 1); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 350 | |
| 351 | /* clang-format off */ |
| 352 | return ManifestDtBuilder() |
| 353 | .StartChild("hypervisor") |
| 354 | .Compatible() |
| 355 | .StartChild("vm1") |
| 356 | .DebugName(valid ? last_valid : first_invalid) |
| 357 | .EndChild() |
| 358 | .EndChild() |
| 359 | .Build(); |
| 360 | /* clang-format on */ |
| 361 | } |
| 362 | |
| 363 | TEST(manifest, long_string) |
| 364 | { |
| 365 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 366 | std::vector<char> dtb_last_valid = gen_long_string_dtb(true); |
| 367 | std::vector<char> dtb_first_invalid = gen_long_string_dtb(false); |
| 368 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 369 | ASSERT_EQ(manifest_from_vec(&m, dtb_last_valid), MANIFEST_SUCCESS); |
| 370 | ASSERT_EQ(manifest_from_vec(&m, dtb_first_invalid), |
| 371 | MANIFEST_ERROR_STRING_TOO_LONG); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | TEST(manifest, reserved_vm_id) |
| 375 | { |
| 376 | struct manifest m; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 377 | |
| 378 | /* clang-format off */ |
| 379 | std::vector<char> dtb = ManifestDtBuilder() |
| 380 | .StartChild("hypervisor") |
| 381 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 382 | .StartChild("vm1") |
| 383 | .DebugName("primary_vm") |
| 384 | .EndChild() |
| 385 | .StartChild("vm0") |
| 386 | .DebugName("reserved_vm") |
| 387 | .VcpuCount(1) |
| 388 | .MemSize(0x1000) |
| 389 | .KernelFilename("kernel") |
| 390 | .EndChild() |
| 391 | .EndChild() |
| 392 | .Build(); |
| 393 | /* clang-format on */ |
| 394 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 395 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_RESERVED_VM_ID); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 398 | static std::vector<char> gen_vcpu_count_limit_dtb(uint32_t vcpu_count) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 399 | { |
| 400 | /* clang-format off */ |
| 401 | return ManifestDtBuilder() |
| 402 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 403 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 404 | .StartChild("vm1") |
| 405 | .DebugName("primary_vm") |
| 406 | .EndChild() |
| 407 | .StartChild("vm2") |
| 408 | .DebugName("secondary_vm") |
| 409 | .VcpuCount(vcpu_count) |
| 410 | .MemSize(0x1000) |
| 411 | .KernelFilename("kernel") |
| 412 | .EndChild() |
| 413 | .EndChild() |
| 414 | .Build(); |
| 415 | /* clang-format on */ |
| 416 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 417 | |
| 418 | TEST(manifest, vcpu_count_limit) |
| 419 | { |
| 420 | struct manifest m; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 421 | std::vector<char> dtb_last_valid = gen_vcpu_count_limit_dtb(UINT16_MAX); |
| 422 | std::vector<char> dtb_first_invalid = |
| 423 | gen_vcpu_count_limit_dtb(UINT16_MAX + 1); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 424 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 425 | ASSERT_EQ(manifest_from_vec(&m, dtb_last_valid), MANIFEST_SUCCESS); |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 426 | ASSERT_EQ(m.vm_count, 2); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 427 | ASSERT_EQ(m.vm[1].secondary.vcpu_count, UINT16_MAX); |
| 428 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 429 | ASSERT_EQ(manifest_from_vec(&m, dtb_first_invalid), |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 430 | MANIFEST_ERROR_INTEGER_OVERFLOW); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 431 | } |
| 432 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 433 | TEST(manifest, no_ramdisk_primary) |
| 434 | { |
| 435 | struct manifest m; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 436 | |
| 437 | /* clang-format off */ |
| 438 | std::vector<char> dtb = ManifestDtBuilder() |
| 439 | .StartChild("hypervisor") |
| 440 | .Compatible() |
| 441 | .StartChild("vm1") |
| 442 | .DebugName("primary_vm") |
| 443 | .EndChild() |
| 444 | .EndChild() |
| 445 | .Build(); |
| 446 | /* clang-format on */ |
| 447 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 448 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 449 | ASSERT_EQ(m.vm_count, 1); |
| 450 | ASSERT_STREQ(string_data(&m.vm[0].debug_name), "primary_vm"); |
| 451 | ASSERT_STREQ(string_data(&m.vm[0].primary.ramdisk_filename), ""); |
| 452 | } |
| 453 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 454 | static std::vector<char> gen_malformed_boolean_dtb( |
| 455 | const std::string_view &value) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 456 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 457 | /* clang-format off */ |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 458 | return ManifestDtBuilder() |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 459 | .StartChild("hypervisor") |
| 460 | .Compatible() |
| 461 | .StartChild("vm1") |
| 462 | .DebugName("primary_vm") |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 463 | .Property("smc_whitelist_permissive", value) |
Andrew Scull | 5dc089e | 2019-11-04 13:21:03 +0000 | [diff] [blame] | 464 | .EndChild() |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 465 | .EndChild() |
| 466 | .Build(); |
| 467 | /* clang-format on */ |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 468 | } |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 469 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 470 | TEST(manifest, malformed_booleans) |
| 471 | { |
| 472 | struct manifest m; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 473 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 474 | std::vector<char> dtb_false = gen_malformed_boolean_dtb("\"false\""); |
| 475 | std::vector<char> dtb_true = gen_malformed_boolean_dtb("\"true\""); |
| 476 | std::vector<char> dtb_0 = gen_malformed_boolean_dtb("\"<0>\""); |
| 477 | std::vector<char> dtb_1 = gen_malformed_boolean_dtb("\"<1>\""); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 478 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 479 | ASSERT_EQ(manifest_from_vec(&m, dtb_false), |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 480 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 481 | ASSERT_EQ(manifest_from_vec(&m, dtb_true), |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 482 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 483 | ASSERT_EQ(manifest_from_vec(&m, dtb_0), |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 484 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 485 | ASSERT_EQ(manifest_from_vec(&m, dtb_1), |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame] | 486 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 487 | } |
| 488 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 489 | TEST(manifest, valid) |
| 490 | { |
| 491 | struct manifest m; |
| 492 | struct manifest_vm *vm; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 493 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 494 | /* clang-format off */ |
| 495 | std::vector<char> dtb = ManifestDtBuilder() |
| 496 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 497 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 498 | .StartChild("vm1") |
| 499 | .DebugName("primary_vm") |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 500 | .KernelFilename("primary_kernel") |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 501 | .RamdiskFilename("primary_ramdisk") |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 502 | .SmcWhitelist({0x32000000, 0x33001111}) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 503 | .EndChild() |
| 504 | .StartChild("vm3") |
| 505 | .DebugName("second_secondary_vm") |
| 506 | .VcpuCount(43) |
| 507 | .MemSize(0x12345) |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 508 | .KernelFilename("second_secondary_kernel") |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 509 | .EndChild() |
| 510 | .StartChild("vm2") |
| 511 | .DebugName("first_secondary_vm") |
| 512 | .VcpuCount(42) |
| 513 | .MemSize(12345) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 514 | .SmcWhitelist({0x04000000, 0x30002222, 0x31445566}) |
| 515 | .SmcWhitelistPermissive() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 516 | .EndChild() |
| 517 | .EndChild() |
| 518 | .Build(); |
| 519 | /* clang-format on */ |
| 520 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame^] | 521 | ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS); |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 522 | ASSERT_EQ(m.vm_count, 3); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 523 | |
| 524 | vm = &m.vm[0]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 525 | ASSERT_STREQ(string_data(&vm->debug_name), "primary_vm"); |
| 526 | ASSERT_STREQ(string_data(&vm->kernel_filename), "primary_kernel"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 527 | ASSERT_STREQ(string_data(&vm->primary.ramdisk_filename), |
| 528 | "primary_ramdisk"); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 529 | ASSERT_THAT( |
| 530 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 531 | ElementsAre(0x32000000, 0x33001111)); |
| 532 | ASSERT_FALSE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 533 | |
| 534 | vm = &m.vm[1]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 535 | ASSERT_STREQ(string_data(&vm->debug_name), "first_secondary_vm"); |
| 536 | ASSERT_STREQ(string_data(&vm->kernel_filename), ""); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 537 | ASSERT_EQ(vm->secondary.vcpu_count, 42); |
| 538 | ASSERT_EQ(vm->secondary.mem_size, 12345); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 539 | ASSERT_THAT( |
| 540 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 541 | ElementsAre(0x04000000, 0x30002222, 0x31445566)); |
| 542 | ASSERT_TRUE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 543 | |
| 544 | vm = &m.vm[2]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 545 | ASSERT_STREQ(string_data(&vm->debug_name), "second_secondary_vm"); |
| 546 | ASSERT_STREQ(string_data(&vm->kernel_filename), |
| 547 | "second_secondary_kernel"); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 548 | ASSERT_EQ(vm->secondary.vcpu_count, 43); |
| 549 | ASSERT_EQ(vm->secondary.mem_size, 0x12345); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 550 | ASSERT_THAT( |
| 551 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 552 | IsEmpty()); |
| 553 | ASSERT_FALSE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | } /* namespace */ |