<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    Wireshark Tests

    建議的準備和運行測試的步驟:

    • 安裝兩個Python包pytest: pip install pytest pytest-xdist
    • 編譯程序(“ wireshark”,“ tshark”等): ninja
    • 為“ unittests”套件構建其他程序: ninja test-programs
    • 在構建目錄中運行測試: pytest

    更換ninja test-programsmake test-programs需要。

    測試套件將嘗試進行盡可能多的測試,并在不滿足其依賴關系時跳過測試。例如,數據包捕獲測試需要環回接口和捕獲特權。為避免捕獲測試,請通過該--disable-capture選項。

    列出可用的測試pytest --collectonly。使用啟用詳細輸出pytest --verbose

    如果出于任何原因pytest太舊或無法使用,則可以使用更有限的測試運行程序test/test.py。使用test/test.py --help看到所有選項。

    測試套件結構

    以下各節描述了測試套件的組織方式。

    測試范圍和可用性

    測試框架可以運行程序并檢查其stdout,stderr和退出代碼。它不能與Wireshark UI交互。測試涵蓋捕獲,命令行選項,解密,文件格式支持和轉換,Lua腳本和其他功能。

    可用的測試取決于構建Wireshark的庫。例如,某些解密測試取決于Libgcrypt的最低版本,而Lua測試則取決于Lua。

    捕獲測試取決于運行測試腳本的用戶的權限。我們假設測試用戶在Windows和macOS上具有捕獲權限,并且默認情況下在這些平臺上啟用了捕獲測試。

    如果某個功能不可用,則將跳過測試。例如,如果使用的是舊版本的Libgcrypt,則將跳過某些解密測試,而其他測試仍可以運行到完成。

    Suites, Cases, and Tests

    test/test.py腳本使用Python的“ unittest”模塊。我們的測試是在此之后進行模式化的,并且根據套件,案例和個別測試來組織個別測試。套件對應于匹配模式“ suite _ *。py”的python模塊。案例對應于每個模塊中的一個或多個類,匹配模式“ test_ *”的案例類方法對應于各個測試。例如,命令行選項套件中TShark捕獲命令行選項測試用例中的無效捕獲過濾器測試的ID為“ suite_clopts.case_tshark_capture_clopts.test_tshark_invalid_capfilter”。

    pytest裝置

    測試通常具有其他依賴性,例如可執行文件的路徑,捕獲文件的路徑,配置目錄,可選庫的可用性等等。Python unittest庫在表達測試依賴項方面非常有限,這些依賴項通常在類實例本身(self)或通過全局變量指定。

    pytest是一個更好的測試框架,具有完整的并行化支持(測試級而不是套件級),提供了更好的測試報告,并允許使用 模塊化夾具。理想情況下,測試套件應完全切換到pytest,但與此同時,通過“夾具”模塊提供一個兼容層。

    固定裝置是一種裝飾有功能的函數@fixtures.fixture,可以調用fixtures.skip("reason")以跳過取決于固定裝置的測試,也可以返回/產生一個值。測試功能(和其他夾具功能)可以通過使用夾具功能的名稱作為功能參數來接收夾具值。常見的固定裝置在可執行文件的路徑中可用,fixtures_ws.py包括 可執行文件cmd_tshark的路徑tsharkcapture_file 工廠功能,這些功能會生成捕獲文件的路徑。

    每個單元測試測試用例都必須修飾 @fixtures.uses_fixtures以確保單元測試測試類可以實際請求夾具依賴性。

    列出并運行測試

    可以通過test/test.pyPython腳本運行測試。運行所有測試,無論是運行test/test.py在包含Wireshark的可執行文件(該目錄wiresharktshark等等),或通過經由所述可執行路徑-p標志:

    $ python3 test / test.py -p / path / to / wireshark-build / run

    您可以通過將一個或多個完整或部分名稱傳遞給來列出測試 tshark.py。該-l標志列出了測試。默認情況下,顯示所有測試。

    # List all tests
    $ python3 test/test.py -l
    $ python3 test/test.py -l all
    $ python3 test/test.py --list
    $ python3 test/test.py --list all
    
    # List only tests containing "dumpcap"
    $ python3 test/test.py -l dumpcap
    
    # List all suites
    $ python3 test/test.py --list-suites
    
    # List all suites and cases
    $ python3 test/test.py --list-cases

    如果列表標志之一不存在,則運行測試。如果未提供名稱或未all提供,則將運行所有測試。否則,將運行匹配的測試。

    # Run all tests
    $ python3 test/test.py
    $ python3 test/test.py all
    
    # Only run tests containing "dumpcap"
    $ python3 test/test.py -l dumpcap
    
    # Run the "clopts" suite
    $ python3 test/test.py suite_clopts
    
    

    運行python3 test/test.py --help所有可用選項。

    列出并運行測試(pytest)

    也可以使用pytest運行測試。優點包括更好的測試選擇,完全的并行性,更好的測試執行摘要,出現故障時更好的輸出(包含變量的內容)以及在失敗的測試中打開PDB調試器的能力。

    首先,請安裝pytest 3.0或更高版本以及 pytest-xdist:

    # Install required packages on Ubuntu 18.04 or Debian jessie-backports
    $ sudo apt install python3-pytest python3-pytest-xdist
    
    # Install required packages on other systems
    $ pip install pytest pytest-xdist
    Run pytest in the Wireshark build directory, Wireshark binaries are assumed to be present in the run subdirectory (or run\RelWithDebInfo on Windows).
    
    # Run all tests
    $ cd /path/to/wireshark/build
    $ pytest
    
    # Run all except capture tests
    $ pytest --disable-capture
    
    # Run all tests with "decryption" in its name
    $ pytest -k decryption
    
    # Run all tests with an explicit path to the Wireshark executables
    $ pytest --program-path /path/to/wireshark/build/run
    To list tests without actually executing them, use the --collect-only option:
    
    # List all tests
    $ pytest --collect-only
    
    # List only tests containing both "dfilter" and "tvb"
    $ pytest --collect-only -k "dfilter and tvb"

    缺少程序時,測試套件將使測試失敗。當僅構建一部分程序或禁用某些程序時,可以指示測試套件跳過而不是失敗測試:

    # Run tests when libpcap support is disabled (-DENABLE_PCAP=OFF)
    $ pytest --skip-missing-programs dumpcap,rawshark
    
    # Run tests and ignore all tests with missing program dependencies
    $ pytest --skip-missing-programs all
    To open a Python debugger (PDB) on failing tests, use the --pdb option and disable parallelism with the -n0 option:
    
    # Run decryption tests sequentially and open a debugger on failing tests
    $ pytest -n0 --pdb -k decryption

    請注意,使用選項–pdb,由于SubprocessTestCase.tearDown未執行該方法,因此不會因測試失敗而終止雜散進程。將來可能會解決此限制。

    添加或修改測試

    測試必須在名稱與“ suite _ *。py”匹配的Python模塊中。該模塊必須包含“ SubprocessTestCase”或“ unittest.TestCase”的一個或多個子類。推薦使用“ SubprocessTestCase”,因為它包含幾種用于運行進程,檢查輸出和顯示錯誤信息的便捷方法。名稱以“ test_”開頭的每個測試用例方法都構成一個單獨的測試。

    可以使用“ unittest.assertXXX()”或“ subprocesstest.assertXXX()”方法來指示成功或失敗情況。

    通過方法參數注入測試依賴項(例如程序,目錄或環境變量)。常用的燈具包括cmd_tsharkcapture_file
    “ subprocesstest”類包含以下用于運行進程的方法。標準輸出和標準錯誤寫入“ <test.id> .log”:

    startProcess

    啟動過程而無需等待其完成。

    運行過程

    開始一個過程,等待它完成。

    assertRun

    啟動一個進程,等待它完成,然后檢查其退出代碼。

    當前的所有測試都運行Wireshark的一組可執行文件中的一個或多個,并檢查其返回代碼或輸出。一個簡單的示例是“ suite_clopts.case_basic_clopts.test_existing_file”,該文件使用TShark讀取捕獲文件并檢查其退出代碼。

    import subprocesstest
    import fixtures
    
    @fixtures.mark_usefixtures('test_env')
    @fixtures.uses_fixtures
    class case_basic_clopts(subprocesstest.SubprocessTestCase):
        def test_existing_file(self, cmd_tshark, capture_file):
            self.assertRun((cmd_tshark, '-r', capture_file('dhcp.pcap')))

    項目輸出可通過檢查SubprocessTestCase.grepOutputSubprocessTestCase.countOutput或其他unittest.assert*方法:

    import subprocesstest
    import fixtures
    
    @fixtures.mark_usefixtures('test_env')
    @fixtures.uses_fixtures
    class case_decrypt_80211(subprocesstest.SubprocessTestCase):
        def test_80211_wpa_psk(self, cmd_tshark, capture_file):
            tshark_proc = self.assertRun((cmd_tshark,
                    '-o', 'wlan.enable_decryption: TRUE',
                    '-Tfields',
                    '-e', 'http.request.uri',
                    '-r', capture_file('wpa-Induction.pcap.gz'),
                    '-Y', 'http',
                ))
            self.assertIn('favicon.ico', tshark_proc.stdout_str)

    測試可以并行運行。這意味著您創建的任何文件對于每個測試都必須是唯一的。“ subprocesstest.filename_from_id”可用于基于當前測試名稱生成文件名。它還確保測試運行后將自動刪除文件。

    本文章首發在 網安wangan.com 網站上。

    上一篇 下一篇
    討論數量: 0
    只看當前版本


    暫無話題~
    亚洲 欧美 自拍 唯美 另类