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 <fcntl.h>
6#include <unistd.h>
7
8#include <unittest/unittest.h>
9#include <zircon/device/dmctl.h>
10#include <zircon/syscalls.h>
11
12// Ask the kernel to run its unit tests.
13bool run_kernel_unittests() {
14    BEGIN_TEST;
15
16    static const char command_string[] = "kerneldebug ut all";
17
18    // Send the command via devmgr.
19    int dmctl_fd = open("/dev/misc/dmctl", O_WRONLY);
20    ASSERT_GE(dmctl_fd, 0);
21    dmctl_cmd_t cmd;
22    ASSERT_LE(sizeof(command_string), sizeof(cmd.name));
23    strcpy(cmd.name, command_string);
24    // devmgr's ioctl() requires us to pass a socket, but we don't read
25    // from the other endpoint.
26    zx_handle_t handle;
27    ASSERT_EQ(zx_socket_create(0, &cmd.h, &handle), ZX_OK);
28    ssize_t result = ioctl_dmctl_command(dmctl_fd, &cmd);
29    ASSERT_EQ(close(dmctl_fd), 0);
30    ASSERT_EQ(zx_handle_close(handle), ZX_OK);
31
32    // Check result of kernel unit tests.
33    ASSERT_EQ(result, ZX_OK);
34
35    END_TEST;
36}
37
38BEGIN_TEST_CASE(kernel_unittests)
39RUN_TEST(run_kernel_unittests)
40END_TEST_CASE(kernel_unittests)
41
42int main(int argc, char** argv) {
43    bool success = unittest_run_all_tests(argc, argv);
44    return success ? 0 : -1;
45}
46