1#  Copyright (C) 2024 The Android Open Source Project
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14
15from mobly import asserts
16from mobly import base_test
17from net_tests_utils.host.python import packet_utils
18
19class TestPacketUtils(base_test.BaseTestClass):
20    def test_unicast_arp_request(self):
21        # Using scapy to generate unicast arp request packet:
22        #   eth = Ether(src="00:01:02:03:04:05", dst="01:02:03:04:05:06")
23        #   arp = ARP(op=1, pdst="192.168.1.1", hwsrc="00:01:02:03:04:05", psrc="192.168.1.2")
24        #   pkt = eth/arp
25        expect_arp_request = """
26            01020304050600010203040508060001080006040001000102030405c0a80102000000000000c0a80101
27        """.upper().replace(" ", "").replace("\n", "")
28        arp_request = packet_utils.construct_arp_packet(
29            src_mac="00:01:02:03:04:05",
30            dst_mac="01:02:03:04:05:06",
31            src_ip="192.168.1.2",
32            dst_ip="192.168.1.1",
33            op=packet_utils.ARP_REQUEST_OP
34        )
35        asserts.assert_equal(expect_arp_request, arp_request)
36
37    def test_broadcast_arp_request(self):
38        # Using scapy to generate unicast arp request packet:
39        #   eth = Ether(src="00:01:02:03:04:05", dst="FF:FF:FF:FF:FF:FF")
40        #   arp = ARP(op=1, pdst="192.168.1.1", hwsrc="00:01:02:03:04:05", psrc="192.168.1.2")
41        #   pkt = eth/arp
42        expect_arp_request = """
43            ffffffffffff00010203040508060001080006040001000102030405c0a80102000000000000c0a80101
44        """.upper().replace(" ", "").replace("\n", "")
45        arp_request = packet_utils.construct_arp_packet(
46            src_mac="00:01:02:03:04:05",
47            dst_mac=packet_utils.ETHER_BROADCAST_MAC_ADDRESS,
48            src_ip="192.168.1.2",
49            dst_ip="192.168.1.1",
50            op=packet_utils.ARP_REQUEST_OP
51        )
52        asserts.assert_equal(expect_arp_request, arp_request)
53
54    def test_arp_reply(self):
55        # Using scapy to generate unicast arp request packet:
56        #   eth = Ether(src="01:02:03:04:05:06", dst="00:01:02:03:04:05")
57        #   arp = ARP(op=2, pdst="192.168.1.2", \
58        #             hwsrc="01:02:03:04:05:06", \
59        #             psrc="192.168.1.1", \
60        #             hwdst="00:01:02:03:04:05")
61        #   pkt = eth/arp
62        expect_arp_reply = """
63            00010203040501020304050608060001080006040002010203040506c0a80101000102030405c0a80102
64        """.upper().replace(" ", "").replace("\n", "")
65        arp_reply = packet_utils.construct_arp_packet(
66            src_mac="01:02:03:04:05:06",
67            dst_mac="00:01:02:03:04:05",
68            src_ip="192.168.1.1",
69            dst_ip="192.168.1.2",
70            op=packet_utils.ARP_REPLY_OP
71        )
72        asserts.assert_equal(expect_arp_reply, arp_reply)
73