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 extern "C" int property_set(const char*, const char*);
29 
30 class MallocDebugConfigTest : public ::testing::Test {
31  protected:
SetUp()32   void SetUp() override {
33     resetLogs();
34   }
35 
TearDown()36   void TearDown() override {
37   }
38 
39   std::unique_ptr<Config> config;
40 
InitConfig(const char * property_value)41   bool InitConfig(const char* property_value) {
42     config.reset(new Config);
43     property_set("libc.debug.malloc.options", property_value);
44     return config->SetFromProperties();
45   }
46 };
47 
48 std::string usage_string(
49   "6 malloc_debug malloc debug options usage:\n"
50   "6 malloc_debug \n"
51   "6 malloc_debug   front_guard[=XX]\n"
52   "6 malloc_debug     Enables a front guard on all allocations. If XX is set\n"
53   "6 malloc_debug     it sets the number of bytes in the guard. The default is\n"
54   "6 malloc_debug     32 bytes, the max bytes is 16384.\n"
55   "6 malloc_debug \n"
56   "6 malloc_debug   rear_guard[=XX]\n"
57   "6 malloc_debug     Enables a rear guard on all allocations. If XX is set\n"
58   "6 malloc_debug     it sets the number of bytes in the guard. The default is\n"
59   "6 malloc_debug     32 bytes, the max bytes is 16384.\n"
60   "6 malloc_debug \n"
61   "6 malloc_debug   guard[=XX]\n"
62   "6 malloc_debug     Enables both a front guard and a rear guard on all allocations.\n"
63   "6 malloc_debug     If XX is set it sets the number of bytes in both guards.\n"
64   "6 malloc_debug     The default is 32 bytes, the max bytes is 16384.\n"
65   "6 malloc_debug \n"
66   "6 malloc_debug   backtrace[=XX]\n"
67   "6 malloc_debug     Enable capturing the backtrace at the point of allocation.\n"
68   "6 malloc_debug     If XX is set it sets the number of backtrace frames.\n"
69   "6 malloc_debug     The default is 16 frames, the max number of frames is 256.\n"
70   "6 malloc_debug \n"
71   "6 malloc_debug   backtrace_enable_on_signal[=XX]\n"
72   "6 malloc_debug     Enable capturing the backtrace at the point of allocation.\n"
73   "6 malloc_debug     The backtrace capture is not enabled until the process\n"
74   "6 malloc_debug     receives a signal. If XX is set it sets the number of backtrace\n"
75   "6 malloc_debug     frames. The default is 16 frames, the max number of frames is 256.\n"
76   "6 malloc_debug \n"
77   "6 malloc_debug   fill_on_alloc[=XX]\n"
78   "6 malloc_debug     On first allocation, fill with the value 0xeb.\n"
79   "6 malloc_debug     If XX is set it will only fill up to XX bytes of the\n"
80   "6 malloc_debug     allocation. The default is to fill the entire allocation.\n"
81   "6 malloc_debug \n"
82   "6 malloc_debug   fill_on_free[=XX]\n"
83   "6 malloc_debug     On free, fill with the value 0xef. If XX is set it will\n"
84   "6 malloc_debug     only fill up to XX bytes of the allocation. The default is to\n"
85   "6 malloc_debug     fill the entire allocation.\n"
86   "6 malloc_debug \n"
87   "6 malloc_debug   fill[=XX]\n"
88   "6 malloc_debug     On both first allocation free, fill with the value 0xeb on\n"
89   "6 malloc_debug     first allocation and the value 0xef. If XX is set, only fill\n"
90   "6 malloc_debug     up to XX bytes. The default is to fill the entire allocation.\n"
91   "6 malloc_debug \n"
92   "6 malloc_debug   expand_alloc[=XX]\n"
93   "6 malloc_debug     Allocate an extra number of bytes for every allocation call.\n"
94   "6 malloc_debug     If XX is set, that is the number of bytes to expand the\n"
95   "6 malloc_debug     allocation by. The default is 16 bytes, the max bytes is 16384.\n"
96   "6 malloc_debug \n"
97   "6 malloc_debug   free_track[=XX]\n"
98   "6 malloc_debug     When a pointer is freed, do not free the memory right away.\n"
99   "6 malloc_debug     Instead, keep XX of these allocations around and then verify\n"
100   "6 malloc_debug     that they have not been modified when the total number of freed\n"
101   "6 malloc_debug     allocations exceeds the XX amount. When the program terminates,\n"
102   "6 malloc_debug     the rest of these allocations are verified. When this option is\n"
103   "6 malloc_debug     enabled, it automatically records the backtrace at the time of the free.\n"
104   "6 malloc_debug     The default is to record 100 allocations, the max allocations\n"
105   "6 malloc_debug     to record is 16384.\n"
106   "6 malloc_debug \n"
107   "6 malloc_debug   free_track_backtrace_num_frames[=XX]\n"
108   "6 malloc_debug     This option only has meaning if free_track is set. This indicates\n"
109   "6 malloc_debug     how many backtrace frames to capture when an allocation is freed.\n"
110   "6 malloc_debug     If XX is set, that is the number of frames to capture. If XX\n"
111   "6 malloc_debug     is set to zero, then no backtrace will be captured.\n"
112   "6 malloc_debug     The default is to record 16 frames, the max number of frames is 256.\n"
113   "6 malloc_debug \n"
114   "6 malloc_debug   leak_track\n"
115   "6 malloc_debug     Enable the leak tracking of memory allocations.\n"
116 );
117 
TEST_F(MallocDebugConfigTest,unknown_option)118 TEST_F(MallocDebugConfigTest, unknown_option) {
119 
120   ASSERT_FALSE(InitConfig("unknown_option"));
121 
122   ASSERT_STREQ("", getFakeLogBuf().c_str());
123   std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
124   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
125 }
126 
TEST_F(MallocDebugConfigTest,good_option_and_bad_option)127 TEST_F(MallocDebugConfigTest, good_option_and_bad_option) {
128   ASSERT_FALSE(InitConfig("backtrace unknown_option"));
129 
130   ASSERT_STREQ("", getFakeLogBuf().c_str());
131   std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
132   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
133 }
134 
TEST_F(MallocDebugConfigTest,unparseable_number)135 TEST_F(MallocDebugConfigTest, unparseable_number) {
136   ASSERT_FALSE(InitConfig("backtrace=XXX"));
137 
138   ASSERT_STREQ("", getFakeLogBuf().c_str());
139   std::string log_msg("6 malloc_debug malloc_testing: bad value for option 'backtrace'\n");
140   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
141 }
142 
TEST_F(MallocDebugConfigTest,illegal_value_zero)143 TEST_F(MallocDebugConfigTest, illegal_value_zero) {
144   ASSERT_FALSE(InitConfig("backtrace=0"));
145 
146   ASSERT_STREQ("", getFakeLogBuf().c_str());
147   std::string log_msg(
148       "6 malloc_debug malloc_testing: bad value for option 'backtrace', value must be >= 1: 0\n");
149   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
150 }
151 
TEST_F(MallocDebugConfigTest,no_space)152 TEST_F(MallocDebugConfigTest, no_space) {
153   ASSERT_FALSE(InitConfig("backtrace=10front_guard"));
154 
155   ASSERT_STREQ("", getFakeLogBuf().c_str());
156   std::string log_msg(
157       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
158       "non space found after option: front_guard\n");
159   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
160 }
161 
TEST_F(MallocDebugConfigTest,illegal_value_negative)162 TEST_F(MallocDebugConfigTest, illegal_value_negative) {
163   ASSERT_FALSE(InitConfig("backtrace=-1"));
164 
165   ASSERT_STREQ("", getFakeLogBuf().c_str());
166   std::string log_msg(
167       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
168       "value cannot be negative: -1\n");
169   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
170 }
171 
TEST_F(MallocDebugConfigTest,overflow)172 TEST_F(MallocDebugConfigTest, overflow) {
173   ASSERT_FALSE(InitConfig("backtrace=99999999999999999999"));
174 
175   ASSERT_STREQ("", getFakeLogBuf().c_str());
176   std::string log_msg(
177       "6 malloc_debug malloc_testing: bad value for option 'backtrace': "
178       "Math result not representable\n");
179   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
180 }
181 
TEST_F(MallocDebugConfigTest,set_value_error)182 TEST_F(MallocDebugConfigTest, set_value_error) {
183   ASSERT_FALSE(InitConfig("leak_track=12"));
184 
185   ASSERT_STREQ("", getFakeLogBuf().c_str());
186   std::string log_msg(
187       "6 malloc_debug malloc_testing: value set for option 'leak_track' "
188       "which does not take a value\n");
189   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
190 }
191 
TEST_F(MallocDebugConfigTest,space_before_equal)192 TEST_F(MallocDebugConfigTest, space_before_equal) {
193   ASSERT_TRUE(InitConfig("backtrace  =10"));
194   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
195   ASSERT_EQ(10U, config->backtrace_frames);
196 
197   ASSERT_STREQ("", getFakeLogBuf().c_str());
198   ASSERT_STREQ("", getFakeLogPrint().c_str());
199 }
200 
TEST_F(MallocDebugConfigTest,space_after_equal)201 TEST_F(MallocDebugConfigTest, space_after_equal) {
202   ASSERT_TRUE(InitConfig("backtrace=  10"));
203   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
204   ASSERT_EQ(10U, config->backtrace_frames);
205 
206   ASSERT_STREQ("", getFakeLogBuf().c_str());
207   ASSERT_STREQ("", getFakeLogPrint().c_str());
208 }
209 
TEST_F(MallocDebugConfigTest,extra_space)210 TEST_F(MallocDebugConfigTest, extra_space) {
211   ASSERT_TRUE(InitConfig("   backtrace=64   "));
212   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
213   ASSERT_EQ(64U, config->backtrace_frames);
214 
215   ASSERT_STREQ("", getFakeLogBuf().c_str());
216   ASSERT_STREQ("", getFakeLogPrint().c_str());
217 }
218 
TEST_F(MallocDebugConfigTest,multiple_options)219 TEST_F(MallocDebugConfigTest, multiple_options) {
220   ASSERT_TRUE(InitConfig("  backtrace=64   front_guard=48"));
221   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS | FRONT_GUARD, config->options);
222   ASSERT_EQ(64U, config->backtrace_frames);
223   ASSERT_EQ(48U, config->front_guard_bytes);
224 
225   ASSERT_STREQ("", getFakeLogBuf().c_str());
226   ASSERT_STREQ("", getFakeLogPrint().c_str());
227 }
228 
TEST_F(MallocDebugConfigTest,front_guard)229 TEST_F(MallocDebugConfigTest, front_guard) {
230   ASSERT_TRUE(InitConfig("front_guard=48"));
231   ASSERT_EQ(FRONT_GUARD, config->options);
232   ASSERT_EQ(48U, config->front_guard_bytes);
233 
234   ASSERT_TRUE(InitConfig("front_guard"));
235   ASSERT_EQ(FRONT_GUARD, config->options);
236   ASSERT_EQ(32U, config->front_guard_bytes);
237 
238   ASSERT_TRUE(InitConfig("front_guard=39"));
239   ASSERT_EQ(FRONT_GUARD, config->options);
240 #if defined(__LP64__)
241   ASSERT_EQ(48U, config->front_guard_bytes);
242 #else
243   ASSERT_EQ(40U, config->front_guard_bytes);
244 #endif
245 
246   ASSERT_TRUE(InitConfig("front_guard=41"));
247   ASSERT_EQ(FRONT_GUARD, config->options);
248   ASSERT_EQ(48U, config->front_guard_bytes);
249 
250   ASSERT_STREQ("", getFakeLogBuf().c_str());
251   ASSERT_STREQ("", getFakeLogPrint().c_str());
252 }
253 
TEST_F(MallocDebugConfigTest,rear_guard)254 TEST_F(MallocDebugConfigTest, rear_guard) {
255   ASSERT_TRUE(InitConfig("rear_guard=50"));
256   ASSERT_EQ(REAR_GUARD, config->options);
257   ASSERT_EQ(50U, config->rear_guard_bytes);
258 
259   ASSERT_TRUE(InitConfig("rear_guard"));
260   ASSERT_EQ(REAR_GUARD, config->options);
261   ASSERT_EQ(32U, config->rear_guard_bytes);
262 
263   ASSERT_STREQ("", getFakeLogBuf().c_str());
264   ASSERT_STREQ("", getFakeLogPrint().c_str());
265 }
266 
TEST_F(MallocDebugConfigTest,guard)267 TEST_F(MallocDebugConfigTest, guard) {
268   ASSERT_TRUE(InitConfig("guard=32"));
269   ASSERT_EQ(FRONT_GUARD | REAR_GUARD, config->options);
270   ASSERT_EQ(32U, config->front_guard_bytes);
271   ASSERT_EQ(32U, config->rear_guard_bytes);
272 
273   ASSERT_TRUE(InitConfig("guard"));
274   ASSERT_EQ(FRONT_GUARD | REAR_GUARD, config->options);
275   ASSERT_EQ(32U, config->front_guard_bytes);
276   ASSERT_EQ(32U, config->rear_guard_bytes);
277 
278   ASSERT_STREQ("", getFakeLogBuf().c_str());
279   ASSERT_STREQ("", getFakeLogPrint().c_str());
280 }
281 
TEST_F(MallocDebugConfigTest,backtrace)282 TEST_F(MallocDebugConfigTest, backtrace) {
283   ASSERT_TRUE(InitConfig("backtrace=64"));
284   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
285   ASSERT_EQ(64U, config->backtrace_frames);
286 
287   ASSERT_TRUE(InitConfig("backtrace"));
288   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
289   ASSERT_EQ(16U, config->backtrace_frames);
290 
291   ASSERT_STREQ("", getFakeLogBuf().c_str());
292   ASSERT_STREQ("", getFakeLogPrint().c_str());
293 }
294 
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal)295 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal) {
296   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal=64"));
297   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
298   ASSERT_EQ(64U, config->backtrace_frames);
299 
300   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal"));
301   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options);
302   ASSERT_EQ(16U, config->backtrace_frames);
303 
304   ASSERT_STREQ("", getFakeLogBuf().c_str());
305   ASSERT_STREQ("", getFakeLogPrint().c_str());
306 }
307 
TEST_F(MallocDebugConfigTest,fill_on_alloc)308 TEST_F(MallocDebugConfigTest, fill_on_alloc) {
309   ASSERT_TRUE(InitConfig("fill_on_alloc=64"));
310   ASSERT_EQ(FILL_ON_ALLOC, config->options);
311   ASSERT_EQ(64U, config->fill_on_alloc_bytes);
312 
313   ASSERT_TRUE(InitConfig("fill_on_alloc"));
314   ASSERT_EQ(FILL_ON_ALLOC, config->options);
315   ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes);
316 
317   ASSERT_STREQ("", getFakeLogBuf().c_str());
318   ASSERT_STREQ("", getFakeLogPrint().c_str());
319 }
320 
TEST_F(MallocDebugConfigTest,fill_on_free)321 TEST_F(MallocDebugConfigTest, fill_on_free) {
322   ASSERT_TRUE(InitConfig("fill_on_free=64"));
323   ASSERT_EQ(FILL_ON_FREE, config->options);
324   ASSERT_EQ(64U, config->fill_on_free_bytes);
325 
326   ASSERT_TRUE(InitConfig("fill_on_free"));
327   ASSERT_EQ(FILL_ON_FREE, config->options);
328   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
329 
330   ASSERT_STREQ("", getFakeLogBuf().c_str());
331   ASSERT_STREQ("", getFakeLogPrint().c_str());
332 }
333 
TEST_F(MallocDebugConfigTest,fill)334 TEST_F(MallocDebugConfigTest, fill) {
335   ASSERT_TRUE(InitConfig("fill=64"));
336   ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options);
337   ASSERT_EQ(64U, config->fill_on_alloc_bytes);
338   ASSERT_EQ(64U, config->fill_on_free_bytes);
339 
340   ASSERT_TRUE(InitConfig("fill"));
341   ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options);
342   ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes);
343   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
344 
345   ASSERT_STREQ("", getFakeLogBuf().c_str());
346   ASSERT_STREQ("", getFakeLogPrint().c_str());
347 }
348 
TEST_F(MallocDebugConfigTest,expand_alloc)349 TEST_F(MallocDebugConfigTest, expand_alloc) {
350   ASSERT_TRUE(InitConfig("expand_alloc=1234"));
351   ASSERT_EQ(EXPAND_ALLOC, config->options);
352   ASSERT_EQ(1234U, config->expand_alloc_bytes);
353 
354   ASSERT_TRUE(InitConfig("expand_alloc"));
355   ASSERT_EQ(EXPAND_ALLOC, config->options);
356   ASSERT_EQ(16U, config->expand_alloc_bytes);
357 
358   ASSERT_STREQ("", getFakeLogBuf().c_str());
359   ASSERT_STREQ("", getFakeLogPrint().c_str());
360 }
361 
TEST_F(MallocDebugConfigTest,free_track)362 TEST_F(MallocDebugConfigTest, free_track) {
363   ASSERT_TRUE(InitConfig("free_track=1234"));
364   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
365   ASSERT_EQ(1234U, config->free_track_allocations);
366   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
367   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
368 
369   ASSERT_TRUE(InitConfig("free_track"));
370   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
371   ASSERT_EQ(100U, config->free_track_allocations);
372   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
373   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
374 
375   ASSERT_STREQ("", getFakeLogBuf().c_str());
376   ASSERT_STREQ("", getFakeLogPrint().c_str());
377 }
378 
TEST_F(MallocDebugConfigTest,free_track_and_fill_on_free)379 TEST_F(MallocDebugConfigTest, free_track_and_fill_on_free) {
380   ASSERT_TRUE(InitConfig("free_track=1234 fill_on_free=32"));
381   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
382   ASSERT_EQ(1234U, config->free_track_allocations);
383   ASSERT_EQ(32U, config->fill_on_free_bytes);
384   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
385 
386   ASSERT_TRUE(InitConfig("free_track fill_on_free=60"));
387   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
388   ASSERT_EQ(100U, config->free_track_allocations);
389   ASSERT_EQ(60U, config->fill_on_free_bytes);
390   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
391 
392   ASSERT_STREQ("", getFakeLogBuf().c_str());
393   ASSERT_STREQ("", getFakeLogPrint().c_str());
394 }
395 
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames)396 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames) {
397   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=123"));
398 
399   ASSERT_EQ(0U, config->options);
400   ASSERT_EQ(123U, config->free_track_backtrace_num_frames);
401 
402   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames"));
403   ASSERT_EQ(0U, config->options);
404   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
405 
406   ASSERT_STREQ("", getFakeLogBuf().c_str());
407   ASSERT_STREQ("", getFakeLogPrint().c_str());
408 }
409 
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_zero)410 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_zero) {
411   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=0"));
412 
413   ASSERT_EQ(0U, config->options);
414   ASSERT_EQ(0U, config->free_track_backtrace_num_frames);
415 
416   ASSERT_STREQ("", getFakeLogBuf().c_str());
417   ASSERT_STREQ("", getFakeLogPrint().c_str());
418 }
419 
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_and_free_track)420 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_and_free_track) {
421   ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames=123"));
422   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
423   ASSERT_EQ(123U, config->free_track_backtrace_num_frames);
424 
425   ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames"));
426   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
427   ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
428 
429   ASSERT_STREQ("", getFakeLogBuf().c_str());
430   ASSERT_STREQ("", getFakeLogPrint().c_str());
431 }
432 
TEST_F(MallocDebugConfigTest,leak_track)433 TEST_F(MallocDebugConfigTest, leak_track) {
434   ASSERT_TRUE(InitConfig("leak_track"));
435   ASSERT_EQ(LEAK_TRACK | TRACK_ALLOCS, config->options);
436 
437   ASSERT_STREQ("", getFakeLogBuf().c_str());
438   ASSERT_STREQ("", getFakeLogPrint().c_str());
439 }
440 
TEST_F(MallocDebugConfigTest,leak_track_fail)441 TEST_F(MallocDebugConfigTest, leak_track_fail) {
442   ASSERT_FALSE(InitConfig("leak_track=100"));
443 
444   ASSERT_STREQ("", getFakeLogBuf().c_str());
445   std::string log_msg(
446       "6 malloc_debug malloc_testing: value set for option 'leak_track' "
447       "which does not take a value\n");
448   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
449 }
450 
TEST_F(MallocDebugConfigTest,guard_min_error)451 TEST_F(MallocDebugConfigTest, guard_min_error) {
452   ASSERT_FALSE(InitConfig("guard=0"));
453 
454   ASSERT_STREQ("", getFakeLogBuf().c_str());
455   std::string log_msg(
456       "6 malloc_debug malloc_testing: bad value for option 'guard', value must be >= 1: 0\n");
457   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
458 }
459 
TEST_F(MallocDebugConfigTest,guard_max_error)460 TEST_F(MallocDebugConfigTest, guard_max_error) {
461   ASSERT_FALSE(InitConfig("guard=20000"));
462 
463   ASSERT_STREQ("", getFakeLogBuf().c_str());
464   std::string log_msg(
465       "6 malloc_debug malloc_testing: bad value for option 'guard', "
466       "value must be <= 16384: 20000\n");
467   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
468 }
469 
TEST_F(MallocDebugConfigTest,front_guard_min_error)470 TEST_F(MallocDebugConfigTest, front_guard_min_error) {
471   ASSERT_FALSE(InitConfig("front_guard=0"));
472 
473   ASSERT_STREQ("", getFakeLogBuf().c_str());
474   std::string log_msg(
475       "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
476       "value must be >= 1: 0\n");
477   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
478 }
479 
TEST_F(MallocDebugConfigTest,front_guard_max_error)480 TEST_F(MallocDebugConfigTest, front_guard_max_error) {
481   ASSERT_FALSE(InitConfig("front_guard=20000"));
482 
483   ASSERT_STREQ("", getFakeLogBuf().c_str());
484   std::string log_msg(
485       "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
486       "value must be <= 16384: 20000\n");
487   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
488 }
489 
TEST_F(MallocDebugConfigTest,rear_guard_min_error)490 TEST_F(MallocDebugConfigTest, rear_guard_min_error) {
491   ASSERT_FALSE(InitConfig("rear_guard=0"));
492 
493   ASSERT_STREQ("", getFakeLogBuf().c_str());
494   std::string log_msg(
495       "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
496       "value must be >= 1: 0\n");
497   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
498 }
499 
TEST_F(MallocDebugConfigTest,rear_guard_max_error)500 TEST_F(MallocDebugConfigTest, rear_guard_max_error) {
501   ASSERT_FALSE(InitConfig("rear_guard=20000"));
502 
503   ASSERT_STREQ("", getFakeLogBuf().c_str());
504   std::string log_msg(
505       "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
506       "value must be <= 16384: 20000\n");
507   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
508 }
509 
TEST_F(MallocDebugConfigTest,fill_min_error)510 TEST_F(MallocDebugConfigTest, fill_min_error) {
511   ASSERT_FALSE(InitConfig("fill=0"));
512 
513   ASSERT_STREQ("", getFakeLogBuf().c_str());
514   std::string log_msg(
515       "6 malloc_debug malloc_testing: bad value for option 'fill', "
516       "value must be >= 1: 0\n");
517   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
518 }
519 
TEST_F(MallocDebugConfigTest,fill_on_alloc_min_error)520 TEST_F(MallocDebugConfigTest, fill_on_alloc_min_error) {
521   ASSERT_FALSE(InitConfig("fill_on_alloc=0"));
522 
523   ASSERT_STREQ("", getFakeLogBuf().c_str());
524   std::string log_msg(
525       "6 malloc_debug malloc_testing: bad value for option 'fill_on_alloc', "
526       "value must be >= 1: 0\n");
527   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
528 }
529 
TEST_F(MallocDebugConfigTest,fill_on_free_min_error)530 TEST_F(MallocDebugConfigTest, fill_on_free_min_error) {
531   ASSERT_FALSE(InitConfig("fill_on_free=0"));
532 
533   ASSERT_STREQ("", getFakeLogBuf().c_str());
534   std::string log_msg(
535       "6 malloc_debug malloc_testing: bad value for option 'fill_on_free', "
536       "value must be >= 1: 0\n");
537   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
538 }
539 
TEST_F(MallocDebugConfigTest,backtrace_min_error)540 TEST_F(MallocDebugConfigTest, backtrace_min_error) {
541   ASSERT_FALSE(InitConfig("backtrace=0"));
542 
543   ASSERT_STREQ("", getFakeLogBuf().c_str());
544   std::string log_msg(
545       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
546       "value must be >= 1: 0\n");
547   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
548 }
549 
TEST_F(MallocDebugConfigTest,backtrace_max_error)550 TEST_F(MallocDebugConfigTest, backtrace_max_error) {
551   ASSERT_FALSE(InitConfig("backtrace=300"));
552 
553   ASSERT_STREQ("", getFakeLogBuf().c_str());
554   std::string log_msg(
555       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
556       "value must be <= 256: 300\n");
557   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
558 }
559 
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal_min_error)560 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_min_error) {
561   ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=0"));
562 
563   ASSERT_STREQ("", getFakeLogBuf().c_str());
564   std::string log_msg(
565       "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
566       "value must be >= 1: 0\n");
567   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
568 }
569 
TEST_F(MallocDebugConfigTest,backtrace_enable_on_signal_max_error)570 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_max_error) {
571   ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=300"));
572 
573   ASSERT_STREQ("", getFakeLogBuf().c_str());
574   std::string log_msg(
575       "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
576       "value must be <= 256: 300\n");
577   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
578 }
579 
TEST_F(MallocDebugConfigTest,expand_alloc_min_error)580 TEST_F(MallocDebugConfigTest, expand_alloc_min_error) {
581   ASSERT_FALSE(InitConfig("expand_alloc=0"));
582 
583   ASSERT_STREQ("", getFakeLogBuf().c_str());
584   std::string log_msg(
585       "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
586       "value must be >= 1: 0\n");
587   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
588 }
589 
TEST_F(MallocDebugConfigTest,expand_alloc_max_error)590 TEST_F(MallocDebugConfigTest, expand_alloc_max_error) {
591   ASSERT_FALSE(InitConfig("expand_alloc=21000"));
592 
593   ASSERT_STREQ("", getFakeLogBuf().c_str());
594   std::string log_msg(
595       "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
596       "value must be <= 16384: 21000\n");
597   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
598 }
599 
TEST_F(MallocDebugConfigTest,free_track_min_error)600 TEST_F(MallocDebugConfigTest, free_track_min_error) {
601   ASSERT_FALSE(InitConfig("free_track=0"));
602 
603   ASSERT_STREQ("", getFakeLogBuf().c_str());
604   std::string log_msg(
605       "6 malloc_debug malloc_testing: bad value for option 'free_track', "
606       "value must be >= 1: 0\n");
607   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
608 }
609 
TEST_F(MallocDebugConfigTest,free_track_max_error)610 TEST_F(MallocDebugConfigTest, free_track_max_error) {
611   ASSERT_FALSE(InitConfig("free_track=21000"));
612 
613   ASSERT_STREQ("", getFakeLogBuf().c_str());
614   std::string log_msg(
615       "6 malloc_debug malloc_testing: bad value for option 'free_track', "
616       "value must be <= 16384: 21000\n");
617   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
618 }
619 
TEST_F(MallocDebugConfigTest,free_track_backtrace_num_frames_max_error)620 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_max_error) {
621   ASSERT_FALSE(InitConfig("free_track_backtrace_num_frames=400"));
622 
623   ASSERT_STREQ("", getFakeLogBuf().c_str());
624   std::string log_msg(
625       "6 malloc_debug malloc_testing: bad value for option 'free_track_backtrace_num_frames', "
626       "value must be <= 256: 400\n");
627   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
628 }
629