Refactor is_packet_capture_supported() by expect_throws()

Test: atest NetworkStaticLibHostPythonTests
Change-Id: Ibbc249e3d6ef182207ba3fe37c47bab7e099f2b0
This commit is contained in:
Jimi Chen 2024-10-15 07:09:29 +00:00
commit 13a9f53d98

View file

@ -182,22 +182,23 @@ def is_packet_capture_supported(
ad: android_device.AndroidDevice,
) -> bool:
# Invoke the shell command with empty argument and see how NetworkStack respond.
# If supported, an IllegalArgumentException with help page will be printed.
functions_with_args = (
# list all functions and args with (func, *args) tuple
(start_capture_packets, (ad, "")),
(stop_capture_packets, (ad, "")),
(get_matched_packet_counts, (ad, "", ""))
)
for func, args in functions_with_args:
try:
func(*args)
except UnsupportedOperationException:
return False
except Exception:
continue
try:
# Invoke the shell command with empty argument and see how NetworkStack respond.
# If supported, an IllegalArgumentException with help page will be printed.
assert_utils.expect_throws(
lambda: start_capture_packets(ad, ""),
assert_utils.UnexpectedBehaviorError
)
assert_utils.expect_throws(
lambda: stop_capture_packets(ad, ""),
assert_utils.UnexpectedBehaviorError
)
assert_utils.expect_throws(
lambda: get_matched_packet_counts(ad, "", ""),
assert_utils.UnexpectedBehaviorError
)
except assert_utils.UnexpectedExceptionError:
return False
# If no UnsupportOperationException is thrown, regard it as supported
return True