1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "logTestUtils.inline.hpp"
  26 #include "logging/logFileOutput.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "runtime/os.hpp"
  29 #include "unittest.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "utilities/ostream.hpp"
  32 
  33 static const char* name = "file=testlog.pid%p.%t.log";
  34 
  35 // Test parsing a bunch of valid file output options
  36 TEST_VM(LogFileOutput, parse_valid) {
  37   const char* valid_options[] = {
  38     "", "filecount=10", "filesize=512",
  39     "filecount=11,filesize=256",
  40     "filesize=256,filecount=11",
  41     "filesize=0", "filecount=1",
  42     "filesize=1m", "filesize=1M",
  43     "filesize=1k", "filesize=1G"
  44   };
  45 
  46   // Override LogOutput's vm_start time to get predictable file name
  47   LogFileOutput::set_file_name_parameters(0);
  48   char expected_filename[1 * K];
  49   int ret = jio_snprintf(expected_filename, sizeof(expected_filename),
  50                          "testlog.pid%d.1970-01-01_01-00-00.log",
  51                          os::current_process_id());
  52   ASSERT_GT(ret, 0) << "Buffer too small";
  53 
  54   for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) {
  55     ResourceMark rm;
  56     stringStream ss;
  57     {
  58       LogFileOutput fo(name);
  59       EXPECT_STREQ(name, fo.name());
  60       EXPECT_TRUE(fo.initialize(valid_options[i], &ss))
  61         << "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string();
  62     }
  63     remove(expected_filename);
  64   }
  65 }
  66 
  67 // Test parsing a bunch of invalid file output options
  68 TEST_VM(LogFileOutput, parse_invalid) {
  69   const char* invalid_options[] = {
  70     "invalidopt", "filecount=",
  71     "filesize=,filecount=10",
  72     "fileco=10", "ilesize=512",
  73     "filecount=11,,filesize=256",
  74     ",filesize=256,filecount=11",
  75     "filesize=256,filecount=11,",
  76     "filesize=-1", "filecount=0.1",
  77     "filecount=-2", "filecount=2.0",
  78     "filecount= 2", "filesize=2 ",
  79     "filecount=ab", "filesize=0xz",
  80     "filecount=1MB", "filesize=99bytes",
  81     "filesize=9999999999999999999999999"
  82     "filecount=9999999999999999999999999"
  83   };
  84 
  85   for (size_t i = 0; i < ARRAY_SIZE(invalid_options); i++) {
  86     ResourceMark rm;
  87     stringStream ss;
  88     LogFileOutput fo(name);
  89     EXPECT_FALSE(fo.initialize(invalid_options[i], &ss))
  90       << "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string();
  91   }
  92 }
  93 
  94 // Test for overflows with filesize
  95 TEST_VM(LogFileOutput, filesize_overflow) {
  96   char buf[256];
  97   int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX);
  98   ASSERT_GT(ret, 0) << "Buffer too small";
  99 
 100   ResourceMark rm;
 101   stringStream ss;
 102   LogFileOutput fo(name);
 103   EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";
 104 }
 105 
 106 TEST(LogFileOutput, startup_rotation) {
 107   const size_t rotations = 5;
 108   const char* filename = "start-rotate-test";
 109   char* rotated_file[rotations];
 110 
 111   ResourceMark rm;
 112   for (size_t i = 0; i < rotations; i++) {
 113     size_t len = strlen(filename) + 3;
 114     rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
 115     int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
 116     ASSERT_NE(-1, ret);
 117     delete_file(rotated_file[i]);
 118   }
 119 
 120   delete_file(filename);
 121   init_log_file(filename);
 122   ASSERT_TRUE(file_exists(filename))
 123     << "configured logging to file '" << filename << "' but file was not found";
 124 
 125   // Initialize the same file a bunch more times to trigger rotations
 126   for (size_t i = 0; i < rotations; i++) {
 127     init_log_file(filename);
 128     EXPECT_TRUE(file_exists(rotated_file[i]));
 129   }
 130 
 131   // Remove a file and expect its slot to be re-used
 132   delete_file(rotated_file[1]);
 133   init_log_file(filename);
 134   EXPECT_TRUE(file_exists(rotated_file[1]));
 135 
 136   // Clean up after test
 137   delete_file(filename);
 138   for (size_t i = 0; i < rotations; i++) {
 139     delete_file(rotated_file[i]);
 140   }
 141 }
 142 
 143 TEST(LogFileOutput, startup_truncation) {
 144   const char* filename = "start-truncate-test";
 145   const char* archived_filename = "start-truncate-test.0";
 146 
 147   delete_file(filename);
 148   delete_file(archived_filename);
 149 
 150   // Use the same log file twice and expect it to be overwritten/truncated
 151   init_log_file(filename, "filecount=0");
 152   ASSERT_TRUE(file_exists(filename))
 153     << "configured logging to file '" << filename << "' but file was not found";
 154 
 155   init_log_file(filename, "filecount=0");
 156   ASSERT_TRUE(file_exists(filename))
 157     << "configured logging to file '" << filename << "' but file was not found";
 158   EXPECT_FALSE(file_exists(archived_filename))
 159     << "existing log file was not properly truncated when filecount was 0";
 160 
 161   // Verify that the file was really truncated and not just appended
 162   EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));
 163   const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };
 164   EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))
 165     << "log file " << filename << " appended rather than truncated";
 166 
 167   delete_file(filename);
 168   delete_file(archived_filename);
 169 }
 170 
 171 TEST(LogFileOutput, invalid_file) {
 172   ResourceMark rm;
 173   stringStream ss;
 174 
 175   // Attempt to log to a directory (existing log not a regular file)
 176   create_directory("tmplogdir");
 177   LogFileOutput bad_file("file=tmplogdir");
 178   EXPECT_FALSE(bad_file.initialize("", &ss))
 179     << "file was initialized when there was an existing directory with the same name";
 180   EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))
 181     << "missing expected error message, received msg: %s" << ss.as_string();
 182   remove("tmplogdir");
 183 }