LCOV - code coverage report
Current view: top level - source4/librpc/tests - binding_string.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 123 127 96.9 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local testing of RPC binding string parsing 
       5             : 
       6             :    Copyright (C) Jelmer Vernooij 2004
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "librpc/gen_ndr/epmapper.h"
      24             : #include "librpc/rpc/dcerpc.h"
      25             : #include "librpc/rpc/dcerpc_proto.h"
      26             : #include "torture/torture.h"
      27             : #include "torture/local/proto.h"
      28             : #include "lib/util/util_net.h"
      29             : 
      30          35 : static bool test_BindingString(struct torture_context *tctx,
      31             :                                                            const void *test_data)
      32             : {
      33          35 :         const char *binding = test_data;
      34          35 :         struct dcerpc_binding *b, *b2;
      35          35 :         char *s, *s2, *p;
      36          35 :         struct epm_tower tower;
      37          35 :         TALLOC_CTX *mem_ctx = tctx;
      38          35 :         const char *host;
      39          35 :         struct GUID object;
      40             : 
      41             :         /* Parse */
      42          35 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(mem_ctx, binding, &b),
      43             :                 "Error parsing binding string");
      44             : 
      45          35 :         object = dcerpc_binding_get_object(b);
      46             : 
      47          35 :         s = dcerpc_binding_string(mem_ctx, b);
      48          35 :         torture_assert(tctx, s != NULL, "Error converting binding back to string");
      49             : 
      50          35 :         torture_assert_casestr_equal(tctx, binding, s, 
      51             :                 "Mismatch while comparing original and regenerated binding strings");
      52             : 
      53             :         /* Generate protocol towers */
      54          35 :         torture_assert_ntstatus_ok(tctx, dcerpc_binding_build_tower(mem_ctx, b, &tower),
      55             :                 "Error generating protocol tower");
      56             : 
      57             :         /* Convert back to binding and then back to string and compare */
      58             : 
      59          35 :         torture_assert_ntstatus_ok(tctx, dcerpc_binding_from_tower(mem_ctx, &tower, &b2),
      60             :                             "Error generating binding from tower for original binding");
      61             : 
      62             :         /* The tower doesn't contain the object */
      63          35 :         torture_assert_ntstatus_ok(tctx, dcerpc_binding_set_object(b2, object),
      64             :                             "set object on tower binding");
      65             : 
      66          35 :         s = dcerpc_binding_string(mem_ctx, b);
      67          35 :         torture_assert(tctx, s != NULL, "Error converting binding back to string for (stripped down)"); 
      68             : 
      69             :         /*
      70             :          * Compare to a stripped down version of the binding string because
      71             :          * the protocol tower doesn't contain the extra option data
      72             :          *
      73             :          * We remove all options except of the endpoint.
      74             :          */
      75          35 :         p = strchr(s, '[');
      76          35 :         if (p != NULL) {
      77          25 :                 char *p2;
      78             : 
      79          25 :                 p2 = strchr(p + 1, ',');
      80          25 :                 if (p2 != NULL) {
      81             :                         /*
      82             :                          * We only look at the first option,
      83             :                          * which might be the endpoint.
      84             :                          */
      85          14 :                         p2[0] = ']';
      86          14 :                         p2[1] = '\0';
      87             :                 }
      88             : 
      89          25 :                 p2 = strchr(p + 1, '=');
      90          25 :                 if (p2 != NULL) {
      91             :                         /*
      92             :                          * It's not the endpoint, so remove the
      93             :                          * whole option section.
      94             :                          */
      95           0 :                         *p = '\0';
      96             :                 }
      97             :         }
      98             : 
      99          35 :         s2 = dcerpc_binding_string(mem_ctx, b2);
     100          35 :         torture_assert(tctx, s != NULL, "Error converting binding back to string"); 
     101             : 
     102          35 :         host = dcerpc_binding_get_string_option(b, "host");
     103          35 :         if (host && is_ipaddress_v4(host)) {
     104          12 :                 torture_assert_casestr_equal(tctx, s, s2, "Mismatch while comparing original and from protocol tower generated binding strings");
     105             :         }
     106             : 
     107           0 :         return true;
     108             : }
     109             : 
     110             : static const char *test_strings[] = {
     111             :         "ncacn_np:", 
     112             :         "ncalrpc:", 
     113             :         "ncalrpc:[,Security=Sane]", 
     114             :         "ncacn_np:[rpcecho]",
     115             :         "ncacn_np:127.0.0.1[rpcecho]",
     116             :         "ncacn_ip_tcp:127.0.0.1",
     117             :         "ncacn_ip_tcp:127.0.0.1[20]",
     118             :         "ncacn_ip_tcp:127.0.0.1[20,sign]",
     119             :         "ncacn_ip_tcp:127.0.0.1[20,sign,Security=Foobar]",
     120             :         "ncacn_http:127.0.0.1",
     121             :         "ncacn_http:127.0.0.1[78]",
     122             :         "ncacn_http:127.0.0.1[78,ProxyServer=myproxy:3128]",
     123             :         "ncacn_np:localhost[rpcecho]",
     124             :         "ncacn_np:[/pipe/rpcecho]",
     125             :         "ncacn_np:localhost[/pipe/rpcecho,sign,seal]",
     126             :         "ncacn_np:[,sign]",
     127             :         "ncadg_ip_udp:",
     128             :         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:localhost",
     129             :         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:127.0.0.1",
     130             :         "ncacn_unix_stream:[/tmp/epmapper]",
     131             :         "ncalrpc:[IDENTIFIER]",
     132             :         "ncacn_unix_stream:[/tmp/epmapper,sign]",
     133             :         "ncacn_ip_tcp:127.0.0.1[75,target_hostname=port75.example.com,target_principal=host/port75.example.com]",
     134             :         "ncacn_ip_tcp:127.0.0.1[75,connect,target_hostname=port75.example.com,target_principal=host/port75.example.com,assoc_group_id=0x01234567]",
     135             :         "ncacn_ip_tcp:127.0.0.1[75,packet,target_hostname=port75.example.com,target_principal=host/port75.example.com,assoc_group_id=0x01234567]",
     136             :         "ncacn_ip_tcp:::",
     137             :         "ncacn_ip_tcp:::[75]",
     138             :         "ncacn_ip_tcp:FD00::5357:5F00",
     139             :         "ncacn_ip_tcp:FD00::5357:5F00[75]",
     140             :         "ncacn_ip_tcp:FD00::5357:5F00[,target_hostname=port75.example.com]",
     141             :         "ncacn_ip_tcp:FD00::5357:5F00[75,target_hostname=port75.example.com]",
     142             :         "ncacn_ip_tcp:fe80::5357:5F00%75",
     143             :         "ncacn_ip_tcp:fe80::5357:5F00%75[75]",
     144             :         "ncacn_ip_tcp:fe80::5357:5F00%75[,target_hostname=port75.example.com]",
     145             :         "ncacn_ip_tcp:fe80::5357:5F00%75[75,target_hostname=port75.example.com]",
     146             : };
     147             : 
     148           1 : static bool test_parse_check_results(struct torture_context *tctx)
     149             : {
     150           1 :         struct dcerpc_binding *b;
     151           1 :         struct GUID uuid;
     152           1 :         struct GUID object;
     153           1 :         struct ndr_syntax_id abstract;
     154           1 :         enum dcerpc_transport_t transport;
     155           1 :         const char *endpoint;
     156           1 :         uint32_t flags;
     157             : 
     158           1 :         torture_assert_ntstatus_ok(tctx, 
     159             :                                    GUID_from_string("308FB580-1EB2-11CA-923B-08002B1075A7", &uuid),
     160             :                                    "parsing uuid");
     161             : 
     162           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER", &b), "parse");
     163           1 :         transport = dcerpc_binding_get_transport(b);
     164           1 :         torture_assert(tctx, transport == NCACN_NP, "ncacn_np expected");
     165           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER", &b), "parse");
     166           1 :         transport = dcerpc_binding_get_transport(b);
     167           1 :         torture_assert(tctx, transport == NCACN_IP_TCP, "ncacn_ip_tcp expected");
     168           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[rpcecho]", &b), "parse");
     169           1 :         endpoint = dcerpc_binding_get_string_option(b, "endpoint");
     170           1 :         torture_assert_str_equal(tctx, endpoint, "rpcecho", "endpoint");
     171           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho]", &b), "parse");
     172           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho,sign,seal]", &b), "parse");
     173           1 :         flags = dcerpc_binding_get_flags(b);
     174           1 :         torture_assert(tctx, flags == DCERPC_SIGN+DCERPC_SEAL, "sign+seal flags");
     175           1 :         endpoint = dcerpc_binding_get_string_option(b, "endpoint");
     176           1 :         torture_assert_str_equal(tctx, endpoint, "/pipe/rpcecho", "endpoint");
     177           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[,sign]", &b), "parse");
     178           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER[,sign]", &b), "parse");
     179           1 :         endpoint = dcerpc_binding_get_string_option(b, "endpoint");
     180           1 :         torture_assert(tctx, endpoint == NULL, "endpoint");
     181           1 :         flags = dcerpc_binding_get_flags(b);
     182           1 :         torture_assert(tctx, flags == DCERPC_SIGN, "sign flag");
     183           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncalrpc:", &b), "parse");
     184           1 :         transport = dcerpc_binding_get_transport(b);
     185           1 :         torture_assert(tctx, transport == NCALRPC, "ncalrpc expected");
     186           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, 
     187             :                 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:$SERVER", &b), "parse");
     188           1 :         object = dcerpc_binding_get_object(b);
     189           1 :         abstract = dcerpc_binding_get_abstract_syntax(b);
     190           1 :         torture_assert(tctx, GUID_equal(&object, &uuid), "object uuid");
     191           1 :         torture_assert(tctx, ndr_syntax_id_equal(&abstract, &ndr_syntax_id_null),
     192             :                        "null abstract syntax");
     193           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, 
     194             :                 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:$SERVER", &b), "parse");
     195           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER[,sign,localaddress=192.168.1.1]", &b), "parse");
     196           1 :         transport = dcerpc_binding_get_transport(b);
     197           1 :         torture_assert(tctx, transport == NCACN_IP_TCP, "ncacn_ip_tcp expected");
     198           1 :         flags = dcerpc_binding_get_flags(b);
     199           1 :         torture_assert(tctx, flags == DCERPC_SIGN, "sign flag");
     200           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "localaddress"),
     201             :                                  "192.168.1.1", "localaddress");
     202           1 :         torture_assert_str_equal(tctx, "ncacn_ip_tcp:$SERVER[,sign,localaddress=192.168.1.1]",
     203             :                                  dcerpc_binding_string(tctx, b), "back to string");
     204           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
     205             :                                  "$SERVER", "host");
     206           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
     207             :                                  "$SERVER", "target_hostname");
     208             : 
     209           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
     210             :                 "ncacn_ip_tcp:$HOST[,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL]",
     211             :                 &b), "parse");
     212           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
     213             :                                  "$HOST", "host");
     214           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
     215             :                                  "$HOSTNAME", "target_hostname");
     216           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_principal"),
     217             :                                  "$PRINCIPAL", "target_principal");
     218           1 :         torture_assert_str_equal(tctx,
     219             :                                  dcerpc_binding_string(tctx, b),
     220             :                 "ncacn_ip_tcp:$HOST[,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL]",
     221             :                                  "back to string");
     222             : 
     223           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
     224             :                 "ncacn_ip_tcp:$HOST[,connect,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
     225             :                 &b), "parse");
     226           1 :         flags = dcerpc_binding_get_flags(b);
     227           1 :         torture_assert(tctx, flags == DCERPC_CONNECT, "connect flag");
     228           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
     229             :                                  "$HOST", "host");
     230           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
     231             :                                  "$HOSTNAME", "target_hostname");
     232           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_principal"),
     233             :                                  "$PRINCIPAL", "target_principal");
     234           1 :         torture_assert_int_equal(tctx, dcerpc_binding_get_assoc_group_id(b), 0x01234567,
     235             :                                  "assoc_group_id");
     236           1 :         torture_assert_str_equal(tctx,
     237             :                                  dcerpc_binding_string(tctx, b),
     238             :                 "ncacn_ip_tcp:$HOST[,connect,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
     239             :                                  "back to string");
     240             : 
     241           1 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
     242             :                 "ncacn_ip_tcp:$HOST[,packet,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
     243             :                 &b), "parse");
     244           1 :         flags = dcerpc_binding_get_flags(b);
     245           1 :         torture_assert(tctx, flags == DCERPC_PACKET, "packet flag");
     246           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
     247             :                                  "$HOST", "host");
     248           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
     249             :                                  "$HOSTNAME", "target_hostname");
     250           1 :         torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_principal"),
     251             :                                  "$PRINCIPAL", "target_principal");
     252           1 :         torture_assert_int_equal(tctx, dcerpc_binding_get_assoc_group_id(b), 0x01234567,
     253             :                                  "assoc_group_id");
     254           1 :         torture_assert_str_equal(tctx,
     255             :                                  dcerpc_binding_string(tctx, b),
     256             :                 "ncacn_ip_tcp:$HOST[,packet,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
     257             :                                  "back to string");
     258             : 
     259           0 :         return true;
     260             : }
     261             : 
     262          18 : static bool test_no_transport(struct torture_context *tctx, const void *test_data)
     263             : {
     264          18 :         const char *binding = test_data;
     265          18 :         struct dcerpc_binding *b;
     266          18 :         enum dcerpc_transport_t transport;
     267          18 :         const char *s;
     268             : 
     269             :         /* Parse */
     270          18 :         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, binding, &b),
     271             :                 "Error parsing binding string");
     272             : 
     273          18 :         transport = dcerpc_binding_get_transport(b);
     274          18 :         torture_assert(tctx, transport == NCA_UNKNOWN, "invalid transport");
     275             : 
     276          18 :         s = dcerpc_binding_string(tctx, b);
     277          18 :         torture_assert(tctx, s != NULL, "Error converting binding back to string");
     278             : 
     279          18 :         torture_assert_casestr_equal(tctx, binding, s, 
     280             :                 "Mismatch while comparing original and regenerated binding strings");
     281             : 
     282           0 :         return true;
     283             : }
     284             : 
     285             : static const char *test_no_strings[] = {
     286             :         "port75.example.com",
     287             :         "port75.example.com[75]",
     288             :         "127.0.0.1",
     289             :         "127.0.0.1[75]",
     290             :         "127.0.0.1[,target_hostname=port75.example.com]",
     291             :         "127.0.0.1[75,target_hostname=port75.example.com]",
     292             :         "::",
     293             :         "::[75]",
     294             :         "::[,target_hostname=port75.example.com]",
     295             :         "::[75,target_hostname=port75.example.com]",
     296             :         "FD00::5357:5F00",
     297             :         "FD00::5357:5F00[75]",
     298             :         "FD00::5357:5F00[,target_hostname=port75.example.com]",
     299             :         "FD00::5357:5F00[75,target_hostname=port75.example.com]",
     300             :         "fe80::5357:5F00%75",
     301             :         "fe80::5357:5F00%75[75]",
     302             :         "fe80::5357:5F00%75[,target_hostname=port75.example.com]",
     303             :         "fe80::5357:5F00%75[75,target_hostname=port75.example.com]",
     304             : };
     305             : 
     306        2354 : struct torture_suite *torture_local_binding_string(TALLOC_CTX *mem_ctx)
     307             : {
     308         125 :         int i;
     309        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "binding");
     310             : 
     311       84869 :         for (i = 0; i < ARRAY_SIZE(test_strings); i++) {
     312       82390 :                 torture_suite_add_simple_tcase_const(suite, test_strings[i],
     313             :                                                 test_BindingString,
     314       82390 :                                                 test_strings[i]);
     315             :         }
     316             : 
     317       44726 :         for (i = 0; i < ARRAY_SIZE(test_no_strings); i++) {
     318       42372 :                 torture_suite_add_simple_tcase_const(suite, test_no_strings[i],
     319             :                                                      test_no_transport,
     320       42372 :                                                      test_no_strings[i]);
     321             :         }
     322             : 
     323        2354 :         torture_suite_add_simple_test(suite, "parsing results",
     324             :                         test_parse_check_results);
     325             : 
     326        2354 :         return suite;
     327             : }

Generated by: LCOV version 1.14