diff mbox series

[v2] test_ap_ciphers: unify IPv6 disabling

Message ID 1588848472-I52a56327f5cd4e4331c1cc5f6342a74879342c18@changeid
State Accepted
Headers show
Series [v2] test_ap_ciphers: unify IPv6 disabling | expand

Commit Message

Johannes Berg May 7, 2020, 10:47 a.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

Add a decorator that disables IPv6 and requires running
under a VM, rather than open-coding that many times.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
v2:
 * don't accidentally drop ap_wpa2_delayed_m1_m3_retransmission2
---
 tests/hwsim/test_ap_ciphers.py | 88 ++++------------------------------
 tests/hwsim/utils.py           | 26 ++++++++++
 2 files changed, 35 insertions(+), 79 deletions(-)

Comments

Jouni Malinen May 16, 2020, 10:51 p.m. UTC | #1
On Thu, May 07, 2020 at 12:47:52PM +0200, Johannes Berg wrote:
> Add a decorator that disables IPv6 and requires running
> under a VM, rather than open-coding that many times.

Thanks, applied.

> diff --git a/tests/hwsim/utils.py b/tests/hwsim/utils.py
> +    # we need the name set right for selecting / printing etc.
> +    wrapper.__name__ = fn.__name__
> +    # reparent to the right module for module filtering
> +    wrapper.__module__ = fn.__module__
> +    return wrapper

__doc__ needs to be copied as well for the short test case description.
diff mbox series

Patch

diff --git a/tests/hwsim/test_ap_ciphers.py b/tests/hwsim/test_ap_ciphers.py
index d43831bc776a..44b84f6d7e66 100644
--- a/tests/hwsim/test_ap_ciphers.py
+++ b/tests/hwsim/test_ap_ciphers.py
@@ -663,24 +663,9 @@  def run_ap_cipher_replay_protection_sta(dev, apdev, cipher, keytype=KT_PTK):
         if replays < 1:
             raise Exception("Replays not reported")
 
+@disable_ipv6
 def test_ap_wpa2_delayed_m3_retransmission(dev, apdev):
     """Delayed M3 retransmission"""
-    require_under_vm()
-    try:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        run_ap_wpa2_delayed_m3_retransmission(dev, apdev)
-    finally:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-
-def run_ap_wpa2_delayed_m3_retransmission(dev, apdev):
     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
     hapd = hostapd.add_ap(apdev[0], params)
 
@@ -726,42 +711,17 @@  def run_ap_wpa2_delayed_m3_retransmission(dev, apdev):
         if a < b:
             raise Exception("GTK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
 
+@disable_ipv6
 def test_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev):
     """Delayed M1+M3 retransmission"""
-    require_under_vm()
-    try:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev)
-    finally:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
+    run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev, False)
 
+@disable_ipv6
 def test_ap_wpa2_delayed_m1_m3_retransmission2(dev, apdev):
     """Delayed M1+M3 retransmission (change M1 ANonce)"""
-    require_under_vm()
-    try:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev, True)
-    finally:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-
-def run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev,
-                                             change_m1_anonce=False):
+    run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev, True)
+
+def run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev, change_m1_anonce=False):
     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
     hapd = hostapd.add_ap(apdev[0], params)
 
@@ -812,24 +772,9 @@  def run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev,
         if a < b:
             raise Exception("GTK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
 
+@disable_ipv6
 def test_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
     """Delayed group M1 retransmission"""
-    require_under_vm()
-    try:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev)
-    finally:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-
-def run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
     hapd = hostapd.add_ap(apdev[0], params)
 
@@ -867,24 +812,9 @@  def run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
         if a < b:
             raise Exception("RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
 
+@disable_ipv6
 def test_ap_wpa2_delayed_group_m1_retransmission_igtk(dev, apdev):
     """Delayed group M1 retransmission (check IGTK protection)"""
-    require_under_vm()
-    try:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=1'],
-                        stdout=open('/dev/null', 'w'))
-        run_ap_wpa2_delayed_group_m1_retransmission_igtk(dev, apdev)
-    finally:
-        subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-        subprocess.call(['sysctl', '-w',
-                         'net.ipv6.conf.default.disable_ipv6=0'],
-                        stdout=open('/dev/null', 'w'))
-
-def run_ap_wpa2_delayed_group_m1_retransmission_igtk(dev, apdev):
     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678",
                                  ieee80211w="2")
     hapd = hostapd.add_ap(apdev[0], params)
diff --git a/tests/hwsim/utils.py b/tests/hwsim/utils.py
index c259a766c0f0..007705f543e8 100644
--- a/tests/hwsim/utils.py
+++ b/tests/hwsim/utils.py
@@ -249,3 +249,29 @@  def set_world_reg(apdev0=None, apdev1=None, dev0=None):
     if dev0:
         dev0.cmd_execute(['iw', 'reg', 'set', '00'])
     time.sleep(0.1)
+
+def disable_ipv6(fn):
+    def wrapper(dev, apdev, params):
+        require_under_vm()
+        try:
+            subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
+                            stdout=open('/dev/null', 'w'))
+            subprocess.call(['sysctl', '-w',
+                             'net.ipv6.conf.default.disable_ipv6=1'],
+                            stdout=open('/dev/null', 'w'))
+            if fn.__code__.co_argcount > 2:
+                return fn(dev, apdev, params)
+            elif fn.__code__.co_argcount > 1:
+                return fn(dev, apdev)
+            return fn(dev)
+        finally:
+            subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
+                            stdout=open('/dev/null', 'w'))
+            subprocess.call(['sysctl', '-w',
+                             'net.ipv6.conf.default.disable_ipv6=0'],
+                            stdout=open('/dev/null', 'w'))
+    # we need the name set right for selecting / printing etc.
+    wrapper.__name__ = fn.__name__
+    # reparent to the right module for module filtering
+    wrapper.__module__ = fn.__module__
+    return wrapper