7
7
import os
8
8
import urllib .parse
9
9
from datetime import datetime
10
+ from hashlib import sha256
10
11
from pathlib import Path
11
12
12
13
import pytest
14
+ from packageurl import PackageURL
13
15
from pytest_httpserver import HTTPServer
14
16
17
+ from macaron .artifact .maven import construct_maven_repository_path
15
18
from macaron .config .defaults import load_defaults
16
19
from macaron .errors import ConfigurationError , InvalidHTTPResponseError
17
20
from macaron .slsa_analyzer .package_registry .maven_central_registry import MavenCentralRegistry
@@ -35,6 +38,28 @@ def maven_central_instance() -> MavenCentralRegistry:
35
38
)
36
39
37
40
41
+ @pytest .fixture (name = "maven_service" )
42
+ def maven_service_ (httpserver : HTTPServer , tmp_path : Path ) -> None :
43
+ """Set up the Maven httpserver."""
44
+ base_url_parsed = urllib .parse .urlparse (httpserver .url_for ("" ))
45
+
46
+ user_config_input = f"""
47
+ [package_registry.maven_central]
48
+ request_timeout = 20
49
+ search_netloc = { base_url_parsed .netloc }
50
+ search_scheme = { base_url_parsed .scheme }
51
+ registry_url_netloc = { base_url_parsed .netloc }
52
+ registry_url_scheme = { base_url_parsed .scheme }
53
+ """
54
+ user_config_path = os .path .join (tmp_path , "config.ini" )
55
+ with open (user_config_path , "w" , encoding = "utf-8" ) as user_config_file :
56
+ user_config_file .write (user_config_input )
57
+ # We don't have to worry about modifying the ``defaults`` object causing test
58
+ # pollution here, since we reload the ``defaults`` object before every test with the
59
+ # ``setup_test`` fixture.
60
+ load_defaults (user_config_path )
61
+
62
+
38
63
def test_load_defaults (tmp_path : Path ) -> None :
39
64
"""Test the ``load_defaults`` method."""
40
65
user_config_path = os .path .join (tmp_path , "config.ini" )
@@ -150,31 +175,14 @@ def test_is_detected(
150
175
def test_find_publish_timestamp (
151
176
resources_path : Path ,
152
177
httpserver : HTTPServer ,
153
- tmp_path : Path ,
178
+ maven_service : dict , # pylint: disable=unused-argument
154
179
purl : str ,
155
180
mc_json_path : str ,
156
181
query_string : str ,
157
182
expected_timestamp : str ,
158
183
) -> None :
159
184
"""Test that the function finds the timestamp correctly."""
160
- base_url_parsed = urllib .parse .urlparse (httpserver .url_for ("" ))
161
-
162
185
maven_central = MavenCentralRegistry ()
163
-
164
- # Set up responses of solrsearch endpoints using the httpserver plugin.
165
- user_config_input = f"""
166
- [package_registry.maven_central]
167
- request_timeout = 20
168
- search_netloc = { base_url_parsed .netloc }
169
- search_scheme = { base_url_parsed .scheme }
170
- """
171
- user_config_path = os .path .join (tmp_path , "config.ini" )
172
- with open (user_config_path , "w" , encoding = "utf-8" ) as user_config_file :
173
- user_config_file .write (user_config_input )
174
- # We don't have to worry about modifying the ``defaults`` object causing test
175
- # pollution here, since we reload the ``defaults`` object before every test with the
176
- # ``setup_test`` fixture.
177
- load_defaults (user_config_path )
178
186
maven_central .load_defaults ()
179
187
180
188
with open (os .path .join (resources_path , "maven_central_files" , mc_json_path ), encoding = "utf8" ) as page :
@@ -208,35 +216,19 @@ def test_find_publish_timestamp(
208
216
def test_find_publish_timestamp_errors (
209
217
resources_path : Path ,
210
218
httpserver : HTTPServer ,
211
- tmp_path : Path ,
219
+ maven_service : dict , # pylint: disable=unused-argument
212
220
purl : str ,
213
221
mc_json_path : str ,
214
222
expected_msg : str ,
215
223
) -> None :
216
224
"""Test that the function handles errors correctly."""
217
- base_url_parsed = urllib .parse .urlparse (httpserver .url_for ("" ))
218
-
219
225
maven_central = MavenCentralRegistry ()
220
-
221
- # Set up responses of solrsearch endpoints using the httpserver plugin.
222
- user_config_input = f"""
223
- [package_registry.maven_central]
224
- request_timeout = 20
225
- search_netloc = { base_url_parsed .netloc }
226
- search_scheme = { base_url_parsed .scheme }
227
- """
228
- user_config_path = os .path .join (tmp_path , "config.ini" )
229
- with open (user_config_path , "w" , encoding = "utf-8" ) as user_config_file :
230
- user_config_file .write (user_config_input )
231
- # We don't have to worry about modifying the ``defaults`` object causing test
232
- # pollution here, since we reload the ``defaults`` object before every test with the
233
- # ``setup_test`` fixture.
234
- load_defaults (user_config_path )
235
226
maven_central .load_defaults ()
236
227
237
228
with open (os .path .join (resources_path , "maven_central_files" , mc_json_path ), encoding = "utf8" ) as page :
238
229
mc_json_response = json .load (page )
239
230
231
+ # Set up responses of solrsearch endpoints using the httpserver plugin.
240
232
httpserver .expect_request (
241
233
"/solrsearch/select" ,
242
234
query_string = "q=g:org.apache.logging.log4j+AND+a:log4j-core+AND+v:3.0.0-beta2&core=gav&rows=1&wt=json" ,
@@ -245,3 +237,67 @@ def test_find_publish_timestamp_errors(
245
237
pat = f"^{ expected_msg } "
246
238
with pytest .raises (InvalidHTTPResponseError , match = pat ):
247
239
maven_central .find_publish_timestamp (purl = purl )
240
+
241
+
242
+ def test_get_artifact_file_name () -> None :
243
+ """Test the artifact file name function."""
244
+ assert not MavenCentralRegistry ().get_artifact_file_name (PackageURL .from_string ("pkg:maven/test/example" ))
245
+
246
+ assert (
247
+ MavenCentralRegistry ().get_artifact_file_name (PackageURL .from_string ("pkg:maven/text/example@1" ))
248
+ == "example-1.jar"
249
+ )
250
+
251
+
252
+ @pytest .mark .parametrize ("purl_string" , ["pkg:maven/example" , "pkg:maven/example/test" , "pkg:maven/example/test@1" ])
253
+ def test_get_artifact_hash_failures (
254
+ httpserver : HTTPServer , maven_service : dict , purl_string : str # pylint: disable=unused-argument
255
+ ) -> None :
256
+ """Test failures of get artifact hash."""
257
+ purl = PackageURL .from_string (purl_string )
258
+
259
+ maven_registry = MavenCentralRegistry ()
260
+ maven_registry .load_defaults ()
261
+
262
+ if (
263
+ purl .namespace
264
+ and purl .version
265
+ and (file_name := MavenCentralRegistry ().get_artifact_file_name (purl ))
266
+ and file_name
267
+ ):
268
+ artifact_path = "/" + construct_maven_repository_path (purl .namespace , purl .name , purl .version ) + "/" + file_name
269
+ hash_algorithm = sha256 ()
270
+ hash_algorithm .update (b"example_data" )
271
+ expected_hash = hash_algorithm .hexdigest ()
272
+ httpserver .expect_request (artifact_path + ".sha256" ).respond_with_data (expected_hash )
273
+ httpserver .expect_request (artifact_path ).respond_with_data (b"example_data_2" )
274
+
275
+ result = maven_registry .get_artifact_hash (purl , sha256 ())
276
+
277
+ assert not result
278
+
279
+
280
+ def test_get_artifact_hash_success (
281
+ httpserver : HTTPServer , maven_service : dict # pylint: disable=unused-argument
282
+ ) -> None :
283
+ """Test success of get artifact hash."""
284
+ purl = PackageURL .from_string ("pkg:maven/example/test@1" )
285
+ assert purl .namespace
286
+ assert purl .version
287
+
288
+ maven_registry = MavenCentralRegistry ()
289
+ maven_registry .load_defaults ()
290
+
291
+ file_name = MavenCentralRegistry ().get_artifact_file_name (purl )
292
+ assert file_name
293
+
294
+ artifact_path = "/" + construct_maven_repository_path (purl .namespace , purl .name , purl .version ) + "/" + file_name
295
+ hash_algorithm = sha256 ()
296
+ hash_algorithm .update (b"example_data" )
297
+ expected_hash = hash_algorithm .hexdigest ()
298
+ httpserver .expect_request (artifact_path + ".sha256" ).respond_with_data (expected_hash )
299
+ httpserver .expect_request (artifact_path ).respond_with_data (b"example_data" )
300
+
301
+ result = maven_registry .get_artifact_hash (purl , sha256 ())
302
+
303
+ assert result
0 commit comments