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