Path: blob/master/test/hotspot/gtest/logging/test_logFileOutput.cpp
41145 views
/*1* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "jvm.h"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"3233static const char* name = prepend_prefix_temp_dir("file=", "testlog.pid%p.%t.log");3435// Test parsing a bunch of valid file output options36TEST_VM(LogFileOutput, parse_valid) {37const 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};4546// Override LogOutput's vm_start time to get predictable file name47LogFileOutput::set_file_name_parameters(0);4849for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) {50ResourceMark rm;51stringStream ss;52{53LogFileOutput fo(name);54EXPECT_STREQ(name, fo.name());55EXPECT_TRUE(fo.initialize(valid_options[i], &ss))56<< "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string();57remove(fo.cur_log_file_name());58}59}60}6162// Test parsing a bunch of invalid file output options63TEST_VM(LogFileOutput, parse_invalid) {64const char* invalid_options[] = {65"invalidopt", "filecount=",66"filesize=,filecount=10",67"fileco=10", "ilesize=512",68"filecount=11,,filesize=256",69",filesize=256,filecount=11",70"filesize=256,filecount=11,",71"filesize=-1", "filecount=0.1",72"filecount=-2", "filecount=2.0",73"filecount= 2", "filesize=2 ",74"filecount=ab", "filesize=0xz",75"filecount=1MB", "filesize=99bytes",76"filesize=9999999999999999999999999"77"filecount=9999999999999999999999999"78};7980for (size_t i = 0; i < ARRAY_SIZE(invalid_options); i++) {81ResourceMark rm;82stringStream ss;83LogFileOutput fo(name);84EXPECT_FALSE(fo.initialize(invalid_options[i], &ss))85<< "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string();86}87}8889// Test for overflows with filesize90TEST_VM(LogFileOutput, filesize_overflow) {91char buf[256];92int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX);93ASSERT_GT(ret, 0) << "Buffer too small";9495ResourceMark rm;96stringStream ss;97LogFileOutput fo(name);98EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";99}100101TEST_VM(LogFileOutput, startup_rotation) {102ResourceMark rm;103const size_t rotations = 5;104const char* filename = prepend_temp_dir("start-rotate-test");105char* rotated_file[rotations];106107for (size_t i = 0; i < rotations; i++) {108size_t len = strlen(filename) + 3;109rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);110int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);111ASSERT_NE(-1, ret);112delete_file(rotated_file[i]);113}114115delete_file(filename);116init_log_file(filename);117ASSERT_TRUE(file_exists(filename))118<< "configured logging to file '" << filename << "' but file was not found";119120// Initialize the same file a bunch more times to trigger rotations121for (size_t i = 0; i < rotations; i++) {122init_log_file(filename);123EXPECT_TRUE(file_exists(rotated_file[i]));124}125126// Remove a file and expect its slot to be re-used127delete_file(rotated_file[1]);128init_log_file(filename);129EXPECT_TRUE(file_exists(rotated_file[1]));130131// Clean up after test132delete_file(filename);133for (size_t i = 0; i < rotations; i++) {134delete_file(rotated_file[i]);135}136}137138TEST_VM(LogFileOutput, startup_truncation) {139ResourceMark rm;140const char* filename = prepend_temp_dir("start-truncate-test");141const char* archived_filename = prepend_temp_dir("start-truncate-test.0");142143delete_file(filename);144delete_file(archived_filename);145146// Use the same log file twice and expect it to be overwritten/truncated147init_log_file(filename, "filecount=0");148ASSERT_TRUE(file_exists(filename))149<< "configured logging to file '" << filename << "' but file was not found";150151init_log_file(filename, "filecount=0");152ASSERT_TRUE(file_exists(filename))153<< "configured logging to file '" << filename << "' but file was not found";154EXPECT_FALSE(file_exists(archived_filename))155<< "existing log file was not properly truncated when filecount was 0";156157// Verify that the file was really truncated and not just appended158EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));159const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };160EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))161<< "log file " << filename << " appended rather than truncated";162163delete_file(filename);164delete_file(archived_filename);165}166167TEST_VM(LogFileOutput, invalid_file) {168ResourceMark rm;169stringStream ss;170171// Attempt to log to a directory (existing log not a regular file)172create_directory("tmplogdir");173LogFileOutput bad_file("file=tmplogdir");174EXPECT_FALSE(bad_file.initialize("", &ss))175<< "file was initialized when there was an existing directory with the same name";176EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))177<< "missing expected error message, received msg: %s" << ss.as_string();178delete_empty_directory("tmplogdir");179}180181182