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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! SourceForge release source tests.
//!
//! The adapter is compiled into `sources` by the integration wave; until that
//! wiring lands this test includes the module directly and provides the
//! `crate::` paths it uses. Every request goes through the scripted
//! [`FakeHttp`] seam; no test touches the network.
#![allow(dead_code)]
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use serde_json::{Value, json};
use vuln_notif::release::http::RegistryHttp;
use vuln_notif::release::source::{
ProjectRef, ReleaseSource, SourceCapabilities, UnsupportedReason, VersionInfo,
};
mod release {
pub mod http {
pub use vuln_notif::release::http::*;
}
pub mod source {
pub use vuln_notif::release::source::*;
}
pub mod versioning {
pub use vuln_notif::release::versioning::*;
}
}
#[path = "../src/release/sources/sourceforge.rs"]
mod sourceforge;
use sourceforge::SourceforgeSource;
const SITE: &str = "https://sourceforge.test";
const DOWNLOADS: &str = "https://downloads.sourceforge.test";
const RSS: &str = include_str!("fixtures/sources/sourceforge/rss.xml");
const FILES: &str = include_str!("fixtures/sources/sourceforge/files.html");
const MISSING: &str = include_str!("fixtures/sources/sourceforge/missing.html");
const RSS_URL: &str = "https://sourceforge.test/projects/widget/rss?path=/";
const FILES_URL: &str = "https://sourceforge.test/projects/widget/files/";
const VERSION_FILES_URL: &str = "https://sourceforge.test/projects/widget/files/1.10.0/";
const VERSION_FILES: &str = r#"<!DOCTYPE html>
<html><body>
<table id="files_list"><tbody>
<tr class="file" title="widget-1.10.0.tar.gz">
<th><a href="/projects/widget/files/1.10.0/widget-1.10.0.tar.gz/download" title="widget-1.10.0.tar.gz">widget-1.10.0.tar.gz</a></th>
<td class="date">2024-07-15</td>
<td class="size">2.0 MB</td>
<td class="downloads">100</td>
</tr>
<tr class="file" title="widget-1.10.0-win64.zip">
<th><a href="/projects/widget/files/1.10.0/widget-1.10.0-win64.zip/download" title="widget-1.10.0-win64.zip">widget-1.10.0-win64.zip</a></th>
<td class="date">2024-07-15</td>
<td class="size">1.0 MB</td>
<td class="downloads">90</td>
</tr>
</tbody></table>
</body></html>"#;
const EMPTY_RSS: &str = r#"<rss version="2.0"><channel>
<title>Empty</title>
<link>https://sourceforge.test/projects/empty/</link>
<description></description>
</channel></rss>"#;
const EMPTY_FILES: &str = r#"<table id="files_list"><tbody></tbody></table>"#;
// ---------------------------------------------------------------------------
// Scripted RegistryHttp fake
// ---------------------------------------------------------------------------
#[derive(Clone, Debug)]
enum Reply {
Text(String),
Error(String),
}
#[derive(Default)]
struct FakeHttp {
replies: Mutex<HashMap<String, VecDeque<Reply>>>,
calls: Mutex<Vec<String>>,
}
impl FakeHttp {
fn enqueue(&self, url: &str, reply: Reply) {
self.replies
.lock()
.unwrap()
.entry(url.to_owned())
.or_default()
.push_back(reply);
}
fn text(&self, url: &str, body: &str) {
self.enqueue(url, Reply::Text(body.to_owned()));
}
fn error(&self, url: &str, message: &str) {
self.enqueue(url, Reply::Error(message.to_owned()));
}
fn urls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
fn take(&self, url: &str) -> anyhow::Result<Reply> {
self.calls.lock().unwrap().push(url.to_owned());
self.replies
.lock()
.unwrap()
.get_mut(url)
.and_then(VecDeque::pop_front)
.ok_or_else(|| anyhow::anyhow!("unexpected GET {url}"))
}
}
#[async_trait]
impl RegistryHttp for FakeHttp {
async fn get_json(&self, url: &str) -> anyhow::Result<Value> {
Err(anyhow::anyhow!("unexpected JSON request for {url}"))
}
async fn get_text(&self, url: &str) -> anyhow::Result<String> {
match self.take(url)? {
Reply::Text(body) => Ok(body),
Reply::Error(message) => Err(anyhow::anyhow!("{message}")),
}
}
async fn head_size(&self, url: &str) -> anyhow::Result<u64> {
Err(anyhow::anyhow!("unexpected HEAD request for {url}"))
}
async fn download(&self, url: &str) -> anyhow::Result<Vec<u8>> {
Err(anyhow::anyhow!("unexpected download of {url}"))
}
}
fn source(fake: &Arc<FakeHttp>) -> SourceforgeSource {
SourceforgeSource::with_bases(fake.clone(), SITE, DOWNLOADS)
}
fn project(name: &str) -> ProjectRef {
ProjectRef {
purl_type: "sourceforge".to_owned(),
name: name.to_owned(),
purl: format!("pkg:sourceforge/{name}"),
}
}
fn typed_project(purl_type: &str, name: &str) -> ProjectRef {
ProjectRef {
purl_type: purl_type.to_owned(),
name: name.to_owned(),
purl: format!("pkg:{purl_type}/{name}"),
}
}
fn version(raw: &str, normalized: &str) -> VersionInfo {
VersionInfo {
raw: raw.to_owned(),
version: normalized.to_owned(),
deprecated: false,
reason: String::new(),
}
}
fn versions_of(versions: &[VersionInfo]) -> Vec<&str> {
versions
.iter()
.map(|version| version.version.as_str())
.collect()
}
fn reason(error: &anyhow::Error) -> Option<UnsupportedReason> {
sourceforge::unsupported_reason(error)
}
// ---------------------------------------------------------------------------
// Capabilities and statuses
// ---------------------------------------------------------------------------
#[test]
fn capabilities_declare_versions_artifacts_and_metadata() {
let source = SourceforgeSource::new(Arc::new(FakeHttp::default()));
assert_eq!(source.name(), "sourceforge");
assert_eq!(
source.capabilities(),
SourceCapabilities {
versions: true,
artifacts: true,
statuses: false,
project_meta: true,
}
);
}
#[tokio::test]
async fn statuses_are_typed_no_statuses_without_requests() {
let fake = Arc::new(FakeHttp::default());
let error = source(&fake)
.statuses(&project("widget"), &version("1.10.0", "1.10.0"))
.await
.expect_err("SourceForge has no status API");
assert_eq!(reason(&error), Some(UnsupportedReason::NoStatuses));
assert!(fake.urls().is_empty());
}
// ---------------------------------------------------------------------------
// Versions
// ---------------------------------------------------------------------------
#[tokio::test]
async fn versions_union_rss_and_listing_and_sort_newest_first() {
let fake = Arc::new(FakeHttp::default());
fake.text(RSS_URL, RSS);
fake.text(FILES_URL, FILES);
let source = source(&fake);
let versions = source
.versions(&project("widget"))
.await
.expect("sourceforge versions");
assert_eq!(versions_of(&versions), ["1.10.0", "1.9.0", "1.8.0"]);
assert_eq!(versions[0].raw, "1.10.0");
assert_eq!(versions[1].raw, "v1.9.0");
assert_eq!(versions[2].raw, "v1.8.0");
assert!(
versions.iter().all(|version| !version.deprecated),
"SourceForge files carry no deprecation signal"
);
assert!(
versions
.iter()
.all(|version| version.version != "docs" && version.version != "1.10.0.tar.gz"),
"folders that are not versions and file extensions are never versions"
);
assert_eq!(fake.urls(), [RSS_URL, FILES_URL]);
fake.text(RSS_URL, RSS);
fake.text(FILES_URL, FILES);
let again = source
.versions(&project("widget"))
.await
.expect("sourceforge versions again");
assert_eq!(again, versions, "ordering is deterministic");
}
#[tokio::test]
async fn empty_project_is_typed_no_versions() {
let fake = Arc::new(FakeHttp::default());
fake.text(RSS_URL, EMPTY_RSS);
fake.text(FILES_URL, EMPTY_FILES);
let error = source(&fake)
.versions(&project("widget"))
.await
.expect_err("empty project");
assert_eq!(reason(&error), Some(UnsupportedReason::NoVersions));
}
// ---------------------------------------------------------------------------
// Artifacts
// ---------------------------------------------------------------------------
#[tokio::test]
async fn artifacts_from_version_folder_use_direct_downloads_and_listing_sizes() {
let fake = Arc::new(FakeHttp::default());
fake.text(FILES_URL, FILES);
fake.text(VERSION_FILES_URL, VERSION_FILES);
let source = source(&fake);
let artifacts = source
.artifacts(&project("widget"), &version("1.10.0", "1.10.0"))
.await
.expect("sourceforge artifacts");
assert_eq!(artifacts.len(), 2);
assert_eq!(artifacts[0].file_name, "widget-1.10.0-win64.zip");
assert_eq!(
artifacts[0].url,
"https://downloads.sourceforge.test/project/widget/1.10.0/widget-1.10.0-win64.zip"
);
assert_eq!(artifacts[0].size, Some(1024 * 1024));
assert_eq!(artifacts[0].mime_type.as_deref(), Some("application/zip"));
assert_eq!(artifacts[1].file_name, "widget-1.10.0.tar.gz");
assert_eq!(
artifacts[1].url,
"https://downloads.sourceforge.test/project/widget/1.10.0/widget-1.10.0.tar.gz"
);
assert_eq!(artifacts[1].size, Some(2 * 1024 * 1024));
assert_eq!(artifacts[1].mime_type.as_deref(), Some("application/gzip"));
assert!(
artifacts.iter().all(|artifact| artifact.sha256.is_none()),
"the digest falls back to the artifact downloader"
);
assert_eq!(fake.urls(), [FILES_URL, VERSION_FILES_URL]);
}
#[tokio::test]
async fn artifacts_from_flat_listing_match_versions_in_file_names() {
let fake = Arc::new(FakeHttp::default());
fake.text(FILES_URL, FILES);
let source = source(&fake);
let artifacts = source
.artifacts(&project("widget"), &version("v1.8.0", "1.8.0"))
.await
.expect("sourceforge flat artifacts");
assert_eq!(artifacts.len(), 1);
assert_eq!(artifacts[0].file_name, "widget-v1.8.0.tar.gz");
assert_eq!(
artifacts[0].url,
"https://downloads.sourceforge.test/project/widget/widget-v1.8.0.tar.gz"
);
assert_eq!(artifacts[0].size, Some(512 * 1024));
assert_eq!(artifacts[0].mime_type.as_deref(), Some("application/gzip"));
assert_eq!(fake.urls(), [FILES_URL]);
}
// ---------------------------------------------------------------------------
// Typed unavailable failures
// ---------------------------------------------------------------------------
#[tokio::test]
async fn missing_project_is_typed_unavailable() {
let fake = Arc::new(FakeHttp::default());
fake.error(
RSS_URL,
"HTTP 404 Not Found for https://sourceforge.test/projects/widget/rss?path=/",
);
fake.error(
FILES_URL,
"HTTP 404 Not Found for https://sourceforge.test/projects/widget/files/",
);
let error = source(&fake)
.versions(&project("widget"))
.await
.expect_err("missing project");
assert_eq!(
reason(&error),
Some(UnsupportedReason::Unavailable(
"SourceForge project is unavailable"
))
);
}
#[tokio::test]
async fn html_error_pages_are_typed_unavailable_and_never_records() {
let fake = Arc::new(FakeHttp::default());
fake.text(RSS_URL, MISSING);
fake.text(FILES_URL, MISSING);
let error = source(&fake)
.versions(&project("widget"))
.await
.expect_err("error page is not a release listing");
assert_eq!(
reason(&error),
Some(UnsupportedReason::Unavailable(
"SourceForge returned an HTML error page"
))
);
}
#[tokio::test]
async fn unknown_purl_type_and_invalid_names_are_typed() {
let fake = Arc::new(FakeHttp::default());
let source = source(&fake);
let error = source
.versions(&typed_project("github", "widget"))
.await
.expect_err("wrong purl type");
assert_eq!(
reason(&error),
Some(UnsupportedReason::UnknownPurlType("github".to_owned()))
);
let error = source
.versions(&project("bad/name"))
.await
.expect_err("invalid project name");
assert!(matches!(
reason(&error),
Some(UnsupportedReason::Unavailable(_))
));
assert!(
fake.urls().is_empty(),
"invalid input never hits the network"
);
}
// ---------------------------------------------------------------------------
// Project metadata
// ---------------------------------------------------------------------------
#[tokio::test]
async fn project_meta_reports_homepage_description_categories_and_latest_release() {
let fake = Arc::new(FakeHttp::default());
fake.text(RSS_URL, RSS);
fake.text(FILES_URL, FILES);
let meta = source(&fake)
.project_meta(&project("widget"))
.await
.expect("sourceforge metadata");
assert_eq!(meta["source"], "sourceforge");
assert_eq!(meta["project"], "widget");
assert_eq!(
meta["homepage"],
"https://sourceforge.test/projects/widget/"
);
assert_eq!(meta["title"], "Widget Toolkit");
assert_eq!(
meta["description"],
"Widget toolkit for testing release ingestion."
);
assert_eq!(meta["categories"], json!(["Development", "Archiving"]));
assert_eq!(meta["latest_release"], "Mon, 15 Jul 2024 12:00:00 +0000");
}