Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit 316c891

Browse files
authored
Merge pull request #451 from BlBana/master
modified test cases
2 parents fb21d1a + d22acd7 commit 316c891

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

cobra/cve_parse.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
import datetime
1414
import os
1515
import requests
16-
import urllib
1716
import threading
1817
import gzip
1918
import xml.etree.cElementTree as eT
2019
import multiprocessing
2120
from .config import project_directory, Config, config_path
2221
from .log import logger
2322
from .dependencies import Dependencies
23+
24+
try:
25+
from urllib import urlretrieve # Python2
26+
except ImportError:
27+
from urllib.request import urlretrieve # Python3
28+
2429
try:
2530
from configparser import ConfigParser
2631
except ImportError:
@@ -158,7 +163,8 @@ def rule_xml(self):
158163
rule_path = project_directory + '/rules/CVI-999'
159164
tree.write(rule_path + str(self.year)[1:] + '.xml')
160165
endtime = datetime.datetime.now()
161-
logger.info('CVE-999' + str(self.year)[1:] + '.xml Rule update succeeds, times:%ds' % (endtime - starttime).seconds)
166+
logger.info(
167+
'CVE-999' + str(self.year)[1:] + '.xml Rule update succeeds, times:%ds' % (endtime - starttime).seconds)
162168

163169
def pretty(self, e, level=0):
164170
"""
@@ -234,7 +240,7 @@ def log_result(self):
234240
for cve_child in self._scan_result[module_]:
235241
cve_id = cve_child
236242
level = self._scan_result[module_][cve_id]
237-
logger.warning('Find the module ' + module_ + ' have ' + cve_id +',level: ' +level)
243+
logger.warning('Find the module ' + module_ + ' have ' + cve_id + ',level: ' + level)
238244
count = len(self._scan_result[module_])
239245
logger.warning('The ' + module_ + ' module have ' + str(count) + ' CVE Vul(s)')
240246

@@ -247,13 +253,13 @@ def rule_parse():
247253
gz_files = download_rule_gz()
248254
un_gz(gz_files)
249255
pool = multiprocessing.Pool()
250-
for year in range(2002, datetime.datetime.now().year+1):
256+
for year in range(2002, datetime.datetime.now().year + 1):
251257
cve_xml = "../rules/%d.xml" % year
252258
pool.apply_async(rule_single, args=(cve_xml, year))
253259
pool.close()
254260
pool.join()
255-
for year in range(2002, datetime.datetime.now().year+1):
256-
os.remove(project_directory+"/rules/%d.xml" % year)
261+
for year in range(2002, datetime.datetime.now().year + 1):
262+
os.remove(project_directory + "/rules/%d.xml" % year)
257263
logger.info("The rule update success, start scan cve vuls")
258264
return True
259265
else:
@@ -264,18 +270,19 @@ def download_rule_gz():
264270
threads = []
265271
files = []
266272
start_time = datetime.datetime.now()
267-
for year in range(2002, datetime.datetime.now().year+1):
273+
for year in range(2002, datetime.datetime.now().year + 1):
268274
url = "https://static.nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-" + str(year) + ".xml.gz"
269275
logger.info("start download " + str(year) + ".xml.gz")
270-
thread = threading.Thread(target=urllib.urlretrieve, args=(url, project_directory+"/rules/"+str(year)+".xml.gz"))
276+
thread = threading.Thread(target=urlretrieve,
277+
args=(url, project_directory + "/rules/" + str(year) + ".xml.gz"))
271278
thread.start()
272279
threads.append(thread)
273280
logger.info('CVE-' + str(year) + " is download success")
274-
files.append(project_directory+"/rules/" + str(year) + ".xml.gz")
281+
files.append(project_directory + "/rules/" + str(year) + ".xml.gz")
275282
for t in threads:
276283
t.join()
277284
end_time = datetime.datetime.now()
278-
logger.info("All CVE xml file already download success, use time:%ds" % (end_time-start_time).seconds)
285+
logger.info("All CVE xml file already download success, use time:%ds" % (end_time - start_time).seconds)
279286
return files
280287

281288

@@ -286,11 +293,11 @@ def un_gz(gz_files):
286293
for gz_file in gz_files:
287294
f_name = gz_file.replace(".gz", "")
288295
g_file = gzip.GzipFile(gz_file)
289-
open(f_name, "w+").write(g_file.read())
296+
open(f_name, "wb+").write(g_file.read())
290297
g_file.close()
291298
os.remove(gz_file)
292299
end_time = datetime.datetime.now()
293-
logger.info("Decompress success, use time:%ds" % (end_time-start_time).seconds)
300+
logger.info("Decompress success, use time:%ds" % (end_time - start_time).seconds)
294301
return True
295302

296303

@@ -300,9 +307,9 @@ def rule_single(target_directory, year):
300307

301308
def is_update():
302309
url = "https://static.nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-modified.meta"
303-
r = requests.get(url)
310+
r = requests.get(url, verify=False)
304311
index = r.text.find('sha256:')
305-
sha256_now = r.text[index+7:].strip()
312+
sha256_now = r.text[index + 7:].strip()
306313
sha256_local = Config(level1='cve', level2='modified').value
307314
if sha256_local != sha256_now:
308315
logger.info("The CVE Rule already update, start update local rule")
@@ -314,7 +321,7 @@ def is_update():
314321
config.write(fi)
315322
fi.close()
316323
except IOError as e:
317-
logger.warning(e.message)
324+
logger.warning(e)
318325
logger.info("The sha256 been update")
319326
return True
320327
return False

cobra/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def md5(content):
213213
:param content:
214214
:return:
215215
"""
216+
content = content.encode('utf8')
216217
return hashlib.md5(content).hexdigest()
217218

