From 2fc0803b5e701471e7a66ef925cb641961443039 Mon Sep 17 00:00:00 2001
From: "elena.parovyshnaya"
Date: Fri, 3 Jul 2020 13:41:38 +0300
Subject: [PATCH 1/2] Bug 564819 - API revision | conditions | stream codecs
review
- add test set for encoding by BcStreamCodec
- Move `FileContent` from test utility for lic.base.io (internal)
Signed-off-by: elena.parovyshnaya
---
.../lic/internal/base/io}/FileContent.java | 10 +-
.../lic/internal/bc/BcDecodedStream.java | 3 +-
.../passage/lic/internal/bc/BcDigest.java | 12 +-
.../lic/internal/bc/BcStreamCodec.java | 3 +-
.../bc/tests/KeyPairGenerationTest.java | 67 +++-----
.../lic/internal/bc/tests/PairContent.java | 3 +
.../internal/bc/tests/StreamEncodingTest.java | 145 ++++++++++++++++++
.../lic/internal/bc/tests/TmpFile.java | 75 +++++++++
8 files changed, 263 insertions(+), 55 deletions(-)
rename {tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests => bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/io}/FileContent.java (77%)
create mode 100644 tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
create mode 100644 tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/TmpFile.java
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/FileContent.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/io/FileContent.java
similarity index 77%
rename from tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/FileContent.java
rename to bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/io/FileContent.java
index 22a4810df..851909b14 100644
--- a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/FileContent.java
+++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/io/FileContent.java
@@ -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);
diff --git a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDecodedStream.java b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDecodedStream.java
index 536255e2c..bf5b102da 100644
--- a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDecodedStream.java
+++ b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDecodedStream.java
@@ -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(//
diff --git a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDigest.java b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDigest.java
index dd52024fc..8b5a0089a 100644
--- a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDigest.java
+++ b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcDigest.java
@@ -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 {
+@SuppressWarnings("restriction")
+public final class BcDigest implements Supplier {
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();
diff --git a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcStreamCodec.java b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcStreamCodec.java
index 2166da6e4..145984356 100644
--- a/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcStreamCodec.java
+++ b/bundles/org.eclipse.passage.lic.bc/src/org/eclipse/passage/lic/internal/bc/BcStreamCodec.java
@@ -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);
}
}
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/KeyPairGenerationTest.java b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/KeyPairGenerationTest.java
index 7aa31e124..a0eb1e4ca 100644
--- a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/KeyPairGenerationTest.java
+++ b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/KeyPairGenerationTest.java
@@ -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;
@@ -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;
@@ -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 {
@@ -69,15 +69,18 @@ public void subsequentPairDiffer() throws IOException {
* NB: It is not symmetric: you can encrypt only with {@code private} key
* and then decipher only with the pairing {@code public}.
*
+ *
+ * Side test: {@code BcStreamCodec} does not have state.
+ *
*
* @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$
@@ -114,7 +117,7 @@ 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$
}
@@ -122,7 +125,7 @@ public void publicKeyPathIsMandatory() throws LicensingException, IOException {
@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$
@@ -131,8 +134,8 @@ 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$
}
@@ -140,8 +143,8 @@ public void usernameIsMandatory() throws LicensingException, IOException {
@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);
}
@@ -149,10 +152,10 @@ public void passwordIsMandatory() throws LicensingException, IOException {
@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$
@@ -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
@@ -180,8 +183,8 @@ private PairInfo pair(ThrowingCtor ctor) throws IOException {
private PairInfo pair(ThrowingCtor 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
@@ -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$
}
}
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/PairContent.java b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/PairContent.java
index 25ba046de..3602fcbdf 100644
--- a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/PairContent.java
+++ b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/PairContent.java
@@ -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 {
PairContent(Path first, Path second) throws IOException {
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
new file mode 100644
index 000000000..aedc9d5f8
--- /dev/null
+++ b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (c) 2020 ArSysOp
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * https://www.eclipse.org/legal/epl-2.0/.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * ArSysOp - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.passage.lic.internal.bc.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Objects;
+
+import org.eclipse.passage.lic.internal.api.LicensedProduct;
+import org.eclipse.passage.lic.internal.api.LicensingException;
+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;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+@SuppressWarnings("restriction")
+public final class StreamEncodingTest {
+
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ @Test
+ public void encodes() throws IOException {
+ // given
+ Path victim = new TmpFile(folder).fileWithContent();
+ Path encoded = new TmpFile(folder).file(".txt"); //$NON-NLS-1$
+ assumeTrue(Files.size(encoded) == 0);
+ String user = "Suer"; //$NON-NLS-1$
+ String pass = "Vyer"; //$NON-NLS-1$
+
+ // when
+ try (//
+ InputStream source = new FileInputStream(victim.toFile());
+ OutputStream destination = new FileOutputStream(encoded.toFile());
+ InputStream key = new FileInputStream(privateKey(user, pass).toFile())) {
+ new BcStreamCodec(this::product).encode(source, destination, key, user, pass);
+ } catch (LicensingException e) {
+ fail("Encoding for valid data is not supposed to fail"); //$NON-NLS-1$
+ }
+
+ // then
+ assertTrue(Files.size(encoded) > 0);
+ assertFalse(Objects.deepEquals(//
+ new FileContent(victim).get(), //
+ new FileContent(encoded).get()));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void sourceIsMandatory() throws IOException {
+ try (OutputStream destination = anOutput(); InputStream key = anInput()) {
+ encodeNull(null, destination, key);
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void destinationIsMandatory() throws IOException {
+ try (InputStream input = anInput(); InputStream key = anInput()) {
+ encodeNull(input, null, key);
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void keyIsMandatory() throws IOException {
+ try (InputStream input = anInput(); OutputStream destination = anOutput()) {
+ encodeNull(input, destination, null);
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void ownerIsMandatory() throws IOException {
+ try (InputStream input = anInput(); OutputStream destination = anOutput(); InputStream key = anInput()) {
+ encodeNull(input, destination, key, null, "pass"); //$NON-NLS-1$
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void passwordIsMandatory() throws IOException {
+ try (InputStream input = anInput(); OutputStream destination = anOutput(); InputStream key = anInput()) {
+ encodeNull(input, destination, key, "owner", null); //$NON-NLS-1$
+ }
+ }
+
+ private Path privateKey(String user, String pass) throws IOException {
+ Path key = new TmpFile(folder).keyFile(new PassageFileExtension.PrivateKey());
+ try {
+ new BcStreamCodec(this::product).createKeyPair(//
+ new TmpFile(folder).keyFile(new PassageFileExtension.PublicKey()), //
+ key, user, pass);
+ } catch (LicensingException e) {
+ fail("Failed to getenate key pair: check corresponging tests set"); //$NON-NLS-1$
+ }
+ return key;
+
+ }
+
+ private InputStream anInput() {
+ return new ByteArrayInputStream(new byte[0]);
+ }
+
+ private OutputStream anOutput() {
+ return new ByteArrayOutputStream();
+ }
+
+ private void encodeNull(InputStream input, OutputStream output, InputStream key) {
+ encodeNull(input, output, key, "usher", "doorZ"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ private void encodeNull(InputStream input, OutputStream output, InputStream key, String user, String pass) {
+ try {
+ new BcStreamCodec(this::product).encode(input, output, key, user, pass);
+ } catch (LicensingException e) {
+ fail("Incorrect incoming data are not intended to use any encoding activity"); //$NON-NLS-1$
+ }
+ }
+
+ private LicensedProduct product() {
+ return new BaseLicensedProduct("encoding-test-product", "2.4.21"); //$NON-NLS-1$//$NON-NLS-2$
+ }
+
+}
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/TmpFile.java b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/TmpFile.java
new file mode 100644
index 000000000..c68f68308
--- /dev/null
+++ b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/TmpFile.java
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2020 ArSysOp
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * https://www.eclipse.org/legal/epl-2.0/.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * ArSysOp - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.passage.lic.internal.bc.tests;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.Path;
+import java.util.stream.IntStream;
+
+import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
+import org.junit.rules.TemporaryFolder;
+
+@SuppressWarnings("restriction")
+final class TmpFile {
+
+ private final TemporaryFolder folder;
+
+ TmpFile(TemporaryFolder folder) {
+ this.folder = folder;
+ }
+
+ /**
+ * Physically creates an empty file with demanded passage file extension
+ */
+ Path keyFile(PassageFileExtension extension) throws IOException {
+ return file(extension.get());
+ }
+
+ /**
+ * Physically creates an empty file with an arbitrary extension
+ */
+ Path file(String extension) throws IOException {
+ return folder.newFile(Long.toHexString(System.nanoTime()) + extension).toPath();
+ }
+
+ /**
+ * Creates a reference for not yet existing file
+ */
+ Path keyPath(PassageFileExtension extension) throws IOException {
+ return folder.getRoot().toPath().resolve(Long.toHexString(System.nanoTime()) + extension.get());
+ }
+
+ Path fileWithContent() throws IOException {
+ Path path = file(".txt"); //$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;
+ }
+
+ Path fileWithRandomContent(int length) throws IOException {
+ Path path = file(".txt"); //$NON-NLS-1$
+ String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //$NON-NLS-1$
+ StringBuilder fast = new StringBuilder();
+ IntStream.range(0, length)//
+ .mapToObj(i -> Character.toString(alphabet.charAt((int) (Math.random() * (alphabet.length() - 1)))))//
+ .forEach(fast::append);
+ try (PrintWriter writer = new PrintWriter(path.toFile())) {
+ writer.println(fast.toString());
+ }
+ return path;
+ }
+}
From 73d1eb286008662afcbc72b92c90f699abdb4c2b Mon Sep 17 00:00:00 2001
From: "elena.parovyshnaya"
Date: Fri, 3 Jul 2020 13:53:15 +0300
Subject: [PATCH 2/2] Bug 564819 - API revision | conditions | stream codecs
review
- add more test to encoding test set
Signed-off-by: elena.parovyshnaya
---
.../internal/bc/tests/StreamEncodingTest.java | 32 +++++++++++++++----
1 file changed, 26 insertions(+), 6 deletions(-)
diff --git a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
index aedc9d5f8..ca5e05c22 100644
--- a/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
+++ b/tests/org.eclipse.passage.lic.bc.tests/src/org/eclipse/passage/lic/internal/bc/tests/StreamEncodingTest.java
@@ -42,13 +42,13 @@
public final class StreamEncodingTest {
@Rule
- public TemporaryFolder folder = new TemporaryFolder();
+ public TemporaryFolder root = new TemporaryFolder();
@Test
- public void encodes() throws IOException {
+ public void encodingIsFunctional() throws IOException {
// given
- Path victim = new TmpFile(folder).fileWithContent();
- Path encoded = new TmpFile(folder).file(".txt"); //$NON-NLS-1$
+ Path victim = new TmpFile(root).fileWithContent();
+ Path encoded = new TmpFile(root).file(".txt"); //$NON-NLS-1$
assumeTrue(Files.size(encoded) == 0);
String user = "Suer"; //$NON-NLS-1$
String pass = "Vyer"; //$NON-NLS-1$
@@ -70,6 +70,26 @@ public void encodes() throws IOException {
new FileContent(encoded).get()));
}
+ /**
+ * Here we do the encoding using the random char sequence as an encryption key.
+ * This supposed to constantly fail as only properly generated keys are
+ * acceptable for encoding purpose.
+ */
+ @Test
+ public void properKeyIsRequired() throws IOException {
+ try (//
+ InputStream source = new FileInputStream(new TmpFile(root).fileWithContent().toFile());
+ OutputStream destination = anOutput();
+ InputStream key = new FileInputStream(new TmpFile(root).fileWithRandomContent(1024).toFile());) {
+ new BcStreamCodec(this::product).encode(//
+ source, destination, key, "user", "pass");//$NON-NLS-1$ //$NON-NLS-2$
+ } catch (LicensingException e) {
+ assertTrue(e.getMessage().contains("key")); //$NON-NLS-1$
+ return;
+ }
+ fail("Enconding is not supposed to encrypt anything with a random sequence of chars as a key"); //$NON-NLS-1$
+ }
+
@Test(expected = NullPointerException.class)
public void sourceIsMandatory() throws IOException {
try (OutputStream destination = anOutput(); InputStream key = anInput()) {
@@ -106,10 +126,10 @@ public void passwordIsMandatory() throws IOException {
}
private Path privateKey(String user, String pass) throws IOException {
- Path key = new TmpFile(folder).keyFile(new PassageFileExtension.PrivateKey());
+ Path key = new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey());
try {
new BcStreamCodec(this::product).createKeyPair(//
- new TmpFile(folder).keyFile(new PassageFileExtension.PublicKey()), //
+ new TmpFile(root).keyFile(new PassageFileExtension.PublicKey()), //
key, user, pass);
} catch (LicensingException e) {
fail("Failed to getenate key pair: check corresponging tests set"); //$NON-NLS-1$