| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 17 | #include <errno.h> | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 18 | #include <sys/cdefs.h> | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> | 
|  | 20 |  | 
|  | 21 | // getauxval() was only added as of glibc version 2.16. | 
|  | 22 | // See: http://lwn.net/Articles/519085/ | 
|  | 23 | // Don't try to compile this code on older glibc versions. | 
|  | 24 |  | 
|  | 25 | #if defined(__BIONIC__) | 
|  | 26 | #define GETAUXVAL_CAN_COMPILE 1 | 
|  | 27 | #elif defined(__GLIBC_PREREQ) | 
|  | 28 | #if __GLIBC_PREREQ(2, 16) | 
|  | 29 | #define GETAUXVAL_CAN_COMPILE 1 | 
|  | 30 | #endif | 
|  | 31 | #endif | 
|  | 32 |  | 
|  | 33 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 34 | #include <sys/auxv.h> | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 35 | #endif | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | TEST(getauxval, expected_values) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 38 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 39 | ASSERT_EQ((unsigned long int) 0, getauxval(AT_SECURE)); | 
|  | 40 | ASSERT_EQ(getuid(), getauxval(AT_UID)); | 
|  | 41 | ASSERT_EQ(geteuid(), getauxval(AT_EUID)); | 
|  | 42 | ASSERT_EQ(getgid(), getauxval(AT_GID)); | 
|  | 43 | ASSERT_EQ(getegid(), getauxval(AT_EGID)); | 
|  | 44 | ASSERT_EQ((unsigned long int) getpagesize(), getauxval(AT_PAGESZ)); | 
|  | 45 |  | 
|  | 46 | ASSERT_NE((unsigned long int) 0, getauxval(AT_PHDR)); | 
|  | 47 | ASSERT_NE((unsigned long int) 0, getauxval(AT_PHNUM)); | 
|  | 48 | ASSERT_NE((unsigned long int) 0, getauxval(AT_ENTRY)); | 
|  | 49 | ASSERT_NE((unsigned long int) 0, getauxval(AT_PAGESZ)); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 50 | #else | 
|  | 51 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 52 | #endif | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | TEST(getauxval, unexpected_values) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 56 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 57 | errno = 0; | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 58 | ASSERT_EQ((unsigned long int) 0, getauxval(0xdeadbeef)); | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 59 | ASSERT_EQ(ENOENT, errno); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 60 | #else | 
|  | 61 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 62 | #endif | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 63 | } |