|
| 1 | +# Author: Navonil Das (@NavonilDas) |
| 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 | + |
| 25 | +from django.test import TestCase |
| 26 | +from vulnerabilities.scraper.npm import remove_spaces, get_all_version, extract_data |
| 27 | +import os |
| 28 | +import json |
| 29 | + |
| 30 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 31 | +TEST_DATA = os.path.join(BASE_DIR, 'test_data/') |
| 32 | + |
| 33 | + |
| 34 | +class NPMScrapperTest(TestCase): |
| 35 | + def test_remove_space(self): |
| 36 | + res = remove_spaces(">= 1.2.1 || <= 2.1.1") |
| 37 | + self.assertEqual(res, '>=1.2.1 || <=2.1.1') |
| 38 | + |
| 39 | + res = remove_spaces(">= v1.2.1 || <= V2.1.1") |
| 40 | + self.assertEqual(res, '>=1.2.1 || <=2.1.1') |
| 41 | + |
| 42 | + def test_get_all_version(self): |
| 43 | + x = get_all_version('electron') |
| 44 | + expected = ['0.1.2', '2.0.0', '3.0.0', |
| 45 | + '4.0.0', '5.0.0', '6.0.0', '7.0.0'] |
| 46 | + self.assertTrue(set(expected) <= set(x)) |
| 47 | + |
| 48 | + def test_extract_data(self): |
| 49 | + with open(os.path.join(TEST_DATA, 'npm_test.json')) as f: |
| 50 | + test_data = json.loads(f.read()) |
| 51 | + |
| 52 | + expected = { |
| 53 | + 'package_name': 'hapi', |
| 54 | + 'vulnerability_id': 'CVE-2014-4671', |
| 55 | + 'fixed_version': [ |
| 56 | + '6.1.0', '6.2.0', '6.2.1', '6.2.2', '6.3.0', '6.4.0', |
| 57 | + '6.5.0', '6.5.1', '6.6.0', '6.7.0', '6.7.1', '6.8.0', |
| 58 | + '6.8.1', '6.9.0', '6.10.0', '6.11.0', '6.11.1', '7.0.0', |
| 59 | + '7.0.1', '7.1.0', '7.1.1', '7.2.0', '7.3.0', '7.4.0', |
| 60 | + '7.5.0', '7.5.1', '7.5.2', '8.0.0', '7.5.3', '8.1.0', |
| 61 | + '8.2.0', '8.3.0', '8.3.1', '8.4.0', '8.5.0', '8.5.1', |
| 62 | + '8.5.2', '8.5.3', '8.6.0', '8.6.1', '8.8.0', '8.8.1', |
| 63 | + '9.0.0', '9.0.1', '9.0.2', '9.0.3', '9.0.4', '9.1.0', |
| 64 | + '9.2.0', '9.3.0', '9.3.1', '10.0.0', '10.0.1', '10.1.0', |
| 65 | + '10.2.1', '10.4.0', '10.4.1', '10.5.0', '11.0.0', '11.0.1', |
| 66 | + '11.0.2', '11.0.3', '11.0.4', '11.0.5', '11.1.0', '11.1.1', |
| 67 | + '11.1.2', '11.1.3', '11.1.4', '12.0.0', '12.0.1', '12.1.0', |
| 68 | + '9.5.1', '13.0.0', '13.1.0', '13.2.0', '13.2.1', '13.2.2', |
| 69 | + '13.3.0', '13.4.0', '13.4.1', '13.4.2', '13.5.0', '14.0.0', |
| 70 | + '13.5.3', '14.1.0', '14.2.0', '15.0.1', '15.0.2', '15.0.3', |
| 71 | + '15.1.0', '15.1.1', '15.2.0', '16.0.0', '16.0.1', '16.0.2', |
| 72 | + '16.0.3', '16.1.0', '16.1.1', '16.2.0', '16.3.0', '16.3.1', |
| 73 | + '16.4.0', '16.4.1', '16.4.2', '16.4.3', '16.5.0', '16.5.1', |
| 74 | + '16.5.2', '16.6.0', '16.6.1', '16.6.2', '17.0.0', '17.0.1', |
| 75 | + '17.0.2', '17.1.0', '17.1.1', '17.2.0', '17.2.1', '16.6.3', |
| 76 | + '17.2.2', '17.2.3', '17.3.0', '17.3.1', '17.4.0', '17.5.0', |
| 77 | + '17.5.1', '17.5.2', '17.5.3', '17.5.4', '17.5.5', '17.6.0', |
| 78 | + '17.6.1', '17.6.2', '17.6.3', '16.6.4', '17.6.4', '16.6.5', |
| 79 | + '17.7.0', '16.7.0', '17.8.0', '17.8.1', '18.0.0', '17.8.2', |
| 80 | + '17.8.3', '18.0.1', '17.8.4', '18.1.0', '17.8.5'], |
| 81 | + 'affected_version': [ |
| 82 | + '0.0.1', '0.0.2', '0.0.3', '0.0.4', '0.0.5', '0.0.6', '0.1.0', |
| 83 | + '0.1.1', '0.1.2', '0.1.3', '0.2.0', '0.2.1', '0.3.0', '0.4.0', |
| 84 | + '0.4.1', '0.4.2', '0.4.3', '0.4.4', '0.5.0', '0.5.1', '0.6.0', |
| 85 | + '0.6.1', '0.5.2', '0.7.0', '0.7.1', '0.8.0', '0.8.1', '0.8.2', |
| 86 | + '0.8.3', '0.8.4', '0.9.0', '0.9.1', '0.9.2', '0.10.0', '0.10.1', |
| 87 | + '0.11.0', '0.11.1', '0.11.2', '0.11.3', '0.12.0', '0.13.0', |
| 88 | + '0.13.1', '0.13.2', '0.11.4', '0.13.3', '0.14.0', '0.14.1', |
| 89 | + '0.14.2', '0.15.0', '0.15.1', '0.15.2', '0.15.3', '0.15.4', |
| 90 | + '0.15.5', '0.15.6', '0.15.7', '0.15.8', '0.15.9', '0.16.0', |
| 91 | + '1.0.0', '1.0.1', '1.0.2', '1.0.3', '1.1.0', '1.2.0', '1.3.0', |
| 92 | + '1.4.0', '1.5.0', '1.6.0', '1.6.1', '1.6.2', '1.7.0', '1.7.1', |
| 93 | + '1.7.2', '1.7.3', '1.8.0', '1.8.1', '1.8.2', '1.8.3', '1.9.0', |
| 94 | + '1.9.1', '1.9.2', '1.9.3', '1.9.4', '1.9.5', '1.9.6', '1.9.7', |
| 95 | + '1.10.0', '1.11.0', '1.11.1', '1.12.0', '1.13.0', '1.14.0', |
| 96 | + '1.15.0', '1.16.0', '1.16.1', '1.17.0', '1.18.0', '1.19.0', |
| 97 | + '1.19.1', '1.19.2', '1.19.3', '1.19.4', '1.19.5', '1.20.0', |
| 98 | + '2.0.0', '2.1.0', '2.1.1', '2.1.2', '2.2.0', '2.3.0', '2.4.0', |
| 99 | + '2.5.0', '2.6.0', '3.0.0', '3.0.1', '3.0.2', '3.1.0', '4.0.0', |
| 100 | + '4.0.1', '4.0.2', '4.0.3', '4.1.0', '4.1.1', '4.1.2', '4.1.3', |
| 101 | + '4.1.4', '5.0.0', '5.1.0', '6.0.0', '6.0.1', '6.0.2'], |
| 102 | + 'severity': 'moderate' |
| 103 | + } |
| 104 | + got = extract_data(test_data)[0] |
| 105 | + # Check if expected affected version and fixed version is subset of what we get from online |
| 106 | + self.assertTrue(set(expected['fixed_version']) |
| 107 | + <= set(got['fixed_version'])) |
| 108 | + self.assertTrue(set(expected['affected_version']) <= set( |
| 109 | + got['affected_version'])) |
| 110 | + |
| 111 | + self.assertEqual(expected['package_name'], got['package_name']) |
| 112 | + self.assertEqual(expected['severity'], got['severity']) |
| 113 | + self.assertEqual(expected['vulnerability_id'], got['vulnerability_id']) |
0 commit comments