From afd2cc250bec4671f637175d5404ca811adf74c4 Mon Sep 17 00:00:00 2001 From: Sara Batllori Date: Mon, 5 Aug 2024 15:09:35 -0700 Subject: [PATCH] WIP: Add unit tests for igd, interrupts, io, iommu utilcmds Signed-off-by: Sara Batllori --- tests/utilcmd/igd_cmd/igd_cmd_dmaread_1.json | 50 ++++++++++++++ tests/utilcmd/igd_cmd/igd_cmd_dmawrite_1.json | 52 ++++++++++++++ tests/utilcmd/igd_cmd/test_igd_cmd.py | 46 +++++++++++++ .../interrupts_cmd_smi_count_1.json | 69 +++++++++++++++++++ .../interrupts_cmd_smi_send_1.json | 10 +++ .../interrupts_cmd/test_interrupts_cmd.py | 46 +++++++++++++ tests/utilcmd/io_cmd/io_cmd_list_1.json | 17 +++++ tests/utilcmd/io_cmd/io_cmd_read_1.json | 7 ++ tests/utilcmd/io_cmd/io_cmd_write_1.json | 7 ++ tests/utilcmd/io_cmd/test_io_cmd.py | 52 ++++++++++++++ tests/utilcmd/iommu_cmd/iommu_cmd_list_1.json | 1 + tests/utilcmd/iommu_cmd/test_iommu_cmd.py | 40 +++++++++++ tests/utilcmd/test_template_cmd.py | 40 +++++++++++ 13 files changed, 437 insertions(+) create mode 100644 tests/utilcmd/igd_cmd/igd_cmd_dmaread_1.json create mode 100644 tests/utilcmd/igd_cmd/igd_cmd_dmawrite_1.json create mode 100644 tests/utilcmd/igd_cmd/test_igd_cmd.py create mode 100644 tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_count_1.json create mode 100644 tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_send_1.json create mode 100644 tests/utilcmd/interrupts_cmd/test_interrupts_cmd.py create mode 100644 tests/utilcmd/io_cmd/io_cmd_list_1.json create mode 100644 tests/utilcmd/io_cmd/io_cmd_read_1.json create mode 100644 tests/utilcmd/io_cmd/io_cmd_write_1.json create mode 100644 tests/utilcmd/io_cmd/test_io_cmd.py create mode 100644 tests/utilcmd/iommu_cmd/iommu_cmd_list_1.json create mode 100644 tests/utilcmd/iommu_cmd/test_iommu_cmd.py create mode 100644 tests/utilcmd/test_template_cmd.py diff --git a/tests/utilcmd/igd_cmd/igd_cmd_dmaread_1.json b/tests/utilcmd/igd_cmd/igd_cmd_dmaread_1.json new file mode 100644 index 0000000000..ebe48e2b1a --- /dev/null +++ b/tests/utilcmd/igd_cmd/igd_cmd_dmaread_1.json @@ -0,0 +1,50 @@ +{ + "read_pci_reg": { + "(0,2,0,2,2)": [ + "18048" + ], + "(0,2,0,24,4)": [ + "12" + ], + "(0,2,0,28,4)": [ + "64" + ], + "(0,2,0,20,4)": [ + "96", + "96", + "96", + "96" + ], + "(0,2,0,16,4)": [ + "4", + "4", + "4", + "4" + ] + }, + "read_mmio_reg": { + "(412325249024,4)": [ + "1283457025" + ], + "(412325249028,4)": [ + "0" + ] + }, + "write_mmio_reg": { + "(412325249024,4,536870913)": [ + "" + ], + "(412325249028,4,0)": [ + "", + "" + ], + "(412325249024,4,1283457025)": [ + "" + ] + }, + "read_phys_mem": { + "(274877906944,4)": [ + "\u001c\ryF" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/igd_cmd/igd_cmd_dmawrite_1.json b/tests/utilcmd/igd_cmd/igd_cmd_dmawrite_1.json new file mode 100644 index 0000000000..4f0a329eba --- /dev/null +++ b/tests/utilcmd/igd_cmd/igd_cmd_dmawrite_1.json @@ -0,0 +1,52 @@ +{ + "read_pci_reg": { + "(0,2,0,2,2)": [ + "18048" + ], + "(0,2,0,24,4)": [ + "12" + ], + "(0,2,0,28,4)": [ + "64" + ], + "(0,2,0,20,4)": [ + "96", + "96", + "96", + "96" + ], + "(0,2,0,16,4)": [ + "4", + "4", + "4", + "4" + ] + }, + "read_mmio_reg": { + "(412325249024,4)": [ + "1283457025" + ], + "(412325249028,4)": [ + "0" + ] + }, + "write_mmio_reg": { + "(412325249024,4,561975297)": [ + "" + ], + "(412325249028,4,2)": [ + "" + ], + "(412325249024,4,1283457025)": [ + "" + ], + "(412325249028,4,0)": [ + "" + ] + }, + "write_phys_mem": { + "(274877906944,4,bytearray(b'\\xde\\xad\\xbe\\xef'))": [ + "" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/igd_cmd/test_igd_cmd.py b/tests/utilcmd/igd_cmd/test_igd_cmd.py new file mode 100644 index 0000000000..f423934205 --- /dev/null +++ b/tests/utilcmd/igd_cmd/test_igd_cmd.py @@ -0,0 +1,46 @@ +# CHIPSEC: Platform Security Assessment Framework +# Copyright (c) 2024, Intel Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; Version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Contact information: +# chipsec@intel.com +# + +"""" +To execute: python[3] -m unittest tests.utilcmd.igd_cmd.test_igd_cmd +""" + +import unittest +import os + +from chipsec.library.file import get_main_dir +from tests.utilcmd.run_chipsec_util import setup_run_destroy_util +from chipsec.testcase import ExitCode + +class TestIgdUtilcmd(unittest.TestCase): + def test_dmaread(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + igd_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "igd_cmd", "igd_cmd_dmaread_1.json") + retval = setup_run_destroy_util(init_replay_file, "igd", "dmaread 0x20000000 4", util_replay_file=igd_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + + def test_dmawrite(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + igd_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "igd_cmd", "igd_cmd_dmawrite_1.json") + retval = setup_run_destroy_util(init_replay_file, "igd", "dmawrite 0x2217F1000 0x4 deadbeef", util_replay_file=igd_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_count_1.json b/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_count_1.json new file mode 100644 index 0000000000..77fa1d5ab2 --- /dev/null +++ b/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_count_1.json @@ -0,0 +1,69 @@ +{ + "get_threads_count": { + "()": [ + "20" + ] + }, + "read_msr": { + "(0,52)": [ + "(461, 0)" + ], + "(1,52)": [ + "(461, 0)" + ], + "(2,52)": [ + "(461, 0)" + ], + "(3,52)": [ + "(461, 0)" + ], + "(4,52)": [ + "(461, 0)" + ], + "(5,52)": [ + "(461, 0)" + ], + "(6,52)": [ + "(461, 0)" + ], + "(7,52)": [ + "(461, 0)" + ], + "(8,52)": [ + "(461, 0)" + ], + "(9,52)": [ + "(461, 0)" + ], + "(10,52)": [ + "(461, 0)" + ], + "(11,52)": [ + "(461, 0)" + ], + "(12,52)": [ + "(461, 0)" + ], + "(13,52)": [ + "(461, 0)" + ], + "(14,52)": [ + "(461, 0)" + ], + "(15,52)": [ + "(461, 0)" + ], + "(16,52)": [ + "(461, 0)" + ], + "(17,52)": [ + "(461, 0)" + ], + "(18,52)": [ + "(461, 0)" + ], + "(19,52)": [ + "(461, 0)" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_send_1.json b/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_send_1.json new file mode 100644 index 0000000000..48ec6b9636 --- /dev/null +++ b/tests/utilcmd/interrupts_cmd/interrupts_cmd_smi_send_1.json @@ -0,0 +1,10 @@ +{ + "write_io_port": { + "(179,0,1)": [ + "True" + ], + "(178,222,1)": [ + "True" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/interrupts_cmd/test_interrupts_cmd.py b/tests/utilcmd/interrupts_cmd/test_interrupts_cmd.py new file mode 100644 index 0000000000..e5ed883067 --- /dev/null +++ b/tests/utilcmd/interrupts_cmd/test_interrupts_cmd.py @@ -0,0 +1,46 @@ +# CHIPSEC: Platform Security Assessment Framework +# Copyright (c) 2024, Intel Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; Version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Contact information: +# chipsec@intel.com +# + +"""" +To execute: python[3] -m unittest tests.utilcmd.interrupts_cmd.test_interrupts_cmd +""" + +import unittest +import os + +from chipsec.library.file import get_main_dir +from tests.utilcmd.run_chipsec_util import setup_run_destroy_util +from chipsec.testcase import ExitCode + +class TestInterruptsUtilcmd(unittest.TestCase): + def test_count(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + interrupts_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "interrupts_cmd", "interrupts_cmd_smi_count_1.json") + retval = setup_run_destroy_util(init_replay_file, "interrupts", "count", util_replay_file=interrupts_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + + def test_send(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + interrupts_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "interrupts_cmd", "interrupts_cmd_smi_send_1.json") + retval = setup_run_destroy_util(init_replay_file, "interrupts", "send 0x0 0xDE 0x0", util_replay_file=interrupts_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/utilcmd/io_cmd/io_cmd_list_1.json b/tests/utilcmd/io_cmd/io_cmd_list_1.json new file mode 100644 index 0000000000..06cecb9d63 --- /dev/null +++ b/tests/utilcmd/io_cmd/io_cmd_list_1.json @@ -0,0 +1,17 @@ +{ + "read_pci_reg": { + "(0,31,2,32,4)": [ + "4294967295", + "4294967295" + ], + "(0,31,4,80,4)": [ + "1025" + ], + "(0,31,0,72,4)": [ + "0" + ], + "(0,31,4,32,4)": [ + "61345" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/io_cmd/io_cmd_read_1.json b/tests/utilcmd/io_cmd/io_cmd_read_1.json new file mode 100644 index 0000000000..7596715097 --- /dev/null +++ b/tests/utilcmd/io_cmd/io_cmd_read_1.json @@ -0,0 +1,7 @@ +{ + "read_io_port": { + "(97,1)": [ + "48" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/io_cmd/io_cmd_write_1.json b/tests/utilcmd/io_cmd/io_cmd_write_1.json new file mode 100644 index 0000000000..762ac46413 --- /dev/null +++ b/tests/utilcmd/io_cmd/io_cmd_write_1.json @@ -0,0 +1,7 @@ +{ + "write_io_port": { + "(1072,0,1)": [ + "True" + ] + } +} \ No newline at end of file diff --git a/tests/utilcmd/io_cmd/test_io_cmd.py b/tests/utilcmd/io_cmd/test_io_cmd.py new file mode 100644 index 0000000000..e79ad56ea9 --- /dev/null +++ b/tests/utilcmd/io_cmd/test_io_cmd.py @@ -0,0 +1,52 @@ +# CHIPSEC: Platform Security Assessment Framework +# Copyright (c) 2024, Intel Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; Version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Contact information: +# chipsec@intel.com +# + +"""" +To execute: python[3] -m unittest tests.utilcmd.io_cmd.test_io_cmd +""" + +import unittest +import os + +from chipsec.library.file import get_main_dir +from tests.utilcmd.run_chipsec_util import setup_run_destroy_util +from chipsec.testcase import ExitCode + +class TestIoUtilcmd(unittest.TestCase): + def test_list(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + io_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "io_cmd", "io_cmd_list_1.json") + retval = setup_run_destroy_util(init_replay_file, "io", "list", util_replay_file=io_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + + def test_read(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + io_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "io_cmd", "io_cmd_read_1.json") + retval = setup_run_destroy_util(init_replay_file, "io", "read", util_replay_file=io_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + + def test_write(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + io_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "io_cmd", "io_cmd_write_1.json") + retval = setup_run_destroy_util(init_replay_file, "io", "write", util_replay_file=io_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/utilcmd/iommu_cmd/iommu_cmd_list_1.json b/tests/utilcmd/iommu_cmd/iommu_cmd_list_1.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/tests/utilcmd/iommu_cmd/iommu_cmd_list_1.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/utilcmd/iommu_cmd/test_iommu_cmd.py b/tests/utilcmd/iommu_cmd/test_iommu_cmd.py new file mode 100644 index 0000000000..6efbdd353c --- /dev/null +++ b/tests/utilcmd/iommu_cmd/test_iommu_cmd.py @@ -0,0 +1,40 @@ +# CHIPSEC: Platform Security Assessment Framework +# Copyright (c) 2024, Intel Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; Version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Contact information: +# chipsec@intel.com +# + +"""" +To execute: python[3] -m unittest tests.utilcmd.iommu_cmd.test_iommu_cmd +""" + +import unittest +import os + +from chipsec.library.file import get_main_dir +from tests.utilcmd.run_chipsec_util import setup_run_destroy_util +from chipsec.testcase import ExitCode + +class TestIommuUtilcmd(unittest.TestCase): + def test_list(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + iommu_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "iommu_cmd", "iommu_cmd_list_1.json") + retval = setup_run_destroy_util(init_replay_file, "iommu", "list", util_replay_file=iommu_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/utilcmd/test_template_cmd.py b/tests/utilcmd/test_template_cmd.py new file mode 100644 index 0000000000..28dbf18ff8 --- /dev/null +++ b/tests/utilcmd/test_template_cmd.py @@ -0,0 +1,40 @@ +# CHIPSEC: Platform Security Assessment Framework +# Copyright (c) 2024, Intel Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; Version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Contact information: +# chipsec@intel.com +# + +"""" +To execute: python[3] -m unittest tests.utilcmd.template_cmd.test_template_cmd +""" + +import unittest +import os + +from chipsec.library.file import get_main_dir +from tests.utilcmd.run_chipsec_util import setup_run_destroy_util +from chipsec.testcase import ExitCode + +class TestTemplateUtilcmd(unittest.TestCase): + def test_command(self) -> None: + init_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "adlenumerate.json") + template_dump_replay_file = os.path.join(get_main_dir(), "tests", "utilcmd", "template_cmd", "template_cmd_command_1.json") + retval = setup_run_destroy_util(init_replay_file, "template", "command", util_replay_file=template_dump_replay_file) + self.assertEqual(retval, ExitCode.OK) + +if __name__ == '__main__': + unittest.main()