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