| Línea 69... |
Línea 69... |
| 69 |
|
69 |
|
| 70 |
protected function tearDown(): void {
|
70 |
protected function tearDown(): void {
|
| 71 |
@unlink($this->tempfile);
|
71 |
@unlink($this->tempfile);
|
| Línea 72... |
Línea 72... |
| 72 |
}
|
72 |
}
|
| 73 |
|
73 |
|
| 74 |
public function test_manager_get_antivirus() {
|
74 |
public function test_manager_get_antivirus(): void {
|
| 75 |
// We are using clamav plugin in the test,
|
75 |
// We are using clamav plugin in the test,
|
| 76 |
// as the only plugin we know exists for sure.
|
76 |
// as the only plugin we know exists for sure.
|
| 77 |
$antivirusviaget = \core\antivirus\manager::get_antivirus('clamav');
|
77 |
$antivirusviaget = \core\antivirus\manager::get_antivirus('clamav');
|
| 78 |
$antivirusdirect = new \antivirus_clamav\scanner();
|
78 |
$antivirusdirect = new \antivirus_clamav\scanner();
|
| Línea 79... |
Línea 79... |
| 79 |
$this->assertEquals($antivirusdirect, $antivirusviaget);
|
79 |
$this->assertEquals($antivirusdirect, $antivirusviaget);
|
| 80 |
}
|
80 |
}
|
| 81 |
|
81 |
|
| 82 |
public function test_manager_scan_file_no_virus() {
|
82 |
public function test_manager_scan_file_no_virus(): void {
|
| 83 |
// Run mock scanning.
|
83 |
// Run mock scanning.
|
| 84 |
$this->assertFileExists($this->tempfile);
|
84 |
$this->assertFileExists($this->tempfile);
|
| 85 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
85 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
| Línea 86... |
Línea 86... |
| 86 |
// File expected to remain in place.
|
86 |
// File expected to remain in place.
|
| 87 |
$this->assertFileExists($this->tempfile);
|
87 |
$this->assertFileExists($this->tempfile);
|
| 88 |
}
|
88 |
}
|
| 89 |
|
89 |
|
| 90 |
public function test_manager_scan_file_error() {
|
90 |
public function test_manager_scan_file_error(): void {
|
| 91 |
// Run mock scanning.
|
91 |
// Run mock scanning.
|
| 92 |
$this->assertFileExists($this->tempfile);
|
92 |
$this->assertFileExists($this->tempfile);
|
| Línea 93... |
Línea 93... |
| 93 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
|
93 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
|
| 94 |
// File expected to remain in place.
|
94 |
// File expected to remain in place.
|
| 95 |
$this->assertFileExists($this->tempfile);
|
95 |
$this->assertFileExists($this->tempfile);
|
| 96 |
}
|
96 |
}
|
| 97 |
|
97 |
|
| 98 |
// Check API for NA status i.e. when no scanners are enabled.
|
98 |
// Check API for NA status i.e. when no scanners are enabled.
|
| 99 |
public function test_antivirus_check_na() {
|
99 |
public function test_antivirus_check_na(): void {
|
| Línea 109... |
Línea 109... |
| 109 |
// File expected to remain in place.
|
109 |
// File expected to remain in place.
|
| 110 |
$this->assertFileExists($this->tempfile);
|
110 |
$this->assertFileExists($this->tempfile);
|
| 111 |
}
|
111 |
}
|
| Línea 112... |
Línea 112... |
| 112 |
|
112 |
|
| 113 |
// Check API for UNKNOWN status i.e. when the system's logstore reader is not '\core\log\sql_internal_table_reader'.
|
113 |
// Check API for UNKNOWN status i.e. when the system's logstore reader is not '\core\log\sql_internal_table_reader'.
|
| 114 |
public function test_antivirus_check_unknown() {
|
114 |
public function test_antivirus_check_unknown(): void {
|
| 115 |
// Run mock scanning.
|
115 |
// Run mock scanning.
|
| 116 |
$this->assertFileExists($this->tempfile);
|
116 |
$this->assertFileExists($this->tempfile);
|
| 117 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
117 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
| 118 |
$this->assertEquals(\core\check\result::UNKNOWN, $this->get_check_api_antivirus_status_result());
|
118 |
$this->assertEquals(\core\check\result::UNKNOWN, $this->get_check_api_antivirus_status_result());
|
| 119 |
// File expected to remain in place.
|
119 |
// File expected to remain in place.
|
| 120 |
$this->assertFileExists($this->tempfile);
|
120 |
$this->assertFileExists($this->tempfile);
|
| Línea 121... |
Línea 121... |
| 121 |
}
|
121 |
}
|
| 122 |
|
122 |
|
| 123 |
// Check API for OK status i.e. antivirus enabled, logstore is ok, no scanner issues occurred recently.
|
123 |
// Check API for OK status i.e. antivirus enabled, logstore is ok, no scanner issues occurred recently.
|
| 124 |
public function test_antivirus_check_ok() {
|
124 |
public function test_antivirus_check_ok(): void {
|
| 125 |
// Enable logs.
|
125 |
// Enable logs.
|
| 126 |
$this->enable_logging();
|
126 |
$this->enable_logging();
|
| 127 |
// Run mock scanning.
|
127 |
// Run mock scanning.
|
| Línea 131... |
Línea 131... |
| 131 |
// File expected to remain in place.
|
131 |
// File expected to remain in place.
|
| 132 |
$this->assertFileExists($this->tempfile);
|
132 |
$this->assertFileExists($this->tempfile);
|
| 133 |
}
|
133 |
}
|
| Línea 134... |
Línea 134... |
| 134 |
|
134 |
|
| 135 |
// Check API for ERROR status i.e. scanner issue within a certain timeframe/threshold.
|
135 |
// Check API for ERROR status i.e. scanner issue within a certain timeframe/threshold.
|
| 136 |
public function test_antivirus_check_error() {
|
136 |
public function test_antivirus_check_error(): void {
|
| 137 |
global $USER, $DB;
|
137 |
global $USER, $DB;
|
| 138 |
// Enable logs.
|
138 |
// Enable logs.
|
| 139 |
$this->enable_logging();
|
139 |
$this->enable_logging();
|
| 140 |
// Set threshold / lookback.
|
140 |
// Set threshold / lookback.
|
| Línea 144... |
Línea 144... |
| 144 |
$this->assertEquals(\core\check\result::ERROR, $this->get_check_api_antivirus_status_result());
|
144 |
$this->assertEquals(\core\check\result::ERROR, $this->get_check_api_antivirus_status_result());
|
| 145 |
// File expected to remain in place.
|
145 |
// File expected to remain in place.
|
| 146 |
$this->assertFileExists($this->tempfile);
|
146 |
$this->assertFileExists($this->tempfile);
|
| 147 |
}
|
147 |
}
|
| Línea 148... |
Línea 148... |
| 148 |
|
148 |
|
| 149 |
public function test_manager_scan_file_virus() {
|
149 |
public function test_manager_scan_file_virus(): void {
|
| 150 |
// Run mock scanning without deleting infected file.
|
150 |
// Run mock scanning without deleting infected file.
|
| 151 |
$this->assertFileExists($this->tempfile);
|
151 |
$this->assertFileExists($this->tempfile);
|
| 152 |
$this->expectException(\core\antivirus\scanner_exception::class);
|
152 |
$this->expectException(\core\antivirus\scanner_exception::class);
|
| 153 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false));
|
153 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false));
|
| Línea 159... |
Línea 159... |
| 159 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true));
|
159 |
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true));
|
| 160 |
// File expected to be deleted.
|
160 |
// File expected to be deleted.
|
| 161 |
$this->assertFileDoesNotExist($this->tempfile);
|
161 |
$this->assertFileDoesNotExist($this->tempfile);
|
| 162 |
}
|
162 |
}
|
| Línea 163... |
Línea 163... |
| 163 |
|
163 |
|
| 164 |
public function test_manager_send_message_to_user_email_scan_file_virus() {
|
164 |
public function test_manager_send_message_to_user_email_scan_file_virus(): void {
|
| 165 |
$sink = $this->redirectEmails();
|
165 |
$sink = $this->redirectEmails();
|
| 166 |
$exception = null;
|
166 |
$exception = null;
|
| 167 |
try {
|
167 |
try {
|
| 168 |
set_config('notifyemail', 'fake@example.com', 'antivirus');
|
168 |
set_config('notifyemail', 'fake@example.com', 'antivirus');
|
| Línea 175... |
Línea 175... |
| 175 |
$this->assertCount(1, $result);
|
175 |
$this->assertCount(1, $result);
|
| 176 |
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
176 |
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
| 177 |
$sink->close();
|
177 |
$sink->close();
|
| 178 |
}
|
178 |
}
|
| Línea 179... |
Línea 179... |
| 179 |
|
179 |
|
| 180 |
public function test_manager_send_message_to_admin_email_scan_file_virus() {
|
180 |
public function test_manager_send_message_to_admin_email_scan_file_virus(): void {
|
| 181 |
$sink = $this->redirectMessages();
|
181 |
$sink = $this->redirectMessages();
|
| 182 |
$exception = null;
|
182 |
$exception = null;
|
| 183 |
try {
|
183 |
try {
|
| 184 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
184 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
| Línea 192... |
Línea 192... |
| 192 |
$this->assertCount(1, $result);
|
192 |
$this->assertCount(1, $result);
|
| 193 |
$this->assertEquals($result[0]->useridto, reset($admins));
|
193 |
$this->assertEquals($result[0]->useridto, reset($admins));
|
| 194 |
$sink->close();
|
194 |
$sink->close();
|
| 195 |
}
|
195 |
}
|
| Línea 196... |
Línea 196... |
| 196 |
|
196 |
|
| 197 |
public function test_manager_quarantine_file_virus() {
|
197 |
public function test_manager_quarantine_file_virus(): void {
|
| 198 |
try {
|
198 |
try {
|
| 199 |
set_config('enablequarantine', true, 'antivirus');
|
199 |
set_config('enablequarantine', true, 'antivirus');
|
| 200 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
200 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
| 201 |
} catch (\core\antivirus\scanner_exception $ex) {
|
201 |
} catch (\core\antivirus\scanner_exception $ex) {
|
| Línea 209... |
Línea 209... |
| 209 |
\core\antivirus\quarantine::clean_up_quarantine_folder(time());
|
209 |
\core\antivirus\quarantine::clean_up_quarantine_folder(time());
|
| 210 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
210 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
| 211 |
$this->assertEquals(0, count($quarantinedfiles));
|
211 |
$this->assertEquals(0, count($quarantinedfiles));
|
| 212 |
}
|
212 |
}
|
| Línea 213... |
Línea 213... |
| 213 |
|
213 |
|
| 214 |
public function test_manager_none_quarantine_file_virus() {
|
214 |
public function test_manager_none_quarantine_file_virus(): void {
|
| 215 |
try {
|
215 |
try {
|
| 216 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
216 |
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
| 217 |
} catch (\core\antivirus\scanner_exception $ex) {
|
217 |
} catch (\core\antivirus\scanner_exception $ex) {
|
| 218 |
$exception = $ex;
|
218 |
$exception = $ex;
|
| 219 |
}
|
219 |
}
|
| 220 |
$this->assertNotEmpty($exception);
|
220 |
$this->assertNotEmpty($exception);
|
| 221 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
221 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
| 222 |
$this->assertEquals(0, count($quarantinedfiles));
|
222 |
$this->assertEquals(0, count($quarantinedfiles));
|
| Línea 223... |
Línea 223... |
| 223 |
}
|
223 |
}
|
| 224 |
|
224 |
|
| 225 |
public function test_manager_scan_data_no_virus() {
|
225 |
public function test_manager_scan_data_no_virus(): void {
|
| 226 |
// Run mock scanning.
|
226 |
// Run mock scanning.
|
| Línea 227... |
Línea 227... |
| 227 |
$this->assertEmpty(\core\antivirus\manager::scan_data('OK'));
|
227 |
$this->assertEmpty(\core\antivirus\manager::scan_data('OK'));
|
| 228 |
}
|
228 |
}
|
| 229 |
|
229 |
|
| 230 |
public function test_manager_scan_data_error() {
|
230 |
public function test_manager_scan_data_error(): void {
|
| Línea 231... |
Línea 231... |
| 231 |
// Run mock scanning.
|
231 |
// Run mock scanning.
|
| 232 |
$this->assertEmpty(\core\antivirus\manager::scan_data('ERROR'));
|
232 |
$this->assertEmpty(\core\antivirus\manager::scan_data('ERROR'));
|
| 233 |
}
|
233 |
}
|
| 234 |
|
234 |
|
| 235 |
public function test_manager_scan_data_virus() {
|
235 |
public function test_manager_scan_data_virus(): void {
|
| Línea 236... |
Línea 236... |
| 236 |
// Run mock scanning.
|
236 |
// Run mock scanning.
|
| 237 |
$this->expectException(\core\antivirus\scanner_exception::class);
|
237 |
$this->expectException(\core\antivirus\scanner_exception::class);
|
| 238 |
$this->assertEmpty(\core\antivirus\manager::scan_data('FOUND'));
|
238 |
$this->assertEmpty(\core\antivirus\manager::scan_data('FOUND'));
|
| 239 |
}
|
239 |
}
|
| 240 |
|
240 |
|
| 241 |
public function test_manager_send_message_to_user_email_scan_data_virus() {
|
241 |
public function test_manager_send_message_to_user_email_scan_data_virus(): void {
|
| Línea 252... |
Línea 252... |
| 252 |
$this->assertCount(1, $result);
|
252 |
$this->assertCount(1, $result);
|
| 253 |
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
253 |
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
| 254 |
$sink->close();
|
254 |
$sink->close();
|
| 255 |
}
|
255 |
}
|
| Línea 256... |
Línea 256... |
| 256 |
|
256 |
|
| 257 |
public function test_manager_send_message_to_admin_email_scan_data_virus() {
|
257 |
public function test_manager_send_message_to_admin_email_scan_data_virus(): void {
|
| 258 |
$sink = $this->redirectMessages();
|
258 |
$sink = $this->redirectMessages();
|
| 259 |
$exception = null;
|
259 |
$exception = null;
|
| 260 |
try {
|
260 |
try {
|
| 261 |
\core\antivirus\manager::scan_data('FOUND');
|
261 |
\core\antivirus\manager::scan_data('FOUND');
|
| Línea 269... |
Línea 269... |
| 269 |
$this->assertCount(1, $result);
|
269 |
$this->assertCount(1, $result);
|
| 270 |
$this->assertEquals($result[0]->useridto, reset($admins));
|
270 |
$this->assertEquals($result[0]->useridto, reset($admins));
|
| 271 |
$sink->close();
|
271 |
$sink->close();
|
| 272 |
}
|
272 |
}
|
| Línea 273... |
Línea 273... |
| 273 |
|
273 |
|
| 274 |
public function test_manager_quarantine_data_virus() {
|
274 |
public function test_manager_quarantine_data_virus(): void {
|
| 275 |
set_config('enablequarantine', true, 'antivirus');
|
275 |
set_config('enablequarantine', true, 'antivirus');
|
| 276 |
$exception = null;
|
276 |
$exception = null;
|
| 277 |
try {
|
277 |
try {
|
| 278 |
\core\antivirus\manager::scan_data('FOUND');
|
278 |
\core\antivirus\manager::scan_data('FOUND');
|
| Línea 288... |
Línea 288... |
| 288 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
288 |
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
| 289 |
$this->assertEquals(0, count($quarantinedfiles));
|
289 |
$this->assertEquals(0, count($quarantinedfiles));
|
| 290 |
}
|
290 |
}
|
| Línea 291... |
Línea 291... |
| 291 |
|
291 |
|
| 292 |
|
292 |
|
| 293 |
public function test_manager_none_quarantine_data_virus() {
|
293 |
public function test_manager_none_quarantine_data_virus(): void {
|
| 294 |
$exception = null;
|
294 |
$exception = null;
|
| 295 |
try {
|
295 |
try {
|
| 296 |
\core\antivirus\manager::scan_data('FOUND');
|
296 |
\core\antivirus\manager::scan_data('FOUND');
|