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