1 // Copyright 2017 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 <fcntl.h>
6 #include <unistd.h>
7 
8 #include <fuchsia/sysinfo/c/fidl.h>
9 #include <lib/fdio/util.h>
10 #include <lib/zx/channel.h>
11 #include <zircon/boot/image.h>
12 #include <zircon/syscalls.h>
13 #include <zircon/syscalls/object.h>
14 #include <unittest/unittest.h>
15 
16 #define SYSINFO_PATH    "/dev/misc/sysinfo"
17 
get_root_resource_succeeds()18 bool get_root_resource_succeeds() {
19     BEGIN_TEST;
20 
21     // Get the resource handle from the driver.
22     int fd = open(SYSINFO_PATH, O_RDWR);
23     ASSERT_GE(fd, 0, "Can't open sysinfo");
24 
25     zx::channel channel;
26     ASSERT_EQ(fdio_get_service_handle(fd, channel.reset_and_get_address()), ZX_OK,
27               "Failed to get channel");
28 
29     zx_handle_t root_resource;
30     zx_status_t status;
31     ASSERT_EQ(fuchsia_sysinfo_DeviceGetRootResource(channel.get(), &status, &root_resource), ZX_OK,
32               "Failed to get root resource");
33     ASSERT_EQ(status, ZX_OK, "Failed to get root resource");
34 
35     // Make sure it's a resource with the expected rights.
36     zx_info_handle_basic_t info;
37     ASSERT_EQ(zx_object_get_info(root_resource, ZX_INFO_HANDLE_BASIC, &info,
38                                  sizeof(info), nullptr, nullptr),
39               ZX_OK, "Can't get handle info");
40     EXPECT_EQ(info.type, ZX_OBJ_TYPE_RESOURCE, "Unexpected type");
41     EXPECT_EQ(info.rights, ZX_RIGHT_TRANSFER, "Unexpected rights");
42 
43     // Clean up.
44     EXPECT_EQ(zx_handle_close(root_resource), ZX_OK);
45 
46     END_TEST;
47 }
48 
get_board_name_succeeds()49 bool get_board_name_succeeds() {
50     BEGIN_TEST;
51 
52     // Get the resource handle from the driver.
53     int fd = open(SYSINFO_PATH, O_RDWR);
54     ASSERT_GE(fd, 0, "Can't open sysinfo");
55 
56     zx::channel channel;
57     ASSERT_EQ(fdio_get_service_handle(fd, channel.reset_and_get_address()), ZX_OK,
58               "Failed to get channel");
59 
60     // Test fuchsia_sysinfo_DeviceGetBoardName().
61     char board_name[ZBI_BOARD_NAME_LEN];
62     zx_status_t status;
63     size_t actual_size;
64     zx_status_t fidl_status = fuchsia_sysinfo_DeviceGetBoardName(channel.get(), &status, board_name,
65                                                                 sizeof(board_name), &actual_size);
66     ASSERT_EQ(fidl_status, ZX_OK, "Failed to get board name");
67     ASSERT_EQ(status, ZX_OK, "Failed to get board name");
68     ASSERT_LE(actual_size, sizeof(board_name), "GetBoardName returned too much data");
69     EXPECT_NE(0, board_name[0], "board name is empty");
70 
71     END_TEST;
72 }
73 
get_interrupt_controller_info_succeeds()74 bool get_interrupt_controller_info_succeeds() {
75     BEGIN_TEST;
76 
77     // Get the resource handle from the driver.
78     int fd = open(SYSINFO_PATH, O_RDWR);
79     ASSERT_GE(fd, 0, "Can't open sysinfo");
80 
81     zx::channel channel;
82     ASSERT_EQ(fdio_get_service_handle(fd, channel.reset_and_get_address()), ZX_OK,
83               "Failed to get channel");
84 
85     // Test fuchsia_sysinfo_DeviceGetInterruptControllerInfo().
86     fuchsia_sysinfo_InterruptControllerInfo info;
87     zx_status_t status;
88     ASSERT_EQ(fuchsia_sysinfo_DeviceGetInterruptControllerInfo(channel.get(), &status, &info),
89               ZX_OK, "Failed to get interrupt controller info");
90     ASSERT_EQ(status, ZX_OK, "Failed to get interrupt controller info");
91     EXPECT_NE(info.type, fuchsia_sysinfo_InterruptControllerType_UNKNOWN,
92               "interrupt controller type is unknown");
93 
94     END_TEST;
95 }
96 
97 BEGIN_TEST_CASE(sysinfo_tests)
RUN_TEST(get_root_resource_succeeds)98 RUN_TEST(get_root_resource_succeeds)
99 RUN_TEST(get_board_name_succeeds)
100 RUN_TEST(get_interrupt_controller_info_succeeds)
101 END_TEST_CASE(sysinfo_tests)
102 
103 int main(int argc, char** argv) {
104     return unittest_run_all_tests(argc, argv) ? 0 : -1;
105 }
106