ctdb-server: Remove duplicate logic
[samba4-gss.git] / source3 / rpc_server / mdssvc / test_mdsparser_es.c
blob02270a9b0662e82237c899693d9176a691ee6ce9
1 /*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Ralph Boehme 2019
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.
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.
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/>.
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"
31 #define PATH_QUERY_SUBEXPR \
32 " AND path.real.fulltext:\\\"/foo/bar\\\""
34 static struct {
35 const char *mds;
36 const char *es;
37 } map[] = {
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
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
164 static struct {
165 const char *mds;
166 const char *es;
167 } map_ignore_failures[] = {
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
198 static void test_mdsparser_es(void **state)
200 TALLOC_CTX *frame = talloc_stackframe();
201 const char *path_scope = "/foo/bar";
202 char *es_query = NULL;
203 const char *path = NULL;
204 json_t *mappings = NULL;
205 json_error_t json_error;
206 int i;
207 bool ok;
209 path = lp_parm_const_string(GLOBAL_SECTION_SNUM,
210 "elasticsearch",
211 "mappings",
212 NULL);
213 assert_non_null(path);
215 mappings = json_load_file(path, 0, &json_error);
216 assert_non_null(mappings);
218 for (i = 0; i < ARRAY_SIZE(map); i++) {
219 DBG_DEBUG("Mapping: %s\n", map[i].mds);
220 ok = map_spotlight_to_es_query(frame,
221 mappings,
222 path_scope,
223 map[i].mds,
224 &es_query);
225 assert_true(ok);
226 assert_string_equal(es_query, map[i].es);
229 if (!lp_parm_bool(GLOBAL_SECTION_SNUM,
230 "elasticsearch",
231 "test mapping failures",
232 false))
234 goto done;
237 for (i = 0; i < ARRAY_SIZE(map_ignore_failures); i++) {
238 DBG_DEBUG("Mapping: %s\n", map_ignore_failures[i].mds);
239 ok = map_spotlight_to_es_query(frame,
240 mappings,
241 path_scope,
242 map_ignore_failures[i].mds,
243 &es_query);
244 assert_true(ok);
245 assert_string_equal(es_query, map_ignore_failures[i].es);
248 done:
249 json_decref(mappings);
250 TALLOC_FREE(frame);
253 int main(int argc, const char *argv[])
255 const struct CMUnitTest tests[] = {
256 cmocka_unit_test(test_mdsparser_es),
258 struct poptOption long_options[] = {
259 POPT_AUTOHELP
260 POPT_COMMON_SAMBA
261 POPT_TABLEEND
263 poptContext pc;
264 int opt;
265 bool ok;
266 TALLOC_CTX *frame = talloc_stackframe();
267 struct loadparm_context *lp_ctx = NULL;
269 smb_init_locale();
271 ok = samba_cmdline_init(frame,
272 SAMBA_CMDLINE_CONFIG_CLIENT,
273 false /* require_smbconf */);
274 if (!ok) {
275 DBG_ERR("Failed to init cmdline parser!\n");
276 TALLOC_FREE(frame);
277 exit(1);
279 lp_ctx = samba_cmdline_get_lp_ctx();
280 lpcfg_set_cmdline(lp_ctx, "log level", "1");
282 pc = samba_popt_get_context(getprogname(),
283 argc,
284 argv,
285 long_options,
287 if (pc == NULL) {
288 DBG_ERR("Failed to setup popt context!\n");
289 TALLOC_FREE(frame);
290 exit(1);
293 while ((opt = poptGetNextOpt(pc)) != -1) {
294 switch(opt) {
295 default:
296 fprintf(stderr, "Unknown Option: %c\n", opt);
297 exit(1);
301 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
303 return cmocka_run_group_tests(tests, NULL, NULL);