@@ -0,0 +1,241 @@
1+'''
2+searx is free software: you can redistribute it and/or modify
3+it under the terms of the GNU Affero General Public License as published by
4+the Free Software Foundation, either version 3 of the License, or
5+(at your option) any later version.
6+
7+searx is distributed in the hope that it will be useful,
8+but WITHOUT ANY WARRANTY; without even the implied warranty of
9+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+GNU Affero General Public License for more details.
11+
12+You should have received a copy of the GNU Affero General Public License
13+along with searx. If not, see < http://www.gnu.org/licenses/ >.
14+
15+'''
16+
17+from sys import version_info
18+
19+from searx.engines import command as command_engine
20+from searx.testing import SearxTestCase
21+
22+
23+class TestCommandEngine(SearxTestCase):
24+ def test_basic_seq_command_engine(self):
25+ ls_engine = command_engine
26+ ls_engine.command = ['seq', '{{QUERY}}']
27+ ls_engine.delimiter = {'chars': ' ', 'keys': ['number']}
28+ expected_results = [
29+ {'number': '1', 'template': 'key-value.html'},
30+ {'number': '2', 'template': 'key-value.html'},
31+ {'number': '3', 'template': 'key-value.html'},
32+ {'number': '4', 'template': 'key-value.html'},
33+ {'number': '5', 'template': 'key-value.html'},
34+ ]
35+ results = ls_engine.search('5'.encode('utf-8'), {'pageno': 1})
36+ self.assertEqual(results, expected_results)
37+
38+ def test_delimiter_parsing_command_engine(self):
39+ searx_logs = '''DEBUG:searx.webapp:static directory is /home/n/p/searx/searx/static
40+DEBUG:searx.webapp:templates directory is /home/n/p/searx/searx/templates
41+DEBUG:searx.engines:soundcloud engine: Starting background initialization
42+DEBUG:searx.engines:wolframalpha engine: Starting background initialization
43+DEBUG:searx.engines:locate engine: Starting background initialization
44+DEBUG:searx.engines:regex search in files engine: Starting background initialization
45+DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): www.wolframalpha.com
46+DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): soundcloud.com
47+DEBUG:searx.engines:find engine: Starting background initialization
48+DEBUG:searx.engines:pattern search in files engine: Starting background initialization
49+DEBUG:searx.webapp:starting webserver on 127.0.0.1:8888
50+WARNING:werkzeug: * Debugger is active!
51+INFO:werkzeug: * Debugger PIN: 299-578-362'''
52+ echo_engine = command_engine
53+ echo_engine.command = ['echo', searx_logs]
54+ echo_engine.delimiter = {'chars': ':', 'keys': ['level', 'component', 'message']}
55+
56+ expected_results_by_page = [
57+ [
58+ {
59+ 'component': 'searx.webapp',
60+ 'message': 'static directory is /home/n/p/searx/searx/static',
61+ 'template': 'key-value.html',
62+ 'level': 'DEBUG',
63+ },
64+ {
65+ 'component': 'searx.webapp',
66+ 'message': 'templates directory is /home/n/p/searx/searx/templates',
67+ 'template': 'key-value.html',
68+ 'level': 'DEBUG',
69+ },
70+ {
71+ 'component': 'searx.engines',
72+ 'message': 'soundcloud engine: Starting background initialization',
73+ 'template': 'key-value.html',
74+ 'level': 'DEBUG',
75+ },
76+ {
77+ 'component': 'searx.engines',
78+ 'message': 'wolframalpha engine: Starting background initialization',
79+ 'template': 'key-value.html',
80+ 'level': 'DEBUG',
81+ },
82+ {
83+ 'component': 'searx.engines',
84+ 'message': 'locate engine: Starting background initialization',
85+ 'template': 'key-value.html',
86+ 'level': 'DEBUG',
87+ },
88+ {
89+ 'component': 'searx.engines',
90+ 'message': 'regex search in files engine: Starting background initialization',
91+ 'template': 'key-value.html',
92+ 'level': 'DEBUG',
93+ },
94+ {
95+ 'component': 'urllib3.connectionpool',
96+ 'message': 'Starting new HTTPS connection (1): www.wolframalpha.com',
97+ 'template': 'key-value.html',
98+ 'level': 'DEBUG',
99+ },
100+ {
101+ 'component': 'urllib3.connectionpool',
102+ 'message': 'Starting new HTTPS connection (1): soundcloud.com',
103+ 'template': 'key-value.html',
104+ 'level': 'DEBUG',
105+ },
106+ {
107+ 'component': 'searx.engines',
108+ 'message': 'find engine: Starting background initialization',
109+ 'template': 'key-value.html',
110+ 'level': 'DEBUG',
111+ },
112+ {
113+ 'component': 'searx.engines',
114+ 'message': 'pattern search in files engine: Starting background initialization',
115+ 'template': 'key-value.html',
116+ 'level': 'DEBUG',
117+ },
118+
119+ ],
120+ [
121+ {
122+ 'component': 'searx.webapp',
123+ 'message': 'starting webserver on 127.0.0.1:8888',
124+ 'template': 'key-value.html',
125+ 'level': 'DEBUG',
126+ },
127+ {
128+ 'component': 'werkzeug',
129+ 'message': ' * Debugger is active!',
130+ 'template': 'key-value.html',
131+ 'level': 'WARNING',
132+ },
133+ {
134+ 'component': 'werkzeug',
135+ 'message': ' * Debugger PIN: 299-578-362',
136+ 'template': 'key-value.html',
137+ 'level': 'INFO',
138+ },
139+ ],
140+
141+ ]
142+
143+ for i in [0, 1]:
144+ results = echo_engine.search(''.encode('utf-8'), {'pageno': i + 1})
145+ self.assertEqual(results, expected_results_by_page[i])
146+
147+ def test_regex_parsing_command_engine(self):
148+ txt = '''commit 35f9a8c81d162a361b826bbcd4a1081a4fbe76a7
149+Author: Noémi Ványi <sitbackandwait@gmail.com>
150+Date: Tue Oct 15 11:31:33 2019 +0200
151+
152+first interesting message
153+
154+commit 6c3c206316153ccc422755512bceaa9ab0b14faa
155+Author: Noémi Ványi <sitbackandwait@gmail.com>
156+Date: Mon Oct 14 17:10:08 2019 +0200
157+
158+second interesting message
159+
160+commit d8594d2689b4d5e0d2f80250223886c3a1805ef5
161+Author: Noémi Ványi <sitbackandwait@gmail.com>
162+Date: Mon Oct 14 14:45:05 2019 +0200
163+
164+third interesting message
165+
166+commit '''
167+ git_log_engine = command_engine
168+ git_log_engine.command = ['echo', txt]
169+ git_log_engine.result_separator = '\n\ncommit '
170+ git_log_engine.delimiter = {}
171+ git_log_engine.parse_regex = {
172+ 'commit': '\w{40}',
173+ 'author': '[\w* ]* <\w*@?\w*\.?\w*>',
174+ 'date': 'Date: .*',
175+ 'message': '\n\n.*$'
176+ }
177+ expected_results = [
178+ {
179+ 'commit': '35f9a8c81d162a361b826bbcd4a1081a4fbe76a7',
180+ 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
181+ 'date': 'Date: Tue Oct 15 11:31:33 2019 +0200',
182+ 'message': '\n\nfirst interesting message',
183+ 'template': 'key-value.html',
184+ },
185+ {
186+ 'commit': '6c3c206316153ccc422755512bceaa9ab0b14faa',
187+ 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
188+ 'date': 'Date: Mon Oct 14 17:10:08 2019 +0200',
189+ 'message': '\n\nsecond interesting message',
190+ 'template': 'key-value.html',
191+ },
192+ {
193+ 'commit': 'd8594d2689b4d5e0d2f80250223886c3a1805ef5',
194+ 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
195+ 'date': 'Date: Mon Oct 14 14:45:05 2019 +0200',
196+ 'message': '\n\nthird interesting message',
197+ 'template': 'key-value.html',
198+ },
199+
200+ ]
201+
202+ results = git_log_engine.search(''.encode('utf-8'), {'pageno': 1})
203+ self.assertEqual(results, expected_results)
204+
205+ def test_working_dir_path_query(self):
206+ ls_engine = command_engine
207+ ls_engine.command = ['ls', '{{QUERY}}']
208+ ls_engine.result_separator = '\n'
209+ ls_engine.delimiter = {'chars': ' ', 'keys': ['file']}
210+ ls_engine.query_type = 'path'
211+
212+ results = ls_engine.search('.'.encode(), {'pageno': 1})
213+ self.assertTrue(len(results) != 0)
214+
215+ forbidden_paths = [
216+ '..',
217+ '../..',
218+ './..',
219+ '~',
220+ '/var',
221+ ]
222+ for forbidden_path in forbidden_paths:
223+ self.assertRaises(ValueError, ls_engine.search, '..'.encode(), {'pageno': 1})
224+
225+ def test_enum_queries(self):
226+ echo_engine = command_engine
227+ echo_engine.command = ['echo', '{{QUERY}}']
228+ echo_engine.query_type = 'enum'
229+ echo_engine.query_enum = ['i-am-allowed-to-say-this', 'and-that']
230+
231+ for allowed in echo_engine.query_enum:
232+ results = echo_engine.search(allowed.encode(), {'pageno': 1})
233+ self.assertTrue(len(results) != 0)
234+
235+ forbidden_queries = [
236+ 'forbidden',
237+ 'banned',
238+ 'prohibited',
239+ ]
240+ for forbidden in forbidden_queries:
241+ self.assertRaises(ValueError, echo_engine.search, forbidden.encode(), {'pageno': 1})