LCOV - code coverage report
Current view: top level - source3/rpc_server/mdssvc - test_mdsparser_es.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 48 58 82.8 %
Date: 2024-02-28 12:06:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  Copyright (C) Ralph Boehme 2019
       4             :  *
       5             :  *  This program is free software; you can redistribute it and/or modify
       6             :  *  it under the terms of the GNU General Public License as published by
       7             :  *  the Free Software Foundation; either version 3 of the License, or
       8             :  *  (at your option) any later version.
       9             :  *
      10             :  *  This program is distributed in the hope that it will be useful,
      11             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :  *  GNU General Public License for more details.
      14             :  *
      15             :  *  You should have received a copy of the GNU General Public License
      16             :  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
      17             :  */
      18             : 
      19             : #include "includes.h"
      20             : #include <setjmp.h>
      21             : #include <cmocka.h>
      22             : #include <jansson.h>
      23             : #include <talloc.h>
      24             : #include "lib/cmdline/cmdline.h"
      25             : #include "libcli/util/ntstatus.h"
      26             : #include "lib/util/samba_util.h"
      27             : #include "lib/torture/torture.h"
      28             : #include "lib/param/param.h"
      29             : #include "rpc_server/mdssvc/es_parser.tab.h"
      30             : 
      31             : #define PATH_QUERY_SUBEXPR \
      32             :         " AND path.real.fulltext:\\\"/foo/bar\\\""
      33             : 
      34             : static struct {
      35             :         const char *mds;
      36             :         const char *es;
      37             : } map[] = {
      38             :         {
      39             :                 "*==\"samba\"",
      40             :                 "(samba)" PATH_QUERY_SUBEXPR
      41             :         }, {
      42             :                 "kMDItemTextContent==\"samba\"",
      43             :                 "(content:samba)" PATH_QUERY_SUBEXPR
      44             :         }, {
      45             :                 "_kMDItemGroupId==\"11\"",
      46             :                 "(file.content_type:(application\\\\/pdf))" PATH_QUERY_SUBEXPR
      47             :         }, {
      48             :                 "kMDItemContentType==\"1\"",
      49             :                 "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR
      50             :         }, {
      51             :                 "kMDItemContentType==\"public.content\"",
      52             :                 "(file.content_type:(message\\\\/rfc822 application\\\\/pdf application\\\\/vnd.oasis.opendocument.presentation image\\\\/* text\\\\/*))" PATH_QUERY_SUBEXPR
      53             :         }, {
      54             :                 "kMDItemContentTypeTree==\"1\"",
      55             :                 "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR
      56             :         }, {
      57             :                 "kMDItemFSContentChangeDate==$time.iso(2018-10-01T10:00:00Z)",
      58             :                 "(file.last_modified:2018\\\\-10\\\\-01T10\\\\:00\\\\:00Z)" PATH_QUERY_SUBEXPR
      59             :         }, {
      60             :                 "kMDItemFSContentChangeDate==\"1\"",
      61             :                 "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
      62             :         }, {
      63             :                 "kMDItemFSCreationDate==\"1\"",
      64             :                 "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
      65             :         }, {
      66             :                 "kMDItemFSName==\"samba*\"",
      67             :                 "(file.filename:samba*)" PATH_QUERY_SUBEXPR
      68             :         }, {
      69             :                 "kMDItemFSOwnerGroupID==\"0\"",
      70             :                 "(attributes.owner:0)" PATH_QUERY_SUBEXPR
      71             :         }, {
      72             :                 "kMDItemFSOwnerUserID==\"0\"",
      73             :                 "(attributes.group:0)" PATH_QUERY_SUBEXPR
      74             :         }, {
      75             :                 "kMDItemFSSize==\"1\"",
      76             :                 "(file.filesize:1)" PATH_QUERY_SUBEXPR
      77             :         }, {
      78             :                 "kMDItemPath==\"/foo/bar\"",
      79             :                 "(path.real:\\\\/foo\\\\/bar)" PATH_QUERY_SUBEXPR
      80             :         }, {
      81             :                 "kMDItemAttributeChangeDate==\"1\"",
      82             :                 "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
      83             :         }, {
      84             :                 "kMDItemAuthors==\"Chouka\"",
      85             :                 "(meta.author:Chouka)" PATH_QUERY_SUBEXPR
      86             :         }, {
      87             :                 "kMDItemContentCreationDate==\"1\"",
      88             :                 "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
      89             :         }, {
      90             :                 "kMDItemContentModificationDate==\"1\"",
      91             :                 "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
      92             :         }, {
      93             :                 "kMDItemCreator==\"Chouka\"",
      94             :                 "(meta.raw.creator:Chouka)" PATH_QUERY_SUBEXPR
      95             :         }, {
      96             :                 "kMDItemDescription==\"Dog\"",
      97             :                 "(meta.raw.description:Dog)" PATH_QUERY_SUBEXPR
      98             :         }, {
      99             :                 "kMDItemDisplayName==\"Samba\"",
     100             :                 "(file.filename:Samba)" PATH_QUERY_SUBEXPR
     101             :         }, {
     102             :                 "kMDItemDurationSeconds==\"1\"",
     103             :                 "(meta.raw.xmpDM\\\\:duration:1)" PATH_QUERY_SUBEXPR
     104             :         }, {
     105             :                 "kMDItemNumberOfPages==\"1\"",
     106             :                 "(meta.raw.xmpTPg\\\\:NPages:1)" PATH_QUERY_SUBEXPR
     107             :         }, {
     108             :                 "kMDItemTitle==\"Samba\"",
     109             :                 "(meta.title:Samba)" PATH_QUERY_SUBEXPR
     110             :         }, {
     111             :                 "kMDItemAlbum==\"Red Roses for Me\"",
     112             :                 "(meta.raw.xmpDM\\\\:album:Red\\\\ Roses\\\\ for\\\\ Me)" PATH_QUERY_SUBEXPR
     113             :         }, {
     114             :                 "kMDItemBitsPerSample==\"1\"",
     115             :                 "(meta.raw.tiff\\\\:BitsPerSample:1)" PATH_QUERY_SUBEXPR
     116             :         }, {
     117             :                 "kMDItemPixelHeight==\"1\"",
     118             :                 "(meta.raw.Image\\\\ Height:1)" PATH_QUERY_SUBEXPR
     119             :         }, {
     120             :                 "kMDItemPixelWidth==\"1\"",
     121             :                 "(meta.raw.Image\\\\ Width:1)" PATH_QUERY_SUBEXPR
     122             :         }, {
     123             :                 "kMDItemResolutionHeightDPI==\"72\"",
     124             :                 "(meta.raw.Y\\\\ Resolution:72)" PATH_QUERY_SUBEXPR
     125             :         }, {
     126             :                 "kMDItemResolutionWidthDPI==\"72\"",
     127             :                 "(meta.raw.X\\\\ Resolution:72)" PATH_QUERY_SUBEXPR
     128             :         },{
     129             :                 "*!=\"samba\"",
     130             :                 "((NOT samba))" PATH_QUERY_SUBEXPR
     131             :         }, {
     132             :                 "kMDItemFSSize!=\"1\"",
     133             :                 "((NOT file.filesize:1))" PATH_QUERY_SUBEXPR
     134             :         }, {
     135             :                 "kMDItemFSSize>\"1\"",
     136             :                 "(file.filesize:{1 TO *})" PATH_QUERY_SUBEXPR
     137             :         }, {
     138             :                 "kMDItemFSSize<\"1\"",
     139             :                 "(file.filesize:{* TO 1})" PATH_QUERY_SUBEXPR
     140             :         }, {
     141             :                 "kMDItemFSCreationDate!=\"1\"",
     142             :                 "((NOT file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z))" PATH_QUERY_SUBEXPR
     143             :         }, {
     144             :                 "kMDItemFSCreationDate>\"1\"",
     145             :                 "(file.created:{2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO *})" PATH_QUERY_SUBEXPR
     146             :         }, {
     147             :                 "kMDItemFSCreationDate<\"1\"",
     148             :                 "(file.created:{* TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z})" PATH_QUERY_SUBEXPR
     149             :         }, {
     150             :                 "kMDItemFSName==\"Samba\"||kMDItemTextContent==\"Samba\"",
     151             :                 "(file.filename:Samba OR content:Samba)" PATH_QUERY_SUBEXPR
     152             :         }, {
     153             :                 "kMDItemFSName==\"Samba\"&&kMDItemTextContent==\"Samba\"",
     154             :                 "((file.filename:Samba) AND (content:Samba))" PATH_QUERY_SUBEXPR
     155             :         }, {
     156             :                 "InRange(kMDItemFSCreationDate,1,2)",
     157             :                 "(file.created:[2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:02Z])" PATH_QUERY_SUBEXPR
     158             :         }, {
     159             :                 "InRange(kMDItemFSSize,1,2)",
     160             :                 "(file.filesize:[1 TO 2])" PATH_QUERY_SUBEXPR
     161             :         }
     162             : };
     163             : 
     164             : static struct {
     165             :         const char *mds;
     166             :         const char *es;
     167             : } map_ignore_failures[] = {
     168             :         {
     169             :                 "*==\"Samba\"||foo==\"bar\"",
     170             :                 "(Samba)" PATH_QUERY_SUBEXPR
     171             :         }, {
     172             :                 "*==\"Samba\"&&foo==\"bar\"",
     173             :                 "(Samba)" PATH_QUERY_SUBEXPR
     174             :         }, {
     175             :                 "*==\"Samba\"||kMDItemContentType==\"666\"",
     176             :                 "(Samba)" PATH_QUERY_SUBEXPR
     177             :         }, {
     178             :                 "*==\"Samba\"&&kMDItemContentType==\"666\"",
     179             :                 "(Samba)" PATH_QUERY_SUBEXPR
     180             :         }, {
     181             :                 "*==\"Samba\"||foo==\"bar\"||kMDItemContentType==\"666\"",
     182             :                 "(Samba)" PATH_QUERY_SUBEXPR
     183             :         }, {
     184             :                 "*==\"Samba\"&&foo==\"bar\"&&kMDItemContentType==\"666\"",
     185             :                 "(Samba)" PATH_QUERY_SUBEXPR
     186             :         }, {
     187             :                 "foo==\"bar\"||kMDItemContentType==\"666\"||*==\"Samba\"||x!=\"6\"",
     188             :                 "(Samba)" PATH_QUERY_SUBEXPR
     189             :         }, {
     190             :                 "*==\"Samba\"||InRange(foo,1,2)",
     191             :                 "(Samba)" PATH_QUERY_SUBEXPR
     192             :         }, {
     193             :                 "*==\"Samba\"||foo==$time.iso(2018-10-01T10:00:00Z)",
     194             :                 "(Samba)" PATH_QUERY_SUBEXPR
     195             :         }
     196             : };
     197             : 
     198           2 : static void test_mdsparser_es(void **state)
     199             : {
     200           2 :         TALLOC_CTX *frame = talloc_stackframe();
     201           2 :         const char *path_scope = "/foo/bar";
     202           2 :         char *es_query = NULL;
     203           2 :         const char *path = NULL;
     204           2 :         json_t *mappings = NULL;
     205           2 :         json_error_t json_error;
     206           2 :         int i;
     207           2 :         bool ok;
     208             : 
     209           2 :         path = lp_parm_const_string(GLOBAL_SECTION_SNUM,
     210             :                                     "elasticsearch",
     211             :                                     "mappings",
     212             :                                     NULL);
     213           2 :         assert_non_null(path);
     214             : 
     215           2 :         mappings = json_load_file(path, 0, &json_error);
     216           2 :         assert_non_null(mappings);
     217             : 
     218          86 :         for (i = 0; i < ARRAY_SIZE(map); i++) {
     219          82 :                 DBG_DEBUG("Mapping: %s\n", map[i].mds);
     220          82 :                 ok = map_spotlight_to_es_query(frame,
     221             :                                                mappings,
     222             :                                                path_scope,
     223             :                                                map[i].mds,
     224             :                                                &es_query);
     225          82 :                 assert_true(ok);
     226          82 :                 assert_string_equal(es_query, map[i].es);
     227             :         }
     228             : 
     229           2 :         if (!lp_parm_bool(GLOBAL_SECTION_SNUM,
     230             :                           "elasticsearch",
     231             :                           "test mapping failures",
     232             :                           false))
     233             :         {
     234           1 :                 goto done;
     235             :         }
     236             : 
     237          10 :         for (i = 0; i < ARRAY_SIZE(map_ignore_failures); i++) {
     238           9 :                 DBG_DEBUG("Mapping: %s\n", map_ignore_failures[i].mds);
     239           9 :                 ok = map_spotlight_to_es_query(frame,
     240             :                                                mappings,
     241             :                                                path_scope,
     242             :                                                map_ignore_failures[i].mds,
     243             :                                                &es_query);
     244           9 :                 assert_true(ok);
     245           9 :                 assert_string_equal(es_query, map_ignore_failures[i].es);
     246             :         }
     247             : 
     248           1 : done:
     249           2 :         json_decref(mappings);
     250           2 :         TALLOC_FREE(frame);
     251           2 : }
     252             : 
     253           2 : int main(int argc, const char *argv[])
     254             : {
     255           2 :         const struct CMUnitTest tests[] = {
     256             :                 cmocka_unit_test(test_mdsparser_es),
     257             :         };
     258           4 :         struct poptOption long_options[] = {
     259             :                 POPT_AUTOHELP
     260           2 :                 POPT_COMMON_SAMBA
     261             :                 POPT_TABLEEND
     262             :         };
     263           2 :         poptContext pc;
     264           2 :         int opt;
     265           2 :         bool ok;
     266           2 :         TALLOC_CTX *frame = talloc_stackframe();
     267           2 :         struct loadparm_context *lp_ctx = NULL;
     268             : 
     269           2 :         smb_init_locale();
     270             : 
     271           2 :         ok = samba_cmdline_init(frame,
     272             :                                 SAMBA_CMDLINE_CONFIG_CLIENT,
     273             :                                 false /* require_smbconf */);
     274           2 :         if (!ok) {
     275           0 :                 DBG_ERR("Failed to init cmdline parser!\n");
     276           0 :                 TALLOC_FREE(frame);
     277           0 :                 exit(1);
     278             :         }
     279           2 :         lp_ctx = samba_cmdline_get_lp_ctx();
     280           2 :         lpcfg_set_cmdline(lp_ctx, "log level", "1");
     281             : 
     282           2 :         pc = samba_popt_get_context(getprogname(),
     283             :                                     argc,
     284             :                                     argv,
     285             :                                     long_options,
     286             :                                     0);
     287           2 :         if (pc == NULL) {
     288           0 :                 DBG_ERR("Failed to setup popt context!\n");
     289           0 :                 TALLOC_FREE(frame);
     290           0 :                 exit(1);
     291             :         }
     292             : 
     293           2 :         while ((opt = poptGetNextOpt(pc)) != -1) {
     294           0 :                 switch(opt) {
     295           0 :                     default:
     296           0 :                             fprintf(stderr, "Unknown Option: %c\n", opt);
     297           0 :                             exit(1);
     298             :                 }
     299             :         }
     300             : 
     301           2 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     302             : 
     303           2 :         return cmocka_run_group_tests(tests, NULL, NULL);
     304             : }

Generated by: LCOV version 1.14