forked from spdx/tools-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_document.py
More file actions
280 lines (229 loc) · 10.7 KB
/
Copy pathtest_document.py
File metadata and controls
280 lines (229 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright (c) 2014 Ahmed H. Ismail
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import os
import shutil
import tempfile
import unittest
from unittest import TestCase
from spdx.checksum import Algorithm
from spdx.config import LICENSE_MAP
from spdx.creationinfo import Tool
from spdx.document import Document, ExternalDocumentRef
from spdx.document import License
from spdx.file import File
from spdx.package import Package
from spdx.utils import NoAssert
from spdx.version import Version
from tests import utils_test
class TestVersion(TestCase):
def test_creation(self):
v = Version(major=2, minor=1)
assert v.major == 2
assert v.minor == 1
def test_comparison(self):
v1 = Version(major=1, minor=2)
v2 = Version(major=2, minor=1)
assert v1 != v2
assert v1 < v2
assert v1 <= v2
assert v2 > v1
assert v2 >= v1
v3 = Version(major=1, minor=2)
assert v3 == v1
assert not v1 < v3
assert v1 <= v3
class TestDocument(TestCase):
def test_creation(self):
document = Document(
version=Version(major=2, minor=1),
data_license=License(full_name='Academic Free License v1.1',
identifier='AFL-1.1')
)
document.add_ext_document_reference(
ExternalDocumentRef('DocumentRef-spdx-tool-2.1',
'https://spdx.org/spdxdocs/spdx-tools-v2.1-3F2504E0-4F89-41D3-9A0C-0305E82C3301',
Algorithm('SHA1', 'SOME-SHA1'))
)
assert document.comment is None
assert document.version == Version(2, 1)
assert document.data_license.identifier == 'AFL-1.1'
assert document.ext_document_references[-1].external_document_id == 'DocumentRef-spdx-tool-2.1'
assert document.ext_document_references[-1].spdx_document_uri == 'https://spdx.org/spdxdocs/spdx-tools-v2.1-3F2504E0-4F89-41D3-9A0C-0305E82C3301'
assert document.ext_document_references[-1].check_sum.identifier == 'SHA1'
assert document.ext_document_references[-1].check_sum.value == 'SOME-SHA1'
def test_document_validate_failures_returns_informative_messages(self):
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'),
'Sample_Document-V2.1', spdx_id='SPDXRef-DOCUMENT',
namespace='https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301')
pack = doc.package = Package('some/path', NoAssert())
file1 = File('./some/path/tofile')
file1.name = './some/path/tofile'
file1.spdx_id = 'SPDXRef-File'
file1.chk_sum = Algorithm('SHA1', 'SOME-SHA1')
lic1 = License.from_identifier('LGPL-2.1')
file1.add_lics(lic1)
pack.add_lics_from_file(lic1)
messages = []
messages = doc.validate(messages)
expected = [
'No creators defined, must have at least one.',
'Creation info missing created date.',
'Package checksum must be instance of spdx.checksum.Algorithm',
'Package verif_code can not be None.',
'Package cr_text can not be None.',
'Package must have at least one file.',
'Package concluded license must be instance of spdx.utils.SPDXNone '
'or spdx.utils.NoAssert or spdx.document.License',
'Package declared license must be instance of spdx.utils.SPDXNone '
'or spdx.utils.NoAssert or spdx.document.License'
]
assert expected == messages
def test_document_is_valid_when_using_or_later_licenses(self):
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'),
'Sample_Document-V2.1', spdx_id='SPDXRef-DOCUMENT',
namespace='https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301')
doc.creation_info.add_creator(Tool('ScanCode'))
doc.creation_info.set_created_now()
package = doc.package = Package(name='some/path', download_location=NoAssert())
package.cr_text = 'Some copyrught'
package.verif_code = 'SOME code'
package.license_declared = NoAssert()
package.conc_lics = NoAssert()
file1 = File('./some/path/tofile')
file1.name = './some/path/tofile'
file1.spdx_id = 'SPDXRef-File'
file1.chk_sum = Algorithm('SHA1', 'SOME-SHA1')
file1.conc_lics = NoAssert()
file1.copyright = NoAssert()
lic1 = License.from_identifier('LGPL-2.1+')
file1.add_lics(lic1)
package.add_lics_from_file(lic1)
package.add_file(file1)
messages = []
is_valid = doc.validate(messages)
assert is_valid
assert not messages
class TestWriters(TestCase):
def _get_lgpl_doc(self, or_later=False):
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'),
'Sample_Document-V2.1', spdx_id='SPDXRef-DOCUMENT',
namespace='https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301')
doc.creation_info.add_creator(Tool('ScanCode'))
doc.creation_info.set_created_now()
package = doc.package = Package(name='some/path', download_location=NoAssert())
package.cr_text = 'Some copyrught'
package.verif_code = 'SOME code'
package.check_sum = Algorithm('SHA1', 'SOME-SHA1')
package.license_declared = NoAssert()
package.conc_lics = NoAssert()
file1 = File('./some/path/tofile')
file1.name = './some/path/tofile'
file1.spdx_id = 'SPDXRef-File'
file1.chk_sum = Algorithm('SHA1', 'SOME-SHA1')
file1.conc_lics = NoAssert()
file1.copyright = NoAssert()
lic1 = License.from_identifier('LGPL-2.1')
if or_later:
lic1 = License.from_identifier('LGPL-2.1+')
file1.add_lics(lic1)
package.add_lics_from_file(lic1)
package.add_file(file1)
return doc
def test_write_document_rdf_with_validate(self):
from spdx.writers.rdf import write_document
doc = self._get_lgpl_doc()
temp_dir = ''
try:
temp_dir = tempfile.mkdtemp(prefix='test_spdx')
result_file = os.path.join(temp_dir, 'spdx-simple.rdf')
with open(result_file, 'wb') as output:
write_document(doc, output, validate=True)
expected_file = utils_test.get_test_loc(
'doc_write/rdf-simple.json',
test_data_dir=utils_test.test_data_dir)
utils_test.check_rdf_scan(expected_file, result_file, regen=False)
finally:
if temp_dir and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
def test_write_document_rdf_with_or_later_with_validate(self):
from spdx.writers.rdf import write_document
doc = self._get_lgpl_doc(or_later=True)
temp_dir = ''
try:
temp_dir = tempfile.mkdtemp(prefix='test_spdx')
result_file = os.path.join(temp_dir, 'spdx-simple-plus.rdf')
# test proper!
with open(result_file, 'wb') as output:
write_document(doc, output, validate=True)
expected_file = utils_test.get_test_loc(
'doc_write/rdf-simple-plus.json',
test_data_dir=utils_test.test_data_dir)
utils_test.check_rdf_scan(expected_file, result_file, regen=False)
finally:
if temp_dir and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
def test_write_document_tv_with_validate(self):
from spdx.writers.tagvalue import write_document
doc = self._get_lgpl_doc()
temp_dir = ''
try:
temp_dir = tempfile.mkdtemp(prefix='test_spdx')
result_file = os.path.join(temp_dir, 'spdx-simple.tv')
with open(result_file, 'w') as output:
write_document(doc, output, validate=True)
expected_file = utils_test.get_test_loc(
'doc_write/tv-simple.tv',
test_data_dir=utils_test.test_data_dir)
utils_test.check_tv_scan(expected_file, result_file, regen=False)
finally:
if temp_dir and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
def test_write_document_tv_with_or_later_with_validate(self):
from spdx.writers.tagvalue import write_document
doc = self._get_lgpl_doc(or_later=True)
temp_dir = ''
try:
temp_dir = tempfile.mkdtemp(prefix='test_spdx')
result_file = os.path.join(temp_dir, 'spdx-simple-plus.tv')
# test proper!
with open(result_file, 'w') as output:
write_document(doc, output, validate=True)
expected_file = utils_test.get_test_loc(
'doc_write/tv-simple-plus.tv',
test_data_dir=utils_test.test_data_dir)
utils_test.check_tv_scan(expected_file, result_file, regen=False)
finally:
if temp_dir and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
class TestLicense(TestCase):
def test_url(self):
lic = License(full_name='Apache License 1.0', identifier='Apache-1.0')
assert lic.url == 'http://spdx.org/licenses/Apache-1.0'
def test_license_list(self):
assert LICENSE_MAP['Aladdin Free Public License'] == 'Aladdin'
assert LICENSE_MAP['Aladdin'] == 'Aladdin Free Public License'
assert LICENSE_MAP['MIT License'] == 'MIT'
assert LICENSE_MAP['MIT'] == 'MIT License'
assert LICENSE_MAP['BSD 4-clause "Original" or "Old" License'] == 'BSD-4-Clause'
assert LICENSE_MAP['BSD-4-Clause'] == 'BSD 4-clause "Original" or "Old" License'
def test_from_full_name(self):
mit = License.from_full_name('MIT License')
assert mit.identifier == 'MIT'
assert mit.url == 'http://spdx.org/licenses/MIT'
def test_from_identifier(self):
mit = License.from_identifier('MIT')
assert mit.full_name == 'MIT License'
assert mit.url == 'http://spdx.org/licenses/MIT'
if __name__ == '__main__':
unittest.main()