1 /* 2 * Copyright (C) 2019 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 <stdint.h> 20 21 #include "chre/util/system/debug_dump.h" 22 23 using chre::DebugDumpWrapper; 24 25 static constexpr size_t kStandardBufferSize = 4000; 26 27 TEST(DebugDumpWrapper, ZeroBuffersInitially) { 28 DebugDumpWrapper debugDump(kStandardBufferSize); 29 const auto &buffers = debugDump.getBuffers(); 30 EXPECT_TRUE(buffers.empty()); 31 } 32 33 TEST(DebugDumpWrapper, OneBufferForOneString) { 34 DebugDumpWrapper debugDump(kStandardBufferSize); 35 const char *str = "Lorem ipsum"; 36 debugDump.print("%s", str); 37 const auto &buffers = debugDump.getBuffers(); 38 EXPECT_EQ(buffers.size(), 1); 39 EXPECT_TRUE(strcmp(buffers.front().get(), str) == 0); 40 } 41 42 TEST(DebugDumpWrapper, TwoStringsFitPerfectlyInOneBuffer) { 43 DebugDumpWrapper debugDump(5); 44 const char *str1 = "ab"; 45 const char *str2 = "cd"; 46 debugDump.print("%s", str1); 47 debugDump.print("%s", str2); 48 const auto &buffers = debugDump.getBuffers(); 49 EXPECT_EQ(buffers.size(), 1); 50 char bothStr[5]; 51 strcpy(bothStr, str1); 52 strcat(bothStr, str2); 53 EXPECT_TRUE(strcmp(buffers.front().get(), bothStr) == 0); 54 } 55 56 TEST(DebugDumpWrapper, TooLargeOfStringToFit) { 57 DebugDumpWrapper debugDump(1); 58 const char *str = "a"; 59 debugDump.print("%s", str); 60 const auto &buffers = debugDump.getBuffers(); 61 62 // One null-terminated buffer will be created for an empty wrapper. 63 EXPECT_EQ(buffers.size(), 1); 64 EXPECT_TRUE(strcmp(buffers.back().get(), "") == 0); 65 66 // Once there's a buffer, it won't be updated. 67 debugDump.print("%s", str); 68 EXPECT_EQ(buffers.size(), 1); 69 EXPECT_TRUE(strcmp(buffers.back().get(), "") == 0); 70 } 71 72 TEST(DebugDumpWrapper, TooLargeOfStringWithPartlyFilledBuffer) { 73 DebugDumpWrapper debugDump(2); 74 const char *str1 = "a"; 75 const char *str2 = "bc"; 76 debugDump.print("%s", str1); 77 const auto &buffers = debugDump.getBuffers(); 78 EXPECT_EQ(buffers.size(), 1); 79 debugDump.print("%s", str2); 80 EXPECT_EQ(buffers.size(), 1); 81 EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0); 82 } 83 84 TEST(DebugDumpWrapper, StringForcesNewBufferWithPartlyFilledBuffer) { 85 DebugDumpWrapper debugDump(4); 86 const char *str1 = "ab"; 87 const char *str2 = "bc"; 88 debugDump.print("%s", str1); 89 debugDump.print("%s", str2); 90 const auto &buffers = debugDump.getBuffers(); 91 EXPECT_EQ(buffers.size(), 2); 92 EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0); 93 EXPECT_TRUE(strcmp(buffers.back().get(), str2) == 0); 94 } 95 96 TEST(DebugDumpWrapper, ManyNewBuffersAllocated) { 97 DebugDumpWrapper debugDump(kStandardBufferSize); 98 constexpr size_t kSizeStrings = 10; 99 constexpr size_t kNumPrints = 1200; 100 // Should be about 12000 chars added to debugDump 101 char str[kSizeStrings]; 102 memset(str, 'a', sizeof(char) * kSizeStrings); 103 str[kSizeStrings - 1] = '\0'; 104 for (size_t i = 0; i < kNumPrints; i++) { 105 debugDump.print("%s", str); 106 } 107 const auto &buffers = debugDump.getBuffers(); 108 EXPECT_EQ(buffers.size(), 3); 109 } 110 111 TEST(DebugDumpWrapper, EmptyStringAllocsOneBuffer) { 112 DebugDumpWrapper debugDump(kStandardBufferSize); 113 debugDump.print("%s", ""); 114 const auto &buffers = debugDump.getBuffers(); 115 EXPECT_EQ(buffers.size(), 1); 116 } 117 118 TEST(DebugDumpWrapper, BuffersClear) { 119 DebugDumpWrapper debugDump(4); 120 const char *str1 = "ab"; 121 const char *str2 = "cd"; 122 const char *str3 = "ef"; 123 124 debugDump.print("%s", str1); 125 debugDump.print("%s", str2); 126 const auto &buffers = debugDump.getBuffers(); 127 EXPECT_EQ(buffers.size(), 2); 128 EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0); 129 EXPECT_TRUE(strcmp(buffers.back().get(), str2) == 0); 130 131 debugDump.clear(); 132 EXPECT_EQ(buffers.size(), 0); 133 134 debugDump.print("%s", str3); 135 EXPECT_EQ(buffers.size(), 1); 136 EXPECT_TRUE(strcmp(buffers.front().get(), str3) == 0); 137 } 138 139 void printVaList(DebugDumpWrapper *debugDump, const char *formatStr, ...) { 140 va_list args; 141 va_start(args, formatStr); 142 debugDump->print(formatStr, args); 143 va_end(args); 144 } 145 146 TEST(DebugDumpWrapper, PrintVaListTwoStrings) { 147 DebugDumpWrapper debugDump(5); 148 const char *str1 = "ab"; 149 const char *str2 = "cd"; 150 printVaList(&debugDump, "%s", str1); 151 printVaList(&debugDump, "%s", str2); 152 const auto &buffers = debugDump.getBuffers(); 153 EXPECT_EQ(buffers.size(), 1); 154 char bothStr[5]; 155 strcpy(bothStr, str1); 156 strcat(bothStr, str2); 157 EXPECT_TRUE(strcmp(buffers.front().get(), bothStr) == 0); 158 } 159