Skip to content

Commit

Permalink
Fix custom open and close token not applied to FilteredFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
alerosmile committed Jan 10, 2025
1 parent ab3424c commit df87db8
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 15 deletions.
10 changes: 4 additions & 6 deletions src/main/java/org/vafer/jdeb/ControlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ void buildControl(BinaryPackageControlFile packageControlFile, File[] controlFil

if (CONFIGURATION_FILENAMES.contains(file.getName()) || MAINTAINER_SCRIPTS.contains(file.getName())) {

FilteredFile configurationFile = new FilteredFile(new FileInputStream(file), resolver, encoding);
configurationFile.setOpenToken(openReplaceToken);
configurationFile.setCloseToken(closeReplaceToken);
FilteredFile configurationFile = new FilteredFile(new FileInputStream(file), resolver, encoding, openReplaceToken, closeReplaceToken);
addControlEntry(file.getName(), configurationFile.toString(), outputStream);

} else {
Expand Down Expand Up @@ -177,7 +175,7 @@ private String createPackageConffilesFile(final List<String> conffiles) {

@Deprecated
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize) throws IOException, ParseException {
return createPackageControlFile(file, pDataSize, Charset.defaultCharset());
return createPackageControlFile(file, pDataSize, Charset.defaultCharset(), "[[", "]]");
}

/**
Expand All @@ -191,8 +189,8 @@ public BinaryPackageControlFile createPackageControlFile(File file, BigInteger p
* @param pDataSize the size of the installed package
* @param encoding the encoding used to read the files
*/
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize, Charset encoding) throws IOException, ParseException {
FilteredFile controlFile = new FilteredFile(new FileInputStream(file), resolver, encoding);
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize, Charset encoding, String openToken, String closeToken) throws IOException, ParseException {
FilteredFile controlFile = new FilteredFile(new FileInputStream(file), resolver, encoding, openToken, closeToken);
BinaryPackageControlFile packageControlFile = new BinaryPackageControlFile(controlFile.toString());

if (packageControlFile.get("Distribution") == null) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/vafer/jdeb/DebMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.vafer.jdeb.debian.BinaryPackageControlFile;
import org.vafer.jdeb.debian.ChangesFile;
import org.vafer.jdeb.signing.PGPSigner;
import org.vafer.jdeb.utils.FilteredFile;
import org.vafer.jdeb.utils.PGPSignatureOutputStream;
import org.vafer.jdeb.utils.Utils;
import org.vafer.jdeb.utils.VariableResolver;
Expand Down Expand Up @@ -144,8 +145,8 @@ public class DebMaker {
private Long outputTimestampMs;

private VariableResolver variableResolver;
private String openReplaceToken;
private String closeReplaceToken;
private String openReplaceToken = FilteredFile.DEFAULT_OPEN_TOKEN;
private String closeReplaceToken = FilteredFile.DEFAULT_CLOSE_TOKEN;

private final Collection<DataProducer> dataProducers = new ArrayList<>();

Expand Down Expand Up @@ -531,7 +532,7 @@ public BinaryPackageControlFile createSignedDeb(Compression compression, final P

console.debug("Building control");
ControlBuilder controlBuilder = new ControlBuilder(console, variableResolver, openReplaceToken, closeReplaceToken, outputTimestampMs);
BinaryPackageControlFile packageControlFile = controlBuilder.createPackageControlFile(new File(control, "control"), size, encoding);
BinaryPackageControlFile packageControlFile = controlBuilder.createPackageControlFile(new File(control, "control"), size, encoding, openReplaceToken, closeReplaceToken);
if (packageControlFile.get("Package") == null) {
packageControlFile.set("Package", packageName);
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/vafer/jdeb/utils/FilteredFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,34 @@

public class FilteredFile {

private String openToken = "[[";
private String closeToken = "]]";
public static final String DEFAULT_OPEN_TOKEN = "[[";
public static final String DEFAULT_CLOSE_TOKEN = "]]";

private String openToken;
private String closeToken;
private List<String> lines = new ArrayList<>();

@Deprecated
public FilteredFile(InputStream in, VariableResolver resolver) throws IOException {
parse(in, resolver, Charset.defaultCharset());
}
this(in, resolver, Charset.defaultCharset());
}

public FilteredFile(InputStream in, VariableResolver resolver, Charset encoding) throws IOException {
parse(in, resolver, encoding);
this(in, resolver, encoding, DEFAULT_OPEN_TOKEN, DEFAULT_CLOSE_TOKEN);
}

public FilteredFile(InputStream in, VariableResolver resolver, Charset encoding, String openToken, String closeToken) throws IOException {
this.openToken = openToken;
this.closeToken = closeToken;
parse(in, resolver, encoding);
}

@Deprecated
public void setOpenToken(String token) {
openToken = token;
}

@Deprecated
public void setCloseToken(String token) {
closeToken = token;
}
Expand Down
73 changes: 72 additions & 1 deletion src/test/java/org/vafer/jdeb/DebMakerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void visit(TarArchiveEntry entry, byte[] content) throws IOException {
if (entry.getName().equals("./control")) {
try {
ControlFile controlFile = new BinaryPackageControlFile(org.apache.commons.io.IOUtils.toString(new ByteArrayInputStream(content), StandardCharsets.UTF_8));
assertEquals("the encoding is wrong", controlFile.get("Maintainer"), "ジョン Doe <[email protected]>");
assertEquals("the encoding is wrong", "ジョン Doe <[email protected]>", controlFile.get("Maintainer"));
} catch(Exception e) {
throw new IOException(e);
}
Expand Down Expand Up @@ -256,6 +256,77 @@ else if (entry.getName().equals("./text.txt")) {
});
}

@Test
public void testCreationCustomToken() throws Exception {
DataProducer[] data = prepareData();
File deb = File.createTempFile("jdeb", ".deb");

File conffile = new File(getClass().getResource("deb/data.tgz").toURI());

Map<String, String> map = new HashMap<>();
map.put("name", "actualName");
map.put("version", "actualVersion");
map.put("maintainer", "John Doe <[email protected]>");
MapVariableResolver variableResolver = new MapVariableResolver(map);

Data conffile1 = new Data();
conffile1.setType("file");
conffile1.setSrc(conffile);
conffile1.setDst("/absolute/path/to/configuration");
conffile1.setConffile(true);
Data conffile2 = new Data();
conffile2.setType("file");
conffile2.setSrc(conffile);
conffile2.setConffile(true);

Mapper mapper = new Mapper();
FieldUtils.writeField(mapper, "type", "perm", true);
FieldUtils.writeField(mapper, "prefix", "/absolute/prefix", true);
FieldUtils.writeField(conffile2, "mapper", mapper, true);

DebMaker maker =
new DebMaker(new NullConsole(), Arrays.asList(data), Arrays.<DataProducer>asList(conffile1, conffile2));
maker.setEncoding(StandardCharsets.UTF_8);
maker.setControl(new File(getClass().getResource("deb/controlcustomtoken").toURI()));
maker.setDeb(deb);
maker.setResolver(variableResolver);
maker.setOpenReplaceToken("{[{");
maker.setCloseReplaceToken("}]}");

BinaryPackageControlFile packageControlFile = maker.createDeb(Compression.GZIP);

assertTrue(packageControlFile.isValid());

final Map<String, TarArchiveEntry> filesInDeb = new HashMap<>();

final Set<String> actualConffileContent = new HashSet<>();

ArchiveWalker.walkControl(deb, new ArchiveVisitor<TarArchiveEntry>() {
@Override
public void visit(TarArchiveEntry entry, byte[] content) throws IOException {
if (entry.getName().equals("./control")) {
try {
ControlFile controlFile = new BinaryPackageControlFile(org.apache.commons.io.IOUtils.toString(new ByteArrayInputStream(content), StandardCharsets.UTF_8));
assertEquals("variable substitution failed", "John Doe <[email protected]>", controlFile.get("Maintainer"));
} catch(Exception e) {
throw new IOException(e);
}
}
else if (entry.getName().equals("./postinst") || entry.getName().equals("./prerm")) {
try {
for(String line : org.apache.commons.io.IOUtils.readLines(new ByteArrayInputStream(content), StandardCharsets.UTF_8)) {
if(line.startsWith("# P")) {
assertTrue("variable substitution failed", line.endsWith("actualName actualVersion"));
}
}
} catch(Exception e) {
throw new IOException(e);
}
}
}
});
}

@Test
public void testControlFilesPermissions() throws Exception {
File deb = new File("target/test-classes/test-control.deb");
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/vafer/jdeb/utils/FilteredFileTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public void testTokenSubstitution() throws Exception {
assertEquals("#!/bin/sh\ncat jdebcustom1 \necho 'custom2'\n", actual);
}

@Test
public void testCustomTokenSubstitution() throws Exception {
InputStream in = new ReaderInputStream(new StringReader("#!/bin/sh\ncat {[{artifactId}]}{[{myProperty1}]} \necho '{[{myProperty2}]}'\n"));

FilteredFile placeHolder = new FilteredFile(in, variableResolver, StandardCharsets.UTF_8, "{[{", "}]}");

String actual = placeHolder.toString();
assertEquals("#!/bin/sh\ncat jdebcustom1 \necho 'custom2'\n", actual);
}

@Test
public void testTokenSubstitutionWithinOpenCloseTokens() throws Exception {
HashMap<String, String> table = new HashMap<String, String>() {{
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/org/vafer/jdeb/deb/controlcustomtoken/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: test
Version: 1.0.1
Section: misc
Priority: optional
Architecture: i386
Depends: some-package
Maintainer: {[{maintainer}]}
Distribution: development
Description: revision @REVISION@, test package
This is a sample package control file.
.
Use for testing purposes only.
XB-UserDefinedField: This is a user defined field.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
#
# Post installation script for {[{name}]} {[{version}]}
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
#
# Pre removal script for {[{name}]} {[{version}]}
#

0 comments on commit df87db8

Please sign in to comment.