1 /* 2 * Copyright (C) 2016 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 <fcntl.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <limits> 21 #include <cstddef> 22 23 #include "android-base/file.h" 24 #include "android-base/test_utils.h" 25 #include <gtest/gtest.h> 26 27 #include <binder/Parcel.h> 28 #include <binder/TextOutput.h> 29 #include <binder/Debug.h> 30 31 static void CheckMessage(const CapturedStderr& cap, 32 const char* expected, 33 bool singleline) { 34 std::string output; 35 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); 36 android::base::ReadFdToString(cap.fd(), &output); 37 if (singleline) 38 output.erase(std::remove(output.begin(), output.end(), '\n')); 39 ASSERT_STREQ(output.c_str(), expected); 40 } 41 42 #define CHECK_LOG_(input, expect, singleline) \ 43 { \ 44 CapturedStderr cap; \ 45 android::aerr << input << android::endl; \ 46 CheckMessage(cap, expect, singleline); \ 47 } \ 48 49 #define CHECK_VAL_(val, singleline) \ 50 { \ 51 std::stringstream ss; \ 52 ss << val; \ 53 std::string s = ss.str(); \ 54 CHECK_LOG_(val, s.c_str(), singleline); \ 55 } \ 56 57 #define CHECK_LOG(input, expect) CHECK_LOG_(input, expect, true) 58 #define CHECK_VAL(val) CHECK_VAL_(val, true) 59 60 TEST(TextOutput, HandlesStdEndl) { 61 CapturedStderr cap; 62 android::aerr << "foobar" << std::endl; 63 std::string output; 64 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); 65 android::base::ReadFdToString(cap.fd(), &output); 66 ASSERT_STREQ(output.c_str(), "foobar\n"); 67 } 68 69 TEST(TextOutput, HandlesCEndl) { 70 CapturedStderr cap; 71 android::aerr << "foobar" << "\n"; 72 std::string output; 73 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); 74 android::base::ReadFdToString(cap.fd(), &output); 75 ASSERT_STREQ(output.c_str(), "foobar\n"); 76 } 77 78 TEST(TextOutput, HandlesAndroidEndl) { 79 CapturedStderr cap; 80 android::aerr << "foobar" << android::endl; 81 std::string output; 82 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); 83 android::base::ReadFdToString(cap.fd(), &output); 84 ASSERT_STREQ(output.c_str(), "foobar\n"); 85 } 86 87 TEST(TextOutput, HandleEmptyString) { 88 CHECK_LOG("", ""); 89 } 90 91 TEST(TextOutput, HandleString) { 92 CHECK_LOG("foobar", "foobar"); 93 } 94 95 TEST(TextOutput, HandleNum) { 96 CHECK_LOG(12345, "12345"); 97 } 98 99 TEST(TextOutput, HandleBool) { 100 CHECK_LOG(false, "false"); 101 } 102 103 TEST(TextOutput, HandleChar) { 104 CHECK_LOG('T', "T"); 105 } 106 107 TEST(TextOutput, HandleParcel) { 108 android::Parcel val; 109 CHECK_LOG(val, "Parcel(NULL)"); 110 } 111 112 TEST(TextOutput, HandleHexDump) { 113 const char buf[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 114 android::HexDump val(buf, sizeof(buf)); 115 CHECK_LOG(val, "03020100 07060504 0b0a0908 0f0e0d0c '................'"); 116 } 117 118 TEST(TextOutput, HandleHexDumpCustom) { 119 const char buf[4] = {0x11,0x22,0x33,0x44}; 120 android::HexDump val(buf, sizeof(buf), 4); 121 CHECK_LOG(val, "11 22 33 44 '.\"3D'"); 122 } 123 124 TEST(TextOutput, HandleTypeCode) { 125 android::TypeCode val(1234); 126 CHECK_LOG(val, "'\\x04\\xd2'"); 127 } 128 129 TEST(TextOutput, HandleCookie) { 130 int32_t val = 321; //0x141 131 CHECK_LOG((void*)(long)val, "0x141"); 132 } 133 134 TEST(TextOutput, HandleString8) { 135 android::String8 val("foobar"); 136 CHECK_LOG(val, "foobar"); 137 } 138 139 TEST(TextOutput, HandleString16) { 140 android::String16 val("foobar"); 141 CHECK_LOG(val, "foobar"); 142 } 143 144 template <typename T> 145 class TextTest : public testing::Test {}; 146 147 typedef testing::Types<short, unsigned short, 148 int, unsigned int, 149 long, unsigned long, 150 long long, unsigned long long, 151 float, double, long double> TestTypes; 152 TYPED_TEST_CASE(TextTest, TestTypes); 153 154 TYPED_TEST(TextTest, TextMax) 155 { 156 TypeParam max = std::numeric_limits<TypeParam>::max(); 157 CHECK_VAL(max); 158 } 159 160 TYPED_TEST(TextTest, TestMin) 161 { 162 TypeParam min = std::numeric_limits<TypeParam>::min(); 163 CHECK_VAL(min); 164 } 165 166 TYPED_TEST(TextTest, TestDenom) 167 { 168 TypeParam min = std::numeric_limits<TypeParam>::denorm_min(); 169 CHECK_VAL(min); 170 } 171 172 TYPED_TEST(TextTest, TestEpsilon) 173 { 174 TypeParam eps = std::numeric_limits<TypeParam>::epsilon(); 175 CHECK_VAL(eps); 176 } 177