string_test.cpp revision ad88a0863110798cef5169dcf917e18b967a7cf6
1/* 2 * Copyright (C) 2012 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 <gtest/gtest.h> 18 19#include <errno.h> 20#include <string.h> 21 22TEST(string, strerror) { 23 // Valid. 24 ASSERT_STREQ("Success", strerror(0)); 25 ASSERT_STREQ("Operation not permitted", strerror(1)); 26 27 // Invalid. 28 ASSERT_STREQ("Unknown error 4294967295", strerror(-1)); 29 ASSERT_STREQ("Unknown error 1234", strerror(1234)); 30} 31 32#if __BIONIC__ // glibc's strerror isn't thread safe, only its strsignal. 33 34static void* ConcurrentStrErrorFn(void*) { 35 bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0); 36 return reinterpret_cast<void*>(equal); 37} 38 39TEST(string, strerror_concurrent) { 40 const char* strerror1001 = strerror(1001); 41 ASSERT_STREQ("Unknown error 1001", strerror1001); 42 43 pthread_t t; 44 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrErrorFn, NULL)); 45 void* result; 46 ASSERT_EQ(0, pthread_join(t, &result)); 47 ASSERT_TRUE(static_cast<bool>(result)); 48 49 ASSERT_STREQ("Unknown error 1001", strerror1001); 50} 51 52#endif 53 54#if __BIONIC__ // glibc's strerror_r doesn't even have the same signature as the POSIX one. 55TEST(string, strerror_r) { 56 char buf[256]; 57 58 // Valid. 59 ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf))); 60 ASSERT_STREQ("Success", buf); 61 ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf))); 62 ASSERT_STREQ("Operation not permitted", buf); 63 64 // Invalid. 65 ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf))); 66 ASSERT_STREQ("Unknown error 4294967295", buf); 67 ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf))); 68 ASSERT_STREQ("Unknown error 1234", buf); 69 70 // Buffer too small. 71 ASSERT_EQ(-1, strerror_r(0, buf, 2)); 72 ASSERT_EQ(ERANGE, errno); 73} 74#endif 75 76TEST(string, strsignal) { 77 // A regular signal. 78 ASSERT_STREQ("Hangup", strsignal(1)); 79 80 // A real-time signal. 81#ifdef __GLIBC__ // glibc reserves real-time signals for internal use, and doesn't count those. 82 ASSERT_STREQ("Real-time signal 14", strsignal(48)); 83#else 84 ASSERT_STREQ("Real-time signal 16", strsignal(48)); 85#endif 86 87 // Errors. 88 ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small. 89 ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small. 90 ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large. 91} 92 93static void* ConcurrentStrSignalFn(void*) { 94 bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0); 95 return reinterpret_cast<void*>(equal); 96} 97 98TEST(string, strsignal_concurrent) { 99 const char* strsignal1001 = strsignal(1001); 100 ASSERT_STREQ("Unknown signal 1001", strsignal1001); 101 102 pthread_t t; 103 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrSignalFn, NULL)); 104 void* result; 105 ASSERT_EQ(0, pthread_join(t, &result)); 106 ASSERT_TRUE(static_cast<bool>(result)); 107 108 ASSERT_STREQ("Unknown signal 1001", strsignal1001); 109} 110