1 // Copyright 2018 The Fuchsia 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 "parent.h"
6
7 #include <lib/devmgr-integration-test/fixture.h>
8 #include <unittest/unittest.h>
9 #include <zircon/hw/gpt.h>
10
11 constexpr fuchsia_hardware_nand_Info kNandInfo = {
12 .page_size = 4096,
13 .pages_per_block = 4,
14 .num_blocks = 5,
15 .ecc_bits = 6,
16 .oob_size = 4,
17 .nand_class = fuchsia_hardware_nand_Class_PARTMAP,
18 .partition_guid = {},
19 };
20
21 constexpr fuchsia_hardware_nand_PartitionMap kPartitionMap = {
22 .device_guid = {},
23 .partition_count = 1,
24 .partitions =
25 {
26 {
27 .type_guid = GUID_TEST_VALUE,
28 .unique_guid = {},
29 .first_block = 0,
30 .last_block = 4,
31 .copy_count = 0,
32 .copy_byte_offset = 0,
33 .name = {'t', 'e', 's', 't'},
34 .hidden = false,
35 .bbt = false,
36 },
37 },
38 };
39
40 // The test can operate over either a ram-nand, or a real device. The simplest
41 // way to control what's going on is to have a place outside the test framework
42 // that controls where to execute, as "creation / teardown" of the external
43 // device happens at the process level.
44 ParentDevice* g_parent_device_;
45
main(int argc,char ** argv)46 int main(int argc, char** argv) {
47 ParentDevice::TestConfig config = {};
48 config.info = kNandInfo;
49 config.partition_map = kPartitionMap;
50
51 ParentDevice parent(config);
52 if (!parent.IsValid()) {
53 printf("Unable to create ram-nand device\n");
54 return -1;
55 }
56
57 // TODO(ZX-3193)
58 #if OPENAT_FIXED
59 // Wait for nandpart to spawn.
60 fbl::unique_fd dir(dup(parent.get()));
61 fbl::unique_fd nandpart;
62 zx_status_t status = devmgr_integration_test::WaitForFile(
63 dir, "test", zx::deadline_after(zx::sec(1)), &nandpart);
64 if (status != ZX_OK) {
65 printf("Unable to attach to device: %d\n", status);
66 return -1;
67 }
68 #else
69 usleep(50000);
70 #endif
71
72 // Construct path to nandpart partition.
73 char path[PATH_MAX];
74 strcpy(path, parent.Path());
75 strcat(path, "/test");
76
77 ParentDevice::TestConfig nandpart_config = {};
78 nandpart_config.path = path;
79
80 ParentDevice nandpart_parent(nandpart_config);
81 if (!nandpart_parent.IsValid()) {
82 printf("Unable to attach to device\n");
83 return -1;
84 }
85
86 g_parent_device_ = &nandpart_parent;
87
88 return unittest_run_all_tests(argc, argv) ? 0 : -1;
89 }
90