Path: blob/master/test/jdk/sun/rmi/log/ReliableLog/LogAlignmentTest.java
41153 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* @modules java.rmi/sun.rmi.log28* @run main/othervm LogAlignmentTest29*/3031/* Fault: ReliableLog used RandomAccessFile.skipBytes() to seek past the end32* of a file. Unfortunately, skipBytes() doesn't do that, so the file was33* being misaligned.34*35* Reproduced by writing an odd number of bytes into an update, and then36* reloading.37*/3839import java.io.File;40import java.io.PrintStream;41import java.io.Serializable;42import sun.rmi.log.LogHandler;43import sun.rmi.log.ReliableLog;4445//import javasoft.sqe.harness.Status;46//import javasoft.sqe.harness.Test;4748public class LogAlignmentTest49extends LogHandler50implements Serializable //, Test51{52static public void main (String[] argv)53{54LogAlignmentTest test = new LogAlignmentTest();55//Status status = test.run (argv, System.err, System.out);56//status.exit();57test.run (argv, System.err, System.out);58}5960//public Status run (String argv[], PrintStream log, PrintStream out)61public void run (String argv[], PrintStream log, PrintStream out)62{63try {64regtest(130, "./logalign_tmp", log, out);65regtest(131, "./logalign_tmp", log, out);66} catch (Exception e) {67e.printStackTrace (log);68//return (Status.failed69throw (new RuntimeException70("Exception in regression test for bugid 4094889"));71}72//return (Status.passed ("OKAY"));73}7475static private void regtest(int updatesz, String dir, PrintStream lg, PrintStream out)76throws Exception77{78try {79LogAlignmentTest handler = new LogAlignmentTest();80ReliableLog log = new ReliableLog (dir, handler, false);8182// Write a preamble update83String c = "[";84handler.basicUpdate (c);85log.update (c, true);8687// Generate the requested size update (single chars)88char[] up = new char[updatesz];89int i;90for (i = 0; i < updatesz; i++) {91up[i] = (char)(65 + (i % 26));92}93c = new String (up);94handler.basicUpdate (c);95log.update (c, true);9697// Write it again, so we can misalign98handler.basicUpdate (c);99log.update (c, true);100101// Write the suffix102c = "]";103handler.basicUpdate (c);104log.update (c, true);105106// Read it back using a new context.107LogAlignmentTest handler2 = new LogAlignmentTest();108ReliableLog carbon = new ReliableLog (dir, handler2, false);109Object thingy = carbon.recover();110111// The report bit112String orig = handler.contents;113String news = ((LogAlignmentTest)thingy).contents;114lg.println ("Original as saved: " + orig);115lg.println ("As restored : " + news);116if (orig.compareTo (news) != 0) {117throw new RuntimeException ("Restored string was different from saved string");118} else {119lg.println ("Matched OK. Test element passed.");120}121} finally {122// Trash the log directory, so a new snap will be taken in the next test123try {124File vs = new File (dir, "Version_Number");125vs.delete();126} catch (Exception e) {127}128try {129File vs = new File (dir, "New_Version_Number");130vs.delete();131} catch (Exception e) {132}133}134}135136private String contents;137138public LogAlignmentTest()139{140super();141this.contents = "?";142}143144// implements LogHandler.initialSnapshot()145public Object initialSnapshot()146throws Exception147{148this.contents = "";149return (this);150}151152// implements LogHandler.applyUpdate()153public Object applyUpdate (Object update, Object state)154throws Exception155{156// basicUpdate appends the string157((LogAlignmentTest)state).basicUpdate ((String)update);158return (state);159}160161// an "update" is a short string to append to this.contents (must ignore state)162public void basicUpdate (String extra)163{164this.contents = this.contents + extra;165}166}167168169