5419b94747
I gave up trying to use the usual thread-local buffer idiom; calls to calloc(3) and free(3) from any of the "dl" functions -- which live in the dynamic linker -- end up resolving to the dynamic linker's stubs. I tried to work around that, but was just making things more complicated. This alternative costs us a well-known TLS slot (instead of the dynamically-allocated TLS slot we'd have used otherwise, so no difference there), plus an extra buffer inside every pthread_internal_t. Bug: 5404023 Change-Id: Ie9614edd05b6d1eeaf7bf9172792d616c6361767
107 lines
3.2 KiB
C++
107 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));
|
|
}
|
|
|
|
static void* ConcurrentStrErrorFn(void* arg) {
|
|
bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
|
|
return reinterpret_cast<void*>(equal);
|
|
}
|
|
|
|
#if __BIONIC__ // glibc's strerror isn't thread safe, only its strsignal.
|
|
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* arg) {
|
|
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);
|
|
}
|