1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17from acts import base_test
18from acts.controllers.sniffer import Sniffer
19
20
21class SnifferSanityTest(base_test.BaseTestClass):
22    def setup_class(self):
23        self._channels = [6, 44]
24
25        # capture (sniff) for 30 seconds or 10 packets - whichever comes first
26        self._capture_sec = 30
27        self._packet_count = 10
28
29        self._filter = {"tcpdump": "type mgt subtype beacon",
30                        "tshark": "type mgt subtype beacon"}
31
32    def test_sniffer_validation_using_with(self):
33        """Validate sniffer configuration & capture API using the 'with' clause.
34
35        This is the standard example - this syntax should typically be used.
36        """
37        index = 0
38        for sniffer in self.sniffers:
39            for channel in self._channels:
40                with sniffer.start_capture(
41                        override_configs={Sniffer.CONFIG_KEY_CHANNEL: channel},
42                        duration=self._capture_sec,
43                        packet_count=self._packet_count):
44                    self.log.info("Capture: %s", sniffer.get_capture_file())
45
46    def test_sniffer_validation_manual(self):
47        """Validate sniffer configuration & capture API using a manual/raw
48        API mechanism.
49
50        The standard process should use a with clause. This demonstrates the
51        manual process which uses an explicit wait_for_capture() call.
52        Alternatively, could also use a sleep() + stop_capture() process
53        (though that mechanism won't terminate early if the capture is done).
54        """
55        index = 0
56        for sniffer in self.sniffers:
57            for channel in self._channels:
58                sniffer.start_capture(
59                    override_configs={Sniffer.CONFIG_KEY_CHANNEL: channel},
60                    packet_count=self._packet_count)
61                self.log.info("Capture: %s", sniffer.get_capture_file())
62                sniffer.wait_for_capture(timeout=self._capture_sec)
63
64    def test_sniffer_validation_capture_3_beacons(self):
65        """Demonstrate the use of additional configuration.
66        """
67        index = 0
68        for sniffer in self.sniffers:
69            for channel in self._channels:
70                with sniffer.start_capture(
71                        override_configs={Sniffer.CONFIG_KEY_CHANNEL: channel},
72                        duration=self._capture_sec,
73                        packet_count=3,
74                        additional_args=self._filter[sniffer.get_subtype()]):
75                    self.log.info("Capture: %s", sniffer.get_capture_file())
76