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 <lib/fzl/memory-probe.h>
6 
7 #include <unittest/unittest.h>
8 
9 namespace {
10 
probe_readwrite()11 bool probe_readwrite() {
12     BEGIN_TEST;
13 
14     int valid = 0;
15     EXPECT_TRUE(probe_for_read(&valid));
16     EXPECT_TRUE(probe_for_write(&valid));
17 
18     END_TEST;
19 }
20 
probe_readonly()21 bool probe_readonly() {
22     BEGIN_TEST;
23 
24     // This uses the address of this function. This assumes that the code section is readable but
25     // not writable.
26     void* this_function = reinterpret_cast<void*>(&probe_readonly);
27     EXPECT_TRUE(probe_for_read(this_function));
28     EXPECT_FALSE(probe_for_write(this_function));
29 
30     END_TEST;
31 }
32 
probe_invalid()33 bool probe_invalid() {
34     BEGIN_TEST;
35 
36     EXPECT_FALSE(probe_for_read(nullptr));
37     EXPECT_FALSE(probe_for_write(nullptr));
38 
39     END_TEST;
40 }
41 
42 }  // namespace
43 
44 BEGIN_TEST_CASE(memory_probe_tests)
45 RUN_TEST(probe_readwrite)
46 RUN_TEST(probe_readonly)
47 RUN_TEST(probe_invalid)
48 END_TEST_CASE(memory_probe_tests)
49 
50