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 <fbl/auto_call.h>
6 #include <fbl/vector.h>
7 #include <fs-management/ramdisk.h>
8 #include <lib/fdio/spawn.h>
9 #include <lib/zx/process.h>
10 #include <unittest/unittest.h>
11 
12 #include <utility>
13 
14 namespace {
15 
16 // This is a simple test of biotime (a block device IO performance
17 // measurement tool).  It runs biotime on a ramdisk and just checks that it
18 // returns a success status.
run_biotime(fbl::Vector<const char * > && args)19 bool run_biotime(fbl::Vector<const char*>&& args) {
20     char ramdisk_path[PATH_MAX];
21     ASSERT_EQ(create_ramdisk(1024, 100, ramdisk_path), ZX_OK);
22     auto ac = fbl::MakeAutoCall([&] {
23         EXPECT_EQ(destroy_ramdisk(ramdisk_path), 0);
24     });
25 
26     args.insert(0, "/boot/bin/biotime");
27     args.push_back(ramdisk_path);
28     args.push_back(nullptr); // fdio_spawn() wants a null-terminated array.
29 
30     zx::process process;
31     ASSERT_EQ(fdio_spawn(ZX_HANDLE_INVALID, FDIO_SPAWN_CLONE_ALL, args[0],
32                          args.get(), process.reset_and_get_address()), ZX_OK);
33 
34     // Wait for the process to exit.
35     ASSERT_EQ(process.wait_one(ZX_PROCESS_TERMINATED, zx::time::infinite(),
36                                nullptr), ZX_OK);
37     zx_info_process_t proc_info;
38     ASSERT_EQ(process.get_info(ZX_INFO_PROCESS, &proc_info, sizeof(proc_info),
39                                nullptr, nullptr), ZX_OK);
40     ASSERT_EQ(proc_info.return_code, 0);
41     return true;
42 }
43 
TestBiotimeLinearAccess()44 bool TestBiotimeLinearAccess() {
45     BEGIN_TEST;
46 
47     fbl::Vector<const char*> args = {"-linear"};
48     EXPECT_TRUE(run_biotime(std::move(args)));
49 
50     END_TEST;
51 }
52 
TestBiotimeRandomAccess()53 bool TestBiotimeRandomAccess() {
54     BEGIN_TEST;
55 
56     fbl::Vector<const char*> args = {"-random"};
57     EXPECT_TRUE(run_biotime(std::move(args)));
58 
59     END_TEST;
60 }
61 
TestBiotimeWrite()62 bool TestBiotimeWrite() {
63     BEGIN_TEST;
64 
65     fbl::Vector<const char*> args = {"-write", "-live-dangerously"};
66     EXPECT_TRUE(run_biotime(std::move(args)));
67 
68     END_TEST;
69 }
70 
71 BEGIN_TEST_CASE(biotime_tests)
72 RUN_TEST(TestBiotimeLinearAccess)
73 RUN_TEST(TestBiotimeRandomAccess)
74 RUN_TEST(TestBiotimeWrite)
75 END_TEST_CASE(biotime_tests)
76 
77 }
78 
main(int argc,char ** argv)79 int main(int argc, char** argv) {
80     bool success = unittest_run_all_tests(argc, argv);
81     return success ? 0 : -1;
82 }
83