LogAlignmentTest.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 1997, 2012, 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/* @test 25 * @bug 4094889 26 * @summary rmid can have a corrupted log 27 * 28 * @run main/othervm LogAlignmentTest 29 */ 30 31/* Fault: ReliableLog used RandomAccessFile.skipBytes() to seek past the end 32 * of a file. Unfortunately, skipBytes() doesn't do that, so the file was 33 * being misaligned. 34 * 35 * Reproduced by writing an odd number of bytes into an update, and then 36 * reloading. 37 */ 38 39import java.io.File; 40import java.io.PrintStream; 41import java.io.Serializable; 42import sun.rmi.log.LogHandler; 43import sun.rmi.log.ReliableLog; 44 45//import javasoft.sqe.harness.Status; 46//import javasoft.sqe.harness.Test; 47 48public class LogAlignmentTest 49 extends LogHandler 50 implements Serializable //, Test 51{ 52 static public void main (String[] argv) 53 { 54 LogAlignmentTest test = new LogAlignmentTest(); 55 //Status status = test.run (argv, System.err, System.out); 56 //status.exit(); 57 test.run (argv, System.err, System.out); 58 } 59 60 //public Status run (String argv[], PrintStream log, PrintStream out) 61 public void run (String argv[], PrintStream log, PrintStream out) 62 { 63 try { 64 regtest(130, "./logalign_tmp", log, out); 65 regtest(131, "./logalign_tmp", log, out); 66 } catch (Exception e) { 67 e.printStackTrace (log); 68 //return (Status.failed 69 throw (new RuntimeException 70 ("Exception in regression test for bugid 4094889")); 71 } 72 //return (Status.passed ("OKAY")); 73 } 74 75 static private void regtest(int updatesz, String dir, PrintStream lg, PrintStream out) 76 throws Exception 77 { 78 try { 79 LogAlignmentTest handler = new LogAlignmentTest(); 80 ReliableLog log = new ReliableLog (dir, handler, false); 81 82 // Write a preamble update 83 String c = "["; 84 handler.basicUpdate (c); 85 log.update (c, true); 86 87 // Generate the requested size update (single chars) 88 char[] up = new char[updatesz]; 89 int i; 90 for (i = 0; i < updatesz; i++) { 91 up[i] = (char)(65 + (i % 26)); 92 } 93 c = new String (up); 94 handler.basicUpdate (c); 95 log.update (c, true); 96 97 // Write it again, so we can misalign 98 handler.basicUpdate (c); 99 log.update (c, true); 100 101 // Write the suffix 102 c = "]"; 103 handler.basicUpdate (c); 104 log.update (c, true); 105 106 // Read it back using a new context. 107 LogAlignmentTest handler2 = new LogAlignmentTest(); 108 ReliableLog carbon = new ReliableLog (dir, handler2, false); 109 Object thingy = carbon.recover(); 110 111 // The report bit 112 String orig = handler.contents; 113 String news = ((LogAlignmentTest)thingy).contents; 114 lg.println ("Original as saved: " + orig); 115 lg.println ("As restored : " + news); 116 if (orig.compareTo (news) != 0) { 117 throw new RuntimeException ("Restored string was different from saved string"); 118 } else { 119 lg.println ("Matched OK. Test element passed."); 120 } 121 } finally { 122 // Trash the log directory, so a new snap will be taken in the next test 123 try { 124 File vs = new File (dir, "Version_Number"); 125 vs.delete(); 126 } catch (Exception e) { 127 } 128 try { 129 File vs = new File (dir, "New_Version_Number"); 130 vs.delete(); 131 } catch (Exception e) { 132 } 133 } 134 } 135 136 private String contents; 137 138 public LogAlignmentTest() 139 { 140 super(); 141 this.contents = "?"; 142 } 143 144 // implements LogHandler.initialSnapshot() 145 public Object initialSnapshot() 146 throws Exception 147 { 148 this.contents = ""; 149 return (this); 150 } 151 152 // implements LogHandler.applyUpdate() 153 public Object applyUpdate (Object update, Object state) 154 throws Exception 155 { 156 // basicUpdate appends the string 157 ((LogAlignmentTest)state).basicUpdate ((String)update); 158 return (state); 159 } 160 161 // an "update" is a short string to append to this.contents (must ignore state) 162 public void basicUpdate (String extra) 163 { 164 this.contents = this.contents + extra; 165 } 166} 167