/
LoadableTestJob.java
executable file
·198 lines (183 loc) · 7.35 KB
/
LoadableTestJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* #%L
* Netarchivesuite - common - test
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.common.utils.batch;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.utils.FileUtils;
/**
* This class is used for loading by LoadableFileBatchJobTester. The compiled class file should be placed under
* data/originals. Create the jar file with jar cvf <jarfilename> <names of compiled classes>
*/
@SuppressWarnings({"unchecked", "rawtypes", "serial"})
public class LoadableTestJob extends FileBatchJob {
String ourName = "me";
public LoadableTestJob() {
}
public LoadableTestJob(boolean innerClass) {
if (innerClass) {
ourName = new InnerClass().innerClassName();
}
}
/**
* Initialize the job before runnning. This is called before the processFile() calls
*
* @param os the OutputStream to which output should be written
*/
public void initialize(OutputStream os) {
try {
os.write(("initialize() called on " + this + "\n").getBytes());
} catch (IOException e) {
throw new IOFailure("Error in initializing " + this + ": ", e);
}
}
/**
* Process one file stored in the bit archive.
*
* @param file the file to be processed.
* @param os the OutputStream to which output should be written
* @return true if the file was successfully processed, false otherwise
*/
public boolean processFile(File file, OutputStream os) {
try {
os.write(("processFile() called on " + this + " with " + file.getName() + "\n").getBytes());
if (file.getName().endsWith("hack")) {
// Try to break out of jail
os.write("Trying direct breakout.\n".getBytes());
new File("fnord").createNewFile();
return true;
} else if (file.getName().endsWith("break")) {
// Try to usurp trusted utilities to break out of jail
os.write("Trying indirect breakout.\n".getBytes());
FileUtils.writeBinaryFile(new File("escape"), "free".getBytes());
return true;
} else if (file.getName().endsWith("climb")) {
os.write("Trying backwards breakout.\n".getBytes());
try {
ClassLoader loader = os.getClass().getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
Class c = loader.loadClass("dk.netarkivet.utils.FileUtils");
c.getMethod("writeBinaryFile", File.class, String.class).invoke(null, new File("free"),
"gotout".getBytes());
} catch (ClassNotFoundException e) {
os.write(e.toString().getBytes());
return false;
} catch (NoSuchMethodException e) {
os.write(e.toString().getBytes());
return false;
} catch (IllegalAccessException e) {
os.write(e.toString().getBytes());
return false;
} catch (InvocationTargetException e) {
os.write(e.toString().getBytes());
return false;
}
return true;
} else if (file.getName().endsWith("smash")) {
// Try to usurp trusted utilities to break out of jail
os.write("Trying vandalism.\n".getBytes());
FileUtils.writeBinaryFile(file, "sign".getBytes());
return true;
} else if (file.getName().endsWith("exec")) {
// Try to usurp trusted utilities to break out of jail
os.write("Trying external process.\n".getBytes());
Runtime.getRuntime().exec("ls");
return true;
} else {
return file.length() % 2 == 1;
}
} catch (IOException e) {
throw new IOFailure("Error in processing " + file + " with " + this + ": ", e);
}
}
/**
* Finish up the job. This is called after the last process() call.
*
* @param os the OutputStream to which output should be written
*/
public void finish(OutputStream os) {
try {
os.write(("finish() called on " + this + "\n").getBytes());
} catch (IOException e) {
throw new IOFailure("Error in finishing " + this + ": ", e);
}
}
public String toString() {
return ourName;
}
class InnerClass {
public String innerClassName() {
return "inner";
}
}
public static class InnerBatchJob extends FileBatchJob {
public InnerBatchJob() {
}
/**
* Initialize the job before runnning. This is called before the processFile() calls
*
* @param os the OutputStream to which output should be written
*/
public void initialize(OutputStream os) {
try {
os.write(("initialize() called on " + this + "\n").getBytes());
} catch (IOException e) {
throw new IOFailure("Error in initializing " + this + ": ", e);
}
}
/**
* Process one file stored in the bit archive.
*
* @param file the file to be processed.
* @param os the OutputStream to which output should be written
* @return true if the file was successfully processed, false otherwise
*/
public boolean processFile(File file, OutputStream os) {
try {
os.write(("processFile() called on " + this + " with " + file.getName() + "\n").getBytes());
return true;
} catch (IOException e) {
throw new IOFailure("Error in processing " + file + " with " + this + ": ", e);
}
}
/**
* Finish up the job. This is called after the last process() call.
*
* @param os the OutputStream to which output should be written
*/
public void finish(OutputStream os) {
try {
os.write(("finish() called on " + this + "\n").getBytes());
} catch (IOException e) {
throw new IOFailure("Error in finishing " + this + ": ", e);
}
}
public String toString() {
return "inner";
}
}
}