133965Sjdp// Copyright 2018 The Fuchsia Authors. All rights reserved.
233965Sjdp// Use of this source code is governed by a BSD-style license that can be
389857Sobrien// found in the LICENSE file.
489857Sobrien
589857Sobrien#include <fbl/auto_call.h>
689857Sobrien#include <fbl/vector.h>
789857Sobrien#include <fs-management/ramdisk.h>
889857Sobrien#include <lib/fdio/spawn.h>
989857Sobrien#include <lib/zx/process.h>
1089857Sobrien#include <unittest/unittest.h>
1189857Sobrien
1289857Sobriennamespace {
1389857Sobrien
1489857Sobrien// This is a simple test of biotime (a block device IO performance
1533965Sjdp// measurement tool).  It runs biotime on a ramdisk and just checks that it
1633965Sjdp// returns a success status.
1733965Sjdpbool run_biotime(fbl::Vector<const char*>&& args) {
18218822Sdim    char ramdisk_path[PATH_MAX];
19130561Sobrien    ASSERT_EQ(create_ramdisk(1024, 100, ramdisk_path), 0);
2033965Sjdp    auto ac = fbl::MakeAutoCall([&] {
21218822Sdim        EXPECT_EQ(destroy_ramdisk(ramdisk_path), 0);
2233965Sjdp    });
2333965Sjdp
2433965Sjdp    args.insert(0, "/boot/bin/biotime");
2533965Sjdp    args.push_back(ramdisk_path);
26    args.push_back(nullptr); // fdio_spawn() wants a null-terminated array.
27
28    zx::process process;
29    ASSERT_EQ(fdio_spawn(ZX_HANDLE_INVALID, FDIO_SPAWN_CLONE_ALL, args[0],
30                         args.get(), process.reset_and_get_address()), ZX_OK);
31
32    // Wait for the process to exit.
33    ASSERT_EQ(process.wait_one(ZX_PROCESS_TERMINATED, zx::time::infinite(),
34                               nullptr), ZX_OK);
35    zx_info_process_t proc_info;
36    ASSERT_EQ(process.get_info(ZX_INFO_PROCESS, &proc_info, sizeof(proc_info),
37                               nullptr, nullptr), ZX_OK);
38    ASSERT_EQ(proc_info.return_code, 0);
39    return true;
40}
41
42bool test_biotime_linear_access() {
43    BEGIN_TEST;
44
45    fbl::Vector<const char*> args = {"-linear"};
46    EXPECT_TRUE(run_biotime(fbl::move(args)));
47
48    END_TEST;
49}
50
51bool test_biotime_random_access() {
52    BEGIN_TEST;
53
54    fbl::Vector<const char*> args = {"-random"};
55    EXPECT_TRUE(run_biotime(fbl::move(args)));
56
57    END_TEST;
58}
59
60bool test_biotime_write() {
61    BEGIN_TEST;
62
63    fbl::Vector<const char*> args = {"-write", "-live-dangerously"};
64    EXPECT_TRUE(run_biotime(fbl::move(args)));
65
66    END_TEST;
67}
68
69BEGIN_TEST_CASE(biotime_tests)
70RUN_TEST(test_biotime_linear_access)
71RUN_TEST(test_biotime_random_access)
72RUN_TEST(test_biotime_write)
73END_TEST_CASE(biotime_tests)
74
75}
76
77int main(int argc, char** argv) {
78    bool success = unittest_run_all_tests(argc, argv);
79    return success ? 0 : -1;
80}
81