LCOV - code coverage report
Current view: top level - source3/lib/smbconf - testsuite.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 132 180 73.3 %
Date: 2024-01-11 09:59:51 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  libsmbconf - Samba configuration library: testsuite
       4             :  *  Copyright (C) Michael Adam 2008
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (at your option) any later version.
      10             :  *
      11             :  *  This program is distributed in the hope that it will be useful,
      12             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  *  GNU General Public License for more details.
      15             :  *
      16             :  *  You should have received a copy of the GNU General Public License
      17             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "includes.h"
      21             : #include "lib/cmdline/cmdline.h"
      22             : #include "lib/smbconf/smbconf.h"
      23             : #include "lib/smbconf/smbconf_init.h"
      24             : #include "lib/smbconf/smbconf_reg.h"
      25             : #include "lib/smbconf/smbconf_txt.h"
      26             : 
      27           4 : static void print_strings(const char *prefix,
      28             :                           uint32_t num_strings,
      29             :                           const char * const *strings)
      30             : {
      31             :         uint32_t count;
      32             : 
      33           4 :         if (prefix == NULL) {
      34           0 :                 prefix = "";
      35             :         }
      36             : 
      37           4 :         for (count = 0; count < num_strings; count++) {
      38           0 :                 printf("%s%s\n", prefix, strings[count]);
      39             :         }
      40           4 : }
      41             : 
      42           4 : static bool test_get_includes(struct smbconf_ctx *ctx)
      43             : {
      44             :         sbcErr err;
      45           4 :         bool ret = false;
      46           4 :         uint32_t num_includes = 0;
      47           4 :         char **includes = NULL;
      48           4 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
      49             : 
      50           4 :         printf("TEST: get_includes\n");
      51           4 :         err = smbconf_get_global_includes(ctx, mem_ctx,
      52             :                                           &num_includes, &includes);
      53           4 :         if (!SBC_ERROR_IS_OK(err)) {
      54           0 :                 printf("FAIL: get_includes - %s\n", sbcErrorString(err));
      55           0 :                 goto done;
      56             :         }
      57             : 
      58           4 :         printf("got %u includes%s\n", num_includes,
      59           4 :                (num_includes > 0) ? ":" : ".");
      60           4 :         print_strings("", num_includes, (const char * const *)includes);
      61             : 
      62           4 :         printf("OK: get_includes\n");
      63           4 :         ret = true;
      64             : 
      65           4 : done:
      66           4 :         talloc_free(mem_ctx);
      67           4 :         return ret;
      68             : }
      69             : 
      70           2 : static bool test_set_get_includes(struct smbconf_ctx *ctx)
      71             : {
      72             :         sbcErr err;
      73             :         uint32_t count;
      74           2 :         bool ret = false;
      75           2 :         const char *set_includes[] = {
      76             :                 "/path/to/include1",
      77             :                 "/path/to/include2"
      78             :         };
      79           2 :         uint32_t set_num_includes = 2;
      80           2 :         char **get_includes = NULL;
      81           2 :         uint32_t get_num_includes = 0;
      82           2 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
      83             : 
      84           2 :         printf("TEST: set_get_includes\n");
      85             : 
      86           2 :         err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
      87           2 :         if (!SBC_ERROR_IS_OK(err)) {
      88           0 :                 printf("FAIL: get_set_includes (setting includes) - %s\n",
      89             :                        sbcErrorString(err));
      90           0 :                 goto done;
      91             :         }
      92             : 
      93           2 :         err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
      94             :                                           &get_includes);
      95           2 :         if (!SBC_ERROR_IS_OK(err)) {
      96           0 :                 printf("FAIL: get_set_includes (getting includes) - %s\n",
      97             :                        sbcErrorString(err));
      98           0 :                 goto done;
      99             :         }
     100             : 
     101           2 :         if (get_num_includes != set_num_includes) {
     102           0 :                 printf("FAIL: get_set_includes - set %d includes, got %d\n",
     103             :                        set_num_includes, get_num_includes);
     104           0 :                 goto done;
     105             :         }
     106             : 
     107           6 :         for (count = 0; count < get_num_includes; count++) {
     108           4 :                 if (!strequal(set_includes[count], get_includes[count])) {
     109           0 :                         printf("expected: \n");
     110           0 :                         print_strings("* ", set_num_includes,
     111             :                                       (const char * const *)set_includes);
     112           0 :                         printf("got: \n");
     113           0 :                         print_strings("* ", get_num_includes,
     114             :                                       (const char * const *)get_includes);
     115           0 :                         printf("FAIL: get_set_includes - data mismatch:\n");
     116           0 :                         goto done;
     117             :                 }
     118             :         }
     119             : 
     120           2 :         printf("OK: set_includes\n");
     121           2 :         ret = true;
     122             : 
     123           2 : done:
     124           2 :         talloc_free(mem_ctx);
     125           2 :         return ret;
     126             : }
     127             : 
     128           2 : static bool test_delete_includes(struct smbconf_ctx *ctx)
     129             : {
     130             :         sbcErr err;
     131           2 :         bool ret = false;
     132           2 :         const char *set_includes[] = {
     133             :                 "/path/to/include",
     134             :         };
     135           2 :         uint32_t set_num_includes = 1;
     136           2 :         char **get_includes = NULL;
     137           2 :         uint32_t get_num_includes = 0;
     138           2 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     139             : 
     140           2 :         printf("TEST: delete_includes\n");
     141             : 
     142           2 :         err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
     143           2 :         if (!SBC_ERROR_IS_OK(err)) {
     144           0 :                 printf("FAIL: delete_includes (setting includes) - %s\n",
     145             :                        sbcErrorString(err));
     146           0 :                 goto done;
     147             :         }
     148             : 
     149           2 :         err = smbconf_delete_global_includes(ctx);
     150           2 :         if (!SBC_ERROR_IS_OK(err)) {
     151           0 :                 printf("FAIL: delete_includes (deleting includes) - %s\n",
     152             :                        sbcErrorString(err));
     153           0 :                 goto done;
     154             :         }
     155             : 
     156           2 :         err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
     157             :                                           &get_includes);
     158           2 :         if (!SBC_ERROR_IS_OK(err)) {
     159           0 :                 printf("FAIL: delete_includes (getting includes) - %s\n",
     160             :                        sbcErrorString(err));
     161           0 :                 goto done;
     162             :         }
     163             : 
     164           2 :         if (get_num_includes != 0) {
     165           0 :                 printf("FAIL: delete_includes (not empty after delete)\n");
     166           0 :                 goto done;
     167             :         }
     168             : 
     169           2 :         err = smbconf_delete_global_includes(ctx);
     170           2 :         if (!SBC_ERROR_IS_OK(err)) {
     171           0 :                 printf("FAIL: delete_includes (delete empty includes) - "
     172             :                        "%s\n", sbcErrorString(err));
     173           0 :                 goto done;
     174             :         }
     175             : 
     176           2 :         printf("OK: delete_includes\n");
     177           2 :         ret = true;
     178             : 
     179           2 : done:
     180           2 :         talloc_free(mem_ctx);
     181           2 :         return ret;
     182             : }
     183             : 
     184           2 : static bool create_conf_file(const char *filename)
     185             : {
     186             :         FILE *f;
     187             : 
     188           2 :         printf("TEST: creating file\n");
     189           2 :         f = fopen(filename, "w");
     190           2 :         if (!f) {
     191           0 :                 printf("failure: failed to open %s for writing: %s\n",
     192           0 :                        filename, strerror(errno));
     193           0 :                 return false;
     194             :         }
     195             : 
     196           2 :         fprintf(f, "[global]\n");
     197           2 :         fprintf(f, "\tserver string = smbconf testsuite\n");
     198           2 :         fprintf(f, "\tworkgroup = SAMBA\n");
     199           2 :         fprintf(f, "\tsecurity = user\n");
     200             : 
     201           2 :         fclose(f);
     202             : 
     203           2 :         printf("OK: create file\n");
     204           2 :         return true;
     205             : }
     206             : 
     207           2 : static bool torture_smbconf_txt(void)
     208             : {
     209             :         sbcErr err;
     210           2 :         bool ret = true;
     211           2 :         const char *filename = "/tmp/smb.conf.smbconf_testsuite";
     212           2 :         struct smbconf_ctx *conf_ctx = NULL;
     213           2 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     214             : 
     215           2 :         printf("test: text backend\n");
     216             : 
     217           2 :         if (!create_conf_file(filename)) {
     218           0 :                 ret = false;
     219           0 :                 goto done;
     220             :         }
     221             : 
     222           2 :         printf("TEST: init\n");
     223           2 :         err = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
     224           2 :         if (!SBC_ERROR_IS_OK(err)) {
     225           0 :                 printf("FAIL: text backend failed: %s\n", sbcErrorString(err));
     226           0 :                 ret = false;
     227           0 :                 goto done;
     228             :         }
     229           2 :         printf("OK: init\n");
     230             : 
     231           2 :         ret &= test_get_includes(conf_ctx);
     232             : 
     233           2 :         smbconf_shutdown(conf_ctx);
     234             : 
     235           2 :         printf("TEST: unlink file\n");
     236           2 :         if (unlink(filename) != 0) {
     237           0 :                 printf("OK: unlink failed: %s\n", strerror(errno));
     238           0 :                 ret = false;
     239           0 :                 goto done;
     240             :         }
     241           2 :         printf("OK: unlink file\n");
     242             : 
     243           2 : done:
     244           2 :         printf("%s: text backend\n", ret ? "success" : "failure");
     245           2 :         talloc_free(mem_ctx);
     246           2 :         return ret;
     247             : }
     248             : 
     249           2 : static bool torture_smbconf_reg(void)
     250             : {
     251             :         sbcErr err;
     252           2 :         bool ret = true;
     253           2 :         struct smbconf_ctx *conf_ctx = NULL;
     254           2 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     255             : 
     256           2 :         printf("test: registry backend\n");
     257             : 
     258           2 :         printf("TEST: init\n");
     259           2 :         err = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
     260           2 :         if (!SBC_ERROR_IS_OK(err)) {
     261           0 :                 printf("FAIL: init failed: %s\n", sbcErrorString(err));
     262           0 :                 ret = false;
     263           0 :                 goto done;
     264             :         }
     265           2 :         printf("OK: init\n");
     266             : 
     267           2 :         ret &= test_get_includes(conf_ctx);
     268           2 :         ret &= test_set_get_includes(conf_ctx);
     269           2 :         ret &= test_delete_includes(conf_ctx);
     270             : 
     271           2 :         smbconf_shutdown(conf_ctx);
     272             : 
     273           2 : done:
     274           2 :         printf("%s: registry backend\n", ret ? "success" : "failure");
     275           2 :         talloc_free(mem_ctx);
     276           2 :         return ret;
     277             : }
     278             : 
     279           2 : static bool torture_smbconf(void)
     280             : {
     281           2 :         bool ret = true;
     282           2 :         ret &= torture_smbconf_txt();
     283           2 :         printf("\n");
     284           2 :         ret &= torture_smbconf_reg();
     285           2 :         return ret;
     286             : }
     287             : 
     288           2 : int main(int argc, const char **argv)
     289             : {
     290             :         bool ret;
     291             :         poptContext pc;
     292           2 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     293             :         int opt;
     294             : 
     295           6 :         struct poptOption long_options[] = {
     296           2 :                 POPT_COMMON_SAMBA
     297           2 :                 POPT_COMMON_VERSION
     298             :                 POPT_TABLEEND
     299             :         };
     300             : 
     301           2 :         smb_init_locale();
     302             : 
     303           2 :         ret = samba_cmdline_init(mem_ctx,
     304             :                                  SAMBA_CMDLINE_CONFIG_CLIENT,
     305             :                                  true /* require_smbconf */);
     306           2 :         if (!ret) {
     307           0 :                 goto done;
     308             :         }
     309             : 
     310             :         /* parse options */
     311           2 :         pc = samba_popt_get_context(getprogname(),
     312             :                                     argc,
     313             :                                     (const char **)argv,
     314             :                                     long_options,
     315             :                                     0);
     316           2 :         if (pc == NULL) {
     317           0 :                 ret = false;
     318           0 :                 goto done;
     319             :         }
     320             : 
     321           2 :         while ((opt = poptGetNextOpt(pc)) != -1) {
     322           0 :                 switch (opt) {
     323           0 :                 case POPT_ERROR_BADOPT:
     324           0 :                         fprintf(stderr, "\nInvalid option %s: %s\n\n",
     325             :                                 poptBadOption(pc, 0), poptStrerror(opt));
     326           0 :                         poptPrintUsage(pc, stderr, 0);
     327           0 :                         exit(1);
     328             :                 }
     329             :         }
     330             : 
     331           2 :         poptFreeContext(pc);
     332             : 
     333           2 :         ret = torture_smbconf();
     334             : 
     335           2 : done:
     336           2 :         talloc_free(mem_ctx);
     337           2 :         return ret ? 0 : -1;
     338             : }

Generated by: LCOV version 1.14