]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
make unit_test_module use Net.*
authorAlan T. DeKok <aland@freeradius.org>
Fri, 1 Sep 2023 15:09:53 +0000 (11:09 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 1 Sep 2023 17:10:27 +0000 (13:10 -0400)
and update the xlat tests to match

src/bin/unit_test_module.c
src/tests/xlat/expr.txt

index 253d085a8439b7e6a0b6986be22d038bb000d243..9897b9176c3f51b7129d3791bf436200a7086d91 100644 (file)
@@ -75,22 +75,10 @@ fr_dict_autoload_t unit_test_module_dict[] = {
        { NULL }
 };
 
-static fr_dict_attr_t const *attr_packet_dst_ip_address;
-static fr_dict_attr_t const *attr_packet_dst_ipv6_address;
-static fr_dict_attr_t const *attr_packet_dst_port;
-static fr_dict_attr_t const *attr_packet_src_ip_address;
-static fr_dict_attr_t const *attr_packet_src_ipv6_address;
-static fr_dict_attr_t const *attr_packet_src_port;
 static fr_dict_attr_t const *attr_packet_type;
 
 extern fr_dict_attr_autoload_t unit_test_module_dict_attr[];
 fr_dict_attr_autoload_t unit_test_module_dict_attr[] = {
-       { .out = &attr_packet_dst_ip_address, .name = "Packet-Dst-IP-Address", .type = FR_TYPE_IPV4_ADDR, .dict = &dict_freeradius },
-       { .out = &attr_packet_dst_ipv6_address, .name = "Packet-Dst-IPv6-Address", .type = FR_TYPE_IPV6_ADDR, .dict = &dict_freeradius },
-       { .out = &attr_packet_dst_port, .name = "Packet-Dst-Port", .type = FR_TYPE_UINT16, .dict = &dict_freeradius },
-       { .out = &attr_packet_src_ip_address, .name = "Packet-Src-IP-Address", .type = FR_TYPE_IPV4_ADDR, .dict = &dict_freeradius },
-       { .out = &attr_packet_src_ipv6_address, .name = "Packet-Src-IPv6-Address", .type = FR_TYPE_IPV6_ADDR, .dict = &dict_freeradius },
-       { .out = &attr_packet_src_port, .name = "Packet-Src-Port", .type = FR_TYPE_UINT16, .dict = &dict_freeradius },
        { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_protocol },
 
        { NULL }
@@ -226,23 +214,21 @@ static request_t *request_from_file(TALLOC_CTX *ctx, FILE *fp, fr_client_t *clie
                }
        };
 
-       for (vp = fr_pair_dcursor_init(&cursor, &request->request_pairs);
-            vp;
-            vp = fr_dcursor_next(&cursor)) {
-               if (vp->da == attr_packet_type) {
-                       request->packet->code = vp->vp_uint32;
-               } else if (vp->da == attr_packet_dst_port) {
-                       request->packet->socket.inet.dst_port = vp->vp_uint16;
-               } else if ((vp->da == attr_packet_dst_ip_address) ||
-                          (vp->da == attr_packet_dst_ipv6_address)) {
-                       memcpy(&request->packet->socket.inet.dst_ipaddr, &vp->vp_ip, sizeof(request->packet->socket.inet.dst_ipaddr));
-               } else if (vp->da == attr_packet_src_port) {
-                       request->packet->socket.inet.src_port = vp->vp_uint16;
-               } else if ((vp->da == attr_packet_src_ip_address) ||
-                          (vp->da == attr_packet_src_ipv6_address)) {
-                       memcpy(&request->packet->socket.inet.src_ipaddr, &vp->vp_ip, sizeof(request->packet->socket.inet.src_ipaddr));
-               }
-       } /* loop over the VP's we read in */
+       /*
+        *      Fill in the packet header from attributes, and then
+        *      re-realize the attributes.
+        */
+       vp = fr_pair_find_by_da(&request->request_pairs, NULL,  attr_packet_type);
+       if (vp) {
+               request->packet->code = vp->vp_uint32;
+       }
+
+       fr_packet_pairs_to_packet(request->packet, &request->request_pairs);
+
+       /*
+        *      @todo - add from_packet() as per below, but for now that screws
+        *      up the various tests which look at &request.[*]
+        */
 
        if (fr_debug_lvl) {
                for (vp = fr_pair_dcursor_init(&cursor, &request->request_pairs);
@@ -376,6 +362,11 @@ static bool do_xlats(fr_event_list_t *el, char const *filename, FILE *fp)
        request->log.lvl = fr_debug_lvl;
        request->async = talloc_zero(request, fr_async_t);
 
+       if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
+               fprintf(stderr, "Failed converting packet IPs to attributes");
+               return false;
+       }
+
        while (fgets(line_buff, sizeof(line_buff), fp) != NULL) {
                lineno++;
 
index 5602e3f7c61ac9f8872352eda426834ad368cc6a..b6bf4fbfec952f08a0533c4e5051896a648b514c 100644 (file)
@@ -37,16 +37,16 @@ match 2147483648
 xlat_expr %n
 match {0}
 
-xlat_expr &Packet-Src-IP-Address =~ /^127.0/
+xlat_expr &Net.Src.IP =~ /^127.0/
 match true
 
-xlat_expr &Packet-Src-IP-Address =~ /^128.0/
+xlat_expr &Net.Src.IP =~ /^128.0/
 match false
 
-xlat_expr &Packet-Src-IP-Address =~ /foo %{Packet-Dst-IP-Address} foo/
+xlat_expr &Net.Src.IP =~ /foo %{Packet-Dst-IP-Address} foo/
 match false
 
-xlat_expr &Packet-Src-IP-Address =~ /%{Packet-Dst-IP-Address}/
+xlat_expr &Net.Src.IP =~ /%{Packet-Dst-IP-Address}/
 match true
 
 # Auto upcast
@@ -92,13 +92,13 @@ match ERROR expanding xlat: Program failed with status 1
 xlat_expr -&NAS-IP-Address
 match ERROR expanding xlat: Input is empty
 
-xlat_expr "foo" + (string)&Packet-Src-IP-Address
+xlat_expr "foo" + (string)&Net.Src.IP
 match foo127.0.0.1
 
-xlat_expr "foo%{Packet-Src-IP-Address}"
+xlat_expr "foo%{Net.Src.IP}"
 match foo127.0.0.1
 
-xlat_expr "foo" + (octets)&Packet-Src-IP-Address
+xlat_expr "foo" + (octets)&Net.Src.IP
 match 0x666f6f7f000001
 
 #
@@ -125,5 +125,5 @@ match 0x010200047f000001
 xlat_expr (uint16) ((uint32) 1 + (uint8) 2)
 match 3
 
-xlat_expr (&Framed-IP-Address || &Packet-Src-IP-Address) == (ipaddr) 127.0.0.1
+xlat_expr (&Framed-IP-Address || &Net.Src.IP) == (ipaddr) 127.0.0.1
 match true