-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestArcRecord.java
263 lines (251 loc) · 13.5 KB
/
TestArcRecord.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Java Web Archive Toolkit - Software to read and validate ARC, WARC
* and GZip files. (http://jwat.org/)
* Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jwat.arc;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.jwat.common.DiagnosisType;
@RunWith(JUnit4.class)
public class TestArcRecord {
@Test
public void test_arcrecord() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in;
ArcReader reader;
ArcRecordBase record;
String arcData;
String payloadData;
try {
Object[][] test_cases = new Object[][] {
/*
* ArchiveLength=0, HTTP payload.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/html ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
return "0";
}
}, new Object[][] {
{DiagnosisType.ERROR_EXPECTED, ArcConstants.ARC_FILE, 1}
}, new Object[][] {}},
/*
* ArchiveLength=0, HTTP payload, no-type.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 no-type ",
"", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
return "0";
}
}, new Object[][] {
{DiagnosisType.ERROR_EXPECTED, ArcConstants.ARC_FILE, 1}
}, new Object[][] {}},
/*
* ArchiveLength=0, TNT payload.
*/
{"tnt://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/explosive ",
"", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
return "0";
}
}, new Object[][] {}, new Object[][] {}},
/*
* ArchiveLength=0, TNT payload, no-type.
*/
{"tnt://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 no-type ",
"", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
return "0";
}
}, new Object[][] {}, new Object[][] {}},
/*
* ArchiveLength=invalid, HTTP payload.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/html ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
return "invalid";
}
}, new Object[][] {
{DiagnosisType.INVALID_EXPECTED, "'" + ArcConstants.FN_ARCHIVE_LENGTH + "' value", 2},
{DiagnosisType.ERROR_EXPECTED, ArcConstants.ARC_FILE, 1}
}, new Object[][] {}},
/*
* ArchiveLength=payload length, HTTP response.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/html ",
"GET / HTTP/1.1\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
int archiveLength = ((String)(test_case[1])).length();
return Integer.toString(archiveLength);
}
}, new Object[][] {}, new Object[][] {
{DiagnosisType.ERROR, "http header", 1}
}},
/*
* ArchiveLength=payload length.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/html ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
int archiveLength = ((String)(test_case[1])).length();
return Integer.toString(archiveLength);
}
}, new Object[][] {}, new Object[][] {}},
/*
* ArchiveLength=payload length, no-type.
*/
{"http://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 no-type ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
int archiveLength = ((String)(test_case[1])).length();
return Integer.toString(archiveLength);
}
}, new Object[][] {}, new Object[][] {}},
/*
* ArchiveLength=payload length.
*/
{"gopher://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 text/html ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
int archiveLength = ((String)(test_case[1])).length();
return Integer.toString(archiveLength);
}
}, new Object[][] {}, new Object[][] {}},
/*
* ArchiveLength=payload length.
*/
{"gopher://cctr.umkc.edu:80/user/jbenz/tst.htm 134.193.4.1 19970417175710 no-type ",
"HTTP/1.0 200 Sending document\r\n"
+ "MIME-version: 1.0\r\n"
+ "Server: OSU/1.8\r\n"
+ "Content-type: text/html\r\n"
+ "Content-transfer-encoding: 8bit\r\n"
+ "Last-Modified: Tuesday, 21-Aug-96 05:14:05 GMT\r\n"
+ "\r\n", new TestHeaderCallback() {
public String getArchiveLength(Object[] test_case) {
int archiveLength = ((String)(test_case[1])).length();
return Integer.toString(archiveLength);
}
}, new Object[][] {}, new Object[][] {}},
};
for (int i=0; i<test_cases.length; ++i) {
out.reset();
out.write(filedesc());
TestHeaderCallback archiveLengthCB = (TestHeaderCallback)test_cases[i][2];
Assert.assertNotNull(archiveLengthCB);
payloadData = (String)test_cases[i][1];
arcData = (String)test_cases[i][0];
arcData += archiveLengthCB.getArchiveLength(test_cases[i]);
arcData += "\n";
arcData += payloadData;
arcData += "\n";
out.write(arcData.getBytes());
out.close();
// debug
//System.out.println(out);
// Save testfile.
SaveArcTestFiles.saveTestArcRecord(out.toByteArray(),
((Object[][])test_cases[i][3]).length == 0 && ((Object[][])test_cases[i][4]).length == 0);
in = new ByteArrayInputStream(out.toByteArray());
reader = ArcReaderFactory.getReader(in, 1024);
record = reader.getNextRecord();
Assert.assertNotNull(record);
Assert.assertFalse(record.diagnostics.hasErrors());
Assert.assertFalse(record.diagnostics.hasWarnings());
record = reader.getNextRecord();
Assert.assertNotNull(record);
TestBaseUtils.compareDiagnoses((Object[][])test_cases[i][3], record.diagnostics.getErrors());
TestBaseUtils.compareDiagnoses((Object[][])test_cases[i][4], record.diagnostics.getWarnings());
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Unexpected exception!");
}
}
public abstract class TestHeaderCallback {
public abstract String getArchiveLength(Object[] test_case);
}
public byte[] filedesc() {
String arcData;
String vbData;
String mdData;
mdData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n";
mdData += "<arcmetadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:arc=\"http://archive.org/arc/1.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://archive.org/arc/1.0/\" xsi:schemaLocation=\"http://archive.org/arc/1.0/ http://www.archive.org/arc/1.0/arc.xsd\">\r\n";
mdData += "<arc:software>Heritrix @VERSION@ http://crawler.archive.org</arc:software>\r\n";
mdData += "<arc:hostname>blackbook</arc:hostname>\r\n";
mdData += "<arc:ip>192.168.1.13</arc:ip>\r\n";
mdData += "<dcterms:isPartOf>archive.org-shallow</dcterms:isPartOf>\r\n";
mdData += "<dc:description>archive.org shallow</dc:description>\r\n";
mdData += "<arc:operator>Admin</arc:operator>\r\n";
mdData += "<ns0:date xmlns:ns0=\"http://purl.org/dc/elements/1.1/\" xsi:type=\"dcterms:W3CDTF\">2008-04-30T20:48:24+00:00</ns0:date>\r\n";
mdData += "<arc:http-header-user-agent>Mozilla/5.0 (compatible; heritrix/1.14.0 +http://crawler.archive.org)</arc:http-header-user-agent>\r\n";
mdData += "<arc:http-header-from>archive-crawler-agent@lists.sourceforge.net</arc:http-header-from>\r\n";
mdData += "<arc:robots>classic</arc:robots>\r\n";
mdData += "<dc:format>ARC file version 1.1</dc:format>\r\n";
mdData += "<dcterms:conformsTo xsi:type=\"dcterms:URI\">http://www.archive.org/web/researcher/ArcFileFormat.php</dcterms:conformsTo>\r\n";
mdData += "</arcmetadata>\r\n";
vbData = "1 1 InternetArchive\n";
vbData += "URL IP-address Archive-date Content-type Archive-length\n";
arcData = "filedesc://BNF-FS-072076.arc.gz 0.0.0.0 20060305082251 text/plain " + (vbData.length() + mdData.length()) + "\n";
arcData += vbData;
arcData += mdData;
arcData += "\n";
return arcData.getBytes();
}
}