Home | History | Annotate | Download | only in tests
      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:
     30   void SetUp() override {
     31     resetLogs();
     32   }
     33 
     34   std::unique_ptr<Config> config;
     35 
     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 malloc debug options usage:\n"
     44   "6 malloc_debug \n"
     45   "6 malloc_debug   front_guard[=XX]\n"
     46   "6 malloc_debug     Enables a front guard on all allocations. If XX is set\n"
     47   "6 malloc_debug     it sets the number of bytes in the guard. The default is\n"
     48   "6 malloc_debug     32 bytes, the max bytes is 16384.\n"
     49   "6 malloc_debug \n"
     50   "6 malloc_debug   rear_guard[=XX]\n"
     51   "6 malloc_debug     Enables a rear guard on all allocations. If XX is set\n"
     52   "6 malloc_debug     it sets the number of bytes in the guard. The default is\n"
     53   "6 malloc_debug     32 bytes, the max bytes is 16384.\n"
     54   "6 malloc_debug \n"
     55   "6 malloc_debug   guard[=XX]\n"
     56   "6 malloc_debug     Enables both a front guard and a rear guard on all allocations.\n"
     57   "6 malloc_debug     If XX is set it sets the number of bytes in both guards.\n"
     58   "6 malloc_debug     The default is 32 bytes, the max bytes is 16384.\n"
     59   "6 malloc_debug \n"
     60   "6 malloc_debug   backtrace[=XX]\n"
     61   "6 malloc_debug     Enable capturing the backtrace at the point of allocation.\n"
     62   "6 malloc_debug     If XX is set it sets the number of backtrace frames.\n"
     63   "6 malloc_debug     This option also enables dumping the backtrace heap data\n"
     64   "6 malloc_debug     when a signal is received. The data is dumped to the file\n"
     65   "6 malloc_debug     backtrace_dump_prefix.<PID>.txt.\n"
     66   "6 malloc_debug     The default is 16 frames, the max number of frames is 256.\n"
     67   "6 malloc_debug \n"
     68   "6 malloc_debug   backtrace_enable_on_signal[=XX]\n"
     69   "6 malloc_debug     Enable capturing the backtrace at the point of allocation.\n"
     70   "6 malloc_debug     The backtrace capture is not enabled until the process\n"
     71   "6 malloc_debug     receives a signal. If XX is set it sets the number of backtrace\n"
     72   "6 malloc_debug     frames. The default is 16 frames, the max number of frames is 256.\n"
     73   "6 malloc_debug \n"
     74   "6 malloc_debug   backtrace_dump_prefix[=FILE]\n"
     75   "6 malloc_debug     This option only has meaning if the backtrace option has been specified.\n"
     76   "6 malloc_debug     This is the prefix of the name of the file to which backtrace heap\n"
     77   "6 malloc_debug     data will be dumped. The file will be named backtrace_dump_prefix.<PID>.txt.\n"
     78   "6 malloc_debug     The default is /data/local/tmp/backtrace_heap.\n"
     79   "6 malloc_debug \n"
     80   "6 malloc_debug   backtrace_dump_on_exit\n"
     81   "6 malloc_debug     This option only has meaning if the backtrace option has been specified.\n"
     82   "6 malloc_debug     This will cause all live allocations to be dumped to the file\n"
     83   "6 malloc_debug     backtrace_dump_prefix.<PID>.final.txt.\n"
     84   "6 malloc_debug     The default is false.\n"
     85   "6 malloc_debug \n"
     86   "6 malloc_debug   fill_on_alloc[=XX]\n"
     87   "6 malloc_debug     On first allocation, fill with the value 0xeb.\n"
     88   "6 malloc_debug     If XX is set it will only fill up to XX bytes of the\n"
     89   "6 malloc_debug     allocation. The default is to fill the entire allocation.\n"
     90   "6 malloc_debug \n"
     91   "6 malloc_debug   fill_on_free[=XX]\n"
     92   "6 malloc_debug     On free, fill with the value 0xef. If XX is set it will\n"
     93   "6 malloc_debug     only fill up to XX bytes of the allocation. The default is to\n"
     94   "6 malloc_debug     fill the entire allocation.\n"
     95   "6 malloc_debug \n"
     96   "6 malloc_debug   fill[=XX]\n"
     97   "6 malloc_debug     On both first allocation free, fill with the value 0xeb on\n"
     98   "6 malloc_debug     first allocation and the value 0xef. If XX is set, only fill\n"
     99   "6 malloc_debug     up to XX bytes. The default is to fill the entire allocation.\n"
    100   "6 malloc_debug \n"
    101   "6 malloc_debug   expand_alloc[=XX]\n"
    102   "6 malloc_debug     Allocate an extra number of bytes for every allocation call.\n"
    103   "6 malloc_debug     If XX is set, that is the number of bytes to expand the\n"
    104   "6 malloc_debug     allocation by. The default is 16 bytes, the max bytes is 16384.\n"
    105   "6 malloc_debug \n"
    106   "6 malloc_debug   free_track[=XX]\n"
    107   "6 malloc_debug     When a pointer is freed, do not free the memory right away.\n"
    108   "6 malloc_debug     Instead, keep XX of these allocations around and then verify\n"
    109   "6 malloc_debug     that they have not been modified when the total number of freed\n"
    110   "6 malloc_debug     allocations exceeds the XX amount. When the program terminates,\n"
    111   "6 malloc_debug     the rest of these allocations are verified. When this option is\n"
    112   "6 malloc_debug     enabled, it automatically records the backtrace at the time of the free.\n"
    113   "6 malloc_debug     The default is to record 100 allocations, the max allocations\n"
    114   "6 malloc_debug     to record is 16384.\n"
    115   "6 malloc_debug \n"
    116   "6 malloc_debug   free_track_backtrace_num_frames[=XX]\n"
    117   "6 malloc_debug     This option only has meaning if free_track is set. This indicates\n"
    118   "6 malloc_debug     how many backtrace frames to capture when an allocation is freed.\n"
    119   "6 malloc_debug     If XX is set, that is the number of frames to capture. If XX\n"
    120   "6 malloc_debug     is set to zero, then no backtrace will be captured.\n"
    121   "6 malloc_debug     The default is to record 16 frames, the max number of frames is 256.\n"
    122   "6 malloc_debug \n"
    123   "6 malloc_debug   leak_track\n"
    124   "6 malloc_debug     Enable the leak tracking of memory allocations.\n"
    125   "6 malloc_debug \n"
    126   "6 malloc_debug   record_allocs[=XX]\n"
    127   "6 malloc_debug     Record every single allocation/free call. When a specific signal\n"
    128   "6 malloc_debug     is sent to the process, the contents of recording are written to\n"
    129   "6 malloc_debug     a file (/data/local/tmp/record_allocs.txt) and the recording is cleared.\n"
    130   "6 malloc_debug     If XX is set, that is the total number of allocations/frees that can\n"
    131   "6 malloc_debug     recorded. of frames to capture. The default value is 8000000.\n"
    132   "6 malloc_debug     If the allocation list fills up, all further allocations are not recorded.\n"
    133   "6 malloc_debug \n"
    134   "6 malloc_debug   record_allocs_file[=FILE]\n"
    135   "6 malloc_debug     This option only has meaning if the record_allocs options has been specified.\n"
    136   "6 malloc_debug     This is the name of the file to which recording information will be dumped.\n"
    137   "6 malloc_debug     The default is /data/local/tmp/record_allocs.txt.\n"
    138   "6 malloc_debug \n"
    139   "6 malloc_debug   verify_pointers\n"
    140   "6 malloc_debug     A lightweight way to verify that free/malloc_usable_size/realloc\n"
    141   "6 malloc_debug     are passed valid pointers.\n"
    142 );
    143 
    144 TEST_F(MallocDebugConfigTest, unknown_option) {
    145 
    146   ASSERT_FALSE(InitConfig("unknown_option"));
    147 
    148   ASSERT_STREQ("", getFakeLogBuf().c_str());
    149   std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
    150   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    151 }
    152 
    153 TEST_F(MallocDebugConfigTest, good_option_and_bad_option) {
    154   ASSERT_FALSE(InitConfig("backtrace unknown_option"));
    155 
    156   ASSERT_STREQ("", getFakeLogBuf().c_str());
    157   std::string log_msg("6 malloc_debug malloc_testing: unknown option unknown_option\n");
    158   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    159 }
    160 
    161 TEST_F(MallocDebugConfigTest, unparseable_number) {
    162   ASSERT_FALSE(InitConfig("backtrace=XXX"));
    163 
    164   ASSERT_STREQ("", getFakeLogBuf().c_str());
    165   std::string log_msg("6 malloc_debug malloc_testing: bad value for option 'backtrace'\n");
    166   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    167 }
    168 
    169 TEST_F(MallocDebugConfigTest, illegal_value_zero) {
    170   ASSERT_FALSE(InitConfig("backtrace=0"));
    171 
    172   ASSERT_STREQ("", getFakeLogBuf().c_str());
    173   std::string log_msg(
    174       "6 malloc_debug malloc_testing: bad value for option 'backtrace', value must be >= 1: 0\n");
    175   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    176 }
    177 
    178 TEST_F(MallocDebugConfigTest, no_space) {
    179   ASSERT_FALSE(InitConfig("backtrace=10front_guard"));
    180 
    181   ASSERT_STREQ("", getFakeLogBuf().c_str());
    182   std::string log_msg(
    183       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
    184       "non space found after option: front_guard\n");
    185   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    186 }
    187 
    188 TEST_F(MallocDebugConfigTest, illegal_value_negative) {
    189   ASSERT_FALSE(InitConfig("backtrace=-1"));
    190 
    191   ASSERT_STREQ("", getFakeLogBuf().c_str());
    192   std::string log_msg(
    193       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
    194       "value cannot be negative: -1\n");
    195   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    196 }
    197 
    198 TEST_F(MallocDebugConfigTest, overflow) {
    199   ASSERT_FALSE(InitConfig("backtrace=99999999999999999999"));
    200 
    201   ASSERT_STREQ("", getFakeLogBuf().c_str());
    202   std::string log_msg(
    203       "6 malloc_debug malloc_testing: bad value for option 'backtrace': "
    204       "Math result not representable\n");
    205   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    206 }
    207 
    208 TEST_F(MallocDebugConfigTest, set_value_error) {
    209   ASSERT_FALSE(InitConfig("leak_track=12"));
    210 
    211   ASSERT_STREQ("", getFakeLogBuf().c_str());
    212   std::string log_msg(
    213       "6 malloc_debug malloc_testing: value set for option 'leak_track' "
    214       "which does not take a value\n");
    215   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    216 }
    217 
    218 TEST_F(MallocDebugConfigTest, space_before_equal) {
    219   ASSERT_TRUE(InitConfig("backtrace  =10")) << getFakeLogPrint();
    220   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    221   ASSERT_EQ(10U, config->backtrace_frames());
    222 
    223   ASSERT_STREQ("", getFakeLogBuf().c_str());
    224   ASSERT_STREQ("", getFakeLogPrint().c_str());
    225 }
    226 
    227 TEST_F(MallocDebugConfigTest, space_after_equal) {
    228   ASSERT_TRUE(InitConfig("backtrace=  10")) << getFakeLogPrint();
    229   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    230   ASSERT_EQ(10U, config->backtrace_frames());
    231 
    232   ASSERT_STREQ("", getFakeLogBuf().c_str());
    233   ASSERT_STREQ("", getFakeLogPrint().c_str());
    234 }
    235 
    236 TEST_F(MallocDebugConfigTest, extra_space) {
    237   ASSERT_TRUE(InitConfig("   backtrace=64   ")) << getFakeLogPrint();
    238   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    239   ASSERT_EQ(64U, config->backtrace_frames());
    240 
    241   ASSERT_STREQ("", getFakeLogBuf().c_str());
    242   ASSERT_STREQ("", getFakeLogPrint().c_str());
    243 }
    244 
    245 TEST_F(MallocDebugConfigTest, multiple_options) {
    246   ASSERT_TRUE(InitConfig("  backtrace=64   front_guard=48")) << getFakeLogPrint();
    247   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS | FRONT_GUARD, config->options());
    248   ASSERT_EQ(64U, config->backtrace_frames());
    249   ASSERT_EQ(48U, config->front_guard_bytes());
    250 
    251   ASSERT_STREQ("", getFakeLogBuf().c_str());
    252   ASSERT_STREQ("", getFakeLogPrint().c_str());
    253 }
    254 
    255 TEST_F(MallocDebugConfigTest, front_guard) {
    256   ASSERT_TRUE(InitConfig("front_guard=48")) << getFakeLogPrint();
    257   ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
    258   ASSERT_EQ(48U, config->front_guard_bytes());
    259 
    260   ASSERT_TRUE(InitConfig("front_guard")) << getFakeLogPrint();
    261   ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
    262   ASSERT_EQ(32U, config->front_guard_bytes());
    263 
    264   ASSERT_TRUE(InitConfig("front_guard=39")) << getFakeLogPrint();
    265   ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
    266 #if defined(__LP64__)
    267   ASSERT_EQ(48U, config->front_guard_bytes());
    268 #else
    269   ASSERT_EQ(40U, config->front_guard_bytes());
    270 #endif
    271 
    272   ASSERT_TRUE(InitConfig("front_guard=41")) << getFakeLogPrint();
    273   ASSERT_EQ(FRONT_GUARD | TRACK_ALLOCS, config->options());
    274   ASSERT_EQ(48U, config->front_guard_bytes());
    275 
    276   ASSERT_STREQ("", getFakeLogBuf().c_str());
    277   ASSERT_STREQ("", getFakeLogPrint().c_str());
    278 }
    279 
    280 TEST_F(MallocDebugConfigTest, rear_guard) {
    281   ASSERT_TRUE(InitConfig("rear_guard=50")) << getFakeLogPrint();
    282   ASSERT_EQ(REAR_GUARD | TRACK_ALLOCS, config->options());
    283   ASSERT_EQ(50U, config->rear_guard_bytes());
    284 
    285   ASSERT_TRUE(InitConfig("rear_guard")) << getFakeLogPrint();
    286   ASSERT_EQ(REAR_GUARD | TRACK_ALLOCS, config->options());
    287   ASSERT_EQ(32U, config->rear_guard_bytes());
    288 
    289   ASSERT_STREQ("", getFakeLogBuf().c_str());
    290   ASSERT_STREQ("", getFakeLogPrint().c_str());
    291 }
    292 
    293 TEST_F(MallocDebugConfigTest, guard) {
    294   ASSERT_TRUE(InitConfig("guard=32")) << getFakeLogPrint();
    295   ASSERT_EQ(FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, config->options());
    296   ASSERT_EQ(32U, config->front_guard_bytes());
    297   ASSERT_EQ(32U, config->rear_guard_bytes());
    298 
    299   ASSERT_TRUE(InitConfig("guard")) << getFakeLogPrint();
    300   ASSERT_EQ(FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, config->options());
    301   ASSERT_EQ(32U, config->front_guard_bytes());
    302   ASSERT_EQ(32U, config->rear_guard_bytes());
    303 
    304   ASSERT_STREQ("", getFakeLogBuf().c_str());
    305   ASSERT_STREQ("", getFakeLogPrint().c_str());
    306 }
    307 
    308 TEST_F(MallocDebugConfigTest, backtrace) {
    309   ASSERT_TRUE(InitConfig("backtrace=64")) << getFakeLogPrint();
    310   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    311   ASSERT_EQ(64U, config->backtrace_frames());
    312   ASSERT_TRUE(config->backtrace_enabled());
    313   ASSERT_FALSE(config->backtrace_enable_on_signal());
    314   ASSERT_FALSE(config->backtrace_dump_on_exit());
    315 
    316   ASSERT_TRUE(InitConfig("backtrace")) << getFakeLogPrint();
    317   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    318   ASSERT_EQ(16U, config->backtrace_frames());
    319   ASSERT_TRUE(config->backtrace_enabled());
    320   ASSERT_FALSE(config->backtrace_enable_on_signal());
    321   ASSERT_FALSE(config->backtrace_dump_on_exit());
    322 
    323   ASSERT_STREQ("", getFakeLogBuf().c_str());
    324   ASSERT_STREQ("", getFakeLogPrint().c_str());
    325 }
    326 
    327 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal) {
    328   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal=64")) << getFakeLogPrint();
    329   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    330   ASSERT_EQ(64U, config->backtrace_frames());
    331   ASSERT_FALSE(config->backtrace_enabled());
    332   ASSERT_TRUE(config->backtrace_enable_on_signal());
    333   ASSERT_FALSE(config->backtrace_dump_on_exit());
    334 
    335   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal")) << getFakeLogPrint();
    336   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    337   ASSERT_EQ(16U, config->backtrace_frames());
    338   ASSERT_FALSE(config->backtrace_enabled());
    339   ASSERT_TRUE(config->backtrace_enable_on_signal());
    340   ASSERT_FALSE(config->backtrace_dump_on_exit());
    341 
    342   ASSERT_STREQ("", getFakeLogBuf().c_str());
    343   ASSERT_STREQ("", getFakeLogPrint().c_str());
    344 }
    345 
    346 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_init) {
    347   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal=64")) << getFakeLogPrint();
    348   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    349   ASSERT_EQ(64U, config->backtrace_frames());
    350   ASSERT_FALSE(config->backtrace_enabled());
    351   ASSERT_TRUE(config->backtrace_enable_on_signal());
    352   ASSERT_FALSE(config->backtrace_dump_on_exit());
    353 
    354   ASSERT_TRUE(InitConfig("backtrace")) << getFakeLogPrint();
    355   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    356   ASSERT_EQ(16U, config->backtrace_frames());
    357   ASSERT_TRUE(config->backtrace_enabled());
    358   ASSERT_FALSE(config->backtrace_enable_on_signal());
    359   ASSERT_FALSE(config->backtrace_dump_on_exit());
    360 
    361   ASSERT_STREQ("", getFakeLogBuf().c_str());
    362   ASSERT_STREQ("", getFakeLogPrint().c_str());
    363 }
    364 
    365 TEST_F(MallocDebugConfigTest, backtrace_enable_and_backtrace) {
    366   ASSERT_TRUE(InitConfig("backtrace_enable_on_signal backtrace")) << getFakeLogPrint();
    367   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    368   ASSERT_EQ(16U, config->backtrace_frames());
    369   ASSERT_TRUE(config->backtrace_enabled());
    370   ASSERT_TRUE(config->backtrace_enable_on_signal());
    371   ASSERT_FALSE(config->backtrace_dump_on_exit());
    372 
    373   ASSERT_TRUE(InitConfig("backtrace backtrace_enable_on_signal")) << getFakeLogPrint();
    374   ASSERT_EQ(BACKTRACE | TRACK_ALLOCS, config->options());
    375   ASSERT_EQ(16U, config->backtrace_frames());
    376   ASSERT_TRUE(config->backtrace_enabled());
    377   ASSERT_TRUE(config->backtrace_enable_on_signal());
    378   ASSERT_FALSE(config->backtrace_dump_on_exit());
    379 
    380   ASSERT_STREQ("", getFakeLogBuf().c_str());
    381   ASSERT_STREQ("", getFakeLogPrint().c_str());
    382 }
    383 
    384 TEST_F(MallocDebugConfigTest, backtrace_dump_on_exit) {
    385   ASSERT_TRUE(InitConfig("backtrace_dump_on_exit")) << getFakeLogPrint();
    386   ASSERT_EQ(0U, config->options());
    387   ASSERT_TRUE(config->backtrace_dump_on_exit());
    388 
    389   ASSERT_STREQ("", getFakeLogBuf().c_str());
    390   ASSERT_STREQ("", getFakeLogPrint().c_str());
    391 }
    392 
    393 TEST_F(MallocDebugConfigTest, backtrace_dump_on_exit_error) {
    394   ASSERT_FALSE(InitConfig("backtrace_dump_on_exit=something")) << getFakeLogPrint();
    395 
    396   ASSERT_STREQ("", getFakeLogBuf().c_str());
    397   std::string log_msg(
    398       "6 malloc_debug malloc_testing: value set for option 'backtrace_dump_on_exit' "
    399       "which does not take a value\n");
    400   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    401 }
    402 
    403 TEST_F(MallocDebugConfigTest, backtrace_dump_prefix) {
    404   ASSERT_TRUE(InitConfig("backtrace_dump_prefix")) << getFakeLogPrint();
    405   ASSERT_EQ(0U, config->options());
    406   ASSERT_EQ("/data/local/tmp/backtrace_heap", config->backtrace_dump_prefix());
    407 
    408   ASSERT_TRUE(InitConfig("backtrace_dump_prefix=/fake/location")) << getFakeLogPrint();
    409   ASSERT_EQ(0U, config->options());
    410   ASSERT_EQ("/fake/location", config->backtrace_dump_prefix());
    411 
    412   ASSERT_STREQ("", getFakeLogBuf().c_str());
    413   ASSERT_STREQ("", getFakeLogPrint().c_str());
    414 }
    415 
    416 TEST_F(MallocDebugConfigTest, fill_on_alloc) {
    417   ASSERT_TRUE(InitConfig("fill_on_alloc=64")) << getFakeLogPrint();
    418   ASSERT_EQ(FILL_ON_ALLOC, config->options());
    419   ASSERT_EQ(64U, config->fill_on_alloc_bytes());
    420 
    421   ASSERT_TRUE(InitConfig("fill_on_alloc")) << getFakeLogPrint();
    422   ASSERT_EQ(FILL_ON_ALLOC, config->options());
    423   ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes());
    424 
    425   ASSERT_STREQ("", getFakeLogBuf().c_str());
    426   ASSERT_STREQ("", getFakeLogPrint().c_str());
    427 }
    428 
    429 TEST_F(MallocDebugConfigTest, fill_on_free) {
    430   ASSERT_TRUE(InitConfig("fill_on_free=64")) << getFakeLogPrint();
    431   ASSERT_EQ(FILL_ON_FREE, config->options());
    432   ASSERT_EQ(64U, config->fill_on_free_bytes());
    433 
    434   ASSERT_TRUE(InitConfig("fill_on_free")) << getFakeLogPrint();
    435   ASSERT_EQ(FILL_ON_FREE, config->options());
    436   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
    437 
    438   ASSERT_STREQ("", getFakeLogBuf().c_str());
    439   ASSERT_STREQ("", getFakeLogPrint().c_str());
    440 }
    441 
    442 TEST_F(MallocDebugConfigTest, fill) {
    443   ASSERT_TRUE(InitConfig("fill=64")) << getFakeLogPrint();
    444   ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options());
    445   ASSERT_EQ(64U, config->fill_on_alloc_bytes());
    446   ASSERT_EQ(64U, config->fill_on_free_bytes());
    447 
    448   ASSERT_TRUE(InitConfig("fill")) << getFakeLogPrint();
    449   ASSERT_EQ(FILL_ON_ALLOC | FILL_ON_FREE, config->options());
    450   ASSERT_EQ(SIZE_MAX, config->fill_on_alloc_bytes());
    451   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
    452 
    453   ASSERT_STREQ("", getFakeLogBuf().c_str());
    454   ASSERT_STREQ("", getFakeLogPrint().c_str());
    455 }
    456 
    457 TEST_F(MallocDebugConfigTest, expand_alloc) {
    458   ASSERT_TRUE(InitConfig("expand_alloc=1234")) << getFakeLogPrint();
    459   ASSERT_EQ(EXPAND_ALLOC, config->options());
    460   ASSERT_EQ(1234U, config->expand_alloc_bytes());
    461 
    462   ASSERT_TRUE(InitConfig("expand_alloc")) << getFakeLogPrint();
    463   ASSERT_EQ(EXPAND_ALLOC, config->options());
    464   ASSERT_EQ(16U, config->expand_alloc_bytes());
    465 
    466   ASSERT_STREQ("", getFakeLogBuf().c_str());
    467   ASSERT_STREQ("", getFakeLogPrint().c_str());
    468 }
    469 
    470 TEST_F(MallocDebugConfigTest, free_track) {
    471   ASSERT_TRUE(InitConfig("free_track=1234")) << getFakeLogPrint();
    472   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    473   ASSERT_EQ(1234U, config->free_track_allocations());
    474   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
    475   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    476 
    477   ASSERT_TRUE(InitConfig("free_track")) << getFakeLogPrint();
    478   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    479   ASSERT_EQ(100U, config->free_track_allocations());
    480   ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes());
    481   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    482 
    483   ASSERT_STREQ("", getFakeLogBuf().c_str());
    484   ASSERT_STREQ("", getFakeLogPrint().c_str());
    485 }
    486 
    487 TEST_F(MallocDebugConfigTest, free_track_and_fill_on_free) {
    488   ASSERT_TRUE(InitConfig("free_track=1234 fill_on_free=32")) << getFakeLogPrint();
    489   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    490   ASSERT_EQ(1234U, config->free_track_allocations());
    491   ASSERT_EQ(32U, config->fill_on_free_bytes());
    492   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    493 
    494   ASSERT_TRUE(InitConfig("free_track fill_on_free=60")) << getFakeLogPrint();
    495   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    496   ASSERT_EQ(100U, config->free_track_allocations());
    497   ASSERT_EQ(60U, config->fill_on_free_bytes());
    498   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    499 
    500   // Now reverse the arguments.
    501   ASSERT_TRUE(InitConfig("fill_on_free=32 free_track=1234")) << getFakeLogPrint();
    502   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    503   ASSERT_EQ(1234U, config->free_track_allocations());
    504   ASSERT_EQ(32U, config->fill_on_free_bytes());
    505   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    506 
    507   ASSERT_STREQ("", getFakeLogBuf().c_str());
    508   ASSERT_STREQ("", getFakeLogPrint().c_str());
    509 }
    510 
    511 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames) {
    512   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=123")) << getFakeLogPrint();
    513 
    514   ASSERT_EQ(0U, config->options());
    515   ASSERT_EQ(123U, config->free_track_backtrace_num_frames());
    516 
    517   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames")) << getFakeLogPrint();
    518   ASSERT_EQ(0U, config->options());
    519   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    520 
    521   ASSERT_STREQ("", getFakeLogBuf().c_str());
    522   ASSERT_STREQ("", getFakeLogPrint().c_str());
    523 }
    524 
    525 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_zero) {
    526   ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=0")) << getFakeLogPrint();
    527 
    528   ASSERT_EQ(0U, config->options());
    529   ASSERT_EQ(0U, config->free_track_backtrace_num_frames());
    530 
    531   ASSERT_STREQ("", getFakeLogBuf().c_str());
    532   ASSERT_STREQ("", getFakeLogPrint().c_str());
    533 }
    534 
    535 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_and_free_track) {
    536   ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames=123")) << getFakeLogPrint();
    537   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    538   ASSERT_EQ(123U, config->free_track_backtrace_num_frames());
    539 
    540   ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames")) << getFakeLogPrint();
    541   ASSERT_EQ(FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, config->options());
    542   ASSERT_EQ(16U, config->free_track_backtrace_num_frames());
    543 
    544   ASSERT_STREQ("", getFakeLogBuf().c_str());
    545   ASSERT_STREQ("", getFakeLogPrint().c_str());
    546 }
    547 
    548 TEST_F(MallocDebugConfigTest, leak_track) {
    549   ASSERT_TRUE(InitConfig("leak_track")) << getFakeLogPrint();
    550   ASSERT_EQ(LEAK_TRACK | TRACK_ALLOCS, config->options());
    551 
    552   ASSERT_STREQ("", getFakeLogBuf().c_str());
    553   ASSERT_STREQ("", getFakeLogPrint().c_str());
    554 }
    555 
    556 TEST_F(MallocDebugConfigTest, leak_track_fail) {
    557   ASSERT_FALSE(InitConfig("leak_track=100")) << getFakeLogPrint();
    558 
    559   ASSERT_STREQ("", getFakeLogBuf().c_str());
    560   std::string log_msg(
    561       "6 malloc_debug malloc_testing: value set for option 'leak_track' "
    562       "which does not take a value\n");
    563   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    564 }
    565 
    566 TEST_F(MallocDebugConfigTest, verify_pointers) {
    567   ASSERT_TRUE(InitConfig("verify_pointers")) << getFakeLogPrint();
    568   ASSERT_EQ(TRACK_ALLOCS, config->options());
    569 
    570   ASSERT_STREQ("", getFakeLogBuf().c_str());
    571   ASSERT_STREQ("", getFakeLogPrint().c_str());
    572 }
    573 
    574 TEST_F(MallocDebugConfigTest, verify_pointers_fail) {
    575   ASSERT_FALSE(InitConfig("verify_pointers=200")) << getFakeLogPrint();
    576 
    577   ASSERT_STREQ("", getFakeLogBuf().c_str());
    578   std::string log_msg(
    579       "6 malloc_debug malloc_testing: value set for option 'verify_pointers' "
    580       "which does not take a value\n");
    581   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    582 }
    583 
    584 TEST_F(MallocDebugConfigTest, record_allocs) {
    585   ASSERT_TRUE(InitConfig("record_allocs=1234")) << getFakeLogPrint();
    586   ASSERT_EQ(RECORD_ALLOCS, config->options());
    587   ASSERT_EQ(1234U, config->record_allocs_num_entries());
    588   ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
    589 
    590   ASSERT_TRUE(InitConfig("record_allocs")) << getFakeLogPrint();
    591   ASSERT_EQ(RECORD_ALLOCS, config->options());
    592   ASSERT_EQ(8000000U, config->record_allocs_num_entries());
    593   ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
    594 
    595   ASSERT_STREQ("", getFakeLogBuf().c_str());
    596   ASSERT_STREQ("", getFakeLogPrint().c_str());
    597 }
    598 
    599 TEST_F(MallocDebugConfigTest, record_allocs_file) {
    600   ASSERT_TRUE(InitConfig("record_allocs=1234 record_allocs_file=/fake/file")) << getFakeLogPrint();
    601   ASSERT_STREQ("/fake/file", config->record_allocs_file().c_str());
    602 
    603   ASSERT_TRUE(InitConfig("record_allocs_file")) << getFakeLogPrint();
    604   ASSERT_STREQ("/data/local/tmp/record_allocs.txt", config->record_allocs_file().c_str());
    605 
    606   ASSERT_STREQ("", getFakeLogBuf().c_str());
    607   ASSERT_STREQ("", getFakeLogPrint().c_str());
    608 }
    609 
    610 TEST_F(MallocDebugConfigTest, guard_min_error) {
    611   ASSERT_FALSE(InitConfig("guard=0"));
    612 
    613   ASSERT_STREQ("", getFakeLogBuf().c_str());
    614   std::string log_msg(
    615       "6 malloc_debug malloc_testing: bad value for option 'guard', value must be >= 1: 0\n");
    616   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    617 }
    618 
    619 TEST_F(MallocDebugConfigTest, guard_max_error) {
    620   ASSERT_FALSE(InitConfig("guard=20000"));
    621 
    622   ASSERT_STREQ("", getFakeLogBuf().c_str());
    623   std::string log_msg(
    624       "6 malloc_debug malloc_testing: bad value for option 'guard', "
    625       "value must be <= 16384: 20000\n");
    626   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    627 }
    628 
    629 TEST_F(MallocDebugConfigTest, front_guard_min_error) {
    630   ASSERT_FALSE(InitConfig("front_guard=0"));
    631 
    632   ASSERT_STREQ("", getFakeLogBuf().c_str());
    633   std::string log_msg(
    634       "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
    635       "value must be >= 1: 0\n");
    636   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    637 }
    638 
    639 TEST_F(MallocDebugConfigTest, front_guard_max_error) {
    640   ASSERT_FALSE(InitConfig("front_guard=20000"));
    641 
    642   ASSERT_STREQ("", getFakeLogBuf().c_str());
    643   std::string log_msg(
    644       "6 malloc_debug malloc_testing: bad value for option 'front_guard', "
    645       "value must be <= 16384: 20000\n");
    646   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    647 }
    648 
    649 TEST_F(MallocDebugConfigTest, rear_guard_min_error) {
    650   ASSERT_FALSE(InitConfig("rear_guard=0"));
    651 
    652   ASSERT_STREQ("", getFakeLogBuf().c_str());
    653   std::string log_msg(
    654       "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
    655       "value must be >= 1: 0\n");
    656   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    657 }
    658 
    659 TEST_F(MallocDebugConfigTest, rear_guard_max_error) {
    660   ASSERT_FALSE(InitConfig("rear_guard=20000"));
    661 
    662   ASSERT_STREQ("", getFakeLogBuf().c_str());
    663   std::string log_msg(
    664       "6 malloc_debug malloc_testing: bad value for option 'rear_guard', "
    665       "value must be <= 16384: 20000\n");
    666   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    667 }
    668 
    669 TEST_F(MallocDebugConfigTest, fill_min_error) {
    670   ASSERT_FALSE(InitConfig("fill=0"));
    671 
    672   ASSERT_STREQ("", getFakeLogBuf().c_str());
    673   std::string log_msg(
    674       "6 malloc_debug malloc_testing: bad value for option 'fill', "
    675       "value must be >= 1: 0\n");
    676   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    677 }
    678 
    679 TEST_F(MallocDebugConfigTest, fill_on_alloc_min_error) {
    680   ASSERT_FALSE(InitConfig("fill_on_alloc=0"));
    681 
    682   ASSERT_STREQ("", getFakeLogBuf().c_str());
    683   std::string log_msg(
    684       "6 malloc_debug malloc_testing: bad value for option 'fill_on_alloc', "
    685       "value must be >= 1: 0\n");
    686   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    687 }
    688 
    689 TEST_F(MallocDebugConfigTest, fill_on_free_min_error) {
    690   ASSERT_FALSE(InitConfig("fill_on_free=0"));
    691 
    692   ASSERT_STREQ("", getFakeLogBuf().c_str());
    693   std::string log_msg(
    694       "6 malloc_debug malloc_testing: bad value for option 'fill_on_free', "
    695       "value must be >= 1: 0\n");
    696   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    697 }
    698 
    699 TEST_F(MallocDebugConfigTest, backtrace_min_error) {
    700   ASSERT_FALSE(InitConfig("backtrace=0"));
    701 
    702   ASSERT_STREQ("", getFakeLogBuf().c_str());
    703   std::string log_msg(
    704       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
    705       "value must be >= 1: 0\n");
    706   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    707 }
    708 
    709 TEST_F(MallocDebugConfigTest, backtrace_max_error) {
    710   ASSERT_FALSE(InitConfig("backtrace=300"));
    711 
    712   ASSERT_STREQ("", getFakeLogBuf().c_str());
    713   std::string log_msg(
    714       "6 malloc_debug malloc_testing: bad value for option 'backtrace', "
    715       "value must be <= 256: 300\n");
    716   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    717 }
    718 
    719 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_min_error) {
    720   ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=0"));
    721 
    722   ASSERT_STREQ("", getFakeLogBuf().c_str());
    723   std::string log_msg(
    724       "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
    725       "value must be >= 1: 0\n");
    726   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    727 }
    728 
    729 TEST_F(MallocDebugConfigTest, backtrace_enable_on_signal_max_error) {
    730   ASSERT_FALSE(InitConfig("backtrace_enable_on_signal=300"));
    731 
    732   ASSERT_STREQ("", getFakeLogBuf().c_str());
    733   std::string log_msg(
    734       "6 malloc_debug malloc_testing: bad value for option 'backtrace_enable_on_signal', "
    735       "value must be <= 256: 300\n");
    736   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    737 }
    738 
    739 TEST_F(MallocDebugConfigTest, expand_alloc_min_error) {
    740   ASSERT_FALSE(InitConfig("expand_alloc=0"));
    741 
    742   ASSERT_STREQ("", getFakeLogBuf().c_str());
    743   std::string log_msg(
    744       "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
    745       "value must be >= 1: 0\n");
    746   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    747 }
    748 
    749 TEST_F(MallocDebugConfigTest, expand_alloc_max_error) {
    750   ASSERT_FALSE(InitConfig("expand_alloc=21000"));
    751 
    752   ASSERT_STREQ("", getFakeLogBuf().c_str());
    753   std::string log_msg(
    754       "6 malloc_debug malloc_testing: bad value for option 'expand_alloc', "
    755       "value must be <= 16384: 21000\n");
    756   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    757 }
    758 
    759 TEST_F(MallocDebugConfigTest, free_track_min_error) {
    760   ASSERT_FALSE(InitConfig("free_track=0"));
    761 
    762   ASSERT_STREQ("", getFakeLogBuf().c_str());
    763   std::string log_msg(
    764       "6 malloc_debug malloc_testing: bad value for option 'free_track', "
    765       "value must be >= 1: 0\n");
    766   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    767 }
    768 
    769 TEST_F(MallocDebugConfigTest, free_track_max_error) {
    770   ASSERT_FALSE(InitConfig("free_track=21000"));
    771 
    772   ASSERT_STREQ("", getFakeLogBuf().c_str());
    773   std::string log_msg(
    774       "6 malloc_debug malloc_testing: bad value for option 'free_track', "
    775       "value must be <= 16384: 21000\n");
    776   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    777 }
    778 
    779 TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_max_error) {
    780   ASSERT_FALSE(InitConfig("free_track_backtrace_num_frames=400"));
    781 
    782   ASSERT_STREQ("", getFakeLogBuf().c_str());
    783   std::string log_msg(
    784       "6 malloc_debug malloc_testing: bad value for option 'free_track_backtrace_num_frames', "
    785       "value must be <= 256: 400\n");
    786   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    787 }
    788 
    789 TEST_F(MallocDebugConfigTest, record_alloc_min_error) {
    790   ASSERT_FALSE(InitConfig("record_allocs=0"));
    791 
    792   ASSERT_STREQ("", getFakeLogBuf().c_str());
    793   std::string log_msg(
    794       "6 malloc_debug malloc_testing: bad value for option 'record_allocs', "
    795       "value must be >= 1: 0\n");
    796   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    797 }
    798 
    799 TEST_F(MallocDebugConfigTest, record_allocs_max_error) {
    800   ASSERT_FALSE(InitConfig("record_allocs=100000000"));
    801 
    802   ASSERT_STREQ("", getFakeLogBuf().c_str());
    803   std::string log_msg(
    804       "6 malloc_debug malloc_testing: bad value for option 'record_allocs', "
    805       "value must be <= 50000000: 100000000\n");
    806   ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
    807 }
    808