< prev index next >

test/native/logging/test_logFileOutput.cpp

Print this page
rev 11942 : 8165600: Convert internal logging tests to GTest


   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 "logging/logFileOutput.hpp"
  26 #include "memory/resourceArea.hpp"
  27 #include "runtime/os.hpp"
  28 #include "unittest.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 #include "utilities/ostream.hpp"
  31 
  32 static const char* name = "file=testlog.pid%p.%t.log";
  33 
  34 // Test parsing a bunch of valid file output options
  35 TEST(LogFileOutput, parse_valid) {
  36   const char* valid_options[] = {
  37     "", "filecount=10", "filesize=512",
  38     "filecount=11,filesize=256",
  39     "filesize=256,filecount=11",
  40     "filesize=0", "filecount=1",
  41     "filesize=1m", "filesize=1M",
  42     "filesize=1k", "filesize=1G"
  43   };
  44 


  83 
  84   for (size_t i = 0; i < ARRAY_SIZE(invalid_options); i++) {
  85     ResourceMark rm;
  86     stringStream ss;
  87     LogFileOutput fo(name);
  88     EXPECT_FALSE(fo.initialize(invalid_options[i], &ss))
  89       << "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string();
  90   }
  91 }
  92 
  93 // Test for overflows with filesize
  94 TEST(LogFileOutput, filesize_overflow) {
  95   char buf[256];
  96   int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX);
  97   ASSERT_GT(ret, 0) << "Buffer too small";
  98 
  99   ResourceMark rm;
 100   stringStream ss;
 101   LogFileOutput fo(name);
 102   EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";















































































 103 }


   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(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 


  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(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 }
< prev index next >