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 
TEST(DebugDumpWrapper,ZeroBuffersInitially)27 TEST(DebugDumpWrapper, ZeroBuffersInitially) {
28   DebugDumpWrapper debugDump(kStandardBufferSize);
29   const auto &buffers = debugDump.getBuffers();
30   EXPECT_TRUE(buffers.empty());
31 }
32 
TEST(DebugDumpWrapper,OneBufferForOneString)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 
TEST(DebugDumpWrapper,TwoStringsFitPerfectlyInOneBuffer)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 
TEST(DebugDumpWrapper,TooLargeOfStringToFit)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 
TEST(DebugDumpWrapper,TooLargeOfStringWithPartlyFilledBuffer)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 
TEST(DebugDumpWrapper,StringForcesNewBufferWithPartlyFilledBuffer)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 
TEST(DebugDumpWrapper,ManyNewBuffersAllocated)96 TEST(DebugDumpWrapper, ManyNewBuffersAllocated) {
97   DebugDumpWrapper debugDump(kStandardBufferSize);
98   const char *str = "aaaaaaaaa";
99   // Should be 12000 chars added to debugDump
100   constexpr size_t kNumPrints = 1200;
101   for (size_t i = 0; i < kNumPrints; i++) {
102     debugDump.print("%s", str);
103   }
104   const auto &buffers = debugDump.getBuffers();
105   EXPECT_EQ(buffers.size(), 3);
106 }
107 
TEST(DebugDumpWrapper,EmptyStringAllocsOneBuffer)108 TEST(DebugDumpWrapper, EmptyStringAllocsOneBuffer) {
109   DebugDumpWrapper debugDump(kStandardBufferSize);
110   debugDump.print("%s", "");
111   const auto &buffers = debugDump.getBuffers();
112   EXPECT_EQ(buffers.size(), 1);
113 }
114 
TEST(DebugDumpWrapper,BuffersClear)115 TEST(DebugDumpWrapper, BuffersClear) {
116   DebugDumpWrapper debugDump(4);
117   const char *str1 = "ab";
118   const char *str2 = "cd";
119   const char *str3 = "ef";
120 
121   debugDump.print("%s", str1);
122   debugDump.print("%s", str2);
123   const auto &buffers = debugDump.getBuffers();
124   EXPECT_EQ(buffers.size(), 2);
125   EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0);
126   EXPECT_TRUE(strcmp(buffers.back().get(), str2) == 0);
127 
128   debugDump.clear();
129   EXPECT_EQ(buffers.size(), 0);
130 
131   debugDump.print("%s", str3);
132   EXPECT_EQ(buffers.size(), 1);
133   EXPECT_TRUE(strcmp(buffers.front().get(), str3) == 0);
134 }
135 
printVaList(DebugDumpWrapper * debugDump,const char * formatStr,...)136 void printVaList(DebugDumpWrapper *debugDump, const char *formatStr, ...) {
137   va_list args;
138   va_start(args, formatStr);
139   debugDump->printVaList(formatStr, args);
140   va_end(args);
141 }
142 
TEST(DebugDumpWrapper,PrintVaListTwoStrings)143 TEST(DebugDumpWrapper, PrintVaListTwoStrings) {
144   DebugDumpWrapper debugDump(5);
145   const char *str1 = "ab";
146   const char *str2 = "cd";
147   printVaList(&debugDump, "%s", str1);
148   printVaList(&debugDump, "%s", str2);
149   const auto &buffers = debugDump.getBuffers();
150   EXPECT_EQ(buffers.size(), 1);
151   char bothStr[5];
152   strcpy(bothStr, str1);
153   strcat(bothStr, str2);
154   EXPECT_TRUE(strcmp(buffers.front().get(), bothStr) == 0);
155 }
156