LCOV - code coverage report
Current view: top level - source4/torture/ldap - cldap.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 54 78 69.2 %
Date: 2024-04-21 15:09:00 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS Implementation.
       3             : 
       4             :    test CLDAP operations
       5             :    
       6             :    Copyright (C) Andrew Tridgell 2005
       7             :    Copyright (C) Matthias Dieter Wallnöfer 2009
       8             :     
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             :    
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             :    
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             :    
      22             : */
      23             : 
      24             : #include "includes.h"
      25             : #include "libcli/cldap/cldap.h"
      26             : #include "libcli/ldap/ldap_client.h"
      27             : #include "libcli/resolve/resolve.h"
      28             : #include "param/param.h"
      29             : #include "../lib/tsocket/tsocket.h"
      30             : 
      31             : #include "torture/torture.h"
      32             : #include "torture/ldap/proto.h"
      33             : 
      34             : #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
      35             : 
      36             : #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
      37             : 
      38             : #define CHECK_STRING(v, correct) torture_assert_str_equal(tctx, v, correct, "incorrect value");
      39             : 
      40             : /*
      41             :   convert a ldap result message to a ldb message. This allows us to
      42             :   use the convenient ldif dump routines in ldb to print out cldap
      43             :   search results
      44             : */
      45           0 : static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
      46             : {
      47           0 :         struct ldb_message *msg;
      48             : 
      49           0 :         msg = ldb_msg_new(mem_ctx);
      50           0 :         msg->dn = ldb_dn_new(msg, ldb, res->dn);
      51           0 :         msg->num_elements = res->num_attributes;
      52           0 :         msg->elements = talloc_steal(msg, res->attributes);
      53           0 :         return msg;
      54             : }
      55             : 
      56             : /*
      57             :   dump a set of cldap results
      58             : */
      59           0 : static void cldap_dump_results(struct cldap_search *search)
      60             : {
      61           0 :         struct ldb_ldif ldif;
      62           0 :         struct ldb_context *ldb;
      63             : 
      64           0 :         if (!search || !(search->out.response)) {
      65           0 :                 return;
      66             :         }
      67             : 
      68             :         /* we need a ldb context to use ldb_ldif_write_file() */
      69           0 :         ldb = ldb_init(NULL, NULL);
      70             : 
      71           0 :         ZERO_STRUCT(ldif);
      72           0 :         ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
      73             : 
      74           0 :         ldb_ldif_write_file(ldb, stdout, &ldif);
      75             : 
      76           0 :         talloc_free(ldb);
      77             : }
      78             : 
      79             : /*
      80             :   test generic cldap operations
      81             : */
      82           1 : static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
      83             : {
      84           0 :         struct cldap_socket *cldap;
      85           0 :         NTSTATUS status;
      86           0 :         struct cldap_search search;
      87           1 :         const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
      88           1 :         const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
      89           1 :         const char *attrs3[] = { "netlogon", NULL };
      90           0 :         struct tsocket_address *dest_addr;
      91           0 :         const char *ip;
      92           0 :         struct nbt_name nbt_name;
      93           0 :         int ret;
      94             : 
      95           1 :         make_nbt_name_server(&nbt_name, dest);
      96             : 
      97           1 :         status = resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
      98             :                                  0, 0, &nbt_name, tctx, &ip, tctx->ev);
      99           1 :         torture_assert_ntstatus_ok(tctx, status,
     100             :                         talloc_asprintf(tctx,"Failed to resolve %s: %s",
     101             :                                         nbt_name.name, nt_errstr(status)));
     102             : 
     103           1 :         ret = tsocket_address_inet_from_strings(tctx, "ip",
     104             :                                                 ip,
     105             :                                                 lpcfg_cldap_port(tctx->lp_ctx),
     106             :                                                 &dest_addr);
     107           1 :         CHECK_VAL(ret, 0);
     108             : 
     109             :         /* cldap_socket_init should now know about the dest. address */
     110           1 :         status = cldap_socket_init(tctx, NULL, dest_addr, &cldap);
     111           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     112             : 
     113           1 :         ZERO_STRUCT(search);
     114           1 :         search.in.dest_address = NULL;
     115           1 :         search.in.dest_port = 0;
     116           1 :         search.in.timeout = 10;
     117           1 :         search.in.retries = 3;
     118             : 
     119           1 :         status = cldap_search(cldap, tctx, &search);
     120           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     121             : 
     122           1 :         printf("fetching whole rootDSE\n");
     123           1 :         search.in.filter = "(objectclass=*)";
     124           1 :         search.in.attributes = NULL;
     125             : 
     126           1 :         status = cldap_search(cldap, tctx, &search);
     127           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     128             : 
     129           1 :         if (DEBUGLVL(3)) cldap_dump_results(&search);
     130             : 
     131           1 :         printf("fetching currentTime and USN\n");
     132           1 :         search.in.filter = "(objectclass=*)";
     133           1 :         search.in.attributes = attrs1;
     134             : 
     135           1 :         status = cldap_search(cldap, tctx, &search);
     136           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     137             :         
     138           1 :         if (DEBUGLVL(3)) cldap_dump_results(&search);
     139             : 
     140           1 :         printf("Testing currentTime, USN and netlogon\n");
     141           1 :         search.in.filter = "(objectclass=*)";
     142           1 :         search.in.attributes = attrs2;
     143             : 
     144           1 :         status = cldap_search(cldap, tctx, &search);
     145           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     146             : 
     147           1 :         if (DEBUGLVL(3)) cldap_dump_results(&search);
     148             : 
     149           1 :         printf("Testing objectClass=* and netlogon\n");
     150           1 :         search.in.filter = "(objectclass=*)";
     151           1 :         search.in.attributes = attrs3;
     152             : 
     153           1 :         status = cldap_search(cldap, tctx, &search);
     154           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     155             : 
     156           1 :         if (DEBUGLVL(3)) cldap_dump_results(&search);
     157             : 
     158           1 :         printf("Testing a false expression\n");
     159           1 :         search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
     160           1 :         search.in.attributes = attrs1;
     161             : 
     162           1 :         status = cldap_search(cldap, tctx, &search);
     163           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     164             : 
     165           1 :         if (DEBUGLVL(3)) cldap_dump_results(&search);       
     166             : 
     167           1 :         return true;    
     168             : }
     169             : 
     170           1 : bool torture_cldap(struct torture_context *torture)
     171             : {
     172           1 :         bool ret = true;
     173           1 :         const char *host = torture_setting_string(torture, "host", NULL);
     174             : 
     175           1 :         ret &= test_cldap_generic(torture, host);
     176             : 
     177           1 :         return ret;
     178             : }
     179             : 

Generated by: LCOV version 1.14