From 2cbaf0de223baaeed47ec9beb59337415c007d4d Mon Sep 17 00:00:00 2001
From: Jouni Malinen <jouni@codeaurora.org>
Date: Wed, 17 Oct 2018 12:33:11 +0300
Subject: [PATCH] tests: Work around tshark bug in wpas_mesh_max_peering

It looks like tshark parser was broken at some point for
wlan.mesh.config.cap which is now (tshark 2.6.3) pointing to incorrect
field (same as wlan.mesh.config.ps_protocol). This used to work with
tshark 2.2.6.

For now, assume the capability field ends up being the last octet of the
frame.

Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
---
 tests/hwsim/test_wpas_mesh.py | 30 +++++++++++++++++++++++++++++-
 tests/hwsim/tshark.py         | 16 ++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/tests/hwsim/test_wpas_mesh.py b/tests/hwsim/test_wpas_mesh.py
index 5feb9bee5..106fad18d 100644
--- a/tests/hwsim/test_wpas_mesh.py
+++ b/tests/hwsim/test_wpas_mesh.py
@@ -10,12 +10,13 @@ import os
 import struct
 import subprocess
 import time
+import json
 
 import hwsim_utils
 import hostapd
 from wpasupplicant import WpaSupplicant
 from utils import HwsimSkip, alloc_fail, fail_test, wait_fail_trigger
-from tshark import run_tshark
+from tshark import run_tshark, run_tshark_json
 from test_ap_ht import set_world_reg
 from hwsim_utils import set_group_map
 
@@ -837,9 +838,12 @@ def test_wpas_mesh_max_peering(dev, apdev, params):
     pkts = out.splitlines()
     one = [ 0, 0, 0 ]
     zero = [ 0, 0, 0 ]
+    all_cap_one = True
     for pkt in pkts:
         addr, cap = pkt.split('\t')
         cap = int(cap, 16)
+        if cap != 1:
+            all_cap_one = False
         if addr == addr0:
             idx = 0
         elif addr == addr1:
@@ -854,6 +858,30 @@ def test_wpas_mesh_max_peering(dev, apdev, params):
             zero[idx] += 1
     logger.info("one: " + str(one))
     logger.info("zero: " + str(zero))
+    if all_cap_one:
+        # It looks like tshark parser was broken at some point for
+        # wlan.mesh.config.cap which is now (tshark 2.6.3) pointing to incorrect
+        # field (same as wlan.mesh.config.ps_protocol). This used to work with
+        # tshark 2.2.6.
+        #
+        # For now, assume the capability field ends up being the last octet of
+        # the frame.
+        one = [ 0, 0, 0 ]
+        zero = [ 0, 0, 0 ]
+        addrs = [ addr0, addr1, addr2 ]
+        for idx in range(3):
+            addr = addrs[idx]
+            out = run_tshark_json(capfile, filt + " && wlan.sa == " + addr)
+            pkts = json.loads(out)
+            for pkt in pkts:
+                frame = pkt["_source"]["layers"]["frame_raw"][0]
+                cap = int(frame[-2:], 16)
+                if cap & 0x01:
+                    one[idx] += 1
+                else:
+                    zero[idx] += 1
+        logger.info("one: " + str(one))
+        logger.info("zero: " + str(zero))
     if zero[0] == 0:
         raise Exception("Accepting Additional Mesh Peerings not cleared")
     if one[0] == 0:
diff --git a/tests/hwsim/tshark.py b/tests/hwsim/tshark.py
index c3b8f36d4..b90f66258 100644
--- a/tests/hwsim/tshark.py
+++ b/tests/hwsim/tshark.py
@@ -99,3 +99,19 @@ def run_tshark(filename, filter, display=None, wait=True):
         return _run_tshark(filename, filter.replace('wlan_mgt', 'wlan'),
                            [x.replace('wlan_mgt', 'wlan') for x in display],
                            wait)
+
+def run_tshark_json(filename, filter):
+    arg = [ "tshark", "-r", filename,
+            _tshark_filter_arg, filter ]
+    arg.append('-Tjson')
+    arg.append('-x')
+    try:
+        cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
+                               stderr=subprocess.PIPE)
+    except Exception, e:
+        logger.info("Could run run tshark: " + str(e))
+        return None
+    output = cmd.communicate()
+    out = output[0]
+    res = cmd.wait()
+    return out