Skip to content

Commit

Permalink
Process review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
twiggler committed Nov 8, 2024
1 parent 304e959 commit 3b56d58
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 148 deletions.
21 changes: 20 additions & 1 deletion dissect/target/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import logging
import re
import urllib.parse
from datetime import datetime, timezone, tzinfo
from enum import Enum
from pathlib import Path
from typing import BinaryIO, Callable, Iterator, Optional, Union
from typing import BinaryIO, Callable, Iterator, Optional, TypeVar, Union

from dissect.util.ts import from_unix

Expand All @@ -24,6 +26,23 @@ def findall(buf: bytes, needle: bytes) -> Iterator[int]:
offset += 1


T = TypeVar("T")


def to_list(value: T | list[T]) -> list[T]:
"""Convert a single value or a list of values to a list.
Args:
value: The value to convert.
Returns:
A list of values.
"""
if not isinstance(value, list):
return [value]
return value


class StrEnum(str, Enum):
"""Sortable and serializible string-based enum"""

Expand Down
2 changes: 1 addition & 1 deletion dissect/target/plugins/os/unix/bsd/osx/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ def _interfaces(self) -> Iterator[MacInterfaceRecord]:
)

except Exception as e:
self.target.log.warning("Error reading configuration for network device %s: %s", name, e)
self.target.log.warning("Error reading configuration for network device %s", name, exc_info=e)

Check warning on line 92 in dissect/target/plugins/os/unix/bsd/osx/network.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/plugins/os/unix/bsd/osx/network.py#L92

Added line #L92 was not covered by tests
continue
270 changes: 148 additions & 122 deletions dissect/target/plugins/os/unix/linux/network.py

Large diffs are not rendered by default.

Git LFS file not shown
Git LFS file not shown
18 changes: 15 additions & 3 deletions tests/helpers/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@
from dissect.target.helpers import fsutil, utils


def test_slugify():
def test_to_list_single_value() -> None:
assert utils.to_list(1) == [1]
assert utils.to_list("a") == ["a"]
assert utils.to_list(None) == [None]


def test_to_list_list_value() -> None:
assert utils.to_list([1, 2, 3]) == [1, 2, 3]
assert utils.to_list(["a", "b", "c"]) == ["a", "b", "c"]
assert utils.to_list([]) == []


def test_slugify() -> None:
assert utils.slugify("foo/bar\\baz bla") == "foo_bar_baz_bla"


def test_filesystem_readinto():
def test_filesystem_readinto() -> None:
data = b"hello_world"
mocked_file = mock_open(read_data=b"hello_world")

Expand All @@ -22,7 +34,7 @@ def test_filesystem_readinto():
assert len(buffer) == 512


def test_helpers_fsutil_year_rollover_helper():
def test_helpers_fsutil_year_rollover_helper() -> None:
vfs = VirtualFilesystem()

content = """
Expand Down
42 changes: 21 additions & 21 deletions tests/plugins/os/unix/linux/test_network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import posixpath
from datetime import datetime
from ipaddress import ip_address, ip_network
Expand All @@ -9,7 +8,7 @@
from dissect.target.filesystem import VirtualFilesystem
from dissect.target.plugins.general.network import UnixInterfaceRecord
from dissect.target.plugins.os.unix.linux.network import (
LinuxConfigParser,
LinuxNetworkConfigParser,
LinuxNetworkPlugin,
NetworkManagerConfigParser,
SystemdNetworkConfigParser,
Expand All @@ -18,20 +17,7 @@

def test_networkmanager_parser(target_linux: Target, fs_linux: VirtualFilesystem) -> None:
fixture_dir = "tests/_data/plugins/os/unix/linux/NetworkManager/"
fs_linux.makedirs("/etc/NetworkManager/system-connections")

fs_linux.map_file(
"/etc/NetworkManager/system-connections/wired-static.nmconnection",
os.path.join(fixture_dir, "wired-static.nmconnection"),
)
fs_linux.map_file(
"/etc/NetworkManager/system-connections/vlan.nmconnection",
os.path.join(fixture_dir, "vlan.nmconnection"),
)
fs_linux.map_file(
"/etc/NetworkManager/system-connections/wireless.nmconnection",
os.path.join(fixture_dir, "wireless.nmconnection"),
)
fs_linux.map_dir("/etc/NetworkManager/system-connections", fixture_dir)

network_manager_config_parser = NetworkManagerConfigParser(target_linux)
interfaces = list(network_manager_config_parser.interfaces())
Expand Down Expand Up @@ -86,14 +72,20 @@ def test_systemd_network_parser(target_linux: Target, fs_linux: VirtualFilesyste
fs_linux.map_file(
"/usr/lib/systemd/network/40-wireless.network", posixpath.join(fixture_dir, "40-wireless.network")
)
fs_linux.map_file(
"/run/systemd/network/40-wireless-ipv4.network", posixpath.join(fixture_dir, "40-wireless-ipv4.network")
)
fs_linux.map_file(
"/usr/local/lib/systemd/network/40-wireless-ipv6.network",
posixpath.join(fixture_dir, "40-wireless-ipv6.network"),
)
fs_linux.map_file("/etc/systemd/network/20-vlan.netdev", posixpath.join(fixture_dir, "20-vlan.netdev"))

systemd_network_config_parser = SystemdNetworkConfigParser(target_linux)
interfaces = list(systemd_network_config_parser.interfaces())

assert len(interfaces) == 3

wired_static, wired_static_complex, wireless = interfaces
assert len(interfaces) == 5
wired_static, wired_static_complex, wireless, wireless_ipv4, wireless_ipv6 = interfaces

assert wired_static.name == "enp1s0"
assert wired_static.type is None
Expand Down Expand Up @@ -146,14 +138,22 @@ def test_systemd_network_parser(target_linux: Target, fs_linux: VirtualFilesyste
assert wireless.source == "/usr/lib/systemd/network/40-wireless.network"
assert wired_static_complex.configurator == "systemd-networkd"

assert wireless_ipv4.dhcp_ipv4
assert not wireless_ipv4.dhcp_ipv6
assert wireless_ipv4.source == "/run/systemd/network/40-wireless-ipv4.network"

assert not wireless_ipv6.dhcp_ipv4
assert wireless_ipv6.dhcp_ipv6
assert wireless_ipv6.source == "/usr/local/lib/systemd/network/40-wireless-ipv6.network"


def test_linux_network_plugin_interfaces(target_linux: Target, fs_linux: VirtualFilesystem) -> None:
"""Assert that the LinuxNetworkPlugin aggregates from all Config Parsers."""

MockLinuxConfigParser1: LinuxConfigParser = MagicMock()
MockLinuxConfigParser1: LinuxNetworkConfigParser = MagicMock()
MockLinuxConfigParser1.return_value.interfaces.return_value = []

MockLinuxConfigParser2: LinuxConfigParser = MagicMock()
MockLinuxConfigParser2: LinuxNetworkConfigParser = MagicMock()
MockLinuxConfigParser2.return_value.interfaces.return_value = [UnixInterfaceRecord()]

with patch(
Expand Down

0 comments on commit 3b56d58

Please sign in to comment.