diff options
author | 2016-10-20 23:18:17 -0700 | |
---|---|---|
committer | 2016-10-22 08:59:07 -0700 | |
commit | 78f2a376144057e1390563c22c4f00a872e37671 (patch) | |
tree | 019c709bbc759caafde9a3cdc2c3c96fe5c70a79 | |
parent | 97349d34d6a28e2e128844c2eea7d1c09e5178ee (diff) |
TextOutput log improvement
Add unit tests for log functions
Add support of std::endl
Add back support of char
Add back support of bool
Add back support of String16
Fix a build warning on union initialization
Test: run /data/nativetest(64)/binderTextOutputTest
Bug: 32181382
Change-Id: I4030b142beacc5dcd88a10904710fd6e19f7240c
-rw-r--r-- | include/binder/TextOutput.h | 30 | ||||
-rw-r--r-- | libs/binder/tests/Android.bp | 10 | ||||
-rw-r--r-- | libs/binder/tests/binderDriverInterfaceTest.cpp | 5 | ||||
-rw-r--r-- | libs/binder/tests/binderTextOutputTest.cpp | 176 |
4 files changed, 216 insertions, 5 deletions
diff --git a/include/binder/TextOutput.h b/include/binder/TextOutput.h index 0e9975a22c..851e01f4b8 100644 --- a/include/binder/TextOutput.h +++ b/include/binder/TextOutput.h @@ -18,6 +18,7 @@ #define ANDROID_TEXTOUTPUT_H #include <utils/Errors.h> +#include <utils/String8.h> #include <stdint.h> #include <string.h> @@ -26,9 +27,6 @@ // --------------------------------------------------------------------------- namespace android { -class String8; -class String16; - class TextOutput { public: @@ -121,6 +119,32 @@ private: }; TextOutput& operator<<(TextOutput& to, const HexDump& val); +inline TextOutput& operator<<(TextOutput& to, + decltype(std::endl<char, + std::char_traits<char>>) + /*val*/) { + endl(to); + return to; +} + +inline TextOutput& operator<<(TextOutput& to, const char &c) +{ + to.print(&c, 1); + return to; +} + +inline TextOutput& operator<<(TextOutput& to, const bool &val) +{ + if (val) to.print("true", 4); + else to.print("false", 5); + return to; +} + +inline TextOutput& operator<<(TextOutput& to, const String16& val) +{ + to << String8(val).string(); + return to; +} // --------------------------------------------------------------------------- // No user servicable parts below. diff --git a/libs/binder/tests/Android.bp b/libs/binder/tests/Android.bp index 4f77eed7ad..2152206a2a 100644 --- a/libs/binder/tests/Android.bp +++ b/libs/binder/tests/Android.bp @@ -51,3 +51,13 @@ cc_test { "-O3", ], } + +cc_test { + name: "binderTextOutputTest", + srcs: ["binderTextOutputTest.cpp"], + shared_libs: [ + "libbinder", + "libutils", + "libbase", + ], +} diff --git a/libs/binder/tests/binderDriverInterfaceTest.cpp b/libs/binder/tests/binderDriverInterfaceTest.cpp index e02844e4ec..ff5912fbe1 100644 --- a/libs/binder/tests/binderDriverInterfaceTest.cpp +++ b/libs/binder/tests/binderDriverInterfaceTest.cpp @@ -229,7 +229,9 @@ TEST_F(BinderDriverInterfaceTest, Transaction) { .sender_euid = 0, .data_size = 0, .offsets_size = 0, - .data = {0, 0}, + .data = { + .ptr = {0, 0}, + }, }, }; struct { @@ -350,4 +352,3 @@ int main(int argc, char **argv) { return RUN_ALL_TESTS(); } - diff --git a/libs/binder/tests/binderTextOutputTest.cpp b/libs/binder/tests/binderTextOutputTest.cpp new file mode 100644 index 0000000000..f6dd22d798 --- /dev/null +++ b/libs/binder/tests/binderTextOutputTest.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2016 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 <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <limits> +#include <cstddef> + +#include "android-base/file.h" +#include "android-base/test_utils.h" +#include <gtest/gtest.h> + +#include <binder/Parcel.h> +#include <binder/TextOutput.h> +#include <binder/Debug.h> + +static void CheckMessage(const CapturedStderr& cap, + const char* expected, + bool singleline) { + std::string output; + ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); + android::base::ReadFdToString(cap.fd(), &output); + if (singleline) + output.erase(std::remove(output.begin(), output.end(), '\n')); + ASSERT_STREQ(output.c_str(), expected); +} + +#define CHECK_LOG_(input, expect, singleline) \ +{ \ + CapturedStderr cap; \ + android::aerr << input << android::endl; \ + CheckMessage(cap, expect, singleline); \ +} \ + +#define CHECK_VAL_(val, singleline) \ +{ \ + std::stringstream ss; \ + ss << val; \ + std::string s = ss.str(); \ + CHECK_LOG_(val, s.c_str(), singleline); \ +} \ + +#define CHECK_LOG(input, expect) CHECK_LOG_(input, expect, true) +#define CHECK_VAL(val) CHECK_VAL_(val, true) + +TEST(TextOutput, HandlesStdEndl) { + CapturedStderr cap; + android::aerr << "foobar" << std::endl; + std::string output; + ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); + android::base::ReadFdToString(cap.fd(), &output); + ASSERT_STREQ(output.c_str(), "foobar\n"); +} + +TEST(TextOutput, HandlesCEndl) { + CapturedStderr cap; + android::aerr << "foobar" << "\n"; + std::string output; + ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); + android::base::ReadFdToString(cap.fd(), &output); + ASSERT_STREQ(output.c_str(), "foobar\n"); +} + +TEST(TextOutput, HandlesAndroidEndl) { + CapturedStderr cap; + android::aerr << "foobar" << android::endl; + std::string output; + ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); + android::base::ReadFdToString(cap.fd(), &output); + ASSERT_STREQ(output.c_str(), "foobar\n"); +} + +TEST(TextOutput, HandleEmptyString) { + CHECK_LOG("", ""); +} + +TEST(TextOutput, HandleString) { + CHECK_LOG("foobar", "foobar"); +} + +TEST(TextOutput, HandleNum) { + CHECK_LOG(12345, "12345"); +} + +TEST(TextOutput, HandleBool) { + CHECK_LOG(false, "false"); +} + +TEST(TextOutput, HandleChar) { + CHECK_LOG('T', "T"); +} + +TEST(TextOutput, HandleParcel) { + android::Parcel val; + CHECK_LOG(val, "Parcel(NULL)"); +} + +TEST(TextOutput, HandleHexDump) { + const char buf[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + android::HexDump val(buf, sizeof(buf)); + CHECK_LOG(val, "03020100 07060504 0b0a0908 0f0e0d0c '................'"); +} + +TEST(TextOutput, HandleHexDumpCustom) { + const char buf[4] = {0x11,0x22,0x33,0x44}; + android::HexDump val(buf, sizeof(buf), 4); + CHECK_LOG(val, "11 22 33 44 '.\"3D'"); +} + +TEST(TextOutput, HandleTypeCode) { + android::TypeCode val(1234); + CHECK_LOG(val, "'\\x04\\xd2'"); +} + +TEST(TextOutput, HandleCookie) { + int32_t val = 321; //0x141 + CHECK_LOG((void*)(long)val, "0x141"); +} + +TEST(TextOutput, HandleString8) { + android::String8 val("foobar"); + CHECK_LOG(val, "foobar"); +} + +TEST(TextOutput, HandleString16) { + android::String16 val("foobar"); + CHECK_LOG(val, "foobar"); +} + +template <typename T> +class TextTest : public testing::Test {}; + +typedef testing::Types<short, unsigned short, + int, unsigned int, + long, unsigned long, + long long, unsigned long long, + float, double, long double> TestTypes; +TYPED_TEST_CASE(TextTest, TestTypes); + +TYPED_TEST(TextTest, TextMax) +{ + TypeParam max = std::numeric_limits<TypeParam>::max(); + CHECK_VAL(max); +} + +TYPED_TEST(TextTest, TestMin) +{ + TypeParam min = std::numeric_limits<TypeParam>::min(); + CHECK_VAL(min); +} + +TYPED_TEST(TextTest, TestDenom) +{ + TypeParam min = std::numeric_limits<TypeParam>::denorm_min(); + CHECK_VAL(min); +} + +TYPED_TEST(TextTest, TestEpsilon) +{ + TypeParam eps = std::numeric_limits<TypeParam>::epsilon(); + CHECK_VAL(eps); +} |