Skip to content

Commit e4bb2fc

Browse files
committed
test: add tests for ruby
1 parent 4c952ad commit e4bb2fc

2 files changed

Lines changed: 78 additions & 33 deletions

File tree

vulnerabilities/scraper/ruby.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,47 @@ def get_patched_range(spec_list):
3737
yield RangeSpecifier(spec)
3838

3939

40-
def import_vulnerabilities():
41-
vulnerability_package_dicts = []
42-
for vulnerability in rubygem_advisories(RUBYSEC_DB_URL):
40+
def load_vulnerability_package(vulnerability):
41+
package_name = vulnerability.get(
42+
'gem')
4343

44-
package_name = vulnerability.get(
45-
'gem')
44+
if not package_name:
45+
return
4646

47-
if not package_name:
48-
continue
47+
if 'cve' in vulnerability:
48+
vulnerability_id = 'CVE-{}'.format(vulnerability['cve'])
49+
else:
50+
return
4951

50-
if 'cve' in vulnerability:
51-
vulnerability_id = 'CVE-{}'.format(vulnerability['cve'])
52-
else:
53-
continue
52+
advisory_url = vulnerability.get('url')
53+
patched_version_ranges = list(
54+
get_patched_range(
55+
vulnerability.get('patched_versions', [])))
56+
all_versions = set(get_all_versions_of_package(package_name))
57+
unaffected_versions = set()
58+
59+
if patched_version_ranges:
60+
for version in all_versions:
61+
for spec in patched_version_ranges:
62+
if version in spec:
63+
unaffected_versions.add(version)
64+
break
65+
66+
affected_versions = all_versions - unaffected_versions
67+
68+
return {
69+
'package_name': package_name,
70+
'cve_id': vulnerability_id,
71+
'fixed_versions': unaffected_versions,
72+
'affected_versions': affected_versions,
73+
'advisory': advisory_url
74+
}
75+
76+
77+
def import_vulnerabilities():
78+
vulnerability_package_dicts = []
79+
for vulnerability in rubygem_advisories(RUBYSEC_DB_URL):
80+
package = load_vulnerability_package(vulnerability)
81+
vulnerability_package_dicts.append(package)
5482

55-
advisory_url = vulnerability.get('url')
56-
patched_version_ranges = list(
57-
get_patched_range(
58-
vulnerability.get('patched_versions', [])))
59-
all_versions = set(get_all_versions_of_package(package_name))
60-
unaffected_versions = set()
61-
62-
if patched_version_ranges:
63-
for version in all_versions:
64-
for spec in patched_version_ranges:
65-
if version in spec:
66-
unaffected_versions.add(version)
67-
break
68-
69-
affected_versions = all_versions - unaffected_versions
70-
vulnerability_package_dicts.append({
71-
'package_name': package_name,
72-
'cve_id': vulnerability_id,
73-
'fixed_versions': unaffected_versions,
74-
'affected_versions': affected_versions,
75-
'advisory': advisory_url
76-
})
7783
return vulnerability_package_dicts

vulnerabilities/tests/test_ruby.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Author: Islam ElHakmi (@EslamHiko)
2+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
3+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
4+
# The VulnerableCode software is licensed under the Apache License version 2.0.
5+
# Data generated with VulnerableCode require an acknowledgment.
6+
#
7+
# You may not use this software except in compliance with the License.
8+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing, software distributed
10+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
# specific language governing permissions and limitations under the License.
13+
#
14+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
15+
# derivative work, you must accompany this data with the following acknowledgment:
16+
#
17+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
18+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
19+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
20+
# for any legal advice.
21+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
22+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
23+
24+
import pytest
25+
from vulnerabilities.scraper.ruby import rubygem_advisories
26+
from vulnerabilities.scraper.ruby import load_vulnerability_package
27+
28+
RUBYSEC_DB_URL = 'https://github.com/rubysec/ruby-advisory-db/archive/master.zip'
29+
30+
31+
@pytest.mark.webtest
32+
def test_extract_data():
33+
for vulnerability in rubygem_advisories(RUBYSEC_DB_URL):
34+
package = load_vulnerability_package(vulnerability)
35+
assert len(package['package_name']) != 0
36+
assert len(package['cve_id']) != 0
37+
assert len(package['affected_versions']) != 0
38+
assert len(package['advisory']) != 0
39+
break

0 commit comments

Comments
 (0)