Skip to content

Commit 799b0e9

Browse files
committed
More tests
1 parent 52cb0fa commit 799b0e9

File tree

4 files changed

+60
-41
lines changed

4 files changed

+60
-41
lines changed

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/io/zip/StudentFileAwareUnzipper.java

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,57 +67,56 @@ public UnzipResult unzip(Path zip, Path target) throws IOException {
6767

6868
while (entries.hasMoreElements()) {
6969
ZipArchiveEntry entry = entries.nextElement();
70+
if (!entry.getName().startsWith(projectDirInZip)) {
71+
log.debug("Skipping non project file from zip - {}", entry.getName());
72+
continue;
73+
}
7074

71-
if (entry.getName().startsWith(projectDirInZip)) {
72-
String restOfPath =
73-
trimSlashes(entry.getName().substring(projectDirInZip.length()));
75+
String restOfPath =
76+
trimSlashes(entry.getName().substring(projectDirInZip.length()));
7477

75-
Path entryTargetPath =
76-
target.resolve(trimSlashes(restOfPath.replace("/", File.separator)));
77-
pathsInZip.add(entryTargetPath);
78+
Path entryTargetPath =
79+
target.resolve(trimSlashes(restOfPath.replace("/", File.separator)));
80+
pathsInZip.add(entryTargetPath);
7881

82+
log.debug(
83+
"Processing zipEntry with name {} to {}",
84+
entry.getName(),
85+
entryTargetPath);
86+
if (entry.isDirectory() || entryTargetPath.toFile().isDirectory()) {
87+
Files.createDirectories(entryTargetPath);
7988
log.debug(
80-
"Processing zipEntry with name {} to {}",
81-
entry.getName(),
82-
entryTargetPath);
83-
if (entry.isDirectory() || entryTargetPath.toFile().isDirectory()) {
84-
Files.createDirectories(entryTargetPath);
85-
log.debug(
86-
"{} is a directory - creating and off to the next file ",
87-
entry.getName());
88-
continue;
89-
}
90-
boolean shouldWrite;
91-
InputStream entryContent = zipFile.getInputStream(entry);
92-
byte[] entryData = IOUtils.toByteArray(entryContent);
93-
if (Files.exists(entryTargetPath)) {
94-
log.trace("Allowed to unzip, unzipping");
95-
96-
if (fileContentEquals(target.toFile(), entryData)) {
97-
shouldWrite = false;
98-
result.unchangedFiles.add(entryTargetPath);
99-
} else if (allowedToUnzip(entryTargetPath, target)) {
100-
shouldWrite = true;
101-
result.overwrittenFiles.add(entryTargetPath);
102-
} else {
103-
shouldWrite = false;
104-
result.skippedFiles.add(entryTargetPath);
105-
}
106-
} else {
89+
"{} is a directory - creating and off to the next file ",
90+
entry.getName());
91+
continue;
92+
}
93+
boolean shouldWrite;
94+
InputStream entryContent = zipFile.getInputStream(entry);
95+
byte[] entryData = IOUtils.toByteArray(entryContent);
96+
if (Files.exists(entryTargetPath)) {
97+
log.trace("Allowed to unzip, unzipping");
98+
99+
if (fileContentEquals(target.toFile(), entryData)) {
100+
shouldWrite = false;
101+
result.unchangedFiles.add(entryTargetPath);
102+
} else if (allowedToUnzip(entryTargetPath, target)) {
107103
shouldWrite = true;
108-
result.newFiles.add(entryTargetPath);
109-
}
110-
if (shouldWrite) {
111-
FileUtils.writeByteArrayToFile(entryTargetPath.toFile(), entryData);
104+
result.overwrittenFiles.add(entryTargetPath);
112105
} else {
113-
log.trace("Not allowed to unzip, skipping file");
106+
shouldWrite = false;
114107
result.skippedFiles.add(entryTargetPath);
115108
}
116-
log.debug("Done with file {}", entryTargetPath);
117-
118109
} else {
119-
log.debug("Skipping non project file from zip - {}", entry.getName());
110+
shouldWrite = true;
111+
result.newFiles.add(entryTargetPath);
112+
}
113+
if (shouldWrite) {
114+
FileUtils.writeByteArrayToFile(entryTargetPath.toFile(), entryData);
115+
} else {
116+
log.trace("Not allowed to unzip, skipping file");
117+
result.skippedFiles.add(entryTargetPath);
120118
}
119+
log.debug("Done with file {}", entryTargetPath);
121120
}
122121
}
123122

tmc-langs-framework/src/test/java/fi/helsinki/cs/tmc/langs/io/zip/StudentFileAwareUnzipperTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ public void canChangePolicy() throws Exception {
129129
assertTrue(originalSize != Files.size(srcFile));
130130
}
131131

132+
@Test
133+
public void testDeletion() throws Exception {
134+
Path bad = TestUtils.getPath(StudentFileAwareUnzipperTest.class, "zip")
135+
.resolve("bad-test-class-name.zip");
136+
Path fixed = TestUtils.getPath(StudentFileAwareUnzipperTest.class, "zip")
137+
.resolve("fixed-test-class-name.zip");
138+
139+
unzipper.unzip(bad, tmpDir);
140+
141+
unzipper.unzip(fixed, tmpDir);
142+
143+
Path tmpDirCorrect = Files.createTempDirectory("tmc-tmp-correct");
144+
unzipper.unzip(fixed, tmpDirCorrect);
145+
146+
Set<Path> found = listFiles(tmpDir);
147+
148+
assertThat(found).containsExactlyElementsIn(listFiles(tmpDirCorrect));
149+
FileUtils.deleteDirectory(tmpDirCorrect.toFile());
150+
}
151+
132152
private StudentFilePolicy getNothingIsStudentFilePolicy() {
133153
return new StudentFilePolicy() {
134154
@Override
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)