Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/log/ReliableLog/LogAlignmentTest.java
38857 views
/*1* Copyright (c) 1997, 2012, 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/* @test24* @bug 409488925* @summary rmid can have a corrupted log26*27* @run main LogAlignmentTest28*/2930/* Fault: ReliableLog used RandomAccessFile.skipBytes() to seek past the end31* of a file. Unfortunately, skipBytes() doesn't do that, so the file was32* being misaligned.33*34* Reproduced by writing an odd number of bytes into an update, and then35* reloading.36*/3738import java.io.File;39import java.io.PrintStream;40import java.io.Serializable;41import sun.rmi.log.LogHandler;42import sun.rmi.log.ReliableLog;4344//import javasoft.sqe.harness.Status;45//import javasoft.sqe.harness.Test;4647public class LogAlignmentTest48extends LogHandler49implements Serializable //, Test50{51static public void main (String[] argv)52{53LogAlignmentTest test = new LogAlignmentTest();54//Status status = test.run (argv, System.err, System.out);55//status.exit();56test.run (argv, System.err, System.out);57}5859//public Status run (String argv[], PrintStream log, PrintStream out)60public void run (String argv[], PrintStream log, PrintStream out)61{62try {63regtest(130, "./logalign_tmp", log, out);64regtest(131, "./logalign_tmp", log, out);65} catch (Exception e) {66e.printStackTrace (log);67//return (Status.failed68throw (new RuntimeException69("Exception in regression test for bugid 4094889"));70}71//return (Status.passed ("OKAY"));72}7374static private void regtest(int updatesz, String dir, PrintStream lg, PrintStream out)75throws Exception76{77try {78LogAlignmentTest handler = new LogAlignmentTest();79ReliableLog log = new ReliableLog (dir, handler, false);8081// Write a preamble update82String c = "[";83handler.basicUpdate (c);84log.update (c, true);8586// Generate the requested size update (single chars)87char[] up = new char[updatesz];88int i;89for (i = 0; i < updatesz; i++) {90up[i] = (char)(65 + (i % 26));91}92c = new String (up);93handler.basicUpdate (c);94log.update (c, true);9596// Write it again, so we can misalign97handler.basicUpdate (c);98log.update (c, true);99100// Write the suffix101c = "]";102handler.basicUpdate (c);103log.update (c, true);104105// Read it back using a new context.106LogAlignmentTest handler2 = new LogAlignmentTest();107ReliableLog carbon = new ReliableLog (dir, handler2, false);108Object thingy = carbon.recover();109110// The report bit111String orig = handler.contents;112String news = ((LogAlignmentTest)thingy).contents;113lg.println ("Original as saved: " + orig);114lg.println ("As restored : " + news);115if (orig.compareTo (news) != 0) {116throw new RuntimeException ("Restored string was different from saved string");117} else {118lg.println ("Matched OK. Test element passed.");119}120} finally {121// Trash the log directory, so a new snap will be taken in the next test122try {123File vs = new File (dir, "Version_Number");124vs.delete();125} catch (Exception e) {126}127try {128File vs = new File (dir, "New_Version_Number");129vs.delete();130} catch (Exception e) {131}132}133}134135private String contents;136137public LogAlignmentTest()138{139super();140this.contents = "?";141}142143// implements LogHandler.initialSnapshot()144public Object initialSnapshot()145throws Exception146{147this.contents = "";148return (this);149}150151// implements LogHandler.applyUpdate()152public Object applyUpdate (Object update, Object state)153throws Exception154{155// basicUpdate appends the string156((LogAlignmentTest)state).basicUpdate ((String)update);157return (state);158}159160// an "update" is a short string to append to this.contents (must ignore state)161public void basicUpdate (String extra)162{163this.contents = this.contents + extra;164}165}166167168