Skip to content

Commit 971e688

Browse files
committed
Add tests
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 4ab850d commit 971e688

File tree

8 files changed

+89
-14
lines changed

8 files changed

+89
-14
lines changed

scanpipe/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def filter(self, qs, value):
411411
("sha1", "sha1"),
412412
("dwarf_included_paths", "dwarf_included_paths"),
413413
("dwarf_compiled_paths", "dwarf_compiled_paths"),
414-
("file_paths", "file_paths"),
414+
("go_file_paths", "go_file_paths"),
415415
)
416416

417417

scanpipe/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ def win_exes(self):
20172017
"""
20182018
return self.files().filter(
20192019
Q(file_type__icontains="for ms windows")
2020-
| Q(filetype_file__istartswith="pe32")
2020+
| Q(file_type__istartswith="pe32")
20212021
)
20222022

20232023
def mach_os(self):
@@ -2027,8 +2027,8 @@ def mach_os(self):
20272027
Keep sync with the content type implementation at ``typecode.contenttype``.
20282028
"""
20292029
return self.files().filter(
2030-
models.Q(filetype__icontains="mach-o")
2031-
| models.Q(mimetype__icontains="application/x-mach-binary")
2030+
models.Q(file_type__icontains="mach-o")
2031+
| models.Q(mime_type__icontains="application/x-mach-binary")
20322032
)
20332033

20342034
def executable_binaries(self):

scanpipe/pipes/d2d.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,9 @@ def _map_paths_resource(
17411741

17421742
def map_paths(project, file_type, collect_paths_func, map_types, logger=None):
17431743
"""Map paths using similarities of path suffixes."""
1744-
project_files = getattr(project.codebaseresources, file_type)()
1745-
from_resources = project_files.from_codebase()
1746-
to_resources = project_files.to_codebase().has_no_relation()
1744+
from_resources = project.codebaseresources.files().from_codebase()
1745+
to_resources = project.codebaseresources.files().to_codebase().has_no_relation()
1746+
to_resources = getattr(to_resources, file_type)()
17471747
for resource in to_resources:
17481748
paths = collect_paths_func(resource.location_path)
17491749
resource.update_extra_data(paths)
@@ -1776,20 +1776,34 @@ def map_paths(project, file_type, collect_paths_func, map_types, logger=None):
17761776

17771777
def map_elfs(project, logger=None):
17781778
map_paths(
1779-
project,
1780-
"elfs",
1781-
get_dwarf_paths,
1782-
["dwarf_compiled_paths", "dwarf_included_paths"],
1783-
logger,
1779+
project=project,
1780+
file_type="elfs",
1781+
collect_paths_func=get_elf_file_dwarf_paths,
1782+
map_types=["dwarf_compiled_paths", "dwarf_included_paths"],
1783+
logger=logger,
17841784
)
17851785

17861786

1787+
def get_elf_file_dwarf_paths(location):
1788+
paths = get_dwarf_paths(location)
1789+
return {
1790+
"dwarf_compiled_paths": paths.get("compiled_paths") or [],
1791+
"dwarf_included_paths": paths.get("included_paths") or [],
1792+
}
1793+
1794+
17871795
def get_go_file_paths(location):
17881796
go_symbols = (
17891797
collect_and_parse_symbols(location, check_type=False).get("go_symbols") or {}
17901798
)
1791-
return {"file_paths": go_symbols.get("file_paths") or []}
1799+
return {"go_file_paths": go_symbols.get("file_paths") or []}
17921800

17931801

17941802
def map_go_paths(project, logger=None):
1795-
map_paths(project, "executable_binaries", get_go_file_paths, ["file_paths"], logger)
1803+
map_paths(
1804+
project=project,
1805+
file_type="executable_binaries",
1806+
collect_paths_func=get_go_file_paths,
1807+
map_types=["go_file_paths"],
1808+
logger=logger,
1809+
)
376 Bytes
Binary file not shown.
61.9 KB
Binary file not shown.
756 Bytes
Binary file not shown.
1.08 MB
Binary file not shown.

scanpipe/tests/pipes/test_d2d.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from django.test import TestCase
3030

3131
from scanpipe import pipes
32+
from scanpipe.models import CodebaseRelation
3233
from scanpipe.models import CodebaseResource
3334
from scanpipe.models import Project
3435
from scanpipe.pipes import d2d
@@ -1461,3 +1462,63 @@ def test_scanpipe_pipes_d2d_match_purldb_resources_post_process(self):
14611462

14621463
self.assertEqual(2, package1_resource_count)
14631464
self.assertEqual(0, package2_resource_count)
1465+
1466+
def test_scanpipe_pipes_d2d_map_elfs(self):
1467+
input_dir = self.project1.input_path
1468+
input_resources = [
1469+
self.data_location / "d2d-elfs/to-data.zip",
1470+
self.data_location / "d2d-elfs/from-data.zip",
1471+
]
1472+
copy_inputs(input_resources, input_dir)
1473+
self.from_files, self.to_files = d2d.get_inputs(self.project1)
1474+
inputs_with_codebase_path_destination = [
1475+
(self.from_files, self.project1.codebase_path / d2d.FROM),
1476+
(self.to_files, self.project1.codebase_path / d2d.TO),
1477+
]
1478+
for input_files, codebase_path in inputs_with_codebase_path_destination:
1479+
for input_file_path in input_files:
1480+
scancode.extract_archive(input_file_path, codebase_path)
1481+
1482+
scancode.extract_archives(
1483+
self.project1.codebase_path,
1484+
recurse=True,
1485+
)
1486+
pipes.collect_and_create_codebase_resources(self.project1)
1487+
print(self.project1.codebaseresources.files().from_codebase())
1488+
d2d.map_elfs(project=self.project1, logger=None)
1489+
assert (
1490+
CodebaseRelation.objects.filter(
1491+
project=self.project1, map_type="dwarf_included_paths"
1492+
).count()
1493+
== 1
1494+
)
1495+
1496+
def test_scanpipe_pipes_d2d_map_go_paths(self):
1497+
input_dir = self.project1.input_path
1498+
input_resources = [
1499+
self.data_location / "d2d-go/to-data.zip",
1500+
self.data_location / "d2d-go/from-data.zip",
1501+
]
1502+
copy_inputs(input_resources, input_dir)
1503+
self.from_files, self.to_files = d2d.get_inputs(self.project1)
1504+
inputs_with_codebase_path_destination = [
1505+
(self.from_files, self.project1.codebase_path / d2d.FROM),
1506+
(self.to_files, self.project1.codebase_path / d2d.TO),
1507+
]
1508+
for input_files, codebase_path in inputs_with_codebase_path_destination:
1509+
for input_file_path in input_files:
1510+
scancode.extract_archive(input_file_path, codebase_path)
1511+
1512+
scancode.extract_archives(
1513+
self.project1.codebase_path,
1514+
recurse=True,
1515+
)
1516+
pipes.collect_and_create_codebase_resources(self.project1)
1517+
1518+
d2d.map_go_paths(project=self.project1, logger=None)
1519+
assert (
1520+
CodebaseRelation.objects.filter(
1521+
project=self.project1, map_type="go_file_paths"
1522+
).count()
1523+
== 1
1524+
)

0 commit comments

Comments
 (0)