Skip to content

Commit

Permalink
additional testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
amakwana committed Jun 9, 2020
1 parent 64685ae commit 9e4230d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyStore;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class DynamicConfigVerifier {
* @throws MissingOptionException
*/
public static void main(String[] args) throws ParseException, InvalidKeyException, NoSuchAlgorithmException,
SignatureException, FileNotFoundException, KeyStoreException, IOException {
SignatureException, FileNotFoundException, KeyStoreException, IOException {

Options options = prepareOptions();
CommandLine cli = new DefaultParser().parse(options, args);
Expand All @@ -77,6 +77,7 @@ public static void main(String[] args) throws ParseException, InvalidKeyExceptio
}
else {
log.error("Could not verify " + modelTarFile + " with details provided");
System.exit(-1);
}
}

Expand Down Expand Up @@ -111,12 +112,24 @@ public static boolean verify(String fileContent, String signature, PublicKey pub
*/
public static String readTarContents(String archiveFile) throws FileNotFoundException, IOException {
StringBuffer sb = new StringBuffer();
TarArchiveInputStream archive = new TarArchiveInputStream(
new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile))));
TarArchiveEntry entry;
while ((entry = archive.getNextTarEntry()) != null) {
sb.append(FileUtils.readFileToString(new File(entry.getName()), StandardCharsets.UTF_8));
BufferedReader br = null;
TarArchiveInputStream archiveInputStream = null;
try {
archiveInputStream = new TarArchiveInputStream(
new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile))));
TarArchiveEntry entry;
while ((entry = archiveInputStream.getNextTarEntry()) != null) {
br = new BufferedReader(new InputStreamReader(archiveInputStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
} finally {
archiveInputStream.close();
br.close();
}

return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@

import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
Expand All @@ -27,16 +39,25 @@ public class DynamicConfigVerifiesTest {

private static KeyPair kp;
private static String signature;
private static String tarContent = null;
private static final String TAR_FILE_PATH = "src/test/resources/test.tar.gz";

@BeforeAll
public static void setUp() throws Exception {
createTarGZ();
kp = generateKeyPair();
signature = sign("testing-signature", kp.getPrivate());
tarContent = DynamicConfigVerifier.readTarContents(TAR_FILE_PATH);
signature = sign(tarContent, kp.getPrivate());
}

@AfterAll
public static void after() {
FileUtils.deleteQuietly(FileUtils.getFile(TAR_FILE_PATH));
}

@Test
public void testValidSignature() throws Exception {
assertTrue(DynamicConfigVerifier.verify("testing-signature", signature, kp.getPublic()));
assertTrue(DynamicConfigVerifier.verify(tarContent, signature, kp.getPublic()));
}

@Test
Expand Down Expand Up @@ -86,4 +107,37 @@ private static String sign(String data, PrivateKey privateKey) throws Exception
byte[] signature = privateSignature.sign();
return Base64.getEncoder().encodeToString(signature);
}

private static void createTarGZ() throws FileNotFoundException, IOException {
TarArchiveOutputStream tarOutputStream = null;
try {
String modelPath = "src/test/resources/models_missing/";
tarOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(TAR_FILE_PATH)))));
addFileToTarGz(tarOutputStream, modelPath, "");
} finally {
tarOutputStream.finish();
tarOutputStream.close();
}
}

private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.putArchiveEntry(tarEntry);

if (f.isFile()) {
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
}

0 comments on commit 9e4230d

Please sign in to comment.