1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "aslr_test.h"
18
19/* run tests if on supported arch */
20#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) || defined(__arm__)
21
22/* make sure the default entropy values matches what we expect */
23TEST_F(AslrMmapTest, match_default) {
24    if (user32) {
25        // running 32-bit userspace on 64-bit kernel, only compat used.
26        return;
27    } else {
28        EXPECT_EQ(def, get_mmap_rnd_bits(false));
29    }
30}
31
32/* make sure the default compat entropy values matches what we expect */
33TEST_F(AslrMmapTest, match_compat_default) {
34    if (compat || user32)
35        EXPECT_EQ(def_cmpt, get_mmap_rnd_bits(true));
36}
37
38/* make sure we can't set entropy below a minimum threshold */
39TEST_F(AslrMmapTest, match_min) {
40    if (user32) {
41        // running 32-bit userspace on 64-bit kernel, only compat used.
42        return;
43    } else {
44        EXPECT_FALSE(set_mmap_rnd_bits(min - 1, false));
45        EXPECT_TRUE(set_mmap_rnd_bits(min, false));
46        EXPECT_EQ(min, get_mmap_rnd_bits(false));
47    }
48}
49
50/* make sure we can't set compat entropy below a minimum threshold */
51TEST_F(AslrMmapTest, match_compat_min) {
52    if (compat || user32) {
53        EXPECT_FALSE(set_mmap_rnd_bits(min_cmpt - 1, true));
54        EXPECT_TRUE(set_mmap_rnd_bits(min_cmpt, true));
55        EXPECT_EQ(min_cmpt, get_mmap_rnd_bits(true));
56    }
57}
58
59/* make sure we can't set entropy above a maximum threshold */
60TEST_F(AslrMmapTest, match_max) {
61    if (user32) {
62        // running 32-bit userspace on 64-bit kernel, only compat used.
63        return;
64    } else {
65        EXPECT_FALSE(set_mmap_rnd_bits(max + 1, false));
66        EXPECT_TRUE(set_mmap_rnd_bits(max, false));
67        EXPECT_EQ(max, get_mmap_rnd_bits(false));
68    }
69}
70
71/* make sure we can't set compat entropy above a maximum threshold */
72TEST_F(AslrMmapTest, match_compat_max) {
73    if (compat || user32) {
74        EXPECT_FALSE(set_mmap_rnd_bits(max_cmpt + 1, true));
75        EXPECT_TRUE(set_mmap_rnd_bits(max_cmpt, true));
76        EXPECT_EQ(max_cmpt, get_mmap_rnd_bits(true));
77    }
78}
79
80/* make sure observed entropy is what we expect when we set min value */
81TEST_F(AslrMmapTest, entropy_min) {
82    if (user32) {
83        // running 32-bit userspace on 64-bit kernel, only compat used.
84        return;
85    } else {
86        EXPECT_TRUE(set_mmap_rnd_bits(min, false));
87        EXPECT_EQ(min, calc_mmap_entropy(path, lib, 16));
88    }
89}
90
91/* make sure observed compat entropy is what we expect when we set min value */
92TEST_F(AslrMmapTest, entropy_cmpt_min) {
93    if (compat || user32) {
94        EXPECT_TRUE(set_mmap_rnd_bits(min_cmpt, true));
95        EXPECT_EQ(min_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
96    }
97}
98
99/* make sure observed entropy is what we expect when we set max value */
100TEST_F(AslrMmapTest, entropy_max) {
101    if (user32) {
102        // running 32-bit userspace on 64-bit kernel, only compat used.
103        return;
104    } else {
105        EXPECT_TRUE(set_mmap_rnd_bits(max, false));
106        EXPECT_EQ(max, calc_mmap_entropy(path, lib, 16));
107    }
108}
109
110/* make sure observed compat entropy is what we expect when we set max value */
111TEST_F(AslrMmapTest, entropy_cmpt_max) {
112    if (compat || user32) {
113        EXPECT_TRUE(set_mmap_rnd_bits(max_cmpt, true));
114        EXPECT_EQ(max_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
115    }
116}
117
118/* make sure observed entropy is what we expect for default value */
119TEST_F(AslrMmapTest, entropy_def) {
120    if (user32) {
121        // running 32-bit userspace on 64-bit kernel, only compat used.
122        return;
123    } else {
124        EXPECT_EQ(def, calc_mmap_entropy(path, lib, 16));
125    }
126}
127
128/* make sure observed entropy is what we expect for default compat value */
129TEST_F(AslrMmapTest, entropy_cmpt_def) {
130    if (compat || user32) {
131        EXPECT_EQ(def_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
132    }
133}
134
135#endif /* supported arch */
136