Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

564819 API revision | conditions | stream codecs review #279

Merged
merged 2 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.bc.tests;
package org.eclipse.passage.lic.internal.base.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

final class FileContent {
public final class FileContent {

private final Path file;

FileContent(Path file) {
public FileContent(Path file) {
Objects.requireNonNull(file, "FileContent::file"); //$NON-NLS-1$
this.file = file;
}

byte[] get() throws IOException {
public byte[] get() throws IOException {
byte[] content = new byte[(int) Files.size(file)];
try (InputStream stream = new FileInputStream(file.toFile())) {
stream.read(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ private void verifyKey(DigestExpectation digest, byte[] key) throws LicensingExc
if (!digest.expected()) {
return;
}
byte[] calculatedDigest = new BcDigest(key).get();
if (Objects.deepEquals(calculatedDigest, digest.value())) {
if (Objects.deepEquals(new BcDigest(key).get(), digest.value())) {
return;
}
throw new LicensingException(//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.bc;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;
import java.util.function.Supplier;

import org.bouncycastle.crypto.digests.SHA512Digest;
import org.eclipse.passage.lic.internal.base.io.FileContent;

final class BcDigest implements Supplier<byte[]> {
@SuppressWarnings("restriction")
public final class BcDigest implements Supplier<byte[]> {

private final byte[] source;

BcDigest(byte[] source) {
public BcDigest(byte[] source) {
Objects.requireNonNull(source, "BcDigest::source"); //$NON-NLS-1$
this.source = source;
}

public BcDigest(Path source) throws IOException {
this(new FileContent(source).get());
}

@Override
public byte[] get() {
SHA512Digest digest = new SHA512Digest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void decode(InputStream input, OutputStream output, InputStream key, Dige
Objects.requireNonNull(output, "BcStreamCodec::decode::output"); //$NON-NLS-1$
Objects.requireNonNull(key, "BcStreamCodec::decode::key"); //$NON-NLS-1$ ;
Objects.requireNonNull(digest, "BcStreamCodec::decode::digest"); //$NON-NLS-1$
new BcDecodedStream(product.get(), input, output).produce(key, digest);
new BcDecodedStream(product.get(), input, output)//
.produce(key, digest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
Expand All @@ -30,6 +29,7 @@
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.io.DigestExpectation;
import org.eclipse.passage.lic.internal.base.BaseLicensedProduct;
import org.eclipse.passage.lic.internal.base.io.FileContent;
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
import org.eclipse.passage.lic.internal.bc.BcStreamCodec;
import org.junit.Rule;
Expand All @@ -40,7 +40,7 @@
public final class KeyPairGenerationTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
public TemporaryFolder root = new TemporaryFolder();

@Test
public void generationSucceeds() throws IOException {
Expand Down Expand Up @@ -69,15 +69,18 @@ public void subsequentPairDiffer() throws IOException {
* <b>NB: </b>It is not symmetric: you can encrypt only with {@code private} key
* and then decipher only with the pairing {@code public}.
* </p>
* <p>
* Side test: {@code BcStreamCodec} does not have state.
* </p>
*
* @throws IOException in case of file system denials
*/
@Test
public void generatedPairIsFunctional() throws IOException {
// given
Path victim = fileWithContent();
Path encoded = file(".txt"); //$NON-NLS-1$
Path decoded = file(".txt"); //$NON-NLS-1$
Path victim = new TmpFile(root).fileWithContent();
Path encoded = new TmpFile(root).file(".txt"); //$NON-NLS-1$
Path decoded = new TmpFile(root).file(".txt"); //$NON-NLS-1$
String user = "fake.user"; //$NON-NLS-1$
String pass = "some$pass#val&1"; //$NON-NLS-1$

Expand Down Expand Up @@ -114,15 +117,15 @@ public void generatedPairIsFunctional() throws IOException {
public void publicKeyPathIsMandatory() throws LicensingException, IOException {
new BcStreamCodec(this::product).createKeyPair(//
null, //
keyFile(new PassageFileExtension.PrivateKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey()), //
"u", //$NON-NLS-1$
"p"); //$NON-NLS-1$
}

@Test(expected = NullPointerException.class)
public void privateKeyPathIsMandatory() throws LicensingException, IOException {
new BcStreamCodec(this::product).createKeyPair(//
keyFile(new PassageFileExtension.PublicKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PublicKey()), //
null, //
"u", //$NON-NLS-1$
"p"); //$NON-NLS-1$
Expand All @@ -131,28 +134,28 @@ public void privateKeyPathIsMandatory() throws LicensingException, IOException {
@Test(expected = NullPointerException.class)
public void usernameIsMandatory() throws LicensingException, IOException {
new BcStreamCodec(this::product).createKeyPair(//
keyFile(new PassageFileExtension.PublicKey()), //
keyFile(new PassageFileExtension.PrivateKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PublicKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey()), //
null, //
"p"); //$NON-NLS-1$
}

@Test(expected = NullPointerException.class)
public void passwordIsMandatory() throws LicensingException, IOException {
new BcStreamCodec(this::product).createKeyPair(//
keyFile(new PassageFileExtension.PublicKey()), //
keyFile(new PassageFileExtension.PrivateKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PublicKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey()), //
"u", //$NON-NLS-1$
null);
}

@Test
public void absentPrivateKeyFileIsCreated() throws LicensingException, IOException {
// given
Path privatePath = keyPath(new PassageFileExtension.PrivateKey());
Path privatePath = new TmpFile(root).keyPath(new PassageFileExtension.PrivateKey());
// when
new BcStreamCodec(this::product).createKeyPair(//
keyFile(new PassageFileExtension.PublicKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PublicKey()), //
privatePath, //
"u", //$NON-NLS-1$
"p"); //$NON-NLS-1$
Expand All @@ -163,11 +166,11 @@ public void absentPrivateKeyFileIsCreated() throws LicensingException, IOExcepti
@Test
public void absentPublicKeyFileIsCreated() throws LicensingException, IOException {
// given
Path publicPath = keyPath(new PassageFileExtension.PublicKey());
Path publicPath = new TmpFile(root).keyPath(new PassageFileExtension.PublicKey());
// when
new BcStreamCodec(this::product).createKeyPair(//
publicPath, //
keyFile(new PassageFileExtension.PrivateKey()), //
new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey()), //
"u", //$NON-NLS-1$
"p"); //$NON-NLS-1$
// then
Expand All @@ -180,8 +183,8 @@ private <I> PairInfo<I> pair(ThrowingCtor<I> ctor) throws IOException {

private <I> PairInfo<I> pair(ThrowingCtor<I> ctor, String user, String pass) throws IOException {
// given:
Path pub = keyFile(new PassageFileExtension.PublicKey());
Path secret = keyFile(new PassageFileExtension.PrivateKey());
Path pub = new TmpFile(root).keyFile(new PassageFileExtension.PublicKey());
Path secret = new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey());
BcStreamCodec codec = new BcStreamCodec(this::product);
try {
// when
Expand All @@ -199,35 +202,7 @@ private void assertFileExists(Path file) {
}

private LicensedProduct product() {
return new BaseLicensedProduct("test-product", "1.0.18"); //$NON-NLS-1$//$NON-NLS-2$
}

/**
* Physically creates an empty file with demanded extension
*/
private Path keyFile(PassageFileExtension extension) throws IOException {
return file(extension.get());
}

private Path file(String extension) throws IOException {
return folder.newFile(Long.toHexString(System.nanoTime()) + extension).toPath();
}

/**
* Creates a reference for not yet existing file
*/
private Path keyPath(PassageFileExtension extension) throws IOException {
return folder.getRoot().toPath().resolve(Long.toHexString(System.nanoTime()) + extension.get());
}

private Path fileWithContent() throws IOException {
Path path = folder.newFile(Long.toHexString(System.nanoTime()) + ".txt").toPath(); //$NON-NLS-1$
try (PrintWriter writer = new PrintWriter(path.toFile())) {
writer.println("content row 1"); //$NON-NLS-1$
writer.println("content row 2"); //$NON-NLS-1$
writer.println("content row 3"); //$NON-NLS-1$
}
return path;
return new BaseLicensedProduct("keygen-test-product", "1.0.18"); //$NON-NLS-1$//$NON-NLS-2$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.io.IOException;
import java.nio.file.Path;

import org.eclipse.passage.lic.internal.base.io.FileContent;

@SuppressWarnings("restriction")
final class PairContent extends PairInfo<byte[]> {

PairContent(Path first, Path second) throws IOException {
Expand Down
Loading