bionic/tests/string_test.cpp
Elliott Hughes ad88a08631 Per-thread -fstack-protector guards for x86.
Based on a pair of patches from Intel:

  https://android-review.googlesource.com/#/c/43909/
  https://android-review.googlesource.com/#/c/44903/

For x86, this patch supports _both_ the global that ARM/MIPS use
and the per-thread TLS entry (%gs:20) that GCC uses by default. This
lets us support binaries built with any x86 toolchain (right now,
the NDK is emitting x86 code that uses the global).

I've also extended the original tests to cover ARM/MIPS too, and
be a little more thorough for x86.

Change-Id: I02f279a80c6b626aecad449771dec91df235ad01
2012-10-25 12:04:03 -07:00

110 lines
3.2 KiB
C++

/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <string.h>
TEST(string, strerror) {
// Valid.
ASSERT_STREQ("Success", strerror(0));
ASSERT_STREQ("Operation not permitted", strerror(1));
// Invalid.
ASSERT_STREQ("Unknown error 4294967295", strerror(-1));
ASSERT_STREQ("Unknown error 1234", strerror(1234));
}
#if __BIONIC__ // glibc's strerror isn't thread safe, only its strsignal.
static void* ConcurrentStrErrorFn(void*) {
bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
return reinterpret_cast<void*>(equal);
}
TEST(string, strerror_concurrent) {
const char* strerror1001 = strerror(1001);
ASSERT_STREQ("Unknown error 1001", strerror1001);
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrErrorFn, NULL));
void* result;
ASSERT_EQ(0, pthread_join(t, &result));
ASSERT_TRUE(static_cast<bool>(result));
ASSERT_STREQ("Unknown error 1001", strerror1001);
}
#endif
#if __BIONIC__ // glibc's strerror_r doesn't even have the same signature as the POSIX one.
TEST(string, strerror_r) {
char buf[256];
// Valid.
ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf)));
ASSERT_STREQ("Success", buf);
ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf)));
ASSERT_STREQ("Operation not permitted", buf);
// Invalid.
ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf)));
ASSERT_STREQ("Unknown error 4294967295", buf);
ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf)));
ASSERT_STREQ("Unknown error 1234", buf);
// Buffer too small.
ASSERT_EQ(-1, strerror_r(0, buf, 2));
ASSERT_EQ(ERANGE, errno);
}
#endif
TEST(string, strsignal) {
// A regular signal.
ASSERT_STREQ("Hangup", strsignal(1));
// A real-time signal.
#ifdef __GLIBC__ // glibc reserves real-time signals for internal use, and doesn't count those.
ASSERT_STREQ("Real-time signal 14", strsignal(48));
#else
ASSERT_STREQ("Real-time signal 16", strsignal(48));
#endif
// Errors.
ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.
ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small.
ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large.
}
static void* ConcurrentStrSignalFn(void*) {
bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0);
return reinterpret_cast<void*>(equal);
}
TEST(string, strsignal_concurrent) {
const char* strsignal1001 = strsignal(1001);
ASSERT_STREQ("Unknown signal 1001", strsignal1001);
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrSignalFn, NULL));
void* result;
ASSERT_EQ(0, pthread_join(t, &result));
ASSERT_TRUE(static_cast<bool>(result));
ASSERT_STREQ("Unknown signal 1001", strsignal1001);
}