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 "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 = "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 45 // Override LogOutput's vm_start time to get predictable file name 46 LogFileOutput::set_file_name_parameters(0); 47 char expected_filename[1 * K]; 48 int ret = jio_snprintf(expected_filename, sizeof(expected_filename), 49 "testlog.pid%d.1970-01-01_01-00-00.log", 50 os::current_process_id()); 51 ASSERT_GT(ret, 0) << "Buffer too small"; 52 53 for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) { 54 ResourceMark rm; 55 stringStream ss; 56 { 57 LogFileOutput fo(name); 58 EXPECT_STREQ(name, fo.name()); 59 EXPECT_TRUE(fo.initialize(valid_options[i], &ss)) 60 << "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string(); 61 } 62 remove(expected_filename); 63 } 64 } 65 66 // Test parsing a bunch of invalid file output options 67 TEST(LogFileOutput, parse_invalid) { 68 const char* invalid_options[] = { 69 "invalidopt", "filecount=", 70 "filesize=,filecount=10", 71 "fileco=10", "ilesize=512", 72 "filecount=11,,filesize=256", 73 ",filesize=256,filecount=11", 74 "filesize=256,filecount=11,", 75 "filesize=-1", "filecount=0.1", 76 "filecount=-2", "filecount=2.0", 77 "filecount= 2", "filesize=2 ", 78 "filecount=ab", "filesize=0xz", 79 "filecount=1MB", "filesize=99bytes", 80 "filesize=9999999999999999999999999" 81 "filecount=9999999999999999999999999" 82 }; 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 }