1 /*
2 * Copyright (C) 2015 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 <limits.h>
18
19 #include <memory>
20 #include <string>
21
22 #include <gtest/gtest.h>
23
24 #include "Config.h"
25
26 #include "log_fake.h"
27
28 class MallocDebugConfigTest : public ::testing::Test {
29 protected:
SetUp()30 void SetUp() override {
31 resetLogs();
32 }
33
34 std::unique_ptr<Config> config;
35
InitConfig(const char * options)36 bool InitConfig(const char* options) {
37 config.reset(new Config);
38 return config->Init(options);
39 }
40 };
41
42 std::string usage_string(
43 "6 malloc_debug For malloc debug option descriptions go to:\n"
44 "6 malloc_debug https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md\n");
45
TEST_F(MallocDebugConfigTest,unknown_option)46 TEST_F(MallocDebugConfigTest, unknown_option) {
47
48 ASSERT_FALSE(InitConfig("unknown_option"));
49
50 ASSERT_STREQ("", getFakeLogBuf().c_str());
51 std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
52 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
53 }
54
TEST_F(MallocDebugConfigTest,good_option_and_bad_option)55 TEST_F(MallocDebugConfigTest, good_option_and_bad_option) {
56 ASSERT_FALSE(InitConfig("backtrace unknown_option"));
57
58 ASSERT_STREQ("", getFakeLogBuf().c_str());
59 std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
60 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
61 }
62
TEST_F(MallocDebugConfigTest,unparseable_number)63 TEST_F(MallocDebugConfigTest, unparseable_number) {
64 ASSERT_FALSE(InitConfig("backtrace=XXX"));
65
66 ASSERT_STREQ("", getFakeLogBuf().c_str());
67 std::string log_msg("6 malloc_debug malloc_testing: bad value for option 'backtrace'\n");
68 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
69 }
70
TEST_F(MallocDebugConfigTest,illegal_value_zero)71 TEST_F(MallocDebugConfigTest, illegal_value_zero) {
72 ASSERT_FALSE(InitConfig("backtrace=0"));
73
74 ASSERT_STREQ("", getFakeLogBuf().c_str());
75 std::string log_msg(
76 "6 malloc_debug malloc_testing: bad value for option 'backtrace', value must be >= 1: 0\n");
77 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
78 }
79
TEST_F(MallocDebugConfigTest,no_space)80 TEST_F(MallocDebugConfigTest, no_space) {
81 ASSERT_FALSE(InitConfig("backtrace=10front_guard"));
82
83 ASSERT_STREQ("", getFakeLogBuf().c_str());
84 std::string log_msg(
85 "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
86 "non space found after option: front_guard\n");
87 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
88 }
89
TEST_F(MallocDebugConfigTest,illegal_value_negative)90 TEST_F(MallocDebugConfigTest, illegal_value_negative) {
91 ASSERT_FALSE(InitConfig("backtrace=-1"));
92
93 ASSERT_STREQ("", getFakeLogBuf().c_str());
94 std::string log_msg(
95 "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
96 "value cannot be negative: -1\n");
97 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
98 }
99
TEST_F(MallocDebugConfigTest,overflow)100 TEST_F(MallocDebugConfigTest, overflow) {
101 ASSERT_FALSE(InitConfig("backtrace=99999999999999999999"));
102
103 ASSERT_STREQ("", getFakeLogBuf().c_str());
104 std::string log_msg(
105 "6 malloc_debug malloc_testing: bad value for option 'backtrace': "
106 "Math result not representable\n");
107 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
108 }
109
TEST_F(MallocDebugConfigTest,set_value_error)110 TEST_F(MallocDebugConfigTest, set_value_error) {
111 ASSERT_FALSE(InitConfig("leak_track=12"));
112
113 ASSERT_STREQ("", getFakeLogBuf().c_str());
114 std::string log_msg(
115 "6 malloc_debug malloc_testing: value set for option 'leak_track' "
116 "which does not take a value\n");
117 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
118 }
119
TEST_F(MallocDebugConfigTest,space_before_equal)120 TEST_F(MallocDebugConfigTest, space_before_equal) {
121 ASSERT_TRUE(InitConfig("backtrace =10")) << getFakeLogPrint();
122 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
123 ASSERT_EQ(10U, config->backtrace_frames());
124
125 ASSERT_STREQ("", getFakeLogBuf().c_str());
126 ASSERT_STREQ("", getFakeLogPrint().c_str());
127 }
128
TEST_F(MallocDebugConfigTest,space_after_equal)129 TEST_F(MallocDebugConfigTest, space_after_equal) {
130 ASSERT_TRUE(InitConfig("backtrace= 10")) << getFakeLogPrint();
131 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
132 ASSERT_EQ(10U, config->backtrace_frames());
133
134 ASSERT_STREQ("", getFakeLogBuf().c_str());
135 ASSERT_STREQ("", getFakeLogPrint().c_str());
136 }
137
TEST_F(MallocDebugConfigTest,extra_space)138 TEST_F(MallocDebugConfigTest, extra_space) {
139 ASSERT_TRUE(InitConfig(" backtrace=64 ")) << getFakeLogPrint();
140 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
141 ASSERT_EQ(64U, config->backtrace_frames());
142
143 ASSERT_STREQ("", getFakeLogBuf().c_str());
144 ASSERT_STREQ("", getFakeLogPrint().c_str());
145 }
146
TEST_F(MallocDebugConfigTest,multiple_options)147 TEST_F(MallocDebugConfigTest, multiple_options) {
148 ASSERT_TRUE(InitConfig(" backtrace=64 front_guard=48")) << getFakeLogPrint();
149 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS | FRONT_GUARD, config->options());
150 ASSERT_EQ(64U, config->backtrace_frames());
151 ASSERT_EQ(48U, config->front_guard_bytes());
152
153 ASSERT_STREQ("", getFakeLogBuf().c_str());
154 ASSERT_STREQ("", getFakeLogPrint().c_str());
155 }
156
TEST_F(MallocDebugConfigTest,front_guard)157 TEST_F(MallocDebugConfigTest, front_guard) {
158 ASSERT_TRUE(InitConfig("front_guard=48")) << getFakeLogPrint();
159 ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
160 ASSERT_EQ(48U, config->front_guard_bytes());
161
162 ASSERT_TRUE(InitConfig("front_guard")) << getFakeLogPrint();
163 ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
164 ASSERT_EQ(32U, config->front_guard_bytes());
165
166 ASSERT_TRUE(InitConfig("front_guard=39")) << getFakeLogPrint();
167 ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
168 #if defined(__LP64__)
169 ASSERT_EQ(48U, config->front_guard_bytes());
170 #else
171 ASSERT_EQ(40U, config->front_guard_bytes());
172 #endif
173
174 ASSERT_TRUE(InitConfig("front_guard=41")) << getFakeLogPrint();
175 ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
176 ASSERT_EQ(48U, config->front_guard_bytes());
177
178 ASSERT_STREQ("", getFakeLogBuf().c_str());
179 ASSERT_STREQ("", getFakeLogPrint().c_str());
180 }
181
TEST_F(MallocDebugConfigTest,rear_guard)182 TEST_F(MallocDebugConfigTest, rear_guard) {
183 ASSERT_TRUE(InitConfig("rear_guard=50")) << getFakeLogPrint();
184 ASSERT_EQ(REAR_GUARD | TRACK_ALLOCS, config->options());
185 ASSERT_EQ(50U, config->rear_guard_bytes());
186
187 ASSERT_TRUE(InitConfig("rear_guard")) << getFakeLogPrint();
188 ASSERT_EQ(REAR_GUARD | TRACK_ALLOCS, config->options());
189 ASSERT_EQ(32U, config->rear_guard_bytes());
190
191 ASSERT_STREQ("", getFakeLogBuf().c_str());
192 ASSERT_STREQ("", getFakeLogPrint().c_str());
193 }
194
TEST_F(MallocDebugConfigTest,guard)195 TEST_F(MallocDebugConfigTest, guard) {
196 ASSERT_TRUE(InitConfig("guard=32")) << getFakeLogPrint();
197 ASSERT_EQ(FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, config->options());
198 ASSERT_EQ(32U, config->front_guard_bytes());
199 ASSERT_EQ(32U, config->rear_guard_bytes());
200
201 ASSERT_TRUE(InitConfig("guard")) << getFakeLogPrint();
202 ASSERT_EQ(FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, config->options());
203 ASSERT_EQ(32U, config->front_guard_bytes());
204 ASSERT_EQ(32U, config->rear_guard_bytes());
205
206 ASSERT_STREQ("", getFakeLogBuf().c_str());
207 ASSERT_STREQ("", getFakeLogPrint().c_str());
208 }
209
TEST_F(MallocDebugConfigTest,backtrace)210 TEST_F(MallocDebugConfigTest, backtrace) {
211 ASSERT_TRUE(InitConfig("backtrace=64")) << getFakeLogPrint();
212 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
213 ASSERT_EQ(64U, config->backtrace_frames());
214 ASSERT_TRUE(config->backtrace_enabled());
215 ASSERT_FALSE(config->backtrace_enable_on_signal());
216 ASSERT_FALSE(config->backtrace_dump_on_exit());
217
218 ASSERT_TRUE(InitConfig("backtrace")) << getFakeLogPrint();
219 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
220 ASSERT_EQ(16U, config->backtrace_frames());
221 ASSERT_TRUE(config->backtrace_enabled());
222 ASSERT_FALSE(config->backtrace_enable_on_signal());
223 ASSERT_FALSE(config->backtrace_dump_on_exit());
224
225 ASSERT_STREQ("", getFakeLogBuf().c_str());
226 ASSERT_STREQ("", getFakeLogPrint().c_str());
227 }
228
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal)229 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal) {
230 ASSERT_TRUE(InitConfig("backtrace_enable_on_signal=64")) << getFakeLogPrint();
231 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
232 ASSERT_EQ(64U, config->backtrace_frames());
233 ASSERT_FALSE(config->backtrace_enabled());
234 ASSERT_TRUE(config->backtrace_enable_on_signal());
235 ASSERT_FALSE(config->backtrace_dump_on_exit());
236
237 ASSERT_TRUE(InitConfig("backtrace_enable_on_signal")) << getFakeLogPrint();
238 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
239 ASSERT_EQ(16U, config->backtrace_frames());
240 ASSERT_FALSE(config->backtrace_enabled());
241 ASSERT_TRUE(config->backtrace_enable_on_signal());
242 ASSERT_FALSE(config->backtrace_dump_on_exit());
243
244 ASSERT_STREQ("", getFakeLogBuf().c_str());
245 ASSERT_STREQ("", getFakeLogPrint().c_str());
246 }
247
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal_init)248 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_init) {
249 ASSERT_TRUE(InitConfig("backtrace_enable_on_signal=64")) << getFakeLogPrint();
250 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
251 ASSERT_EQ(64U, config->backtrace_frames());
252 ASSERT_FALSE(config->backtrace_enabled());
253 ASSERT_TRUE(config->backtrace_enable_on_signal());
254 ASSERT_FALSE(config->backtrace_dump_on_exit());
255
256 ASSERT_TRUE(InitConfig("backtrace")) << getFakeLogPrint();
257 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
258 ASSERT_EQ(16U, config->backtrace_frames());
259 ASSERT_TRUE(config->backtrace_enabled());
260 ASSERT_FALSE(config->backtrace_enable_on_signal());
261 ASSERT_FALSE(config->backtrace_dump_on_exit());
262
263 ASSERT_STREQ("", getFakeLogBuf().c_str());
264 ASSERT_STREQ("", getFakeLogPrint().c_str());
265 }
266
TEST_F(MallocDebugConfigTest,backtrace_enable_and_backtrace)267 TEST_F(MallocDebugConfigTest, backtrace_enable_and_backtrace) {
268 ASSERT_TRUE(InitConfig("backtrace_enable_on_signal backtrace")) << getFakeLogPrint();
269 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
270 ASSERT_EQ(16U, config->backtrace_frames());
271 ASSERT_TRUE(config->backtrace_enabled());
272 ASSERT_TRUE(config->backtrace_enable_on_signal());
273 ASSERT_FALSE(config->backtrace_dump_on_exit());
274
275 ASSERT_TRUE(InitConfig("backtrace backtrace_enable_on_signal")) << getFakeLogPrint();
276 ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
277 ASSERT_EQ(16U, config->backtrace_frames());
278 ASSERT_TRUE(config->backtrace_enabled());
279 ASSERT_TRUE(config->backtrace_enable_on_signal());
280 ASSERT_FALSE(config->backtrace_dump_on_exit());
281
282 ASSERT_STREQ("", getFakeLogBuf().c_str());
283 ASSERT_STREQ("", getFakeLogPrint().c_str());
284 }
285
TEST_F(MallocDebugConfigTest,backtrace_dump_on_exit)286 TEST_F(MallocDebugConfigTest, backtrace_dump_on_exit) {
287 ASSERT_TRUE(InitConfig("backtrace_dump_on_exit")) << getFakeLogPrint();
288 ASSERT_EQ(0U, config->options());
289 ASSERT_TRUE(config->backtrace_dump_on_exit());
290
291 ASSERT_STREQ("", getFakeLogBuf().c_str());
292 ASSERT_STREQ("", getFakeLogPrint().c_str());
293 }
294
TEST_F(MallocDebugConfigTest,backtrace_dump_on_exit_error)295 TEST_F(MallocDebugConfigTest, backtrace_dump_on_exit_error) {
296 ASSERT_FALSE(InitConfig("backtrace_dump_on_exit=something")) << getFakeLogPrint();
297
298 ASSERT_STREQ("", getFakeLogBuf().c_str());
299 std::string log_msg(
300 "6 malloc_debug malloc_testing: value set for option 'backtrace_dump_on_exit' "
301 "which does not take a value\n");
302 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
303 }
304
TEST_F(MallocDebugConfigTest,backtrace_dump_prefix)305 TEST_F(MallocDebugConfigTest, backtrace_dump_prefix) {
306 ASSERT_TRUE(InitConfig("backtrace_dump_prefix")) << getFakeLogPrint();
307 ASSERT_EQ(0U, config->options());
308 ASSERT_EQ("/data/local/tmp/backtrace_heap", config->backtrace_dump_prefix());
309
310 ASSERT_TRUE(InitConfig("backtrace_dump_prefix=/fake/location")) << getFakeLogPrint();
311 ASSERT_EQ(0U, config->options());
312 ASSERT_EQ("/fake/location", config->backtrace_dump_prefix());
313
314 ASSERT_STREQ("", getFakeLogBuf().c_str());
315 ASSERT_STREQ("", getFakeLogPrint().c_str());
316 }
317
TEST_F(MallocDebugConfigTest,backtrace_full)318 TEST_F(MallocDebugConfigTest, backtrace_full) {
319 ASSERT_TRUE(InitConfig("backtrace_full")) << getFakeLogPrint();
320 ASSERT_EQ(BACKTRACE_FULL, config->options());
321
322 ASSERT_STREQ("", getFakeLogBuf().c_str());
323 ASSERT_STREQ("", getFakeLogPrint().c_str());
324 }
325
TEST_F(MallocDebugConfigTest,backtrace_full_fail)326 TEST_F(MallocDebugConfigTest, backtrace_full_fail) {
327 ASSERT_FALSE(InitConfig("backtrace_full=200")) << getFakeLogPrint();
328
329 ASSERT_STREQ("", getFakeLogBuf().c_str());
330 std::string log_msg(
331 "6 malloc_debug malloc_testing: value set for option 'backtrace_full' "
332 "which does not take a value\n");
333 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
334 }
335
TEST_F(MallocDebugConfigTest,fill_on_alloc)336 TEST_F(MallocDebugConfigTest, fill_on_alloc) {
337 ASSERT_TRUE(InitConfig("fill_on_alloc=64")) << getFakeLogPrint();
338 ASSERT_EQ(FILL_ON_ALLOC, config->options());
339 ASSERT_EQ(64U, config->fill_on_alloc_bytes());
340
341 ASSERT_TRUE(InitConfig("fill_on_alloc")) << getFakeLogPrint();
342 ASSERT_EQ(FILL_ON_ALLOC, config->options());
343 ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes());
344
345 ASSERT_STREQ("", getFakeLogBuf().c_str());
346 ASSERT_STREQ("", getFakeLogPrint().c_str());
347 }
348
TEST_F(MallocDebugConfigTest,fill_on_free)349 TEST_F(MallocDebugConfigTest, fill_on_free) {
350 ASSERT_TRUE(InitConfig("fill_on_free=64")) << getFakeLogPrint();
351 ASSERT_EQ(FILL_ON_FREE, config->options());
352 ASSERT_EQ(64U, config->fill_on_free_bytes());
353
354 ASSERT_TRUE(InitConfig("fill_on_free")) << getFakeLogPrint();
355 ASSERT_EQ(FILL_ON_FREE, config->options());
356 ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
357
358 ASSERT_STREQ("", getFakeLogBuf().c_str());
359 ASSERT_STREQ("", getFakeLogPrint().c_str());
360 }
361
TEST_F(MallocDebugConfigTest,fill)362 TEST_F(MallocDebugConfigTest, fill) {
363 ASSERT_TRUE(InitConfig("fill=64")) << getFakeLogPrint();
364 ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options());
365 ASSERT_EQ(64U, config->fill_on_alloc_bytes());
366 ASSERT_EQ(64U, config->fill_on_free_bytes());
367
368 ASSERT_TRUE(InitConfig("fill")) << getFakeLogPrint();
369 ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options());
370 ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes());
371 ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
372
373 ASSERT_STREQ("", getFakeLogBuf().c_str());
374 ASSERT_STREQ("", getFakeLogPrint().c_str());
375 }
376
TEST_F(MallocDebugConfigTest,expand_alloc)377 TEST_F(MallocDebugConfigTest, expand_alloc) {
378 ASSERT_TRUE(InitConfig("expand_alloc=1234")) << getFakeLogPrint();
379 ASSERT_EQ(EXPAND_ALLOC, config->options());
380 ASSERT_EQ(1234U, config->expand_alloc_bytes());
381
382 ASSERT_TRUE(InitConfig("expand_alloc")) << getFakeLogPrint();
383 ASSERT_EQ(EXPAND_ALLOC, config->options());
384 ASSERT_EQ(16U, config->expand_alloc_bytes());
385
386 ASSERT_STREQ("", getFakeLogBuf().c_str());
387 ASSERT_STREQ("", getFakeLogPrint().c_str());
388 }
389
TEST_F(MallocDebugConfigTest,free_track)390 TEST_F(MallocDebugConfigTest, free_track) {
391 ASSERT_TRUE(InitConfig("free_track=1234")) << getFakeLogPrint();
392 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
393 ASSERT_EQ(1234U, config->free_track_allocations());
394 ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
395 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
396
397 ASSERT_TRUE(InitConfig("free_track")) << getFakeLogPrint();
398 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
399 ASSERT_EQ(100U, config->free_track_allocations());
400 ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
401 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
402
403 ASSERT_STREQ("", getFakeLogBuf().c_str());
404 ASSERT_STREQ("", getFakeLogPrint().c_str());
405 }
406
TEST_F(MallocDebugConfigTest,free_track_and_fill_on_free)407 TEST_F(MallocDebugConfigTest, free_track_and_fill_on_free) {
408 ASSERT_TRUE(InitConfig("free_track=1234 fill_on_free=32")) << getFakeLogPrint();
409 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
410 ASSERT_EQ(1234U, config->free_track_allocations());
411 ASSERT_EQ(32U, config->fill_on_free_bytes());
412 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
413
414 ASSERT_TRUE(InitConfig("free_track fill_on_free=60")) << getFakeLogPrint();
415 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
416 ASSERT_EQ(100U, config->free_track_allocations());
417 ASSERT_EQ(60U, config->fill_on_free_bytes());
418 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
419
420 // Now reverse the arguments.
421 ASSERT_TRUE(InitConfig("fill_on_free=32 free_track=1234")) << getFakeLogPrint();
422 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
423 ASSERT_EQ(1234U, config->free_track_allocations());
424 ASSERT_EQ(32U, config->fill_on_free_bytes());
425 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
426
427 ASSERT_STREQ("", getFakeLogBuf().c_str());
428 ASSERT_STREQ("", getFakeLogPrint().c_str());
429 }
430
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames)431 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames) {
432 ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=123")) << getFakeLogPrint();
433
434 ASSERT_EQ(0U, config->options());
435 ASSERT_EQ(123U, config->free_track_backtrace_num_frames());
436
437 ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames")) << getFakeLogPrint();
438 ASSERT_EQ(0U, config->options());
439 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
440
441 ASSERT_STREQ("", getFakeLogBuf().c_str());
442 ASSERT_STREQ("", getFakeLogPrint().c_str());
443 }
444
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_zero)445 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_zero) {
446 ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=0")) << getFakeLogPrint();
447
448 ASSERT_EQ(0U, config->options());
449 ASSERT_EQ(0U, config->free_track_backtrace_num_frames());
450
451 ASSERT_STREQ("", getFakeLogBuf().c_str());
452 ASSERT_STREQ("", getFakeLogPrint().c_str());
453 }
454
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_and_free_track)455 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_and_free_track) {
456 ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames=123")) << getFakeLogPrint();
457 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
458 ASSERT_EQ(123U, config->free_track_backtrace_num_frames());
459
460 ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames")) << getFakeLogPrint();
461 ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
462 ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
463
464 ASSERT_STREQ("", getFakeLogBuf().c_str());
465 ASSERT_STREQ("", getFakeLogPrint().c_str());
466 }
467
TEST_F(MallocDebugConfigTest,leak_track)468 TEST_F(MallocDebugConfigTest, leak_track) {
469 ASSERT_TRUE(InitConfig("leak_track")) << getFakeLogPrint();
470 ASSERT_EQ(LEAK_TRACK | TRACK_ALLOCS, config->options());
471
472 ASSERT_STREQ("", getFakeLogBuf().c_str());
473 ASSERT_STREQ("", getFakeLogPrint().c_str());
474 }
475
TEST_F(MallocDebugConfigTest,leak_track_fail)476 TEST_F(MallocDebugConfigTest, leak_track_fail) {
477 ASSERT_FALSE(InitConfig("leak_track=100")) << getFakeLogPrint();
478
479 ASSERT_STREQ("", getFakeLogBuf().c_str());
480 std::string log_msg(
481 "6 malloc_debug malloc_testing: value set for option 'leak_track' "
482 "which does not take a value\n");
483 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
484 }
485
TEST_F(MallocDebugConfigTest,verify_pointers)486 TEST_F(MallocDebugConfigTest, verify_pointers) {
487 ASSERT_TRUE(InitConfig("verify_pointers")) << getFakeLogPrint();
488 ASSERT_EQ(TRACK_ALLOCS, config->options());
489
490 ASSERT_STREQ("", getFakeLogBuf().c_str());
491 ASSERT_STREQ("", getFakeLogPrint().c_str());
492 }
493
TEST_F(MallocDebugConfigTest,verify_pointers_fail)494 TEST_F(MallocDebugConfigTest, verify_pointers_fail) {
495 ASSERT_FALSE(InitConfig("verify_pointers=200")) << getFakeLogPrint();
496
497 ASSERT_STREQ("", getFakeLogBuf().c_str());
498 std::string log_msg(
499 "6 malloc_debug malloc_testing: value set for option 'verify_pointers' "
500 "which does not take a value\n");
501 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
502 }
503
TEST_F(MallocDebugConfigTest,record_allocs)504 TEST_F(MallocDebugConfigTest, record_allocs) {
505 ASSERT_TRUE(InitConfig("record_allocs=1234")) << getFakeLogPrint();
506 ASSERT_EQ(RECORD_ALLOCS, config->options());
507 ASSERT_EQ(1234U, config->record_allocs_num_entries());
508 ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
509
510 ASSERT_TRUE(InitConfig("record_allocs")) << getFakeLogPrint();
511 ASSERT_EQ(RECORD_ALLOCS, config->options());
512 ASSERT_EQ(8000000U, config->record_allocs_num_entries());
513 ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
514
515 ASSERT_STREQ("", getFakeLogBuf().c_str());
516 ASSERT_STREQ("", getFakeLogPrint().c_str());
517 }
518
TEST_F(MallocDebugConfigTest,record_allocs_file)519 TEST_F(MallocDebugConfigTest, record_allocs_file) {
520 ASSERT_TRUE(InitConfig("record_allocs=1234 record_allocs_file=/fake/file")) << getFakeLogPrint();
521 ASSERT_STREQ("/fake/file", config->record_allocs_file().c_str());
522
523 ASSERT_TRUE(InitConfig("record_allocs_file")) << getFakeLogPrint();
524 ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
525
526 ASSERT_STREQ("", getFakeLogBuf().c_str());
527 ASSERT_STREQ("", getFakeLogPrint().c_str());
528 }
529
TEST_F(MallocDebugConfigTest,guard_min_error)530 TEST_F(MallocDebugConfigTest, guard_min_error) {
531 ASSERT_FALSE(InitConfig("guard=0"));
532
533 ASSERT_STREQ("", getFakeLogBuf().c_str());
534 std::string log_msg(
535 "6 malloc_debug malloc_testing: bad value for option 'guard', value must be >= 1: 0\n");
536 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
537 }
538
TEST_F(MallocDebugConfigTest,guard_max_error)539 TEST_F(MallocDebugConfigTest, guard_max_error) {
540 ASSERT_FALSE(InitConfig("guard=20000"));
541
542 ASSERT_STREQ("", getFakeLogBuf().c_str());
543 std::string log_msg(
544 "6 malloc_debug malloc_testing: bad value for option 'guard', "
545 "value must be <= 16384: 20000\n");
546 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
547 }
548
TEST_F(MallocDebugConfigTest,front_guard_min_error)549 TEST_F(MallocDebugConfigTest, front_guard_min_error) {
550 ASSERT_FALSE(InitConfig("front_guard=0"));
551
552 ASSERT_STREQ("", getFakeLogBuf().c_str());
553 std::string log_msg(
554 "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
555 "value must be >= 1: 0\n");
556 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
557 }
558
TEST_F(MallocDebugConfigTest,front_guard_max_error)559 TEST_F(MallocDebugConfigTest, front_guard_max_error) {
560 ASSERT_FALSE(InitConfig("front_guard=20000"));
561
562 ASSERT_STREQ("", getFakeLogBuf().c_str());
563 std::string log_msg(
564 "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
565 "value must be <= 16384: 20000\n");
566 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
567 }
568
TEST_F(MallocDebugConfigTest,rear_guard_min_error)569 TEST_F(MallocDebugConfigTest, rear_guard_min_error) {
570 ASSERT_FALSE(InitConfig("rear_guard=0"));
571
572 ASSERT_STREQ("", getFakeLogBuf().c_str());
573 std::string log_msg(
574 "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
575 "value must be >= 1: 0\n");
576 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
577 }
578
TEST_F(MallocDebugConfigTest,rear_guard_max_error)579 TEST_F(MallocDebugConfigTest, rear_guard_max_error) {
580 ASSERT_FALSE(InitConfig("rear_guard=20000"));
581
582 ASSERT_STREQ("", getFakeLogBuf().c_str());
583 std::string log_msg(
584 "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
585 "value must be <= 16384: 20000\n");
586 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
587 }
588
TEST_F(MallocDebugConfigTest,fill_min_error)589 TEST_F(MallocDebugConfigTest, fill_min_error) {
590 ASSERT_FALSE(InitConfig("fill=0"));
591
592 ASSERT_STREQ("", getFakeLogBuf().c_str());
593 std::string log_msg(
594 "6 malloc_debug malloc_testing: bad value for option 'fill', "
595 "value must be >= 1: 0\n");
596 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
597 }
598
TEST_F(MallocDebugConfigTest,fill_on_alloc_min_error)599 TEST_F(MallocDebugConfigTest, fill_on_alloc_min_error) {
600 ASSERT_FALSE(InitConfig("fill_on_alloc=0"));
601
602 ASSERT_STREQ("", getFakeLogBuf().c_str());
603 std::string log_msg(
604 "6 malloc_debug malloc_testing: bad value for option 'fill_on_alloc', "
605 "value must be >= 1: 0\n");
606 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
607 }
608
TEST_F(MallocDebugConfigTest,fill_on_free_min_error)609 TEST_F(MallocDebugConfigTest, fill_on_free_min_error) {
610 ASSERT_FALSE(InitConfig("fill_on_free=0"));
611
612 ASSERT_STREQ("", getFakeLogBuf().c_str());
613 std::string log_msg(
614 "6 malloc_debug malloc_testing: bad value for option 'fill_on_free', "
615 "value must be >= 1: 0\n");
616 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
617 }
618
TEST_F(MallocDebugConfigTest,backtrace_min_error)619 TEST_F(MallocDebugConfigTest, backtrace_min_error) {
620 ASSERT_FALSE(InitConfig("backtrace=0"));
621
622 ASSERT_STREQ("", getFakeLogBuf().c_str());
623 std::string log_msg(
624 "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
625 "value must be >= 1: 0\n");
626 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
627 }
628
TEST_F(MallocDebugConfigTest,backtrace_max_error)629 TEST_F(MallocDebugConfigTest, backtrace_max_error) {
630 ASSERT_FALSE(InitConfig("backtrace=300"));
631
632 ASSERT_STREQ("", getFakeLogBuf().c_str());
633 std::string log_msg(
634 "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
635 "value must be <= 256: 300\n");
636 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
637 }
638
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal_min_error)639 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_min_error) {
640 ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=0"));
641
642 ASSERT_STREQ("", getFakeLogBuf().c_str());
643 std::string log_msg(
644 "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
645 "value must be >= 1: 0\n");
646 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
647 }
648
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal_max_error)649 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_max_error) {
650 ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=300"));
651
652 ASSERT_STREQ("", getFakeLogBuf().c_str());
653 std::string log_msg(
654 "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
655 "value must be <= 256: 300\n");
656 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
657 }
658
TEST_F(MallocDebugConfigTest,expand_alloc_min_error)659 TEST_F(MallocDebugConfigTest, expand_alloc_min_error) {
660 ASSERT_FALSE(InitConfig("expand_alloc=0"));
661
662 ASSERT_STREQ("", getFakeLogBuf().c_str());
663 std::string log_msg(
664 "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
665 "value must be >= 1: 0\n");
666 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
667 }
668
TEST_F(MallocDebugConfigTest,expand_alloc_max_error)669 TEST_F(MallocDebugConfigTest, expand_alloc_max_error) {
670 ASSERT_FALSE(InitConfig("expand_alloc=21000"));
671
672 ASSERT_STREQ("", getFakeLogBuf().c_str());
673 std::string log_msg(
674 "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
675 "value must be <= 16384: 21000\n");
676 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
677 }
678
TEST_F(MallocDebugConfigTest,free_track_min_error)679 TEST_F(MallocDebugConfigTest, free_track_min_error) {
680 ASSERT_FALSE(InitConfig("free_track=0"));
681
682 ASSERT_STREQ("", getFakeLogBuf().c_str());
683 std::string log_msg(
684 "6 malloc_debug malloc_testing: bad value for option 'free_track', "
685 "value must be >= 1: 0\n");
686 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
687 }
688
TEST_F(MallocDebugConfigTest,free_track_max_error)689 TEST_F(MallocDebugConfigTest, free_track_max_error) {
690 ASSERT_FALSE(InitConfig("free_track=21000"));
691
692 ASSERT_STREQ("", getFakeLogBuf().c_str());
693 std::string log_msg(
694 "6 malloc_debug malloc_testing: bad value for option 'free_track', "
695 "value must be <= 16384: 21000\n");
696 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
697 }
698
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_max_error)699 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_max_error) {
700 ASSERT_FALSE(InitConfig("free_track_backtrace_num_frames=400"));
701
702 ASSERT_STREQ("", getFakeLogBuf().c_str());
703 std::string log_msg(
704 "6 malloc_debug malloc_testing: bad value for option 'free_track_backtrace_num_frames', "
705 "value must be <= 256: 400\n");
706 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
707 }
708
TEST_F(MallocDebugConfigTest,record_alloc_min_error)709 TEST_F(MallocDebugConfigTest, record_alloc_min_error) {
710 ASSERT_FALSE(InitConfig("record_allocs=0"));
711
712 ASSERT_STREQ("", getFakeLogBuf().c_str());
713 std::string log_msg(
714 "6 malloc_debug malloc_testing: bad value for option 'record_allocs', "
715 "value must be >= 1: 0\n");
716 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
717 }
718
TEST_F(MallocDebugConfigTest,record_allocs_max_error)719 TEST_F(MallocDebugConfigTest, record_allocs_max_error) {
720 ASSERT_FALSE(InitConfig("record_allocs=100000000"));
721
722 ASSERT_STREQ("", getFakeLogBuf().c_str());
723 std::string log_msg(
724 "6 malloc_debug malloc_testing: bad value for option 'record_allocs', "
725 "value must be <= 50000000: 100000000\n");
726 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
727 }
728
TEST_F(MallocDebugConfigTest,abort_on_error)729 TEST_F(MallocDebugConfigTest, abort_on_error) {
730 ASSERT_TRUE(InitConfig("abort_on_error")) << getFakeLogPrint();
731 ASSERT_EQ(ABORT_ON_ERROR, config->options());
732
733 ASSERT_STREQ("", getFakeLogBuf().c_str());
734 ASSERT_STREQ("", getFakeLogPrint().c_str());
735 }
736
TEST_F(MallocDebugConfigTest,trigger_abort_fail)737 TEST_F(MallocDebugConfigTest, trigger_abort_fail) {
738 ASSERT_FALSE(InitConfig("abort_on_error=200")) << getFakeLogPrint();
739
740 ASSERT_STREQ("", getFakeLogBuf().c_str());
741 std::string log_msg(
742 "6 malloc_debug malloc_testing: value set for option 'abort_on_error' "
743 "which does not take a value\n");
744 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
745 }
746
TEST_F(MallocDebugConfigTest,verbose)747 TEST_F(MallocDebugConfigTest, verbose) {
748 ASSERT_TRUE(InitConfig("verbose")) << getFakeLogPrint();
749 ASSERT_EQ(VERBOSE, config->options());
750
751 ASSERT_STREQ("", getFakeLogBuf().c_str());
752 ASSERT_STREQ("", getFakeLogPrint().c_str());
753 }
754
TEST_F(MallocDebugConfigTest,trigger_verbose_fail)755 TEST_F(MallocDebugConfigTest, trigger_verbose_fail) {
756 ASSERT_FALSE(InitConfig("verbose=200")) << getFakeLogPrint();
757
758 ASSERT_STREQ("", getFakeLogBuf().c_str());
759 std::string log_msg(
760 "6 malloc_debug malloc_testing: value set for option 'verbose' "
761 "which does not take a value\n");
762 ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
763 }
764