Skip to content

Commit 4ad9453

Browse files
Lenny HalsethLenny Halseth
authored andcommitted
Update import/export capabilities and related unit tests
1 parent e799076 commit 4ad9453

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

codepulse/src/main/scala/com/secdec/codepulse/tracer/export/ProjectExporter.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ object ProjectExporter extends JsonHelpers {
124124
for (sourceFileId <- node.sourceFileId) jg.writeNumberField("sourceFileId", sourceFileId)
125125
for (sourceLocationCount <- node.sourceLocationCount) jg.writeNumberField("sourceLocationCount", sourceLocationCount)
126126
for (methodStartLine <- node.methodStartLine) jg.writeNumberField("methodStartLine", methodStartLine)
127+
for (methodEndLine <- node.methodEndLine) jg.writeNumberField("methodEndLine", methodEndLine)
127128
for (isSurfaceMethod <- node.isSurfaceMethod) {
128129
if (node.isSurfaceMethod.isDefined) jg.writeBooleanField("isSurfaceMethod", isSurfaceMethod)
129130
}
Binary file not shown.

codepulse/src/test/scala/com/secdec/codepulse/tracer/export/test/ExportSuite.scala

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class ExportSuite extends FunSpec with BeforeAndAfter {
117117
val outputStream = new ByteArrayOutputStream()
118118
ProjectExporter.exportTo(outputStream, data)
119119
val contents = unzipExport(outputStream)
120-
assert(contents.get("nodes.json").get == "[ {\r\n \"id\" : 1,\r\n \"label\" : \"method1\",\r\n \"kind\" : \"method\",\r\n \"size\" : 50,\r\n \"sourceFileId\" : 1,\r\n \"sourceLocationCount\" : 2,\r\n \"methodStartLine\" : 3\r\n} ]")
120+
assert(contents.get("nodes.json").get == "[ {\r\n \"id\" : 1,\r\n \"label\" : \"method1\",\r\n \"kind\" : \"method\",\r\n \"size\" : 50,\r\n \"sourceFileId\" : 1,\r\n \"sourceLocationCount\" : 2,\r\n \"methodStartLine\" : 3,\r\n \"methodEndLine\" : 4\r\n} ]")
121121
}
122122

123123
it("should reference source file when source file is unavailable") {
@@ -149,6 +149,26 @@ class ExportSuite extends FunSpec with BeforeAndAfter {
149149
val contents = unzipExport(outputStream)
150150
assert(contents.get("nodes.json").get == "[ {\r\n \"id\" : 5,\r\n \"label\" : \"method1\",\r\n \"kind\" : \"method\",\r\n \"size\" : 50,\r\n \"sourceFileId\" : 10,\r\n \"sourceLocationCount\" : 42,\r\n \"isSurfaceMethod\" : true\r\n} ]")
151151
}
152+
153+
it("should reference method-start-line") {
154+
data.sourceData.importSourceFiles(Map[Int,String]((10, "C:\\code\\program.java")))
155+
data.treeNodeData.storeNode(TreeNodeData(5, None, "method1", CodeTreeNodeKind.Mth, Option(50), Option(10), Option(42), Option(1), None, Some(true)))
156+
157+
val outputStream = new ByteArrayOutputStream()
158+
ProjectExporter.exportTo(outputStream, data)
159+
val contents = unzipExport(outputStream)
160+
assert(contents.get("nodes.json").get == "[ {\r\n \"id\" : 5,\r\n \"label\" : \"method1\",\r\n \"kind\" : \"method\",\r\n \"size\" : 50,\r\n \"sourceFileId\" : 10,\r\n \"sourceLocationCount\" : 42,\r\n \"methodStartLine\" : 1,\r\n \"isSurfaceMethod\" : true\r\n} ]")
161+
}
162+
163+
it("should reference method-end-line") {
164+
data.sourceData.importSourceFiles(Map[Int,String]((10, "C:\\code\\program.java")))
165+
data.treeNodeData.storeNode(TreeNodeData(5, None, "method1", CodeTreeNodeKind.Mth, Option(50), Option(10), Option(42), None, Option(7), Some(true)))
166+
167+
val outputStream = new ByteArrayOutputStream()
168+
ProjectExporter.exportTo(outputStream, data)
169+
val contents = unzipExport(outputStream)
170+
assert(contents.get("nodes.json").get == "[ {\r\n \"id\" : 5,\r\n \"label\" : \"method1\",\r\n \"kind\" : \"method\",\r\n \"size\" : 50,\r\n \"sourceFileId\" : 10,\r\n \"sourceLocationCount\" : 42,\r\n \"methodEndLine\" : 7,\r\n \"isSurfaceMethod\" : true\r\n} ]")
171+
}
152172
}
153173

154174
describe("Encounters of exported project unassociated with a recorcing") {

codepulse/src/test/scala/com/secdec/codepulse/tracer/export/test/ImportSuite.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ImportSuite extends FunSpec with BeforeAndAfter with MockFactory {
4242
var projectDb: JdbcBackend.DatabaseDef = _
4343

4444
describe("Version v2.1 of a project export file") {
45-
it("should import surface method") {
45+
it("should import surface method, start lines, and end lines") {
4646
val file = new ZipFile(getClass.getResource("WebApi.v2.1.pulse").getPath.replaceAll("%20", " "))
4747

4848
val mockInputStore = mock[InputStore]
@@ -56,17 +56,17 @@ class ImportSuite extends FunSpec with BeforeAndAfter with MockFactory {
5656
(0, None, "Classes", "g", None, None, None, None, None, None),
5757
(1, Option(0), "WebApi", "p", None, None, None, None, None, None),
5858
(2, Option(1), "RouteConfig", "c", None, Option(1), Option(3), None, None, None),
59-
(3, Option(2), "public static Void RegisterRoutes(RouteCollection)", "m", Option(13), Option(1), Option(3), Option(10), None, None),
59+
(3, Option(2), "public static Void RegisterRoutes(RouteCollection)", "m", Option(13), Option(1), Option(3), Option(10), Option(17), None),
6060
(4, Option(1), "WebApiConfig", "c", None, Option(2), Option(3), None, None, None),
61-
(5, Option(4), "public static Void Register(HttpConfiguration)", "m", Option(11), Option(2), Option(3), Option(9), None, None),
61+
(5, Option(4), "public static Void Register(HttpConfiguration)", "m", Option(11), Option(2), Option(3), Option(9), Option(16), None),
6262
(6, Option(1), "WebApiApplication", "c", None, Option(3), Option(3), None, None, None),
63-
(7, Option(6), "protected Void Application_Start()", "m", Option(7), Option(3), Option(3), Option(10), None, None),
63+
(7, Option(6), "protected Void Application_Start()", "m", Option(7), Option(3), Option(3), Option(10), Option(12), None),
6464
(8, Option(1), "WebApi.Controllers", "p", None, None, None, None, None, None),
6565
(9, Option(8), "HomeController", "c", None, Option(4), Option(1), None, None, None),
66-
(10, Option(9), "public ActionResult Index()", "m", Option(3), Option(4), Option(1), Option(9), None, Option(true)),
66+
(10, Option(9), "public ActionResult Index()", "m", Option(3), Option(4), Option(1), Option(9), Option(9), Option(true)),
6767
(11, Option(8), "ValuesController", "c", None, Option(5), Option(1), None, None, None),
68-
(12, Option(11), "public HttpResponseMessage Get()", "m", Option(6), Option(5), Option(1), Option(11), None, Option(true)),
69-
(13, Option(11), "private static String GetResponseContent()", "m", Option(2), Option(5), Option(1), Option(16), None, None))
68+
(12, Option(11), "public HttpResponseMessage Get()", "m", Option(6), Option(5), Option(1), Option(11), Option(11), Option(true)),
69+
(13, Option(11), "private static String GetResponseContent()", "m", Option(2), Option(5), Option(1), Option(16), Option(16), None))
7070

7171
assertTreeNodeData(treeNodeDataSet)
7272
}

0 commit comments

Comments
 (0)