Skip to content

Commit

Permalink
Properly close resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Apr 4, 2024
1 parent 828ed4f commit 836eb97
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void test1() throws Exception {
}

private void testField(final ControlFieldDefinition field, final String value, final String expectedResult) throws Exception {
final StringWriter sw = new StringWriter();
final ControlFileWriter writer = new ControlFileWriter(sw);
writer.writeEntry(field.name, value, Optional.ofNullable(field.formatter));
sw.close();
final String result = sw.toString();
System.out.println("Actual: '" + result + "'");
Assertions.assertEquals(expectedResult, result);
try (final StringWriter sw = new StringWriter()) {
final ControlFileWriter writer = new ControlFileWriter(sw);
writer.writeEntry(field.name, value, Optional.ofNullable(field.formatter));
final String result = sw.toString();
System.out.println("Actual: '" + result + "'");
Assertions.assertEquals(expectedResult, result);
}
}
}
2 changes: 1 addition & 1 deletion rpm/src/test/java/org/eclipse/packager/rpm/WriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void test1() throws IOException {
requirements.add(new Dependency("rpmlib(CompressedFileNames)", "3.0.4-1", RpmDependencyFlags.LESS, RpmDependencyFlags.EQUAL, RpmDependencyFlags.RPMLIB));
Dependencies.putRequirements(header, requirements);

try (final PayloadRecorder.Finished finished = new PayloadRecorder().finish()) {
try (final PayloadRecorder payloadRecorder = new PayloadRecorder(); final PayloadRecorder.Finished finished = payloadRecorder.finish()) {
try (final RpmWriter writer = new RpmWriter(rpm1, new LeadBuilder("test1", new RpmVersion("1.0.0")), header)) {
writer.setPayload(finished);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

@TestMethodOrder(OrderAnnotation.class)
public class RpmFileSignatureProcessorTest {

private static final String SOURCE_FILE_PATH = "src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm";

private static final String PRIVATE_KEY_PATH = "src/test/resources/key/private_key.txt";
Expand Down Expand Up @@ -79,38 +78,36 @@ public void testSigningExistingRpm() throws IOException, PGPException {
RpmFileSignatureProcessor.perform(rpm, privateKeyStream, passPhrase, resultOut, HashAlgorithm.SHA256);

// Read the initial (unsigned) rpm file
RpmInputStream initialRpm = new RpmInputStream(new FileInputStream(rpm));
initialRpm.available();
initialRpm.close();
InputHeader<RpmSignatureTag> initialHeader = initialRpm.getSignatureHeader();

// Read the signed rpm file
RpmInputStream rpmSigned = new RpmInputStream(new FileInputStream(signedRpm));
rpmSigned.available();
rpmSigned.close();
InputHeader<RpmSignatureTag> signedHeader = rpmSigned.getSignatureHeader();

// Get information of the initial rpm file
int initialSize = (int) initialHeader.getEntry(RpmSignatureTag.SIZE).get().getValue();
int initialPayloadSize = (int) initialHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue();
String initialSha1 = initialHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString();
String initialMd5 = Rpms.dumpValue(initialHeader.getEntry(RpmSignatureTag.MD5).get().getValue());

// Get information of the signed rpm file
int signedSize = (int) signedHeader.getEntry(RpmSignatureTag.SIZE).get().getValue();
int signedPayloadSize = (int) signedHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue();
String signedSha1 = signedHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString();
String signedMd5 = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.MD5).get().getValue());
String pgpSignature = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.PGP).get().getValue());

// Compare information values of initial rpm and signed rpm
assertEquals(initialSize, signedSize);
assertEquals(initialPayloadSize, signedPayloadSize);
assertEquals(initialSha1, signedSha1);
assertEquals(initialMd5, signedMd5);

// Verify if signature is present
assertNotNull(pgpSignature);
try (final RpmInputStream initialRpm = new RpmInputStream(new FileInputStream(rpm))) {
InputHeader<RpmSignatureTag> initialHeader = initialRpm.getSignatureHeader();

// Read the signed rpm file
try (final RpmInputStream rpmSigned = new RpmInputStream(new FileInputStream(signedRpm))) {
InputHeader<RpmSignatureTag> signedHeader = rpmSigned.getSignatureHeader();

// Get information of the initial rpm file
int initialSize = (int) initialHeader.getEntry(RpmSignatureTag.SIZE).get().getValue();
int initialPayloadSize = (int) initialHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue();
String initialSha1 = initialHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString();
String initialMd5 = Rpms.dumpValue(initialHeader.getEntry(RpmSignatureTag.MD5).get().getValue());

// Get information of the signed rpm file
int signedSize = (int) signedHeader.getEntry(RpmSignatureTag.SIZE).get().getValue();
int signedPayloadSize = (int) signedHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue();
String signedSha1 = signedHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString();
String signedMd5 = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.MD5).get().getValue());
String pgpSignature = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.PGP).get().getValue());

// Compare information values of initial rpm and signed rpm
assertEquals(initialSize, signedSize);
assertEquals(initialPayloadSize, signedPayloadSize);
assertEquals(initialSha1, signedSha1);
assertEquals(initialMd5, signedMd5);

// Verify if signature is present
assertNotNull(pgpSignature);
}
}
}
}

Expand Down Expand Up @@ -181,5 +178,4 @@ private static String run(String... command) throws IOException, InterruptedExce
process.waitFor();
return stdout;
}

}

0 comments on commit 836eb97

Please sign in to comment.