diff --git a/AGENTS.md b/AGENTS.md index cd97b8b..cc57850 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -46,7 +46,7 @@ Lint, tests, packaging and metadata checks are listed in ## Architecture primer - **Entry** (`mcrit_plugin/ida/ida_mcrit.py`) registers IDA menus/actions/hotkeys and the MCRIT widget subviews. -- **`McritInterface`** owns the connection to the MCRIT server. Uploads, the Function and Block Scope queries, fetching a matching result and fetching labels go through `Backend.run_request`: on Binary Ninja they run as background tasks, named in the status bar; on IDA they run on a small worker pool and a queued Qt signal hands each result back to the main thread (not an IDA timer: requests start inside the cursor-settle timer's callback, and IDA 9.4 hangs in `register_timer` called there); headless runs them synchronously. The widgets drop a live query's answer once the cursor has moved to another function. The connection check, the family and sample list download and the lookup of the converted sample also run in the background. IDA's API is main-thread only: on IDA, `run_background` work runs on a worker too, the backend methods that read the database run on the main thread through `execute_sync`, and the export reads the database into a snapshot there (`SmdaInterfaceSnapshot`, through smda's IDAPython backend rather than its slower ida_domain one) before disassembling and hashing it on the worker. +- **`McritInterface`** owns the connection to the MCRIT server. Uploads, the Function and Block Scope queries, fetching a matching result and fetching labels go through `Backend.run_request`: on Binary Ninja they run as background tasks, named in the status bar; on IDA they run on a small worker pool and a queued Qt signal hands each result back to the main thread (not an IDA timer: requests start inside the cursor-settle timer's callback, and IDA 9.4 hangs in `register_timer` called there); headless runs them synchronously. The widgets drop a live query's answer once the cursor has moved to another function. The connection check, the family and sample list download and the lookup of the converted sample also run in the background. IDA's API is main-thread only: on IDA, `run_background` work runs on a worker too, the backend methods that read the database run on the main thread through `execute_sync`, and the export reads the database into a snapshot there (`SmdaInterfaceSnapshot`, through smda's IDAPython backend rather than its slower ida_domain one) before disassembling and hashing it on the worker. A request pins its client and server generation with `_pinConnection` and stores its answer inside `with self._storing(generation)`, so an answer from a server the session left is dropped; every new method that writes session state has to do the same. - **`McritClient`** (internalized under `mcrit_plugin/core/minimcrit/`) is the HTTP client speaking the MCRIT REST API. The plugin intentionally vendors a minified copy of the core client so it has no hard dependency on the `mcrit` package. - **IDB→SMDA conversion** uses SMDA (optionally as the analysis backend via `use_smda_for_analysis`); results feed matching and label sync. - **Widgets** render matches/blocks/functions/overview and are built on PySide6 through `QtShim`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 4022993..ffdd64d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,28 @@ Ninja or Ghidra applies to that plugin only. program. Reports are exported from Ghidra's own analysis through SMDA (4.9.0 or newer), matched local blocks are tinted in the listing with markers that are not saved with the program, remote CFGs open in Ghidra's graph view, and the settings are under Tool Options > MCRIT. It runs from - this repository's `ghidra_scripts` folder; see the README for setup. Not released yet; versioned - on its own from 0.1.0. + this repository's `ghidra_scripts` folder; see the README for setup. Its toolbar and tabs use + the same icons, at 24 px in the toolbar and in the theme's text colour from when the window + opens. Not released yet; versioned on its own from 0.1.0. +- IDA and Binary Ninja: a Settings button on the toolbar, a "Change..." link on the remote server + line, and a Settings entry in the menus (IDA: Edit → Plugins → MCRIT4IDA Settings...; Binary + Ninja: Plugins → MCRIT → Settings). In IDA they open the Plugin Settings Manager on mcrit-ida when + `ida-settings-editor` is installed; in Binary Ninja, Settings on the MCRIT group. Without the + settings editor, IDA shows the plugin's own form for the server, API token, username and timeout; + it saves only the fields you changed, greys out the ones `config_override.json` forces, and + connects again at once. Closing the settings window or tab opened this way also picks up a + changed token, username or timeout without reopening the plugin. A changed server is picked up + the same way until the plugin holds results from the current one; after that, it says so, keeps + the current server with its token and username, and reopening the plugin switches. An answer + that arrives from the previous server after a switch is dropped. +- A failed connection check while the server is still the default `http://127.0.0.1:8000/` points + at the Settings button (Ghidra: Edit > Tool Options > MCRIT). + +### Changed + +- IDA and Binary Ninja: new toolbar and tab icons from one set (Lucide, plus the YARA mark from + YARA-X), each tab with its own icon; credits and licences are in `icons/NOTICE.md`. The toolbar + icons are 24 px in both (IDA drew them at 32 px). ### Fixed @@ -26,6 +46,9 @@ Ninja or Ghidra applies to that plugin only. example, branch targets kept their exact bytes. The escaping now follows the report's architecture; one without an SMDA escaper keeps its raw bytes. Copy Escaped Bytes falls back to raw bytes like the rule text when escaping fails, instead of copying nothing. +- IDA and Binary Ninja: the toolbar and tab icons take the theme's text color, also after a theme + change, so they are visible on dark themes; disabled toolbar icons show dimmed instead of almost + black. ## [2.1.1] - 2026-10-15 diff --git a/README.md b/README.md index c32fef3..7817742 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ For offline machines, each release has a wheelhouse bundle: unpack it and run `p Settings are handled by [ida-settings](https://github.com/williballenthin/ida-settings): - HCLI: `hcli plugin config mcrit-ida set mcrit_server https://mcrit.example.com/api/` (`list`, `get`, `export` and `import` also work) -- GUI: install `ida-settings-editor` and use Edit → Plugins → Plugin Settings Manager +- GUI: the Settings button on the MCRIT toolbar (also Edit → Plugins → MCRIT4IDA Settings...). With `ida-settings-editor` installed it opens the Plugin Settings Manager on mcrit-ida; without it, a small form for the server, API token, username and timeout. - File: put a `config_override.json` next to `ida_mcrit.py`. Its keys take precedence over the other two, so keep only the ones you want to force; `docs/config_override.json.template` shows the format, and the plugin prints the keys it forces when it loads. ### Use @@ -79,7 +79,7 @@ Install MCRIT from the Extension Manager, or clone this repository into your Bin ### Configure -Settings are under Settings → MCRIT, with the same keys as the IDA plugin. Where a system keychain is available, an API token entered there is moved into it and the field is cleared; otherwise the token stays in the Settings entry. Plugins → MCRIT → Clear Stored API Token removes it. The sidebar reads the connection settings (server, token, username, timeout) when it opens for a file, so reopen the file after changing them. A `config_override.json` in the plugin's folder, next to `plugin.json`, takes precedence as it does in IDA. +Settings are under Settings → MCRIT, with the same keys as the IDA plugin; the Settings button on the sidebar toolbar and Plugins → MCRIT → Settings open them there. Where a system keychain is available, an API token entered there is moved into it and the field is cleared; otherwise the token stays in the Settings entry. Plugins → MCRIT → Clear Stored API Token removes it. The sidebar reads the connection settings (server, token, username, timeout) when it opens for a file, and again when you close the Settings tab its button opened (a different server only until the sidebar holds results from the current one); after changing them any other way, reopen the file. A `config_override.json` in the plugin's folder, next to `plugin.json`, takes precedence as it does in IDA. ### Use @@ -147,7 +147,8 @@ See [CHANGELOG.md](CHANGELOG.md) for the full release history. ## License -GPL-3.0, see [LICENSE](LICENSE). +GPL-3.0, see [LICENSE](LICENSE). The third-party icons and their licences are listed in +[icons/NOTICE.md](icons/NOTICE.md). ## Authors diff --git a/icons/NOTICE.md b/icons/NOTICE.md new file mode 100644 index 0000000..5f5b7b4 --- /dev/null +++ b/icons/NOTICE.md @@ -0,0 +1,93 @@ +# Icon credits + +The toolbar and tab icons are drawn from these sets and rendered to 48 px PNGs on a 2x grid. +`mcrit.png`, the plugin's logo, is this project's own. + +## Lucide + +`fingerprint_scan.png`, `cloud-upload.png`, `satellite_dish.png`, `export.png`, `settings.png`, +`blocks.png`, `compare.png`, `network.png` and `chart.png` are the Lucide icons `fingerprint-pattern`, +`cloud-upload`, `satellite-dish`, `file-down`, `settings-2`, `blocks`, `git-compare-arrows`, +`network` and `chart-column` (lucide-static 1.52.0, ). + +```text +ISC License + +Copyright (c) 2026 Lucide Icons and Contributors + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +--- + +The following Lucide icons are derived from the Feather project: + +airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out + +`cloud-upload` is Feather's `upload-cloud` under its Lucide name, so it falls under this licence too. + +The MIT License (MIT) (for the icons listed above) + +Copyright (c) 2013-present Cole Bemis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` + +## YARA-X + +`yara.png` is the YARA mark from the YARA-X playground +(), in one color. + +```text +Copyright (c) 2024. The YARA-X Authors. All Rights Reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( +INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +``` diff --git a/icons/blocks.png b/icons/blocks.png new file mode 100644 index 0000000..1754855 Binary files /dev/null and b/icons/blocks.png differ diff --git a/icons/chart.png b/icons/chart.png new file mode 100644 index 0000000..cbd933f Binary files /dev/null and b/icons/chart.png differ diff --git a/icons/cloud-upload.png b/icons/cloud-upload.png index ae9632f..48f6590 100644 Binary files a/icons/cloud-upload.png and b/icons/cloud-upload.png differ diff --git a/icons/compare.png b/icons/compare.png new file mode 100644 index 0000000..9c70290 Binary files /dev/null and b/icons/compare.png differ diff --git a/icons/export.png b/icons/export.png index 09652c4..a353e42 100644 Binary files a/icons/export.png and b/icons/export.png differ diff --git a/icons/fingerprint_scan.png b/icons/fingerprint_scan.png index 69b6cb8..c3bda1c 100644 Binary files a/icons/fingerprint_scan.png and b/icons/fingerprint_scan.png differ diff --git a/icons/flag-triangle.png b/icons/flag-triangle.png deleted file mode 100644 index 02fdfdb..0000000 Binary files a/icons/flag-triangle.png and /dev/null differ diff --git a/icons/network.png b/icons/network.png new file mode 100644 index 0000000..af091b5 Binary files /dev/null and b/icons/network.png differ diff --git a/icons/puzzle.png b/icons/puzzle.png deleted file mode 100644 index 9893a77..0000000 Binary files a/icons/puzzle.png and /dev/null differ diff --git a/icons/relationship.png b/icons/relationship.png deleted file mode 100644 index 7d4ffd8..0000000 Binary files a/icons/relationship.png and /dev/null differ diff --git a/icons/satellite_dish.png b/icons/satellite_dish.png index b2d5e9a..4d13418 100644 Binary files a/icons/satellite_dish.png and b/icons/satellite_dish.png differ diff --git a/icons/scan.png b/icons/scan.png deleted file mode 100644 index 28a9643..0000000 Binary files a/icons/scan.png and /dev/null differ diff --git a/icons/settings.png b/icons/settings.png new file mode 100644 index 0000000..de84117 Binary files /dev/null and b/icons/settings.png differ diff --git a/icons/upload.png b/icons/upload.png deleted file mode 100644 index 016eb11..0000000 Binary files a/icons/upload.png and /dev/null differ diff --git a/icons/yara.png b/icons/yara.png index 0b3d566..00b3616 100644 Binary files a/icons/yara.png and b/icons/yara.png differ diff --git a/mcrit_plugin/binja/BinjaBackend.py b/mcrit_plugin/binja/BinjaBackend.py index 7cefb06..2b93bb4 100644 --- a/mcrit_plugin/binja/BinjaBackend.py +++ b/mcrit_plugin/binja/BinjaBackend.py @@ -38,6 +38,7 @@ class BinjaBackend(Backend): name = "Binary Ninja" plugin_name = "MCRIT4BinaryNinja" + reopen_hint = "reopen the file" def __init__(self, bv): self.bv = bv @@ -247,6 +248,31 @@ def show_warning(self, message): TITLE, message, MessageBoxButtonSet.OKButtonSet, MessageBoxIcon.WarningIcon ) + def open_settings(self, config): + from binaryninjaui import SettingsView, UIContext + from PySide6.QtWidgets import QApplication + + from mcrit_plugin.binja.config import GROUP + + context = UIContext.activeContext() or next(iter(UIContext.allContexts()), None) + if context is not None: + # the "Settings" action opens the Settings tab, or brings an open one to the front + context.globalActions().executeAction("Settings") + views = [w for w in QApplication.allWidgets() if isinstance(w, SettingsView)] + # with several windows open, watch the tab this window just showed + views.sort(key=lambda view: not context.mainWindow().isAncestorOf(view)) + for view in views: + view.setDefaultGroupSelection(GROUP) + if views: + return views[0] + return None + + def save_settings(self, values): + from mcrit_plugin.binja.config import set_setting + + for key, value in values.items(): + set_setting(key, value) + def theme_color(self, role, default): try: from binaryninjaui import getThemeColor diff --git a/mcrit_plugin/binja/McritSidebar.py b/mcrit_plugin/binja/McritSidebar.py index 13f335a..e19a277 100644 --- a/mcrit_plugin/binja/McritSidebar.py +++ b/mcrit_plugin/binja/McritSidebar.py @@ -217,6 +217,7 @@ def _has_report(session): # (action name, handler(session) or None to only open the sidebar, enabled(session) or None when always available) _ACTIONS = [ ("MCRIT\\Show Sidebar", None, None), + ("MCRIT\\Settings", lambda session: session.main_widget._onSettingsButtonClicked(), None), ( "MCRIT\\Convert to SMDA Report", lambda session: session.main_widget._onConvertSmdaButtonClicked(), diff --git a/mcrit_plugin/binja/config.py b/mcrit_plugin/binja/config.py index 09e2fa2..35f9bb2 100644 --- a/mcrit_plugin/binja/config.py +++ b/mcrit_plugin/binja/config.py @@ -71,6 +71,25 @@ def _get_secret(key, settings): raise KeyError(key) +def set_setting(key, value): + full_key = f"{GROUP}.{key}" + settings = Settings() + if key in SECRET_SETTINGS: + provider = SecretsProvider.get(KEYCHAIN_PROVIDER) + if provider is not None and provider.has_data(full_key): + provider.delete_data(full_key) + settings.reset(full_key) + if value: + # moved into the keychain by the next read, as for a value typed into Settings + settings.set_string(full_key, value) + elif key in NUMBER_SETTINGS: + settings.set_integer(full_key, int(value)) + elif isinstance(value, bool): + settings.set_bool(full_key, value) + else: + settings.set_string(full_key, value) + + def clear_stored_secrets(): provider = SecretsProvider.get(KEYCHAIN_PROVIDER) settings = Settings() diff --git a/mcrit_plugin/core/Backend.py b/mcrit_plugin/core/Backend.py index 7d3e774..f172a4c 100644 --- a/mcrit_plugin/core/Backend.py +++ b/mcrit_plugin/core/Backend.py @@ -119,6 +119,25 @@ def ask_yes_no(self, prompt): def show_warning(self, message): """Modal warning.""" + # shown under the plugin's own connection form, e.g. where the remaining settings live + settings_hint = "" + # what rebuilds the session, so a new server can take over + reopen_hint = "reopen MCRIT" + # where the user changes the server, named in the hint after a failed connection check + settings_location = "the Settings button" + + def open_settings(self, config): + """Open the disassembler's own settings UI on this plugin and return its QWidget, or True + when it opened but cannot be watched for closing. + + None means there is none, and the plugin shows its own connection form instead. + """ + return None + + def save_settings(self, values): + """Store the connection form's values (setting key -> value) in the settings store.""" + raise NotImplementedError + def theme_color(self, role, default): """RGB tuple for a ScoreColorProvider.ThemeRole; None keeps the widget's own color and is valid only for TEXT_ON_TINT. diff --git a/mcrit_plugin/core/HeadlessMcritContext.py b/mcrit_plugin/core/HeadlessMcritContext.py index 9a7c8d5..008addc 100644 --- a/mcrit_plugin/core/HeadlessMcritContext.py +++ b/mcrit_plugin/core/HeadlessMcritContext.py @@ -41,4 +41,5 @@ def __init__(self, config, backend): self.family_infos = None self.function_id_to_offset = {} self.function_matches = {} + self.blockhash_matches = {} self.mcrit_interface = McritInterface(self, backend) diff --git a/mcrit_plugin/core/McritInterface.py b/mcrit_plugin/core/McritInterface.py index 114a4ef..6c2bb0e 100644 --- a/mcrit_plugin/core/McritInterface.py +++ b/mcrit_plugin/core/McritInterface.py @@ -1,5 +1,6 @@ import threading import traceback +from contextlib import contextmanager import requests @@ -51,16 +52,103 @@ def __init__(self, parent, backend): self.config = parent.config self._cache_lock = threading.Lock() self._remote_info_lock = threading.Lock() - self._mcrit_server = self.config.MCRIT_SERVER - self.mcrit_client = McritClient(self.config.MCRIT_SERVER) + self._check_id = 0 + # a switch tests for held data, and an answer is checked and stored, under this lock: the + # switch then either sees the stored answer and waits, or the answer sees the switch + self._switch_lock = threading.Lock() + self._server_generation = 0 + self._mcrit_server = None + self._buildClient(self._readConnectionSettings()) + + def _readConnectionSettings(self): + return ( + self.config.MCRIT_SERVER, + self.config.MCRIT_REQUEST_TIMEOUT, + self.config.MCRITWEB_API_TOKEN, + self.config.MCRITWEB_USERNAME, + ) + + def _buildClient(self, connection_settings): + server, timeout, api_token, username = connection_settings + client = McritClient(server) # setTimeout maps 0 to no limit; skipping it would keep the client's 10 s connect timeout - self.mcrit_client.setTimeout(self.config.MCRIT_REQUEST_TIMEOUT) - if self.config.MCRITWEB_API_TOKEN: - self.mcrit_client.setApitoken(self.config.MCRITWEB_API_TOKEN) - if self.config.MCRITWEB_USERNAME: - self.mcrit_client.setUsername(self.config.MCRITWEB_USERNAME) + client.setTimeout(timeout) + if api_token: + client.setApitoken(api_token) + if username: + client.setUsername(username) + self._connection_settings = connection_settings + # a connection check started before this client answers for nothing any more + self._check_id += 1 + # requests already running keep the client they started with; set before the generation + # moves, as _pinConnection reads the two without the lock + self.mcrit_client = client + if server != self._mcrit_server: + self._mcrit_server = server + self._server_generation += 1 + + def _pinConnection(self): + """The client a request works with and the server generation that tells whether its answer + still belongs to the session. Read in this order, a switch in between can only make an + answer from the new server look stale, never one from the old server look current.""" + generation = self._server_generation + return self.mcrit_client, generation + + def _isStale(self, generation): + """True, after saying so, when the server changed while the request ran.""" + if generation == self._server_generation: + return False + self.parent.local_widget.updateActivityInfo( + "Dropped an answer from the previous server; the server changed meanwhile." + ) + return True + + @contextmanager + def _storing(self, generation): + """Yields whether an answer still belongs to the session; store it inside the block.""" + with self._switch_lock: + yield not self._isStale(generation) + + def _holdsServerData(self): + parent = self.parent + return bool( + parent.remote_sample_id is not None + or parent.remote_sample_entry is not None + or parent.matching_report is not None + or parent.matched_function_entries + or parent.function_matches + or parent.blockhash_matches + or parent.sample_infos is not None + or parent.family_infos is not None + ) - def _getMcritServerAddress(self): + def reloadSettings(self, recheck=False): + """Pick up changed connection settings and check the connection again; UI thread only. + + With recheck, the connection is checked even when nothing changed. A new server is only + taken while the session holds nothing from the current one, whose ids mean nothing there; + until then the session keeps the current server's token and username and takes only the new + timeout. Returns the server waiting for a reopen, or None. + """ + connection_settings = self._readConnectionSettings() + waiting_server = None + with self._switch_lock: + if connection_settings[0] != self._mcrit_server and self._holdsServerData(): + waiting_server = connection_settings[0] + server, _timeout, api_token, username = self._connection_settings + connection_settings = (server, connection_settings[1], api_token, username) + changed = connection_settings != self._connection_settings + if changed: + self._buildClient(connection_settings) + if changed: + self.checkConnection(async_=True) + elif recheck: + self.checkConnection(async_=True) + return waiting_server + + @property + def server(self): + """The server the session works with.""" return self._mcrit_server @staticmethod @@ -84,7 +172,7 @@ def _reportFailure(self, operation, exc): self.parent.local_widget.updateActivityInfo( "%s failed (%s)." % (operation, self._describeError(exc)) ) - self.parent.local_widget.updateServerInfo(self._getMcritServerAddress()) + self.parent.local_widget.updateServerInfo(self.server) def _select_smda_backend(self, binary_info): """One of SMDA's own backend names, or None for SMDA's default; an unknown name leaves the @@ -125,9 +213,9 @@ def convertToSmdaUsingSmda(self): self.parent.local_widget.updateActivityInfo("Conversion to SMDA finished.") return report - def _check_connection_impl(self): + def _check_connection_impl(self, client=None): try: - mcrit_version = self.mcrit_client.getVersion() + mcrit_version = (client or self.mcrit_client).getVersion() return mcrit_version, None except Exception as exc: self._logTraceback(exc) @@ -135,46 +223,62 @@ def _check_connection_impl(self): def checkConnection(self, async_=False): self.parent.local_widget.updateActivityInfo( - "Checking connection to server: %s" % self._getMcritServerAddress() + "Checking connection to server: %s" % self.server ) + self.parent.local_widget.updateServerInfo(self.server, version="checking...") + client = self.mcrit_client + self._check_id += 1 + check_id = self._check_id def apply_result(result): + if check_id != self._check_id: + # a newer check or other settings came since; that one reports instead + return mcrit_version, err = result if mcrit_version: self.parent.local_widget.updateActivityInfo("Connection check successful!") - self.parent.local_widget.updateServerInfo( - self._getMcritServerAddress(), version=mcrit_version - ) + self.parent.local_widget.updateServerInfo(self.server, version=mcrit_version) else: reason = ( "server rejected the request or sent no version" if err is None else self._describeError(err) ) + hint = ( + " The server is still the default one, use %s to change it." + % self.backend.settings_location + if self.config.isDefaultServer(self.server) + else "" + ) self.parent.local_widget.updateActivityInfo( - "Connection check failed (%s)." % reason + "Connection check failed (%s).%s" % (reason, hint) ) - self.parent.local_widget.updateServerInfo(self._getMcritServerAddress()) + self.parent.local_widget.updateServerInfo(self.server) if async_: def runner(): - result = self._check_connection_impl() + result = self._check_connection_impl(client) self.backend.run_on_ui_thread(lambda: apply_result(result)) thread = threading.Thread(target=runner, daemon=True) thread.start() return - apply_result(self._check_connection_impl()) + apply_result(self._check_connection_impl(client)) def querySampleSha256(self, sha256): self.parent.local_widget.updateActivityInfo("Querying for SHA256") + client, generation = self._pinConnection() try: - sample_by_sha256 = self.mcrit_client.getSampleBySha256(sha256) + sample_by_sha256 = client.getSampleBySha256(sha256) + with self._storing(generation) as current: + if not current: + return + if sample_by_sha256: + self.parent.remote_sample_entry = sample_by_sha256 + self.parent.remote_sample_id = sample_by_sha256.sample_id if sample_by_sha256: - self.parent.remote_sample_entry = sample_by_sha256 - self.parent.remote_sample_id = sample_by_sha256.sample_id self.parent.local_widget.updateActivityInfo( "Success! Received remote Sample Entry." ) @@ -185,10 +289,17 @@ def querySampleSha256(self, sha256): def uploadReport(self, report): self.parent.local_widget.updateActivityInfo( - "Sending SMDA report to server %s" % self._getMcritServerAddress() + "Sending SMDA report to server %s" % self.server ) + client, generation = self._pinConnection() try: - sample_entry, job_id = self.mcrit_client.addReport(report) or (None, None) + sample_entry, job_id = client.addReport(report) or (None, None) + with self._storing(generation) as current: + if not current: + return + if sample_entry: + self.parent.remote_sample_entry = sample_entry + self.parent.remote_sample_id = sample_entry.sample_id if sample_entry: if job_id: self.parent.local_widget.updateActivityInfo( @@ -199,8 +310,6 @@ def uploadReport(self, report): self.parent.local_widget.updateActivityInfo( "Upload finished, remote sample_id is: %d." % sample_entry.sample_id ) - self.parent.remote_sample_entry = sample_entry - self.parent.remote_sample_id = sample_entry.sample_id else: self.parent.local_widget.updateActivityInfo("Upload failed.") except Exception as exc: @@ -258,11 +367,17 @@ def requestMatchingJob(self, sample_id, force_update=False): def getMatchingJobById(self, job_id): self.parent.local_widget.updateActivityInfo("Querying result for job with id: %s" % job_id) + client, generation = self._pinConnection() try: - matching_result = self.mcrit_client.getResultForJob(job_id) - if matching_result: - self.parent.matching_job_id = job_id - self.parent.matching_report = MatchingResult.fromDict(matching_result) + matching_result = client.getResultForJob(job_id) + matching_report = MatchingResult.fromDict(matching_result) if matching_result else None + with self._storing(generation) as current: + if not current: + return + if matching_report is not None: + self.parent.matching_job_id = job_id + self.parent.matching_report = matching_report + if matching_report is not None: self.parent.local_widget.updateActivityInfo("Success! Downloaded MatchResult.") else: self.parent.local_widget.updateActivityInfo("Result query failed.") @@ -287,11 +402,16 @@ def ensureRemoteInformation(self): def queryAllFamilyEntries(self): self.parent.local_widget.updateActivityInfo("Querying for FamilyEntries") + client, generation = self._pinConnection() try: - family_entries = self.mcrit_client.getFamilies() + family_entries = client.getFamilies() # None is a failed request; an empty dict is a server without families + with self._storing(generation) as current: + if not current: + return + if family_entries is not None: + self.parent.family_infos = {int(k): v for k, v in family_entries.items()} if family_entries is not None: - self.parent.family_infos = {int(k): v for k, v in family_entries.items()} self.parent.local_widget.updateActivityInfo( "Success! Received all remote FamilyEntries." ) @@ -306,12 +426,18 @@ def querySmdaFunctionMatches(self, smda_report): if not functions: return None smda_function = functions[0] + client, generation = self._pinConnection() if smda_function.offset not in self.parent.function_matches: - match_report_dict = self.mcrit_client.getMatchesForSmdaFunction( + match_report_dict = client.getMatchesForSmdaFunction( smda_report, exclude_self_matches=False ) - if match_report_dict: - self.parent.function_matches.update({smda_function.offset: match_report_dict}) + with self._storing(generation) as current: + if not current: + return None + if match_report_dict: + self.parent.function_matches.update( + {smda_function.offset: match_report_dict} + ) if match_report_dict: match_report = MatchingResult.fromDict(match_report_dict) matched_function_ids = [ @@ -323,21 +449,25 @@ def querySmdaFunctionMatches(self, smda_report): if fid not in self.parent.function_id_to_offset ] if unknown_function_ids: - function_entries = self.queryFunctionEntriesById(unknown_function_ids) - if function_entries: - for function_id, function_entry in function_entries.items(): - self.parent.function_id_to_offset[function_id] = ( - function_entry.offset - ) + function_entries = self.queryFunctionEntriesById( + unknown_function_ids, connection=(client, generation) + ) + with self._storing(generation) as current: + if current and function_entries: + for function_id, function_entry in function_entries.items(): + self.parent.function_id_to_offset[function_id] = ( + function_entry.offset + ) return match_report except Exception as exc: self._reportFailure("querySmdaFunctionMatches", exc) - def queryFunctionEntriesById(self, function_ids, with_label_only=False): - """The entries by id, or None when the request raised; McritClient answers {} both when - none qualify and when the server rejects the request.""" + def queryFunctionEntriesById(self, function_ids, with_label_only=False, connection=None): + """The entries by id, or None when the request raised or the server changed meanwhile; + McritClient answers {} both when none qualify and when the server rejects the request.""" + client, generation = connection or self._pinConnection() try: - function_entries = self.mcrit_client.getFunctionsByIds( + function_entries = client.getFunctionsByIds( function_ids, with_label_only=with_label_only ) except Exception as exc: @@ -345,20 +475,28 @@ def queryFunctionEntriesById(self, function_ids, with_label_only=False): return None if function_entries is None: return None - if function_entries: - # requests run on worker threads while the UI iterates the cache: swap in a new dict - with self._cache_lock: - merged = dict(self.parent.matched_function_entries or {}) - merged.update(function_entries) - self.parent.matched_function_entries = merged + with self._storing(generation) as current: + if not current: + return None + if function_entries: + # requests run on worker threads while the UI iterates the cache: swap in a new dict + with self._cache_lock: + merged = dict(self.parent.matched_function_entries or {}) + merged.update(function_entries) + self.parent.matched_function_entries = merged return function_entries def queryAllSampleEntries(self): self.parent.local_widget.updateActivityInfo("Querying for SampleEntries") + client, generation = self._pinConnection() try: - sample_entries = self.mcrit_client.getSamples() + sample_entries = client.getSamples() + with self._storing(generation) as current: + if not current: + return + if sample_entries is not None: + self.parent.sample_infos = sample_entries if sample_entries is not None: - self.parent.sample_infos = sample_entries self.parent.local_widget.updateActivityInfo( "Success! Received all remote SampleEntries." ) @@ -371,8 +509,9 @@ def queryRemoteFunction(self, function_id): """(function entry with its CFG, its SmdaFunction, its sample entry), or None after reporting why; touches no widget, so it can run off the UI thread. The sample entry comes from the downloaded sample list when that has it.""" + client, generation = self._pinConnection() try: - function_entry = self.mcrit_client.getFunctionById(function_id, with_xcfg=True) + function_entry = client.getFunctionById(function_id, with_xcfg=True) if function_entry is None: self.parent.local_widget.updateActivityInfo( f"Failed to fetch function entry {function_id}." @@ -385,12 +524,14 @@ def queryRemoteFunction(self, function_id): else None ) if sample_entry is None: - sample_entry = self.mcrit_client.getSampleById(function_entry.sample_id) + sample_entry = client.getSampleById(function_entry.sample_id) if sample_entry is None: self.parent.local_widget.updateActivityInfo( f"Failed to fetch sample entry {function_entry.sample_id}." ) return None + if self._isStale(generation): + return None # a malformed CFG raises here; the widgets then still get their None answer return function_entry, function_entry.toSmdaFunction(), sample_entry except Exception as exc: @@ -398,8 +539,18 @@ def queryRemoteFunction(self, function_id): self._reportFailure(f"Fetching function {function_id} for the graph viewer", exc) return None - def getMatchesForPicBlockHash(self, picblockhash): + def getMatchesForPicBlockHash(self, picblockhash, connection=None): + """The matches, or None when the request failed or the server changed meanwhile; the + widgets keep None out of the session's cache.""" + client, generation = connection or self._pinConnection() try: - return self.mcrit_client.getMatchesForPicBlockHash(picblockhash) + matches = client.getMatchesForPicBlockHash(picblockhash) + return None if self._isStale(generation) else matches except Exception as exc: self._reportFailure("getMatchesForPicBlockHash", exc) + + def storeBlockMatches(self, found, connection): + """Caches block matches looked up through connection, unless the server changed since.""" + with self._storing(connection[1]) as current: + if current: + self.parent.blockhash_matches.update(found) diff --git a/mcrit_plugin/core/config.py b/mcrit_plugin/core/config.py index c673503..ded94b5 100644 --- a/mcrit_plugin/core/config.py +++ b/mcrit_plugin/core/config.py @@ -171,6 +171,14 @@ def MCRIT_SERVER(self): """URL of the MCRIT server.""" return self._get("mcrit_server") + def isDefaultServer(self, server): + """True for the shipped localhost default of mcrit_server.""" + return server == self._defaults["mcrit_server"] + + def overriddenSettings(self): + """Keys forced by config_override.json, which the settings store cannot change.""" + return set(self._overrides) + @property def MCRITWEB_API_TOKEN(self): """API token for MCRITWeb authentication.""" diff --git a/mcrit_plugin/ghidra/GhidraBackend.py b/mcrit_plugin/ghidra/GhidraBackend.py index d073f42..825527a 100644 --- a/mcrit_plugin/ghidra/GhidraBackend.py +++ b/mcrit_plugin/ghidra/GhidraBackend.py @@ -32,6 +32,7 @@ class GhidraBackend(Backend): name = "Ghidra" plugin_name = "MCRIT4Ghidra" + settings_location = "Edit > Tool Options > MCRIT" def __init__(self, program, tool=None): self.program = program diff --git a/mcrit_plugin/ghidra/ui/BlockMatchPanel.py b/mcrit_plugin/ghidra/ui/BlockMatchPanel.py index bb5d43e..e931cf1 100644 --- a/mcrit_plugin/ghidra/ui/BlockMatchPanel.py +++ b/mcrit_plugin/ghidra/ui/BlockMatchPanel.py @@ -41,7 +41,7 @@ def __init__(self, parent): self._shown_function = None self._shown_block = None self.name = "Block Scope" - self.icon_file = "puzzle.png" + self.icon_file = "blocks.png" config = parent.config self.label_current_function_matches = Label("Block Matches for: ") self.cb_filter_library = checkbox( @@ -228,21 +228,22 @@ def _lookupBlockHashes(self, hashes, stop=None): missing = [h for h in dict.fromkeys(hashes) if h not in self.parent.blockhash_matches] if not missing: return + interface = self.parent.mcrit_interface + connection = interface._pinConnection() failed = threading.Event() def lookup(block_hash): if failed.is_set() or (stop is not None and stop()): return None - matches = self.parent.mcrit_interface.getMatchesForPicBlockHash(block_hash) + matches = interface.getMatchesForPicBlockHash(block_hash, connection) if matches is None: failed.set() return matches start = time.time() with ThreadPoolExecutor(BLOCK_QUERY_WORKERS) as pool: - for block_hash, matches in zip(missing, pool.map(lookup, missing)): - if matches is not None: - self.parent.blockhash_matches[block_hash] = matches + found = {h: m for h, m in zip(missing, pool.map(lookup, missing)) if m is not None} + interface.storeBlockMatches(found, connection) duration = time.time() - start print(f"[MCRIT] querying {len(missing)} blocks took {duration:5.3f} seconds.") diff --git a/mcrit_plugin/ghidra/ui/FunctionMatchPanel.py b/mcrit_plugin/ghidra/ui/FunctionMatchPanel.py index 99b4c53..cafdcae 100644 --- a/mcrit_plugin/ghidra/ui/FunctionMatchPanel.py +++ b/mcrit_plugin/ghidra/ui/FunctionMatchPanel.py @@ -39,7 +39,7 @@ def __init__(self, parent): self.scp = ScoreColorProvider(self.cc.backend) self.last_viewed = None self.name = "Function Scope" - self.icon_file = "flag-triangle.png" + self.icon_file = "compare.png" self.current_function_offset = None self._graph_fetches = set() config = parent.config diff --git a/mcrit_plugin/ghidra/ui/FunctionOverviewPanel.py b/mcrit_plugin/ghidra/ui/FunctionOverviewPanel.py index 939c58d..8a4bed5 100644 --- a/mcrit_plugin/ghidra/ui/FunctionOverviewPanel.py +++ b/mcrit_plugin/ghidra/ui/FunctionOverviewPanel.py @@ -53,7 +53,7 @@ def __init__(self, parent): self.parent = parent self.cc = parent.cc self.name = "Function Overview" - self.icon_file = "relationship.png" + self.icon_file = "network.png" self.scp = ScoreColorProvider(self.cc.backend) self.last_selected_fields = {} self.resolved_function_labels = {} diff --git a/mcrit_plugin/ghidra/ui/MainPanel.py b/mcrit_plugin/ghidra/ui/MainPanel.py index bf81e07..b954f71 100644 --- a/mcrit_plugin/ghidra/ui/MainPanel.py +++ b/mcrit_plugin/ghidra/ui/MainPanel.py @@ -35,7 +35,11 @@ def __init__(self, parent): def tool(icon, tooltip, handler, enabled): widget = button( - "", handler, enabled=enabled, icon=load_icon(icons + icon), tooltip=tooltip + "", + handler, + enabled=enabled, + icon=load_icon(icons + icon, 24), + tooltip=tooltip, ) self.toolbar.add(widget) return widget diff --git a/mcrit_plugin/ghidra/ui/SampleInfoPanel.py b/mcrit_plugin/ghidra/ui/SampleInfoPanel.py index c2467c6..a733374 100644 --- a/mcrit_plugin/ghidra/ui/SampleInfoPanel.py +++ b/mcrit_plugin/ghidra/ui/SampleInfoPanel.py @@ -57,7 +57,7 @@ class SampleInfoPanel: def __init__(self, parent): self.parent = parent self.name = "Sample Match Summary" - self.icon_file = "puzzle.png" + self.icon_file = "chart.png" self.last_family_selected = None self._matching_data = None self._populating = False diff --git a/mcrit_plugin/ghidra/ui/components.py b/mcrit_plugin/ghidra/ui/components.py index 4084c4b..a80cefb 100644 --- a/mcrit_plugin/ghidra/ui/components.py +++ b/mcrit_plugin/ghidra/ui/components.py @@ -213,15 +213,29 @@ def grid(pairs): def load_icon(path, size=20): - """An ImageIcon scaled to size pixels, or None when the file cannot be read.""" + """An ImageIcon scaled to size pixels, or None when the file cannot be read. The icons are + one-colour glyphs, drawn here in the theme's label colour so they show on dark themes too.""" try: - from java.awt import Image - from javax.swing import ImageIcon + from java.awt import AlphaComposite, Image + from java.awt.image import BufferedImage + from javax.swing import ImageIcon, UIManager icon = ImageIcon(str(path)) if icon.getIconWidth() <= 0: return None - return ImageIcon(icon.getImage().getScaledInstance(size, size, Image.SCALE_SMOOTH)) + image = icon.getImage() + color = UIManager.getColor("Label.foreground") + if color is not None: + width, height = icon.getIconWidth(), icon.getIconHeight() + tinted = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) + graphics = tinted.createGraphics() + graphics.drawImage(image, 0, 0, None) + graphics.setComposite(AlphaComposite.SrcIn) + graphics.setColor(color) + graphics.fillRect(0, 0, width, height) + graphics.dispose() + image = tinted + return ImageIcon(image.getScaledInstance(size, size, Image.SCALE_SMOOTH)) except Exception: return None diff --git a/mcrit_plugin/ida/IdaBackend.py b/mcrit_plugin/ida/IdaBackend.py index 9a82983..a75120b 100644 --- a/mcrit_plugin/ida/IdaBackend.py +++ b/mcrit_plugin/ida/IdaBackend.py @@ -7,6 +7,7 @@ from contextlib import contextmanager import ida_bytes +import ida_expr import ida_funcs import ida_idaapi import ida_kernwin @@ -133,9 +134,20 @@ def _cbEnumImports(self, addr, name, ordinal): return IdaInterface() +HCLI_PLUGIN_NAME = "mcrit-ida" +SETTINGS_EDITOR_IDC_FUNC = "ida_settings_show_plugin_settings" +SETTINGS_EDITOR_CAPTION = "Plugin Settings Manager" + + class IdaBackend(Backend): name = "IDA" plugin_name = "MCRIT4IDA" + reopen_hint = "close and reopen MCRIT" + settings_hint = ( + "The other settings are in the Plugin Settings Manager, which comes with " + "hcli plugin install ida-settings-editor (restart IDA afterwards), " + "or in a terminal: hcli plugin config mcrit-ida setup." + ) def __init__(self): self._mutation_depth = 0 @@ -375,6 +387,22 @@ def ask_yes_no(self, prompt): def show_warning(self, message): ida_kernwin.warning(message) + def open_settings(self, config): + # registered by the ida-settings-editor plugin, which installing mcrit-ida does not bring + if ida_expr.find_idc_func(SETTINGS_EDITOR_IDC_FUNC) == SETTINGS_EDITOR_IDC_FUNC: + idc.eval_idc('%s("%s")' % (SETTINGS_EDITOR_IDC_FUNC, HCLI_PLUGIN_NAME)) + widget = ida_kernwin.find_widget(SETTINGS_EDITOR_CAPTION) + if widget is None: + return True + return ida_kernwin.PluginForm.FormToPyQtWidget(widget) + return None + + def save_settings(self, values): + from mcrit_plugin.ida.config import set_setting + + for key, value in values.items(): + set_setting(key, value) + def show_function_graph(self, parent, sample_entry, function_entry, smda_function, coloring): from mcrit_plugin.ida.SmdaGraphViewer import SmdaGraphViewer diff --git a/mcrit_plugin/ida/config.py b/mcrit_plugin/ida/config.py index 989bffb..de77cc7 100644 --- a/mcrit_plugin/ida/config.py +++ b/mcrit_plugin/ida/config.py @@ -19,4 +19,10 @@ def _get_setting(key): raise KeyError(key) from exc +def set_setting(key, value): + ida_settings.set_plugin_setting( + PLUGIN_NAME, key, value if isinstance(value, bool) else str(value) + ) + + config = McritConfig(VERSION, _get_setting) diff --git a/mcrit_plugin/ida/ida_mcrit.py b/mcrit_plugin/ida/ida_mcrit.py index 7bb4307..7d608c8 100644 --- a/mcrit_plugin/ida/ida_mcrit.py +++ b/mcrit_plugin/ida/ida_mcrit.py @@ -6,6 +6,7 @@ import time import traceback +import weakref import ida_idaapi import ida_kernwin @@ -17,10 +18,14 @@ IdaBackend = None +def _has_gui(): + is_idaq = getattr(ida_kernwin, "is_idaq", None) + return callable(is_idaq) and bool(is_idaq()) + + def _require_gui(): """Fail clearly when a GUI-only plugin action is invoked through IDALib.""" - is_idaq = getattr(ida_kernwin, "is_idaq", None) - if not callable(is_idaq) or not is_idaq(): + if not _has_gui(): raise RuntimeError("MCRIT4IDA's Qt interface requires the IDA GUI") @@ -207,12 +212,52 @@ def show_mcrit_form(): return form +SETTINGS_ACTION = "mcrit4ida:settings" +# the open database's plugmod; weak, so a database that closes takes its plugmod and form along +_current_plugmod = None + + +def _register_settings_action(): + """Edit > Plugins > MCRIT4IDA Settings..., beside the plugin's own entry. Registered once per + IDA process and never removed: a plugmod's __del__ may run late, after the next database's + plugmod exists, so tying the action to a plugmod could remove the next database's entry.""" + + class SettingsHandler(ida_kernwin.action_handler_t): + def activate(self, ctx): + current = _current_plugmod() if _current_plugmod is not None else None + if current is not None: + current.run(0) + if current.form is not None: + current.form.main_widget._onSettingsButtonClicked() + return 1 + + def update(self, ctx): + return ida_kernwin.AST_ENABLE_ALWAYS + + # registering fails once the action exists, e.g. for the second database + if ida_kernwin.register_action( + ida_kernwin.action_desc_t( + SETTINGS_ACTION, + "MCRIT4IDA Settings...", + SettingsHandler(), + None, + "Set the MCRIT server, API token and other plugin settings.", + -1, + ) + ): + ida_kernwin.attach_action_to_menu("Edit/Plugins/", SETTINGS_ACTION, ida_kernwin.SETMENU_APP) + + class Mcrit4IdaPlugmod(ida_idaapi.plugmod_t): """Per-database plugin instance (PLUGIN_MULTI); owns the form it opened.""" def __init__(self): + global _current_plugmod super().__init__() self.form = None + if _has_gui(): + _current_plugmod = weakref.ref(self) + _register_settings_action() def run(self, arg): # a closed form still holds the previous session's reports, so reopening needs a new form diff --git a/mcrit_plugin/ui_qt/McritSession.py b/mcrit_plugin/ui_qt/McritSession.py index ad61f91..d8b5337 100644 --- a/mcrit_plugin/ui_qt/McritSession.py +++ b/mcrit_plugin/ui_qt/McritSession.py @@ -15,7 +15,7 @@ def __init__(self, backend, config): from mcrit_plugin.ui_qt.ClassCollection import ClassCollection self._initSharedState(ClassCollection(QtShim, backend), config) - self.icon = self.cc.QIcon(config.ICON_FILE_PATH + "relationship.png") + self.icon = self.cc.QIcon(config.ICON_FILE_PATH + "mcrit.png") self.mcrit_interface = McritInterface(self, backend) def _initSharedState(self, cc, config): diff --git a/mcrit_plugin/ui_qt/widgets/BlockMatchWidget.py b/mcrit_plugin/ui_qt/widgets/BlockMatchWidget.py index d1ab6f7..10b08d9 100644 --- a/mcrit_plugin/ui_qt/widgets/BlockMatchWidget.py +++ b/mcrit_plugin/ui_qt/widgets/BlockMatchWidget.py @@ -35,7 +35,7 @@ def __init__(self, parent): self._shown_function = None self._shown_block = None self.name = "Block Scope" - self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "puzzle.png") + self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "blocks.png") self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.label_current_function_matches = self.cc.QLabel("Block Matches for: ") @@ -279,23 +279,25 @@ def _lookupBlockHashes(self, hashes, stop=None): missing = [h for h in dict.fromkeys(hashes) if h not in self.parent.blockhash_matches] if not missing: return + interface = self.parent.mcrit_interface + # one connection for every lookup and the cache write, so a server switch drops them all + connection = interface._pinConnection() failed = threading.Event() def lookup(block_hash): # after a failure the remaining lookups would each wait out the same timeout if failed.is_set() or (stop is not None and stop()): return None - matches = self.parent.mcrit_interface.getMatchesForPicBlockHash(block_hash) + matches = interface.getMatchesForPicBlockHash(block_hash, connection) if matches is None: failed.set() return matches start = time.time() with ThreadPoolExecutor(BLOCK_QUERY_WORKERS) as pool: - for block_hash, matches in zip(missing, pool.map(lookup, missing)): - # a failed query stays uncached so the next visit retries it - if matches is not None: - self.parent.blockhash_matches[block_hash] = matches + # a failed query stays uncached so the next visit retries it + found = {h: m for h, m in zip(missing, pool.map(lookup, missing)) if m is not None} + interface.storeBlockMatches(found, connection) duration = time.time() - start print( f"Querying {len(missing)} blocks took {duration:5.3f} seconds, " diff --git a/mcrit_plugin/ui_qt/widgets/ConnectionSettingsDialog.py b/mcrit_plugin/ui_qt/widgets/ConnectionSettingsDialog.py new file mode 100644 index 0000000..47f8900 --- /dev/null +++ b/mcrit_plugin/ui_qt/widgets/ConnectionSettingsDialog.py @@ -0,0 +1,116 @@ +from urllib.parse import urlparse + +import mcrit_plugin.ui_qt.QtShim as QtShim + +QDialog = QtShim.get_QDialog() + + +class ConnectionSettingsDialog(QDialog): + """Edit the four connection settings in place; values() holds them once accepted.""" + + def __init__(self, parent, config): + self.cc = parent.cc + self.cc.QDialog.__init__(self, parent) + self.setWindowTitle("MCRIT Settings") + self.setMinimumWidth(460) + intro = self.cc.QLabel("Where should the plugin find your MCRIT server?") + self.edit_server = self.cc.QLineEdit(config.MCRIT_SERVER or "") + self.edit_server.setPlaceholderText("https://mcrit.example.com/api") + self.edit_token = self.cc.QLineEdit(config.MCRITWEB_API_TOKEN or "") + self.edit_token.setEchoMode(self.cc.QLineEdit.Password) + self.edit_token.setPlaceholderText("only needed for MCRITweb") + self.cb_show_token = self.cc.QCheckBox("Show") + self.cb_show_token.toggled.connect(self._onShowTokenToggled) + self.edit_username = self.cc.QLineEdit(config.MCRITWEB_USERNAME or "") + self.edit_username.setPlaceholderText("optional, the token implies it") + self.spin_timeout = self.cc.QSpinBox() + self.spin_timeout.setRange(0, 3600) + self.spin_timeout.setSuffix(" s") + self.spin_timeout.setSpecialValueText("no limit") + self.spin_timeout.setValue(config.MCRIT_REQUEST_TIMEOUT) + token_row = self.cc.QHBoxLayout() + token_row.addWidget(self.edit_token) + token_row.addWidget(self.cb_show_token) + grid = self.cc.QGridLayout() + grid.addWidget(self.cc.QLabel("Server URL:"), 0, 0) + grid.addWidget(self.edit_server, 0, 1) + grid.addWidget(self.cc.QLabel("API token:"), 1, 0) + grid.addLayout(token_row, 1, 1) + grid.addWidget(self.cc.QLabel("Username:"), 2, 0) + grid.addWidget(self.edit_username, 2, 1) + grid.addWidget(self.cc.QLabel("Timeout:"), 3, 0) + grid.addWidget(self.spin_timeout, 3, 1) + self.label_error = self.cc.QLabel("") + self.label_error.setStyleSheet("color: #d9534f;") + self.label_error.setVisible(False) + layout = self.cc.QVBoxLayout() + layout.addWidget(intro) + layout.addLayout(grid) + layout.addWidget(self.label_error) + notes = [] + fields = { + "mcrit_server": self.edit_server, + "mcritweb_api_token": self.edit_token, + "mcritweb_username": self.edit_username, + "mcrit_request_timeout": self.spin_timeout, + } + forced = sorted(key for key in fields if key in config.overriddenSettings()) + for key in forced: + fields[key].setEnabled(False) + if forced: + notes.append("Greyed out: set by config_override.json, so only that file changes them.") + if self.cc.backend.settings_hint: + notes.append(self.cc.backend.settings_hint) + for note in notes: + label = self.cc.QLabel(note) + label.setWordWrap(True) + label.setTextInteractionFlags(self.cc.QtCore.Qt.TextSelectableByMouse) + layout.addWidget(label) + buttons = self.cc.QHBoxLayout() + self.save_button = self.cc.QPushButton("Save and connect") + self.save_button.setDefault(True) + self.cancel_button = self.cc.QPushButton("Cancel") + self.save_button.clicked.connect(self.accept) + self.cancel_button.clicked.connect(self.reject) + buttons.addStretch(1) + buttons.addWidget(self.save_button) + buttons.addWidget(self.cancel_button) + layout.addLayout(buttons) + self.setLayout(layout) + self._initial_values = self.values() + + def _onShowTokenToggled(self, checked): + self.edit_token.setEchoMode( + self.cc.QLineEdit.Normal if checked else self.cc.QLineEdit.Password + ) + + def values(self): + return { + "mcrit_server": self.edit_server.text().strip(), + "mcritweb_api_token": self.edit_token.text().strip(), + "mcritweb_username": self.edit_username.text().strip(), + "mcrit_request_timeout": self.spin_timeout.value(), + } + + def changedValues(self): + """The values the user changed, so untouched settings keep what the store has.""" + return { + key: value for key, value in self.values().items() if value != self._initial_values[key] + } + + def accept(self): + try: + url = urlparse(self.edit_server.text().strip()) + # urllib raises here for a port that is not a number from 0 to 65535 + _port = url.port + valid = url.scheme.lower() in ("http", "https") and bool(url.hostname) + except ValueError: + valid = False + if not valid: + self.label_error.setText( + "The server URL has to look like https://mcrit.example.com/api" + ) + self.label_error.setVisible(True) + self.edit_server.setFocus() + return + self.cc.QDialog.accept(self) diff --git a/mcrit_plugin/ui_qt/widgets/FunctionMatchWidget.py b/mcrit_plugin/ui_qt/widgets/FunctionMatchWidget.py index ea981b2..f1ebd7f 100644 --- a/mcrit_plugin/ui_qt/widgets/FunctionMatchWidget.py +++ b/mcrit_plugin/ui_qt/widgets/FunctionMatchWidget.py @@ -19,7 +19,7 @@ def __init__(self, parent): self.scp = ScoreColorProvider(self.cc.backend) self.last_viewed = None self.name = "Function Scope" - self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "flag-triangle.png") + self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "compare.png") self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.current_function_offset = None diff --git a/mcrit_plugin/ui_qt/widgets/FunctionOverviewWidget.py b/mcrit_plugin/ui_qt/widgets/FunctionOverviewWidget.py index df8b11a..7c97029 100644 --- a/mcrit_plugin/ui_qt/widgets/FunctionOverviewWidget.py +++ b/mcrit_plugin/ui_qt/widgets/FunctionOverviewWidget.py @@ -36,7 +36,7 @@ def __init__(self, parent): self.resolved_function_labels = {} # offset -> resolved label string self.parent = parent self.name = "Function Overview" - self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "relationship.png") + self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "network.png") self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.b_fetch_labels = self.cc.QPushButton("Fetch labels for matches") diff --git a/mcrit_plugin/ui_qt/widgets/LocalInfoWidget.py b/mcrit_plugin/ui_qt/widgets/LocalInfoWidget.py index add0e21..fa1173e 100644 --- a/mcrit_plugin/ui_qt/widgets/LocalInfoWidget.py +++ b/mcrit_plugin/ui_qt/widgets/LocalInfoWidget.py @@ -1,4 +1,5 @@ import datetime +import html import mcrit_plugin.ui_qt.QtShim as QtShim from mcrit_plugin.core.revision import core_revision @@ -19,7 +20,6 @@ def __init__(self, parent): print("[|] loading LocalInfoWidget") self.parent = parent self.name = "LocalInfo" - self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "scan.png") self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.label_mcrit_activity_info = self.cc.QLabel("Activity Info: ") @@ -27,7 +27,11 @@ def __init__(self, parent): f"{self.cc.backend.plugin_name} v{parent.config.VERSION} started " f"(core {core_revision()})." ) - self.label_mcrit_server_info = self.cc.QLabel("MCRIT Remote server: ") + self.label_mcrit_server_info = self.cc.QLabel() + self.label_mcrit_server_info.setTextFormat(self.cc.QtCore.Qt.RichText) + self.label_mcrit_server_info.linkActivated.connect(self._onChangeServerClicked) + # the Change link is there before the first connection check answers + self._setServerInfo((parent.config.MCRIT_SERVER, "checking...", None)) self.label_remote_sample_info = self.cc.QLabel("Remote sample: ") for label in ( self.label_mcrit_activity_info, @@ -170,5 +174,9 @@ def _setServerInfo(self, payload): else "No statistics" ) self.label_mcrit_server_info.setText( - "Remote server: %s -- %s -- %s." % (mcrit_server, version_text, status_text) + 'Remote server: %s -- %s -- %s. Change...' + % (html.escape(str(mcrit_server)), html.escape(str(version_text)), status_text) ) + + def _onChangeServerClicked(self, _link): + self.parent.main_widget._onSettingsButtonClicked() diff --git a/mcrit_plugin/ui_qt/widgets/MainWidget.py b/mcrit_plugin/ui_qt/widgets/MainWidget.py index bbab001..686899f 100644 --- a/mcrit_plugin/ui_qt/widgets/MainWidget.py +++ b/mcrit_plugin/ui_qt/widgets/MainWidget.py @@ -1,6 +1,7 @@ import json import mcrit_plugin.ui_qt.QtShim as QtShim +from mcrit_plugin.ui_qt.widgets.ConnectionSettingsDialog import ConnectionSettingsDialog from mcrit_plugin.ui_qt.widgets.ResultChooserDialog import ResultChooserDialog from mcrit_plugin.ui_qt.widgets.SmdaInfoDialog import SmdaInfoDialog from mcrit_plugin.ui_qt.widgets.YaraStringBuilderDialog import YaraStringBuilderDialog @@ -27,8 +28,11 @@ def __init__(self, parent): self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.SmdaInfoDialog = SmdaInfoDialog + self.ConnectionSettingsDialog = ConnectionSettingsDialog self.ResultChooserDialog = ResultChooserDialog self.YaraStringBuilderDialog = YaraStringBuilderDialog + self._watchedSettingsWidget = None + self._reportedWaitingServer = None self._createGui() self.parent.mcrit_interface.checkConnection(async_=True) @@ -42,7 +46,7 @@ def _createGui(self): self.tabs = self.cc.QTabWidget() self.tabs.setTabsClosable(False) for widget in self.tabbed_widgets: - self.tabs.addTab(widget, widget.icon, widget.name) + self.tabs.addTab(widget, widget.name) layout = self.cc.QVBoxLayout() self.splitter = self.cc.QSplitter(self.cc.QtCore.Qt.Vertical) q_clean_style = self.cc.QStyleFactory.create("Plastique") @@ -53,6 +57,7 @@ def _createGui(self): layout.addWidget(self.splitter) self.central_widget.setLayout(layout) self.setTabFocus(self.parent.sample_widget.name) + self._applyIconTint() def _createToolbar(self): """ @@ -60,6 +65,8 @@ def _createToolbar(self): """ # TODO for MCRIT 1.0.0 release, we hide the other buttons until they are properly developed self.toolbar = self.addToolBar(self.cc.backend.plugin_name + " Toolbar") + # the hosts' styles pick different sizes; 24 is the icons' design size + self.toolbar.setIconSize(self.cc.QtCore.QSize(24, 24)) self._createParseSmdaAction() self.toolbar.addAction(self.parseSmdaAction) self._createUploadSmdaAction() @@ -70,13 +77,50 @@ def _createToolbar(self): self.toolbar.addAction(self.exportSmdaAction) self._createBuildYaraStringAction() self.toolbar.addAction(self.buildYaraStringAction) + self._createSettingsAction() + self.toolbar.addAction(self.settingsAction) + self.toolbar.widgetForAction(self.settingsAction).setToolButtonStyle( + self.cc.QtCore.Qt.ToolButtonTextBesideIcon + ) + self._toolbarIconFiles = [ + (self.parseSmdaAction, "fingerprint_scan.png"), + (self.uploadSmdaAction, "cloud-upload.png"), + (self.getMatchResultAction, "satellite_dish.png"), + (self.exportSmdaAction, "export.png"), + (self.buildYaraStringAction, "yara.png"), + (self.settingsAction, "settings.png"), + ] + + def changeEvent(self, event): + QMainWindow.changeEvent(self, event) + # a theme switch while the plugin is open changes the text color the icons are drawn in + event_types = (self.cc.QtCore.QEvent.PaletteChange, self.cc.QtCore.QEvent.StyleChange) + if event.type() in event_types and getattr(self, "tabs", None) is not None: + self._applyIconTint() + + def _applyIconTint(self): + for action, filename in self._toolbarIconFiles: + action.setIcon(self._toolbarIcon(filename)) + for index, widget in enumerate(self.tabbed_widgets): + self.tabs.setTabIcon(index, self._themedIcon(widget.icon.pixmap(48, 48))) + + def _toolbarIcon(self, filename): + return self._themedIcon(self.cc.QtGui.QPixmap(self.parent.config.ICON_FILE_PATH + filename)) + + def _themedIcon(self, pixmap): + """The black glyph in the theme's text color; Qt derives a dimmed disabled state from it, + so the icons stay visible on dark themes.""" + painter = self.cc.QPainter(pixmap) + painter.setCompositionMode(self.cc.QPainter.CompositionMode_SourceIn) + painter.fillRect(pixmap.rect(), self.palette().color(self.cc.QPalette.WindowText)) + painter.end() + return self.cc.QIcon(pixmap) def _createParseSmdaAction(self): """ Create an action for parsing the IDB into a SMDA report. """ self.parseSmdaAction = self.cc.QAction( - self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "fingerprint_scan.png"), "Convert this database to a SMDA report which can then be used to query MCRIT.", self, ) @@ -88,7 +132,6 @@ def _createUploadSmdaAction(self): TODO: will require addition of some more meta data. """ self.uploadSmdaAction = self.cc.QAction( - self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "cloud-upload.png"), "Reparse and upload the SMDA report to the MCRIT server.", self, ) @@ -100,7 +143,6 @@ def _createGetMatchResultAction(self): Create an action for requesting a MatchReport for the given remote sample """ self.getMatchResultAction = self.cc.QAction( - self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "satellite_dish.png"), "Request the MatchResult for the uploaded sample.", self, ) @@ -112,7 +154,6 @@ def _createExportSmdaAction(self): Create an action for exporting the parsed SMDA report into json/smda format. """ self.exportSmdaAction = self.cc.QAction( - self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "export.png"), "Export the SMDA report to local disk.", self, ) @@ -124,13 +165,63 @@ def _createBuildYaraStringAction(self): Create an action for building a YARA string from the current selection. """ self.buildYaraStringAction = self.cc.QAction( - self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "yara.png"), "Build a YARA string from the current selection.", self, ) self.buildYaraStringAction.setEnabled(False) self.buildYaraStringAction.triggered.connect(self._onBuildYaraStringButtonClicked) + def _createSettingsAction(self): + """ + Create an action for opening the plugin settings. + """ + self.settingsAction = self.cc.QAction("Settings", self) + self.settingsAction.setToolTip("Set the MCRIT server, API token and other plugin settings.") + self.settingsAction.triggered.connect(self._onSettingsButtonClicked) + + def _onSettingsButtonClicked(self): + widget = self.cc.backend.open_settings(self.parent.config) + if widget is None: + dialog = self.ConnectionSettingsDialog(self, self.parent.config) + if dialog.exec_(): + try: + self.cc.backend.save_settings(dialog.changedValues()) + except Exception as exc: + self.cc.backend.show_warning("Saving the MCRIT settings failed: %s" % exc) + return + self._reportWaitingServer( + self.parent.mcrit_interface.reloadSettings(recheck=True), always=True + ) + return + if widget is True: + self.parent.local_widget.updateActivityInfo( + "Settings opened; %s after changing the connection." % self.cc.backend.reopen_hint + ) + return + if widget is not self._watchedSettingsWidget: + self._watchedSettingsWidget = widget + widget.destroyed.connect(self._onSettingsClosed) + + def _onSettingsClosed(self): + self._watchedSettingsWidget = None + waiting_server = self.parent.mcrit_interface.reloadSettings() + # not modal from inside the destroyed signal of the closing settings window + self.cc.QtCore.QTimer.singleShot(0, lambda: self._reportWaitingServer(waiting_server)) + + def _reportWaitingServer(self, waiting_server, always=False): + """Warn once per waiting server when a settings window closes, every time after the form.""" + if waiting_server is None: + self._reportedWaitingServer = None + return + if not always and waiting_server == self._reportedWaitingServer: + return + self._reportedWaitingServer = waiting_server + self.cc.backend.show_warning( + "The new settings are saved. MCRIT keeps using %s for the results it already has; " + "%s to switch to %s." + % (self.parent.mcrit_interface.server, self.cc.backend.reopen_hint, waiting_server) + ) + def getLocalSmdaReport(self): local_report = None if self.parent.config.USE_SMDA_FOR_ANALYSIS: diff --git a/mcrit_plugin/ui_qt/widgets/SampleInfoWidget.py b/mcrit_plugin/ui_qt/widgets/SampleInfoWidget.py index 7acaf62..4fbf447 100644 --- a/mcrit_plugin/ui_qt/widgets/SampleInfoWidget.py +++ b/mcrit_plugin/ui_qt/widgets/SampleInfoWidget.py @@ -14,7 +14,7 @@ def __init__(self, parent): self.name = "Sample Match Summary" self.last_family_selected = None self._matching_data = None - self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "puzzle.png") + self.icon = self.cc.QIcon(self.parent.config.ICON_FILE_PATH + "chart.png") self.central_widget = self.cc.QWidget() self.setCentralWidget(self.central_widget) self.cb_filter_library = self.cc.QCheckBox("Filter out Library Matches") diff --git a/scripts/ghidra/run_gui_integration.py b/scripts/ghidra/run_gui_integration.py index 4d5c618..a5de59b 100644 --- a/scripts/ghidra/run_gui_integration.py +++ b/scripts/ghidra/run_gui_integration.py @@ -238,8 +238,17 @@ def configure(): 30, ) _check(True, "MCRIT window open and connected") - pressed_tab = gui.press_tab(main.tabs) - _wait(lambda: main.tabs.getSelectedIndex() == pressed_tab, "tab selected by a click", 10) + # a press can land before the freshly docked window takes input; a broken click fails all three + for attempt in range(3): + pressed_tab = gui.press_tab(main.tabs) + try: + _wait( + lambda: main.tabs.getSelectedIndex() == pressed_tab, "tab selected by a click", 10 + ) + break + except TimeoutError: + if attempt == 2: + raise _check(True, "clicking a tab header selects it") gui.shot("opened") @@ -302,7 +311,15 @@ def configure(): gui.edt(lambda: main.setTabFocus(function_widget.name)) target = max(report.getFunctions(), key=lambda function: function.num_instructions) gui.edt(lambda: backend.jump_to(target.offset)) - _wait(lambda: function_widget.table_function_matches.rows, "live function query", 120) + # turning live tracking on queries the function the cursor was on, so rows alone may be its + _wait( + lambda: ( + function_widget.current_function_offset == target.offset + and function_widget.table_function_matches.rows + ), + "live function query", + 120, + ) _check( session.current_function == target.offset, "cursor move reached the Function Scope", diff --git a/tests/binja/test_secrets.py b/tests/binja/test_secrets.py index fb1acf8..b8b5511 100644 --- a/tests/binja/test_secrets.py +++ b/tests/binja/test_secrets.py @@ -16,6 +16,9 @@ class _Settings: def get_string(self, key): return self.store.get(key, "") + def set_string(self, key, value): + self.store[key] = value + def reset(self, key): self.store.pop(key, None) @@ -89,3 +92,24 @@ def test_without_a_keychain_the_token_stays_in_settings(binja_config, monkeypatc assert module._get_setting("mcritweb_api_token") == "secret" assert keychain.data == {} assert _Settings.store["mcrit.mcritweb_api_token"] == "secret" + + +def test_saving_a_new_token_replaces_the_stored_one_and_moves_into_the_keychain(binja_config): + module, keychain = binja_config + keychain.data["mcrit.mcritweb_api_token"] = "old" + + module.set_setting("mcritweb_api_token", "new") + + assert keychain.data == {} + assert module._get_setting("mcritweb_api_token") == "new" + assert keychain.data == {"mcrit.mcritweb_api_token": "new"} + + +def test_saving_an_empty_token_clears_it_everywhere(binja_config): + module, keychain = binja_config + keychain.data["mcrit.mcritweb_api_token"] = "old" + + module.set_setting("mcritweb_api_token", "") + + assert keychain.data == {} + assert "mcrit.mcritweb_api_token" not in _Settings.store diff --git a/tests/core/test_config.py b/tests/core/test_config.py index 882ccfa..98db83a 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -229,3 +229,10 @@ def test_a_config_override_that_is_not_an_object_is_ignored(fresh_config, tmp_pa settings = fresh_config.McritConfig("0.0.0", {"mcrit_server": "http://store:8000/"}.__getitem__) assert settings.MCRIT_SERVER == "http://store:8000/" + + +def test_only_the_shipped_localhost_server_is_the_default(fresh_config): + settings = fresh_config.McritConfig("0.0.0") + + assert settings.isDefaultServer(settings.MCRIT_SERVER) + assert not settings.isDefaultServer("https://mcrit.example.com/") diff --git a/tests/core/test_icon_files.py b/tests/core/test_icon_files.py new file mode 100644 index 0000000..e8b3217 --- /dev/null +++ b/tests/core/test_icon_files.py @@ -0,0 +1,28 @@ +"""Every icon the plugins name exists in icons/, and every icon there is used: a missing file only +shows as a blank button or tab.""" + +import re +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +ICON_NAME = re.compile(r"""["']([\w-]+\.png)["']""") + + +def _named_icons(): + sources = list((ROOT / "mcrit_plugin").rglob("*.py")) + list( + (ROOT / "mcrit_plugin").rglob("*.json") + ) + return {name for path in sources for name in ICON_NAME.findall(path.read_text("utf-8"))} + + +def test_every_icon_the_code_names_exists(): + named = _named_icons() + + assert named + assert sorted(name for name in named if not (ROOT / "icons" / name).is_file()) == [] + + +def test_every_shipped_icon_is_used(): + shipped = {path.name for path in (ROOT / "icons").glob("*.png")} + + assert sorted(shipped - _named_icons()) == [] diff --git a/tests/core/test_mcrit_interface.py b/tests/core/test_mcrit_interface.py index 5a96e07..c55b118 100644 --- a/tests/core/test_mcrit_interface.py +++ b/tests/core/test_mcrit_interface.py @@ -42,6 +42,9 @@ def _make_interface(timeout=10): inst._mcrit_server = "http://127.0.0.1:8000" inst._cache_lock = threading.Lock() inst._remote_info_lock = threading.Lock() + inst._check_id = 0 + inst._switch_lock = threading.Lock() + inst._server_generation = 0 inst.mcrit_client = MagicMock() return inst @@ -287,3 +290,393 @@ def test_a_cfg_that_cannot_be_read_is_reported_as_a_failed_fetch(self): interface.parent.local_widget.updateActivityInfo.assert_called_once_with( "Fetching function 9 for the graph viewer failed (unexpected KeyError: 'xcfg')." ) + + +class TestReloadSettings: + @staticmethod + def _interface(server="http://127.0.0.1:8000/"): + config = SimpleNamespace( + MCRIT_SERVER=server, + MCRIT_REQUEST_TIMEOUT=10, + MCRITWEB_API_TOKEN="", + MCRITWEB_USERNAME="", + ) + parent = SimpleNamespace( + config=config, + local_widget=MagicMock(), + remote_sample_id=None, + remote_sample_entry=None, + matching_report=None, + matched_function_entries=None, + function_matches={}, + blockhash_matches={}, + sample_infos=None, + family_infos=None, + ) + interface = McritInterface(parent, backend=None) + interface.checkConnection = MagicMock() + return interface, config + + def test_unchanged_settings_keep_the_client(self): + interface, _config = self._interface() + client = interface.mcrit_client + + interface.reloadSettings() + + assert interface.mcrit_client is client + interface.checkConnection.assert_not_called() + + def test_a_changed_server_and_token_build_a_new_client_and_check_it(self): + interface, config = self._interface() + old_client = interface.mcrit_client + config.MCRIT_SERVER = "https://mcrit.example.com/" + config.MCRITWEB_API_TOKEN = "token" + + interface.reloadSettings() + + assert interface.mcrit_client is not old_client + assert interface.mcrit_client.mcrit_server == "https://mcrit.example.com/" + assert interface.mcrit_client.headers["apitoken"] == "token" + assert interface.server == "https://mcrit.example.com/" + interface.checkConnection.assert_called_once_with(async_=True) + + def test_recheck_checks_unchanged_settings_again_on_the_same_client(self): + interface, _config = self._interface() + client = interface.mcrit_client + + interface.reloadSettings(recheck=True) + + assert interface.mcrit_client is client + interface.checkConnection.assert_called_once_with(async_=True) + + @pytest.mark.parametrize( + "field, value", + [ + ("remote_sample_id", 23), + ("remote_sample_entry", object()), + ("matching_report", object()), + ("matched_function_entries", {7: object()}), + ("function_matches", {0x401000: {}}), + ("blockhash_matches", {1: []}), + ("sample_infos", {}), + ("family_infos", {}), + ], + ) + def test_a_new_server_waits_for_a_reopen_while_results_of_the_old_one_are_held( + self, field, value + ): + interface, config = self._interface() + client = interface.mcrit_client + setattr(interface.parent, field, value) + config.MCRIT_SERVER = "https://mcrit.example.com/" + + assert interface.reloadSettings() == "https://mcrit.example.com/" + + assert interface.mcrit_client is client + assert interface.server == "http://127.0.0.1:8000/" + interface.checkConnection.assert_not_called() + + def test_the_old_connection_stays_whole_while_the_new_server_waits(self): + interface, config = self._interface() + interface.parent.remote_sample_id = 23 + config.MCRIT_SERVER = "https://mcrit.example.com/" + config.MCRITWEB_API_TOKEN = "token for the new server" + config.MCRIT_REQUEST_TIMEOUT = 30 + + assert interface.reloadSettings() == "https://mcrit.example.com/" + + assert interface.server == "http://127.0.0.1:8000/" + assert "apitoken" not in interface.mcrit_client.headers + assert interface.mcrit_client.timeout == 30 + + +def test_a_check_that_answers_after_the_settings_changed_is_dropped(): + config = SimpleNamespace( + MCRIT_SERVER="http://old:8000/", + MCRIT_REQUEST_TIMEOUT=10, + MCRITWEB_API_TOKEN="", + MCRITWEB_USERNAME="", + isDefaultServer=lambda server: False, + ) + local_widget = MagicMock() + interface = McritInterface(SimpleNamespace(config=config, local_widget=local_widget), None) + old_client = interface.mcrit_client + started = threading.Event() + release = threading.Event() + applied = threading.Event() + + class _Backend: + @staticmethod + def run_on_ui_thread(func): + func() + applied.set() + + interface.backend = _Backend() + + def slow_version(): + started.set() + release.wait(5) + return "1.0.0" + + old_client.getVersion = slow_version + threads_before = set(threading.enumerate()) + interface.checkConnection(async_=True) + assert started.wait(5) + interface._buildClient(("http://new:8000/", 10, "", "")) + release.set() + for thread in set(threading.enumerate()) - threads_before: + thread.join(5) + + assert applied.is_set() + assert not any( + "successful" in call.args[0] for call in local_widget.updateActivityInfo.call_args_list + ) + + +def test_an_older_check_on_the_same_client_does_not_overwrite_a_newer_one(): + interface = _make_interface() + interface.config.isDefaultServer = lambda server: False + pending = [] + interface.backend = SimpleNamespace(run_on_ui_thread=lambda func: pending.append(func)) + interface.mcrit_client.getVersion.side_effect = [ + requests.exceptions.Timeout("slow"), + "1.15.0", + ] + threads_before = set(threading.enumerate()) + + interface.checkConnection(async_=True) + for thread in set(threading.enumerate()) - threads_before: + thread.join(5) + interface.checkConnection(async_=True) + for thread in set(threading.enumerate()) - threads_before: + thread.join(5) + for apply in reversed(pending): + apply() + + messages = [ + call.args[0] for call in interface.parent.local_widget.updateActivityInfo.call_args_list + ] + assert messages[-1] == "Connection check successful!" + assert not any("timed out" in message for message in messages) + + +_SMDA_REPORT = SimpleNamespace(getFunctions=lambda: [SimpleNamespace(offset=0x401000)]) + + +def _answers_after(interface, request_name, answer, settings): + """Let the request answer only after the session switched to other connection settings.""" + + def answer_after_the_switch(*_args, **_kwargs): + interface._buildClient(settings) + return answer + + getattr(interface.mcrit_client, request_name).side_effect = answer_after_the_switch + + +_STORING_REQUESTS = [ + ( + "querySampleSha256", + ("abc",), + "getSampleBySha256", + SimpleNamespace(sample_id=5), + "remote_sample_id", + ), + ( + "uploadReport", + (object(),), + "addReport", + (SimpleNamespace(sample_id=5), None), + "remote_sample_id", + ), + ("getMatchingJobById", ("job",), "getResultForJob", {"matches": {}}, "matching_job_id"), + ("queryAllFamilyEntries", (), "getFamilies", {1: "family"}, "family_infos"), + ("queryAllSampleEntries", (), "getSamples", {1: "sample"}, "sample_infos"), + ( + "querySmdaFunctionMatches", + (_SMDA_REPORT,), + "getMatchesForSmdaFunction", + {"x": 1}, + "function_matches", + ), + ( + "queryFunctionEntriesById", + ([7],), + "getFunctionsByIds", + {7: "entry"}, + "matched_function_entries", + ), +] + + +@pytest.fixture +def decoded_results(monkeypatch): + """Any answer decodes, so the tests reach the store instead of a decoding error.""" + import mcrit_plugin.core.McritInterface as module + + monkeypatch.setattr( + module.MatchingResult, + "fromDict", + staticmethod(lambda entry: SimpleNamespace(function_matches=[])), + ) + + +def _empty_session(interface): + for field in ( + "remote_sample_id", + "remote_sample_entry", + "matching_job_id", + "family_infos", + "sample_infos", + "matched_function_entries", + ): + setattr(interface.parent, field, None) + interface.parent.function_matches = {} + + +@pytest.mark.parametrize("method, args, request_name, answer, field", _STORING_REQUESTS) +def test_an_answer_from_the_previous_server_is_not_stored( + decoded_results, method, args, request_name, answer, field +): + interface = _make_interface() + _empty_session(interface) + _answers_after(interface, request_name, answer, ("http://other:8000/", 10, "", "")) + + getattr(interface, method)(*args) + + assert not getattr(interface.parent, field) + + +@pytest.mark.parametrize( + "method, args, request_name", + [ + ("getMatchesForPicBlockHash", (0x1234,), "getMatchesForPicBlockHash"), + ("queryRemoteFunction", (7,), "getFunctionById"), + ], +) +def test_answers_handed_to_the_widgets_from_the_previous_server_count_as_failed( + method, args, request_name +): + interface = _make_interface() + interface.parent.sample_infos = None + _answers_after(interface, request_name, MagicMock(), ("http://other/", 10, "", "")) + + assert getattr(interface, method)(*args) is None + + +class _LockCheckedSession(SimpleNamespace): + """A session whose fields may only change while the interface holds its switch lock.""" + + def __setattr__(self, name, value): + lock = self.__dict__.get("switch_lock") + assert lock is None or name.startswith("_") or lock.locked(), name + super().__setattr__(name, value) + + +class _LockCheckedDict(dict): + def __init__(self, lock): + super().__init__() + self.lock = lock + + def __setitem__(self, key, value): + assert self.lock.locked() + super().__setitem__(key, value) + + def update(self, *args, **kwargs): + assert self.lock.locked() + super().update(*args, **kwargs) + + +@pytest.mark.parametrize("method, args, request_name, answer, field", _STORING_REQUESTS) +def test_answers_are_stored_under_the_switch_lock( + decoded_results, method, args, request_name, answer, field +): + interface = _make_interface() + _empty_session(interface) + session = _LockCheckedSession(**vars(interface.parent)) + session.function_matches = _LockCheckedDict(interface._switch_lock) + session.function_id_to_offset = _LockCheckedDict(interface._switch_lock) + session.switch_lock = interface._switch_lock + interface.parent = session + getattr(interface.mcrit_client, request_name).return_value = answer + + getattr(interface, method)(*args) + + assert getattr(session, field) + + +def test_function_offsets_from_a_match_are_stored_under_the_switch_lock(monkeypatch): + import mcrit_plugin.core.McritInterface as module + + match = SimpleNamespace(matched_function_id=7) + monkeypatch.setattr( + module.MatchingResult, + "fromDict", + staticmethod(lambda entry: SimpleNamespace(function_matches=[match])), + ) + interface = _make_interface() + _empty_session(interface) + session = _LockCheckedSession(**vars(interface.parent)) + session.function_matches = _LockCheckedDict(interface._switch_lock) + session.function_id_to_offset = _LockCheckedDict(interface._switch_lock) + session.switch_lock = interface._switch_lock + interface.parent = session + interface.mcrit_client.getMatchesForSmdaFunction.return_value = {"x": 1} + interface.mcrit_client.getFunctionsByIds.return_value = {7: SimpleNamespace(offset=0x10)} + + interface.querySmdaFunctionMatches(_SMDA_REPORT) + + assert session.function_id_to_offset == {7: 0x10} + + +def test_the_switch_checks_for_held_data_and_swaps_the_client_under_the_lock(): + interface = _make_interface() + _empty_session(interface) + interface.parent.blockhash_matches = {} + interface.parent.matching_report = None + interface._connection_settings = (interface.server, 10, "", "") + seen = [] + holds = interface._holdsServerData + interface._holdsServerData = lambda: seen.append(interface._switch_lock.locked()) or holds() + build = interface._buildClient + interface._buildClient = lambda settings: ( + seen.append(interface._switch_lock.locked()) or build(settings) + ) + interface.config.MCRIT_SERVER = "http://other:8000/" + interface.config.MCRITWEB_API_TOKEN = interface.config.MCRITWEB_USERNAME = "" + interface.checkConnection = MagicMock() + + interface.reloadSettings() + + assert seen == [True, True] + assert interface.server == "http://other:8000/" + + +@pytest.mark.parametrize("method, args, request_name, answer, field", _STORING_REQUESTS) +def test_an_answer_from_the_same_server_is_kept_after_a_token_change( + decoded_results, method, args, request_name, answer, field +): + interface = _make_interface() + _empty_session(interface) + _answers_after(interface, request_name, answer, (interface.server, 10, "new token", "")) + + getattr(interface, method)(*args) + + assert getattr(interface.parent, field) + + +@pytest.mark.parametrize("server_is_default, hinted", [(True, True), (False, False)]) +def test_a_failed_check_points_at_settings_only_while_no_server_is_set(server_is_default, hinted): + interface = _make_interface() + # the session's server counts, not a new one waiting in the settings for a reopen + interface.config.isDefaultServer = lambda server: ( + server_is_default and server == interface.server + ) + interface.config.MCRIT_SERVER = "http://waiting:8000/" + interface.backend = SimpleNamespace(settings_location="Edit > Tool Options > MCRIT") + interface.mcrit_client.getVersion.side_effect = requests.exceptions.ConnectionError("refused") + + interface.checkConnection() + + message = interface.parent.local_widget.updateActivityInfo.call_args_list[-1].args[0] + assert message.startswith("Connection check failed (server unreachable).") + assert ("use Edit > Tool Options > MCRIT to change it" in message) == hinted diff --git a/tests/ida/test_ida_backend.py b/tests/ida/test_ida_backend.py index b613ed4..12024a9 100644 --- a/tests/ida/test_ida_backend.py +++ b/tests/ida/test_ida_backend.py @@ -44,7 +44,7 @@ def backend(monkeypatch): register_timer=_no_ida_timers, waker=waker, ) - for name in ("ida_bytes", "ida_funcs", "ida_idaapi", "ida_nalt", "ida_undo", "idc"): + for name in ("ida_bytes", "ida_expr", "ida_funcs", "ida_idaapi", "ida_nalt", "ida_undo", "idc"): monkeypatch.setitem(sys.modules, name, types.ModuleType(name)) monkeypatch.setitem(sys.modules, "ida_kernwin", kernwin) monkeypatch.setitem(sys.modules, "ida_hexrays", None) @@ -209,6 +209,7 @@ def test_the_waker_runs_its_callback_on_the_thread_that_created_it(monkeypatch): QtWidgets = pytest.importorskip("PySide6.QtWidgets") for name in ( "ida_bytes", + "ida_expr", "ida_funcs", "ida_idaapi", "ida_kernwin", @@ -389,3 +390,60 @@ def test_an_import_by_ordinal_without_a_name_is_named_by_its_ordinal(fast_interf 0x1008: "WS2_32!connect", 0x1010: "#7", } + + +def test_open_settings_opens_the_settings_editor_on_mcrit_ida(backend, monkeypatch): + instance, kernwin = backend + module = sys.modules["mcrit_plugin.ida.IdaBackend"] + monkeypatch.setattr(module.ida_expr, "find_idc_func", lambda prefix, n=0: prefix, raising=False) + eval_idc = MagicMock() + monkeypatch.setattr(module.idc, "eval_idc", eval_idc, raising=False) + editor, qt_widget = object(), object() + kernwin.find_widget = MagicMock(return_value=editor) + to_qt = MagicMock(return_value=qt_widget) + kernwin.PluginForm = types.SimpleNamespace(FormToPyQtWidget=to_qt) + + assert instance.open_settings(MagicMock()) is qt_widget + + eval_idc.assert_called_once_with('ida_settings_show_plugin_settings("mcrit-ida")') + kernwin.find_widget.assert_called_once_with("Plugin Settings Manager") + to_qt.assert_called_once_with(editor) + + +def test_an_editor_window_that_cannot_be_found_still_counts_as_opened(backend, monkeypatch): + instance, kernwin = backend + module = sys.modules["mcrit_plugin.ida.IdaBackend"] + monkeypatch.setattr(module.ida_expr, "find_idc_func", lambda prefix, n=0: prefix, raising=False) + monkeypatch.setattr(module.idc, "eval_idc", MagicMock(), raising=False) + kernwin.find_widget = MagicMock(return_value=None) + + assert instance.open_settings(MagicMock()) is True + + +def test_open_settings_without_the_editor_leaves_it_to_the_plugin_form(backend, monkeypatch): + instance, _kernwin = backend + module = sys.modules["mcrit_plugin.ida.IdaBackend"] + monkeypatch.setattr(module.ida_expr, "find_idc_func", lambda prefix, n=0: "", raising=False) + eval_idc = MagicMock() + monkeypatch.setattr(module.idc, "eval_idc", eval_idc, raising=False) + + assert instance.open_settings(MagicMock()) is None + eval_idc.assert_not_called() + + +def test_save_settings_stores_strings_through_ida_settings(backend, monkeypatch): + instance, _kernwin = backend + ida_settings = types.SimpleNamespace(set_plugin_setting=MagicMock()) + monkeypatch.setitem(sys.modules, "ida_settings", ida_settings) + # recorded first, so teardown also drops the copy imported against the fake ida_settings + monkeypatch.setitem(sys.modules, "mcrit_plugin.ida.config", None) + monkeypatch.delitem(sys.modules, "mcrit_plugin.ida.config") + + instance.save_settings( + {"mcrit_server": "https://mcrit.example.com/", "mcrit_request_timeout": 30} + ) + + ida_settings.set_plugin_setting.assert_any_call( + "mcrit-ida", "mcrit_server", "https://mcrit.example.com/" + ) + ida_settings.set_plugin_setting.assert_any_call("mcrit-ida", "mcrit_request_timeout", "30") diff --git a/tests/ida/test_plugin_entry.py b/tests/ida/test_plugin_entry.py index 01bce4f..4d75ae6 100644 --- a/tests/ida/test_plugin_entry.py +++ b/tests/ida/test_plugin_entry.py @@ -173,3 +173,52 @@ def test_an_event_from_another_view_keeps_the_disassembly_move(monkeypatch): disassembly, hex_view, ] + + +def test_the_settings_menu_entry_opens_the_current_database_s_settings(monkeypatch): + """Edit > Plugins > MCRIT4IDA Settings... opens the settings of the database open now; a + previous database's plugmod going away late leaves the entry in place.""" + actions = {} + + class _ActionDesc: + def __init__(self, name, label, handler, shortcut, tooltip, icon): + self.name, self.handler = name, handler + + kernwin = types.SimpleNamespace( + PluginForm=_PluginForm, + is_idaq=lambda: True, + View_Hooks=_ViewHooks, + action_handler_t=object, + action_desc_t=_ActionDesc, + AST_ENABLE_ALWAYS=0, + SETMENU_APP=1, + # like IDA, registering an action name a second time fails + register_action=lambda desc: ( + desc.name not in actions and actions.setdefault(desc.name, desc.handler) is not None + ), + attach_action_to_menu=MagicMock(), + detach_action_from_menu=MagicMock(), + ) + monkeypatch.setitem( + sys.modules, + "ida_idaapi", + types.SimpleNamespace(plugin_t=_PluginT, plugmod_t=_PlugmodT, PLUGIN_MULTI=1), + ) + monkeypatch.setitem(sys.modules, "ida_kernwin", kernwin) + monkeypatch.delitem(sys.modules, "mcrit_plugin.ida.ida_mcrit", raising=False) + module = importlib.import_module("mcrit_plugin.ida.ida_mcrit") + form = types.SimpleNamespace( + released=False, Show=lambda: True, release=lambda: None, main_widget=MagicMock() + ) + monkeypatch.setattr(module, "show_mcrit_form", lambda: form) + + previous = module.Mcrit4IdaPlugmod() + plugmod = module.Mcrit4IdaPlugmod() + del previous + kernwin.attach_action_to_menu.assert_called_once_with( + "Edit/Plugins/", module.SETTINGS_ACTION, kernwin.SETMENU_APP + ) + + actions[module.SETTINGS_ACTION].activate(None) + assert plugmod.form is form + form.main_widget._onSettingsButtonClicked.assert_called_once_with() diff --git a/tests/ui_qt/test_block_lookups.py b/tests/ui_qt/test_block_lookups.py index 5555245..9308841 100644 --- a/tests/ui_qt/test_block_lookups.py +++ b/tests/ui_qt/test_block_lookups.py @@ -2,7 +2,9 @@ import os import threading +from contextlib import contextmanager from types import SimpleNamespace +from unittest.mock import MagicMock import pytest @@ -10,15 +12,20 @@ pytest.importorskip("PySide6.QtWidgets") import mcrit_plugin.ui_qt.widgets.BlockMatchWidget as block_match_module # noqa: E402 +from mcrit_plugin.core.McritInterface import McritInterface # noqa: E402 from mcrit_plugin.ui_qt.widgets.BlockMatchWidget import BlockMatchWidget # noqa: E402 def _widget(lookup, cached=None): - widget = BlockMatchWidget.__new__(BlockMatchWidget) - widget.parent = SimpleNamespace( - blockhash_matches=dict(cached or {}), - mcrit_interface=SimpleNamespace(getMatchesForPicBlockHash=lookup), + interface = McritInterface.__new__(McritInterface) + interface.parent = SimpleNamespace( + blockhash_matches=dict(cached or {}), local_widget=MagicMock(), mcrit_interface=interface ) + interface._switch_lock = threading.Lock() + interface._server_generation = 0 + interface.mcrit_client = SimpleNamespace(getMatchesForPicBlockHash=lookup) + widget = BlockMatchWidget.__new__(BlockMatchWidget) + widget.parent = interface.parent return widget @@ -83,3 +90,45 @@ def lookup(block_hash): assert queried == [1, 2] assert widget.parent.blockhash_matches == {1: [1], 2: [2]}, "skipped hashes stay uncached" + + +def test_block_matches_from_the_previous_server_are_not_cached(): + widget = _widget(lambda block_hash: [block_hash]) + interface = widget.parent.mcrit_interface + storing = interface._storing + + @contextmanager + def switch_first(generation): + # the server changes after every lookup passed its own check, before the cache write + interface._server_generation += 1 + with storing(generation) as current: + yield current + + interface._storing = switch_first + + widget._lookupBlockHashes([1, 2]) + + assert widget.parent.blockhash_matches == {} + + +class _LockCheckedDict(dict): + def __init__(self, lock): + super().__init__() + self.lock = lock + + def __setitem__(self, key, value): + assert self.lock.locked() + super().__setitem__(key, value) + + def update(self, *args, **kwargs): + assert self.lock.locked() + super().update(*args, **kwargs) + + +def test_block_matches_are_cached_under_the_switch_lock(): + widget = _widget(lambda block_hash: [block_hash]) + widget.parent.blockhash_matches = _LockCheckedDict(widget.parent.mcrit_interface._switch_lock) + + widget._lookupBlockHashes([1, 2]) + + assert widget.parent.blockhash_matches == {1: [1], 2: [2]} diff --git a/tests/ui_qt/test_settings_flow.py b/tests/ui_qt/test_settings_flow.py new file mode 100644 index 0000000..f86fcad --- /dev/null +++ b/tests/ui_qt/test_settings_flow.py @@ -0,0 +1,287 @@ +"""The Settings button opens the disassembler's own settings, or the plugin's connection form, and +a changed connection is picked up without reopening the plugin.""" + +import os +import types +from types import SimpleNamespace +from unittest.mock import MagicMock + +import pytest + +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") +QtWidgets = pytest.importorskip("PySide6.QtWidgets") + +import mcrit_plugin.ui_qt.QtShim as QtShim # noqa: E402 +from mcrit_plugin.ui_qt.ClassCollection import ClassCollection # noqa: E402 +from mcrit_plugin.ui_qt.widgets.ConnectionSettingsDialog import ( # noqa: E402 + ConnectionSettingsDialog, +) +from mcrit_plugin.ui_qt.widgets.MainWidget import MainWidget # noqa: E402 + +_ICON_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "icons") + os.sep + + +@pytest.fixture(scope="module") +def app(): + return QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) + + +def _config(**overrides): + values = dict( + MCRIT_SERVER="http://127.0.0.1:8000/", + MCRITWEB_API_TOKEN="", + MCRITWEB_USERNAME="", + MCRIT_REQUEST_TIMEOUT=10, + ) + values.update(overrides) + config = SimpleNamespace(**values) + config.overriddenSettings = lambda: set() + return config + + +def _dialog(app, config, hint=""): + host = QtWidgets.QWidget() + host.cc = ClassCollection(QtShim, SimpleNamespace(settings_hint=hint)) + return ConnectionSettingsDialog(host, config), host + + +def test_the_form_starts_from_the_current_settings(app): + config = _config(MCRITWEB_API_TOKEN="secret", MCRIT_REQUEST_TIMEOUT=30) + dialog, _host = _dialog(app, config) + + assert dialog.values() == { + "mcrit_server": "http://127.0.0.1:8000/", + "mcritweb_api_token": "secret", + "mcritweb_username": "", + "mcrit_request_timeout": 30, + } + assert dialog.edit_token.echoMode() == QtWidgets.QLineEdit.Password + dialog.cb_show_token.setChecked(True) + assert dialog.edit_token.echoMode() == QtWidgets.QLineEdit.Normal + + +@pytest.mark.parametrize( + "server, accepted", + [ + ("mcrit.example.com", False), + ("http://", False), + ("http://[::1", False), + ("http://:8000", False), + ("http://mcrit:abc/", False), + ("http://mcrit:99999/", False), + ("http://[::1]:8000/", True), + ("HTTPS://mcrit.example.com/api", True), + (" https://mcrit.example.com/api ", True), + ], +) +def test_the_form_accepts_only_an_http_url_with_a_host(app, server, accepted): + dialog, _host = _dialog(app, _config()) + dialog.edit_server.setText(server) + + dialog.accept() + + assert (dialog.result() == QtWidgets.QDialog.Accepted) == accepted + assert dialog.label_error.isHidden() == accepted + + +def test_only_the_changed_fields_are_saved(app): + dialog, _host = _dialog(app, _config(MCRITWEB_API_TOKEN="secret")) + assert dialog.changedValues() == {} + + dialog.edit_server.setText("https://mcrit.example.com/") + + assert dialog.changedValues() == {"mcrit_server": "https://mcrit.example.com/"} + + +def test_fields_forced_by_config_override_are_greyed_out(app): + config = _config() + config.overriddenSettings = lambda: {"mcrit_server", "blocks_min_size"} + dialog, _host = _dialog(app, config, hint="other settings live elsewhere") + + texts = [label.text() for label in dialog.findChildren(QtWidgets.QLabel)] + + assert not dialog.edit_server.isEnabled() + assert dialog.edit_token.isEnabled() + assert any("config_override.json" in text for text in texts) + assert "other settings live elsewhere" in texts + + +def _main_widget(opened, waiting_server=None): + backend = SimpleNamespace( + open_settings=MagicMock(return_value=opened), + save_settings=MagicMock(), + show_warning=MagicMock(), + reopen_hint="reopen MCRIT", + ) + interface = SimpleNamespace( + reloadSettings=MagicMock(return_value=waiting_server), + server="http://old:8000/", + ) + widget = SimpleNamespace( + cc=SimpleNamespace(backend=backend, QtCore=QtShim.get_QtCore()), + parent=SimpleNamespace( + config=_config(), mcrit_interface=interface, local_widget=MagicMock() + ), + _watchedSettingsWidget=None, + _reportedWaitingServer=None, + ) + for name in ("_onSettingsButtonClicked", "_onSettingsClosed", "_reportWaitingServer"): + setattr(widget, name, types.MethodType(getattr(MainWidget, name), widget)) + return widget, backend, interface + + +@pytest.mark.parametrize("accepted", [True, False]) +def test_without_a_settings_ui_the_form_saves_and_reconnects_only_when_accepted(accepted): + widget, backend, interface = _main_widget(opened=None) + values = {"mcrit_server": "https://mcrit.example.com/"} + widget.ConnectionSettingsDialog = lambda parent, config: SimpleNamespace( + exec_=lambda: accepted, changedValues=lambda: values + ) + + widget._onSettingsButtonClicked() + + if accepted: + backend.save_settings.assert_called_once_with(values) + interface.reloadSettings.assert_called_once_with(recheck=True) + else: + backend.save_settings.assert_not_called() + interface.reloadSettings.assert_not_called() + + +def test_closing_the_opened_settings_reloads_them_once(app): + settings_ui = QtWidgets.QWidget() + widget, backend, interface = _main_widget(opened=settings_ui) + + widget._onSettingsButtonClicked() + widget._onSettingsButtonClicked() + settings_ui.deleteLater() + app.sendPostedEvents(None, 52) # QEvent.DeferredDelete + app.processEvents() + + interface.reloadSettings.assert_called_once_with() + assert widget._watchedSettingsWidget is None + backend.show_warning.assert_not_called() + + +def test_a_server_that_waits_for_a_reopen_is_reported_once_after_closing_settings(app): + widget, backend, interface = _main_widget(opened=None, waiting_server="https://new/") + + widget._onSettingsClosed() + backend.show_warning.assert_not_called() + app.processEvents() + widget._onSettingsClosed() + app.processEvents() + assert backend.show_warning.call_count == 1 + + # switched back, then to the new server again: that is worth another warning + interface.reloadSettings.return_value = None + widget._onSettingsClosed() + app.processEvents() + interface.reloadSettings.return_value = "https://new/" + widget._onSettingsClosed() + app.processEvents() + assert backend.show_warning.call_count == 2 + + +def test_a_server_that_waits_for_a_reopen_is_reported_after_the_form(app): + widget, backend, _interface = _main_widget(opened=None, waiting_server="https://new/") + widget.ConnectionSettingsDialog = lambda parent, config: SimpleNamespace( + exec_=lambda: True, changedValues=lambda: {"mcrit_server": "https://new/"} + ) + + widget._onSettingsButtonClicked() + + message = backend.show_warning.call_args.args[0] + assert "keeps using http://old:8000/" in message + assert "reopen MCRIT to switch to https://new/" in message + + +def test_a_failed_save_warns_and_keeps_the_connection(app): + widget, backend, interface = _main_widget(opened=None) + backend.save_settings.side_effect = RuntimeError("mcrit-ida is not installed") + widget.ConnectionSettingsDialog = lambda parent, config: SimpleNamespace( + exec_=lambda: True, changedValues=lambda: {"mcrit_server": "https://x/"} + ) + + widget._onSettingsButtonClicked() + + assert "mcrit-ida is not installed" in backend.show_warning.call_args.args[0] + interface.reloadSettings.assert_not_called() + + +def test_settings_opened_without_a_widget_to_watch_say_how_to_apply_them(): + widget, backend, interface = _main_widget(opened=True) + widget.ConnectionSettingsDialog = MagicMock() + + widget._onSettingsButtonClicked() + + widget.ConnectionSettingsDialog.assert_not_called() + assert widget._watchedSettingsWidget is None + message = widget.parent.local_widget.updateActivityInfo.call_args.args[0] + assert "reopen MCRIT after changing the connection" in message + + +def test_toolbar_icons_take_the_theme_text_color_and_keep_their_shape(app): + host = QtWidgets.QWidget() + palette = host.palette() + palette.setColor(QtShim.get_QPalette().WindowText, QtShim.get_QtGui().QColor(230, 230, 230)) + host.setPalette(palette) + host.cc = ClassCollection(QtShim, None) + host.parent = SimpleNamespace(config=SimpleNamespace(ICON_FILE_PATH=_ICON_PATH)) + host._themedIcon = types.MethodType(MainWidget._themedIcon, host) + + source = QtShim.get_QtGui().QImage(_ICON_PATH + "settings.png") + size, row = source.width(), source.height() * 7 // 24 # the upper slider line + icon = MainWidget._toolbarIcon(host, "settings.png") + image = icon.pixmap(size, size).toImage() + + opaque_x = next(x for x in range(size) if source.pixelColor(x, row).alpha() == 255) + assert image.pixelColor(opaque_x, row).red() == 230 + assert [image.pixelColor(x, row).alpha() for x in range(size)] == [ + source.pixelColor(x, row).alpha() for x in range(size) + ] + + +def test_a_theme_change_tints_the_icons_again(app): + methods = ("changeEvent", "_applyIconTint", "_toolbarIcon", "_themedIcon") + host_class = type( + "Host", (QtWidgets.QMainWindow,), {name: getattr(MainWidget, name) for name in methods} + ) + host = host_class() + host.cc = ClassCollection(QtShim, None) + host.parent = SimpleNamespace(config=SimpleNamespace(ICON_FILE_PATH=_ICON_PATH)) + action = QtShim.get_QtGui().QAction("Settings", host) + host._toolbarIconFiles = [(action, "settings.png")] + host.tabbed_widgets = [] + host.tabs = QtWidgets.QTabWidget() + source = QtShim.get_QtGui().QImage(_ICON_PATH + "settings.png") + size = source.width() + opaque = next( + (x, y) for y in range(size) for x in range(size) if source.pixelColor(x, y).alpha() == 255 + ) + + for gray in (30, 230): + palette = host.palette() + palette.setColor( + QtShim.get_QPalette().WindowText, QtShim.get_QtGui().QColor(gray, gray, gray) + ) + host.setPalette(palette) + assert action.icon().pixmap(size, size).toImage().pixelColor(*opaque).red() == gray + + +def test_the_server_line_escapes_what_the_server_sends_and_offers_the_change_link(app): + from mcrit_plugin.ui_qt.widgets.LocalInfoWidget import LocalInfoWidget + + session = SimpleNamespace( + cc=ClassCollection(QtShim, SimpleNamespace(plugin_name="MCRIT4Test")), + config=SimpleNamespace(ICON_FILE_PATH=_ICON_PATH, VERSION="0", MCRIT_SERVER="http://s/"), + main_widget=MagicMock(), + ) + widget = LocalInfoWidget(session) + assert "checking..." in widget.label_mcrit_server_info.text() + + widget._setServerInfo(("http://s/", "1.0", None)) + widget.label_mcrit_server_info.linkActivated.emit("settings") + + assert "<b>1.0</b>" in widget.label_mcrit_server_info.text() + session.main_widget._onSettingsButtonClicked.assert_called_once_with() diff --git a/tests/ui_qt/test_stale_answers.py b/tests/ui_qt/test_stale_answers.py index 8963fb6..da63181 100644 --- a/tests/ui_qt/test_stale_answers.py +++ b/tests/ui_qt/test_stale_answers.py @@ -46,7 +46,11 @@ def test_block_scope_shows_only_the_latest_block_size(cc, monkeypatch): family_infos=None, sample_infos=None, blockhash_matches={}, - mcrit_interface=SimpleNamespace(getMatchesForPicBlockHash=lambda block_hash: []), + mcrit_interface=SimpleNamespace( + _pinConnection=lambda: (None, 0), + getMatchesForPicBlockHash=lambda block_hash, connection: [], + storeBlockMatches=lambda found, connection: None, + ), ) widget = BlockMatchWidgetModule.BlockMatchWidget(session) monkeypatch.setattr( @@ -82,7 +86,11 @@ def test_block_lookups_stop_once_the_form_closed(cc, monkeypatch): family_infos=None, sample_infos=None, blockhash_matches={}, - mcrit_interface=SimpleNamespace(getMatchesForPicBlockHash=queried.append), + mcrit_interface=SimpleNamespace( + _pinConnection=lambda: (None, 0), + getMatchesForPicBlockHash=lambda block_hash, connection: queried.append(block_hash), + storeBlockMatches=lambda found, connection: None, + ), ) widget = BlockMatchWidgetModule.BlockMatchWidget(session) monkeypatch.setattr(