|
| 1 | +package io.joern.swiftsrc2cpg.passes.config |
| 2 | + |
| 3 | +import com.dd.plist.PropertyListConverter |
| 4 | +import flatgraph.DNode |
| 5 | +import io.joern.swiftsrc2cpg.Config |
| 6 | +import io.joern.swiftsrc2cpg.passes.ConfigFileCreationPass |
| 7 | +import io.shiftleft.codepropertygraph.generated.GraphSchema |
| 8 | +import io.shiftleft.codepropertygraph.generated.nodes.NewConfigFile |
| 9 | +import io.shiftleft.semanticcpg.utils.FileUtil |
| 10 | +import org.scalatest.funspec.AnyFunSpec |
| 11 | +import org.scalatest.matchers.should.Matchers |
| 12 | + |
| 13 | +import java.nio.file.Files |
| 14 | + |
| 15 | +class ConfigFileCreationPassTests extends AnyFunSpec with Matchers { |
| 16 | + |
| 17 | + private class TestDiffGraphBuilder extends io.shiftleft.codepropertygraph.generated.DiffGraphBuilder(GraphSchema) { |
| 18 | + val nodes = scala.collection.mutable.Buffer[DNode]() |
| 19 | + override def addNode(newNode: DNode): this.type = { |
| 20 | + this.nodes.append(newNode) |
| 21 | + this |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private val Xml: String = |
| 26 | + """<?xml version="1.0" encoding="UTF-8"?> |
| 27 | + |<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 28 | + |<plist version="1.0"> |
| 29 | + |<dict> |
| 30 | + | <key>Example</key> |
| 31 | + | <string>value</string> |
| 32 | + |</dict> |
| 33 | + |</plist>""".stripMargin |
| 34 | + // com.dd.plist.NSObject#toXMLPropertyList uses line separator when converting |
| 35 | + .replace("\n", System.lineSeparator()) |
| 36 | + // com.dd.plist.NSObject#toXMLPropertyList uses tabs when converting |
| 37 | + .replace(" ", "\t") |
| 38 | + |
| 39 | + describe("ConfigFileCreationPass") { |
| 40 | + it("creates config node for an XML plist") { |
| 41 | + FileUtil.usingTemporaryFile("test", ".plist") { xmlFile => |
| 42 | + Files.write(xmlFile, Xml.getBytes("UTF-8")) |
| 43 | + |
| 44 | + val pass = new ConfigFileCreationPass(null /* cpg not used in here */, Config()) |
| 45 | + val diff = new TestDiffGraphBuilder |
| 46 | + |
| 47 | + pass.runOnPart(diff, xmlFile) |
| 48 | + diff.nodes.nonEmpty shouldBe true |
| 49 | + val node = diff.nodes.collectFirst { case n: NewConfigFile => n }.get |
| 50 | + node.name should (startWith("test") and endWith(".plist")) |
| 51 | + node.content shouldBe Xml |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + it("creates config node for a binary plist") { |
| 56 | + FileUtil.usingTemporaryFile("testbin-src", ".plist") { xmlFile => |
| 57 | + FileUtil.usingTemporaryFile("testbin", ".plist") { binFile => |
| 58 | + Files.write(xmlFile, Xml.getBytes("UTF-8")) |
| 59 | + PropertyListConverter.convertToBinary(xmlFile, binFile) |
| 60 | + |
| 61 | + val pass = new ConfigFileCreationPass(null /* cpg not used in here */, Config()) |
| 62 | + val diff = new TestDiffGraphBuilder |
| 63 | + |
| 64 | + pass.runOnPart(diff, binFile) |
| 65 | + |
| 66 | + diff.nodes.nonEmpty shouldBe true |
| 67 | + val node = diff.nodes.collectFirst { case n: NewConfigFile => n }.get |
| 68 | + node.name should (startWith("testbin") and endWith(".plist")) |
| 69 | + // binary should have been converted to XML content: |
| 70 | + node.content should startWith(Xml) |
| 71 | + // and the content should have a comment referencing the original file at the very end |
| 72 | + node.content.linesIterator.toList.last should startWith("<!--This file was generated from") |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments