blob: b3311500773b123ded8ba19f4781077681e69818 [file] [log] [blame]
Nick Kralevich2c5153b2013-01-11 14:43:05 -08001/*
2 * Copyright (C) 2013 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 <sys/cdefs.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080018#include <gtest/gtest.h>
19
20// getauxval() was only added as of glibc version 2.16.
21// See: http://lwn.net/Articles/519085/
22// Don't try to compile this code on older glibc versions.
23
24#if defined(__BIONIC__)
25 #define GETAUXVAL_CAN_COMPILE 1
26#elif defined(__GLIBC_PREREQ)
27 #if __GLIBC_PREREQ(2, 16)
28 #define GETAUXVAL_CAN_COMPILE 1
29 #endif
30#endif
31
32#if defined(GETAUXVAL_CAN_COMPILE)
Nick Kralevich2c5153b2013-01-11 14:43:05 -080033#include <sys/auxv.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080034#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080035
36TEST(getauxval, expected_values) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080037#if defined(GETAUXVAL_CAN_COMPILE)
Nick Kralevich2c5153b2013-01-11 14:43:05 -080038 ASSERT_EQ((unsigned long int) 0, getauxval(AT_SECURE));
39 ASSERT_EQ(getuid(), getauxval(AT_UID));
40 ASSERT_EQ(geteuid(), getauxval(AT_EUID));
41 ASSERT_EQ(getgid(), getauxval(AT_GID));
42 ASSERT_EQ(getegid(), getauxval(AT_EGID));
43 ASSERT_EQ((unsigned long int) getpagesize(), getauxval(AT_PAGESZ));
44
45 ASSERT_NE((unsigned long int) 0, getauxval(AT_PHDR));
46 ASSERT_NE((unsigned long int) 0, getauxval(AT_PHNUM));
47 ASSERT_NE((unsigned long int) 0, getauxval(AT_ENTRY));
48 ASSERT_NE((unsigned long int) 0, getauxval(AT_PAGESZ));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080049#else
50 GTEST_LOG_(INFO) << "This test does nothing.\n";
51#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080052}
53
54TEST(getauxval, unexpected_values) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080055#if defined(GETAUXVAL_CAN_COMPILE)
Nick Kralevich2c5153b2013-01-11 14:43:05 -080056 ASSERT_EQ((unsigned long int) 0, getauxval(0xdeadbeef));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080057#else
58 GTEST_LOG_(INFO) << "This test does nothing.\n";
59#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080060}