Skip to content

Commit 2579dca

Browse files
fix for review comment
1 parent db69413 commit 2579dca

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/passes/ConfigFileCreationPass.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,26 @@ class ConfigFileCreationPass(cpg: Cpg, config: Config)
2525

2626
override val configFileFilters: List[Path => Boolean] = List(extensionFilter(PlistExt), extensionFilter(".xib"))
2727

28+
private def isBinaryPlist(file: Path): Boolean = {
29+
if (!file.extension().contains(PlistExt)) return false
30+
Try(IOUtils.readLinesInFile(file)) match {
31+
case Success(dataBeginning :: _) => dataBeginning.trim.startsWith("bplist")
32+
case _ => false
33+
}
34+
}
35+
2836
override def runOnPart(diffGraph: DiffGraphBuilder, file: Path): Unit = {
29-
val contentMaybe = if (file.extension().contains(PlistExt)) {
30-
Try(PropertyListParser.parse(file.toFile).toXMLPropertyList)
37+
val parseResult = if (isBinaryPlist(file)) {
38+
val header = s"<!--This has been generated from ${file.toAbsolutePath}-->\n"
39+
Try((PropertyListParser.parse(file.toFile).toXMLPropertyList, header))
3140
} else {
32-
Try(IOUtils.readEntireFile(file))
41+
Try((IOUtils.readEntireFile(file), ""))
3342
}
34-
contentMaybe match {
35-
case Success(content) =>
36-
val configFileContent =
37-
s"""<!--This has been generated from ${file.toAbsolutePath}-->
38-
|$content""".stripMargin
39-
val name = configFileName(file)
40-
val configNode = NewConfigFile().name(name).content(configFileContent)
43+
parseResult match {
44+
case Success((content, header)) =>
45+
val configFileContent = s"$header$content"
46+
val name = configFileName(file)
47+
val configNode = NewConfigFile().name(name).content(configFileContent)
4148
logger.debug(s"Adding config file $name")
4249
diffGraph.addNode(configNode)
4350
case Failure(error) =>

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/config/ConfigFileCreationPassTests.scala

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import java.nio.file.Files
1515
class ConfigFileCreationPassTests extends AnyFunSpec with Matchers {
1616

1717
private class TestDiffGraphBuilder extends io.shiftleft.codepropertygraph.generated.DiffGraphBuilder(GraphSchema) {
18-
val nodes = scala.collection.mutable.Buffer[Any]()
18+
val nodes = scala.collection.mutable.Buffer[DNode]()
1919
override def addNode(newNode: DNode): this.type = {
2020
this.nodes.append(newNode)
2121
this
2222
}
2323
}
2424

25-
private val xml: String =
25+
private val Xml: String =
2626
"""<?xml version="1.0" encoding="UTF-8"?>
2727
|<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
2828
|<plist version="1.0">
@@ -31,44 +31,43 @@ class ConfigFileCreationPassTests extends AnyFunSpec with Matchers {
3131
| <string>value</string>
3232
|</dict>
3333
|</plist>""".stripMargin
34+
// com.dd.plist.NSObject#toXMLPropertyList uses line separator when converting
3435
.replace("\n", System.lineSeparator())
35-
.replace(" ", "\t") // com.dd.plist.NSObject#toXMLPropertyList uses tabs when converting
36+
// com.dd.plist.NSObject#toXMLPropertyList uses tabs when converting
37+
.replace(" ", "\t")
3638

3739
describe("ConfigFileCreationPass") {
3840
it("creates config node for an XML plist") {
3941
FileUtil.usingTemporaryFile("test", ".plist") { xmlFile =>
40-
Files.write(xmlFile, xml.getBytes("UTF-8"))
42+
Files.write(xmlFile, Xml.getBytes("UTF-8"))
4143

42-
val config = Config()
43-
val pass = new ConfigFileCreationPass(null /* cpg not used in here */, config)
44-
val diff = new TestDiffGraphBuilder
44+
val pass = new ConfigFileCreationPass(null /* cpg not used in here */, Config())
45+
val diff = new TestDiffGraphBuilder
4546

46-
// run the pass on the XML file
4747
pass.runOnPart(diff, xmlFile)
4848
diff.nodes.nonEmpty shouldBe true
4949
val node = diff.nodes.collectFirst { case n: NewConfigFile => n }.get
5050
node.name should (startWith("test") and endWith(".plist"))
51-
node.content should (startWith("<!--This has been generated from") and endWith(xml))
51+
node.content shouldBe Xml
5252
}
5353
}
5454

5555
it("creates config node for a binary plist") {
5656
FileUtil.usingTemporaryFile("testbin-src", ".plist") { xmlFile =>
5757
FileUtil.usingTemporaryFile("testbin", ".plist") { binFile =>
58-
Files.write(xmlFile, xml.getBytes("UTF-8"))
58+
Files.write(xmlFile, Xml.getBytes("UTF-8"))
5959
PropertyListConverter.convertToBinary(xmlFile, binFile)
6060

61-
val config = Config()
62-
val pass = new ConfigFileCreationPass(null /* cpg not used in here */, config)
63-
val diff = new TestDiffGraphBuilder
61+
val pass = new ConfigFileCreationPass(null /* cpg not used in here */, Config())
62+
val diff = new TestDiffGraphBuilder
6463

6564
pass.runOnPart(diff, binFile)
6665

6766
diff.nodes.nonEmpty shouldBe true
6867
val node = diff.nodes.collectFirst { case n: NewConfigFile => n }.get
6968
node.name should (startWith("testbin") and endWith(".plist"))
7069
// binary should have been converted to XML content:
71-
node.content should (startWith("<!--This has been generated from") and endWith(xml))
70+
node.content should (startWith("<!--This has been generated from") and endWith(Xml))
7271
}
7372
}
7473
}

0 commit comments

Comments
 (0)