218219

@@ -243,6 +244,7 @@ def path_to_short(path, max_length=36):
243244
return path
244245
paths = path.split('/')
245246
paths = filter(None, paths)
247+
paths = list(paths)
246248
tmp_path = ''
247249
for i in range(0, len(paths)):
248250
logger.debug((i, str(paths[i]), str(paths[len(paths) - i - 1])))

tests/test_cve_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from cobra.cve_parse import *
1919
from cobra.cve_parse import CveParse, project_directory
2020
try:
21-
from configparser import ConfigParser
21+
from configparser import ConfigParser, NoSectionError
2222
except ImportError:
2323
from ConfigParser import ConfigParser, NoSectionError
2424

tests/test_detection.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def test_get_dict():
5050
extension = ['php', 'js', 'java']
5151
type_num = {}
5252
type_num = detection.get_dict(extension, type_num)
53-
assert isinstance(extension, type(type_num.keys()))
53+
print(type(type_num))
54+
assert type_num['php']['blank'] == 0
5455

5556

5657
def test_project_information():
@@ -61,37 +62,32 @@ def test_project_information():
6162

6263
def test_count_py_line():
6364
count = Detection.count_py_line(examples_path+'/cloc.py')
64-
type_ = count.keys()
6565
type_count = ['count_blank', 'count_code', 'count_pound']
66-
assert type_ == type_count
66+
assert count['count_code'] == 5
6767

6868

6969
def test_count_php_line():
7070
count = Detection.count_php_line(examples_path+'/cloc.php')
71-
type_ = count.keys()
7271
type_count = ['count_blank', 'count_code', 'count_pound']
73-
assert type_ == type_count
72+
assert count['count_code'] == 2
7473

7574

7675
def test_count_java_line():
7776
count = Detection.count_java_line(examples_path+'/cloc.java')
78-
type_ = count.keys()
7977
type_count = ['count_blank', 'count_code', 'count_pound']
80-
assert type_ == type_count
78+
assert count['count_code'] == 1
8179

8280

8381
def test_count_html_line():
8482
count = Detection.count_html_line(examples_path+'/cloc.html')
85-
type_ = count.keys()
8683
type_count = ['count_blank', 'count_code', 'count_pound']
87-
assert type_ == type_count
84+
assert count['count_code'] == 9
8885

8986

9087
def test_count_data_line():
9188
count = Detection.count_data_line(examples_path+'/param_xml.xml')
92-
type_ = count.keys()
9389
type_count = ['count_blank', 'count_code', 'count_pound']
94-
assert type_ == type_count
90+
assert count['count_code'] == 81
9591

9692

9793
def test_countnum():

0 commit comments

Comments
 (0)