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