| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you 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.apache.karaf.obr.command.util; |
| |
| import org.junit.Test; |
| |
| import static org.junit.Assert.fail; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.util.jar.JarInputStream; |
| import java.util.zip.ZipEntry; |
| import java.util.zip.ZipOutputStream; |
| |
| public class FileUtilTest { |
| |
| @Test |
| public void goodExtractTest() throws Exception { |
| File base = new File("target/test"); |
| base.mkdirs(); |
| File goodKarFile = new File(base, "good.kar"); |
| ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(goodKarFile)); |
| ZipEntry entry = new ZipEntry("foo.bar"); |
| zos.putNextEntry(entry); |
| |
| byte[] data = "Test Data".getBytes(); |
| zos.write(data, 0, data.length); |
| zos.closeEntry(); |
| zos.close(); |
| |
| JarInputStream jis = new JarInputStream(new FileInputStream(goodKarFile)); |
| FileUtil.unjar(jis, base); |
| |
| goodKarFile.delete(); |
| } |
| |
| @Test |
| public void badExtractTest() throws Exception { |
| File base = new File("target/test"); |
| base.mkdirs(); |
| File badKarFile = new File(base, "bad.kar"); |
| ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(badKarFile)); |
| ZipEntry entry = new ZipEntry("../../../../../../../../../tmp/foo.bar"); |
| zos.putNextEntry(entry); |
| |
| byte[] data = "Test Data".getBytes(); |
| zos.write(data, 0, data.length); |
| zos.closeEntry(); |
| zos.close(); |
| |
| JarInputStream jis = new JarInputStream(new FileInputStream(badKarFile)); |
| try { |
| FileUtil.unjar(jis, base); |
| fail("Failure expected on extracting a jar with relative file paths"); |
| } catch (Exception ex) { |
| // expected |
| } |
| |
| badKarFile.delete(); |
| } |
| |
| } |