1
- # Copyright (C) 2021 Intel Corporation
1
+ # Copyright (C) 2022 Intel Corporation
2
2
# SPDX-License-Identifier: GPL-3.0-or-later
3
3
4
4
""" CVE Binary Tool tests for the extractor function """
5
5
import shutil
6
+ import sys
6
7
import tarfile
7
8
import tempfile
8
9
import unittest
9
10
import unittest .mock
11
+ from asyncio import coroutine
10
12
from io import BytesIO
11
13
from pathlib import Path
12
14
from test .utils import (
16
18
TMUX_DEB ,
17
19
download_file ,
18
20
)
19
- from typing import List
21
+ from typing import Dict , List
20
22
from zipfile import ZipFile , ZipInfo
21
23
22
24
import pytest
25
+ from pytest_mock import MockerFixture
23
26
27
+ from cve_bin_tool .error_handler import ERROR_CODES , ExtractionToolNotFound
24
28
from cve_bin_tool .extractor import Extractor
25
29
from cve_bin_tool .util import inpath
26
30
@@ -179,9 +183,31 @@ def setup_method(self):
179
183
def extension_list (self ) -> List [str ]:
180
184
return self .extractor .file_extractors [self .extractor .extract_file_pkg ]
181
185
186
+ @pytest .mark .parametrize (
187
+ "inpath_return_values" ,
188
+ (
189
+ {"tar" : True , "7z" : False }, # use `tar` to extract
190
+ {"tar" : False , "7z" : True }, # use `7z` to extract
191
+ ),
192
+ )
182
193
@pytest .mark .asyncio
183
- async def test_extract_file_pkg (self , extension_list : List [str ]):
194
+ async def test_extract_file_pkg (
195
+ self ,
196
+ extension_list : List [str ],
197
+ inpath_return_values : Dict [str , bool ],
198
+ mocker : MockerFixture ,
199
+ ):
184
200
"""Test the pkg file extraction"""
201
+
202
+ if sys .version_info >= (3 , 8 ):
203
+ mock_func = mocker .AsyncMock (side_effect = inpath_return_values .get )
204
+ else :
205
+ mock_func = coroutine (
206
+ mocker .MagicMock (side_effect = inpath_return_values .get )
207
+ )
208
+
209
+ mocker .patch ("cve_bin_tool.extractor.aio_inpath" , mock_func )
210
+
185
211
async for extracted_path in self .extract_files (
186
212
[f"test{ extension } " for extension in extension_list ]
187
213
):
@@ -229,6 +255,26 @@ async def test_extract_file_deb(self, extension_list: List[str]):
229
255
):
230
256
assert (Path (extracted_path ) / "usr" / "bin" / "tmux" ).is_file ()
231
257
258
+ @pytest .mark .asyncio
259
+ async def test_extract_file_deb_no_tool (
260
+ self , extension_list : List [str ], mocker : MockerFixture
261
+ ):
262
+ """Test the deb file extraction with no extraction tool"""
263
+
264
+ if sys .version_info >= (3 , 8 ):
265
+ mocker .patch ("cve_bin_tool.extractor.aio_inpath" , return_value = False )
266
+ else :
267
+ mocker .patch (
268
+ "cve_bin_tool.extractor.aio_inpath" ,
269
+ coroutine (mocker .Mock (return_value = False )),
270
+ )
271
+
272
+ with pytest .raises (SystemExit ) as e :
273
+ async for _ in self .extract_files (
274
+ [f"test{ extension } " for extension in extension_list ]
275
+ ):
276
+ assert e .value .args [0 ] == ERROR_CODES [ExtractionToolNotFound ]
277
+
232
278
233
279
class TestExtractFileCab (TestExtractorBase ):
234
280
"""Tests for the cab file extractor"""
@@ -248,6 +294,26 @@ async def test_extract_file_cab(self, extension_list: List[str]):
248
294
):
249
295
assert (Path (extracted_path ) / "usr" / "bin" / "python3.8" ).is_file ()
250
296
297
+ @pytest .mark .asyncio
298
+ async def test_extract_file_cab_no_cabextract (
299
+ self , extension_list : List [str ], mocker : MockerFixture
300
+ ):
301
+ """Test the cab file extraction with no extraction tool"""
302
+
303
+ if sys .version_info >= (3 , 8 ):
304
+ mocker .patch ("cve_bin_tool.extractor.aio_inpath" , return_value = False )
305
+ else :
306
+ mocker .patch (
307
+ "cve_bin_tool.extractor.aio_inpath" ,
308
+ coroutine (mocker .Mock (return_value = False )),
309
+ )
310
+
311
+ with pytest .raises (SystemExit ) as e :
312
+ async for _ in self .extract_files (
313
+ [f"test{ extension } " for extension in extension_list ]
314
+ ):
315
+ assert e .value .args [0 ] == ERROR_CODES [ExtractionToolNotFound ]
316
+
251
317
252
318
class TestExtractFileZip (TestExtractorBase ):
253
319
"""Tests for the zip file extractor
@@ -268,9 +334,33 @@ def setup_method(self, extension_list: List[str]):
268
334
with ZipFile (zippath , "w" ) as zipfile :
269
335
zipfile .writestr (ZipInfo ("test.txt" ), "feedface" )
270
336
337
+ @pytest .mark .parametrize (
338
+ "inpath_return_values" ,
339
+ (
340
+ {"unzip" : True , "7z" : False , "zipinfo" : False , "file" : False },
341
+ {"unzip" : False , "7z" : True , "zipinfo" : False , "file" : False },
342
+ {"unzip" : False , "7z" : False , "zipinfo" : True , "file" : False },
343
+ {"unzip" : False , "7z" : False , "zipinfo" : False , "file" : True },
344
+ ),
345
+ )
271
346
@pytest .mark .asyncio
272
- async def test_extract_file_zip (self , extension_list : List [str ]):
347
+ async def test_extract_file_zip (
348
+ self ,
349
+ extension_list : List [str ],
350
+ inpath_return_values : Dict [str , bool ],
351
+ mocker : MockerFixture ,
352
+ ):
273
353
"""Test the zip file extraction"""
354
+
355
+ if sys .version_info >= (3 , 8 ):
356
+ mock_func = mocker .AsyncMock (side_effect = inpath_return_values .get )
357
+ else :
358
+ mock_func = coroutine (
359
+ mocker .MagicMock (side_effect = inpath_return_values .get )
360
+ )
361
+
362
+ mocker .patch ("cve_bin_tool.extractor.aio_inpath" , mock_func )
363
+
274
364
async for dir_path in self .extract_files (
275
365
[f"test{ extension } " for extension in extension_list ]
276
366
):
0 commit comments