[go: up one dir, main page]

cargo-deny 0.18.2

Cargo plugin to help you manage large dependency graphs
Documentation
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
#![doc = include_str!("../README.md")]

pub use semver::Version;
use std::{cmp, collections::BTreeMap, fmt};
use url::Url;

pub mod advisories;
pub mod bans;
pub mod cfg;
pub mod diag;
/// Configuration and logic for checking crate licenses
pub mod licenses;
pub mod root_cfg;
pub mod sources;

#[doc(hidden)]
pub mod test_utils;

pub use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
pub use cfg::UnvalidatedConfig;
use krates::cm;
pub use krates::{DepKind, Kid};
pub use toml_span::{
    Deserialize, Error,
    span::{Span, Spanned},
};

/// The possible lint levels for the various lints. These function similarly
/// to the standard [Rust lint levels](https://doc.rust-lang.org/rustc/lints/levels.html)
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default, strum::VariantNames, strum::VariantArray)]
#[cfg_attr(test, derive(serde::Serialize))]
#[cfg_attr(test, serde(rename_all = "kebab-case"))]
#[strum(serialize_all = "kebab-case")]
pub enum LintLevel {
    /// A debug or info diagnostic _may_ be emitted if the lint is violated
    Allow,
    /// A warning will be emitted if the lint is violated, but the command
    /// will succeed
    #[default]
    Warn,
    /// An error will be emitted if the lint is violated, and the command
    /// will fail with a non-zero exit code
    Deny,
}

#[macro_export]
macro_rules! enum_deser {
    ($enum:ty) => {
        impl<'de> toml_span::Deserialize<'de> for $enum {
            fn deserialize(
                value: &mut toml_span::value::Value<'de>,
            ) -> Result<Self, toml_span::DeserError> {
                let s = value.take_string(Some(stringify!($enum)))?;

                use strum::{VariantArray, VariantNames};

                let Some(pos) = <$enum as VariantNames>::VARIANTS
                    .iter()
                    .position(|v| *v == s.as_ref())
                else {
                    return Err(toml_span::Error::from((
                        toml_span::ErrorKind::UnexpectedValue {
                            expected: <$enum as VariantNames>::VARIANTS,
                            value: None,
                        },
                        value.span,
                    ))
                    .into());
                };

                Ok(<$enum as VariantArray>::VARIANTS[pos])
            }
        }
    };
}

enum_deser!(LintLevel);

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Source {
    /// crates.io, the boolean indicates whether it is a sparse index
    CratesIo(bool),
    /// A remote git patch
    Git {
        spec: GitSpec,
        url: Url,
        spec_value: Option<String>,
    },
    /// A remote non-sparse registry index
    Registry(Url),
    /// A remote sparse index
    Sparse(Url),
}

/// The directory name under which crates sourced from the crates.io sparse
/// registry are placed
fn crates_io_sparse_dir() -> &'static str {
    static mut CRATES_IO_SPARSE_DIR: String = String::new();
    static CRATES_IO_INIT: parking_lot::Once = parking_lot::Once::new();

    #[allow(unsafe_code)]
    // SAFETY: We're mutating a static, but we only allow one mutation
    unsafe {
        CRATES_IO_INIT.call_once(|| {
            let Ok(version) = tame_index::utils::cargo_version(None) else {
                return;
            };
            let Ok(url_dir) = tame_index::utils::url_to_local_dir(
                tame_index::CRATES_IO_HTTP_INDEX,
                version >= semver::Version::new(1, 85, 0),
            ) else {
                return;
            };
            CRATES_IO_SPARSE_DIR = url_dir.dir_name;
        });

        #[allow(static_mut_refs)]
        &CRATES_IO_SPARSE_DIR
    }
}

impl Source {
    pub fn crates_io(is_sparse: bool) -> Self {
        Self::CratesIo(is_sparse)
    }

    /// Parses the source url to get its kind
    ///
    /// Note that the path is the path to the manifest of the package. This is
    /// used to determine if the crates.io registry is git or sparse, as, currently,
    /// cargo always uses the git registry+ url for crates.io, even if it uses the
    /// sparse registry.
    ///
    /// This method therefore assumes that the crates sources are laid out in the
    /// canonical cargo structure, though it can be rooted somewhere other than
    /// `CARGO_HOME`
    fn from_metadata(urls: String, manifest_path: &Path) -> anyhow::Result<Self> {
        use anyhow::Context as _;

        let (kind, url_str) = urls
            .split_once('+')
            .with_context(|| format!("'{urls}' is not a valid crate source"))?;

        match kind {
            "sparse" => {
                // This code won't ever be hit in current cargo, but could in the future
                if urls == tame_index::CRATES_IO_HTTP_INDEX {
                    Ok(Self::crates_io(true))
                } else {
                    Url::parse(&urls)
                        .map(Self::Sparse)
                        .context("failed to parse url")
                }
            }
            "registry" => {
                if url_str == tame_index::CRATES_IO_INDEX {
                    // registry/src/index.crates.io-6f17d22bba15001f/crate-version/Cargo.toml
                    let is_sparse = manifest_path.ancestors().nth(2).is_some_and(|dir| {
                        dir.file_name()
                            .is_some_and(|dir_name| dir_name == crates_io_sparse_dir())
                    });
                    Ok(Self::crates_io(is_sparse))
                } else {
                    Url::parse(url_str)
                        .map(Self::Registry)
                        .context("failed to parse url")
                }
            }
            "git" => {
                let mut url = Url::parse(url_str).context("failed to parse url")?;
                let (spec, spec_value) = normalize_git_url(&mut url);

                Ok(Self::Git {
                    url,
                    spec,
                    spec_value,
                })
            }
            unknown => anyhow::bail!("unknown source spec '{unknown}' for url {urls}"),
        }
    }

    #[inline]
    pub fn is_git(&self) -> bool {
        matches!(self, Self::Git { .. })
    }

    #[inline]
    pub fn git_spec(&self) -> Option<GitSpec> {
        let Self::Git { spec, .. } = self else {
            return None;
        };
        Some(*spec)
    }

    #[inline]
    pub fn is_registry(&self) -> bool {
        !self.is_git()
    }

    #[inline]
    pub fn is_crates_io(&self) -> bool {
        matches!(self, Self::CratesIo(_))
    }

    #[inline]
    pub fn to_rustsec(&self) -> rustsec::package::SourceId {
        use rustsec::package::SourceId;
        // TODO: Change this once rustsec supports sparse indices
        match self {
            Self::CratesIo(_) => SourceId::default(),
            Self::Registry(url) => SourceId::for_registry(url).unwrap(),
            Self::Sparse(sparse) => {
                // There is currently no way to publicly construct a sparse registry
                // id other than this method
                SourceId::from_url(sparse.as_str()).unwrap()
            }
            Self::Git { .. } => unreachable!(),
        }
    }

    #[inline]
    pub fn matches_rustsec(&self, sid: Option<&rustsec::package::SourceId>) -> bool {
        let Some(sid) = sid else {
            return self.is_crates_io();
        };
        if !sid.is_remote_registry() {
            return false;
        }

        let (Self::Registry(url) | Self::Sparse(url)) = self else {
            return false;
        };
        sid.url() == url
    }
}

impl fmt::Display for Source {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CratesIo(_) => {
                write!(f, "registry+{}", tame_index::CRATES_IO_INDEX)
            }
            Self::Git { url, .. } => {
                write!(f, "git+{url}")
            }
            Self::Registry(url) => {
                write!(f, "registry+{url}")
            }
            Self::Sparse(url) => {
                write!(f, "{url}")
            }
        }
    }
}

#[derive(Debug)]
pub struct Krate {
    pub name: String,
    pub id: Kid,
    pub version: Version,
    pub source: Option<Source>,
    pub authors: Vec<String>,
    pub repository: Option<String>,
    pub description: Option<String>,
    pub manifest_path: PathBuf,
    pub license: Option<String>,
    pub license_file: Option<PathBuf>,
    pub deps: Vec<cm::Dependency>,
    pub features: BTreeMap<String, Vec<String>>,
    pub targets: Vec<cm::Target>,
    pub publish: Option<Vec<String>>,
}

#[cfg(test)]
impl Default for Krate {
    fn default() -> Self {
        Self {
            name: "".to_owned(),
            version: Version::new(0, 1, 0),
            authors: Vec::new(),
            id: Kid::default(),
            source: None,
            description: None,
            deps: Vec::new(),
            license: None,
            license_file: None,
            targets: Vec::new(),
            features: BTreeMap::new(),
            manifest_path: PathBuf::new(),
            repository: None,
            publish: None,
        }
    }
}

impl PartialOrd for Krate {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Krate {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.id.cmp(&other.id)
    }
}

impl PartialEq for Krate {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Krate {}

impl krates::KrateDetails for Krate {
    #[inline]
    fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    fn version(&self) -> &semver::Version {
        &self.version
    }
}

impl From<cm::Package> for Krate {
    fn from(pkg: cm::Package) -> Self {
        let source = pkg.source.and_then(|src| {
            let url = src.to_string();

            Source::from_metadata(url, &pkg.manifest_path)
                .map_err(|err| {
                    log::warn!(
                        "unable to parse source url for {}:{}: {err}",
                        pkg.name,
                        pkg.version
                    );
                    err
                })
                .ok()
        });

        Self {
            name: pkg.name,
            id: pkg.id.into(),
            version: pkg.version,
            authors: pkg.authors,
            repository: pkg.repository,
            source,
            targets: pkg.targets,
            license: pkg.license.map(|lf| {
                // cargo used to allow / in place of OR which is not valid
                // in SPDX expression, we force correct it here
                if lf.contains('/') {
                    lf.replace('/', " OR ")
                } else {
                    lf
                }
            }),
            license_file: pkg.license_file,
            description: pkg.description,
            manifest_path: pkg.manifest_path,
            deps: pkg.dependencies,
            // {
            //     let mut deps = pkg.dependencies;
            //     deps.sort_by(|a, b| a.name.cmp(&b.name));
            //     deps
            // },
            features: pkg.features,
            publish: pkg.publish,
        }
    }
}

impl Krate {
    /// Returns true if the crate is marked as `publish = false`, or
    /// it is only published to the specified private registries
    pub(crate) fn is_private(&self, private_registries: &[&str]) -> bool {
        self.publish.as_ref().is_some_and(|v| {
            if v.is_empty() {
                true
            } else {
                v.iter()
                    .all(|reg| private_registries.contains(&reg.as_str()))
            }
        })
    }

    /// Determines if the specified url matches the source
    #[inline]
    pub(crate) fn matches_url(&self, url: &Url, exact: bool) -> bool {
        let Some(src) = &self.source else {
            return false;
        };

        let kurl = match src {
            Source::CratesIo(_is_sparse) => {
                // It's irrelevant if it's sparse or not for crates.io, they're the same
                // index, just different protocols/kinds
                return url
                    .as_str()
                    .ends_with(&tame_index::CRATES_IO_HTTP_INDEX[8..])
                    || url.as_str().ends_with(&tame_index::CRATES_IO_INDEX[10..]);
            }
            Source::Sparse(surl) | Source::Registry(surl) | Source::Git { url: surl, .. } => surl,
        };

        kurl.host() == url.host()
            && ((exact && kurl.path() == url.path())
                || (!exact && kurl.path().starts_with(url.path())))
    }

    #[inline]
    pub(crate) fn is_crates_io(&self) -> bool {
        self.source.as_ref().is_some_and(|src| src.is_crates_io())
    }

    #[inline]
    pub(crate) fn is_git_source(&self) -> bool {
        self.source.as_ref().is_some_and(|src| src.is_git())
    }

    #[inline]
    pub(crate) fn is_registry(&self) -> bool {
        self.source.as_ref().is_some_and(|src| src.is_registry())
    }
}

impl fmt::Display for Krate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} = {}", self.name, self.version)
    }
}

pub type Krates = krates::Krates<Krate>;

#[inline]
pub fn binary_search<T, Q>(s: &[T], query: &Q) -> Result<usize, usize>
where
    T: std::borrow::Borrow<Q>,
    Q: Ord + ?Sized,
{
    s.binary_search_by(|i| i.borrow().cmp(query))
}

#[inline]
pub fn contains<T, Q>(s: &[T], query: &Q) -> bool
where
    T: std::borrow::Borrow<Q>,
    Q: Eq + ?Sized,
{
    s.iter().any(|i| i.borrow() == query)
}

#[inline]
pub fn hash(data: &[u8]) -> u32 {
    use std::hash::Hasher;
    // We use the 32-bit hash instead of the 64 even though
    // it is significantly slower due to the TOML limitation
    // if only supporting i64
    let mut xx = twox_hash::XxHash32::default();
    xx.write(data);
    xx.finish() as u32
}

/// Common context for the various checks. Some checks require additional
/// information though.
pub struct CheckCtx<'ctx, T> {
    /// The configuration for the check
    pub cfg: T,
    /// The krates graph to check
    pub krates: &'ctx Krates,
    /// The spans for each unique crate in a synthesized "lock file"
    pub krate_spans: &'ctx diag::KrateSpans<'ctx>,
    /// Requests for additional information the check can provide to be
    /// serialized to the diagnostic
    pub serialize_extra: bool,
    /// Allows for ANSI colorization of diagnostic content
    pub colorize: bool,
    /// Log level specified by the user, may be used by checks to determine what
    /// information to emit in diagnostics
    pub log_level: log::LevelFilter,
    /// Files that can show span information in diagnostics
    pub files: &'ctx diag::Files,
}

/// Checks if a version satisfies the specifies the specified version requirement.
/// If the requirement is `None` then it is also satisfied.
#[inline]
pub fn match_req(version: &Version, req: Option<&semver::VersionReq>) -> bool {
    req.is_none_or(|req| req.matches(version))
}

#[inline]
pub fn match_krate(krate: &Krate, pid: &cfg::PackageSpec) -> bool {
    krate.name == pid.name.value && match_req(&krate.version, pid.version_req.as_ref())
}

use sources::cfg::GitSpec;

/// Normalizes the URL so that different representations can be compared to each other.
///
/// At the moment we just remove a tailing `.git` but there are more possible optimisations.
///
/// See <https://github.com/rust-lang/cargo/blob/1f6c6bd5e7bbdf596f7e88e6db347af5268ab113/src/cargo/util/canonical_url.rs#L31-L57>
/// for what cargo does
#[inline]
pub(crate) fn normalize_git_url(url: &mut Url) -> (GitSpec, Option<String>) {
    const GIT_EXT: &str = ".git";

    let needs_chopping = url.path().ends_with(&GIT_EXT);
    if needs_chopping {
        let last = {
            let last = url.path_segments().unwrap().next_back().unwrap();
            last[..last.len() - GIT_EXT.len()].to_owned()
        };
        url.path_segments_mut().unwrap().pop().push(&last);
    }

    if url.path().ends_with('/') {
        url.path_segments_mut().unwrap().pop_if_empty();
    }

    let mut spec = GitSpec::Any;
    let mut spec_value = None;

    for (k, v) in url.query_pairs() {
        spec = match k.as_ref() {
            "branch" | "ref" => GitSpec::Branch,
            "tag" => GitSpec::Tag,
            "rev" => GitSpec::Rev,
            _ => continue,
        };

        spec_value = Some(v.into_owned());
    }

    if url
        .query_pairs()
        .any(|(k, v)| k == "branch" && v == "master")
    {
        if url.query_pairs().count() == 1 {
            url.set_query(None);
        } else {
            let mut nq = String::new();
            for (k, v) in url.query_pairs() {
                if k == "branch" && v == "master" {
                    continue;
                }

                use std::fmt::Write;
                write!(&mut nq, "{k}={v}&").unwrap();
            }

            // pop trailing &
            nq.pop();
            url.set_query(Some(&nq));
        }
    }

    (spec, spec_value)
}

/// Helper function to convert a std `PathBuf` to a camino one
#[inline]
#[allow(clippy::disallowed_types)]
pub fn utf8path(pb: std::path::PathBuf) -> anyhow::Result<PathBuf> {
    use anyhow::Context;
    PathBuf::try_from(pb).context("non-utf8 path")
}

/// Adds the crates.io index with the specified settings to the builder for
/// feature resolution
pub fn krates_with_index(
    kb: &mut krates::Builder,
    config_root: Option<PathBuf>,
    cargo_home: Option<PathBuf>,
) -> anyhow::Result<()> {
    use anyhow::Context as _;
    let crates_io = tame_index::IndexUrl::crates_io(config_root, cargo_home.as_deref(), None)
        .context("unable to determine crates.io url")?;

    let index = tame_index::index::ComboIndexCache::new(
        tame_index::IndexLocation::new(crates_io).with_root(cargo_home.clone()),
    )
    .context("unable to open local crates.io index")?;

    // Note we don't take a lock here ourselves, since we are calling cargo
    // it will take the lock and only give us results if it gets access, if we
    // took a look we would deadlock here
    let lock = tame_index::utils::flock::FileLock::unlocked();

    let index_cache_build = move |krates: std::collections::BTreeSet<String>| {
        let mut cache = std::collections::BTreeMap::new();
        for name in krates {
            let read = || -> Option<krates::index::IndexKrate> {
                let name = name.as_str().try_into().ok()?;
                let krate = index.cached_krate(name, &lock).ok()??;
                let versions = krate
                    .versions
                    .into_iter()
                    .filter_map(|kv| {
                        // The index (currently) can have both features, and
                        // features2, the features method gives us an iterator
                        // over both
                        kv.version.parse::<semver::Version>().ok().map(|version| {
                            krates::index::IndexKrateVersion {
                                version,
                                features: kv
                                    .features()
                                    .map(|(k, v)| (k.clone(), v.clone()))
                                    .collect(),
                            }
                        })
                    })
                    .collect();

                Some(krates::index::IndexKrate { versions })
            };

            let krate = read();
            cache.insert(name, krate);
        }

        cache
    };

    kb.with_crates_io_index(Box::new(index_cache_build));

    Ok(())
}

#[cfg(test)]
mod test {
    use super::{Krate, PathBuf, Source, Url};

    #[test]
    fn parses_sources() {
        let empty_dir = super::Path::new("");
        let crates_io_git = Source::from_metadata(
            format!("registry+{}", tame_index::CRATES_IO_INDEX),
            empty_dir,
        )
        .unwrap();
        let crates_io_sparse =
            Source::from_metadata(tame_index::CRATES_IO_HTTP_INDEX.to_owned(), empty_dir).unwrap();
        let crates_io_sparse_but_git = Source::from_metadata(
            format!("registry+{}", tame_index::CRATES_IO_INDEX),
            super::Path::new(&format!(
                "registry/src/{}/cargo-deny-0.69.0/Cargo.toml",
                super::crates_io_sparse_dir(),
            )),
        )
        .unwrap();

        assert!(
            crates_io_git.is_registry()
                && crates_io_sparse.is_registry()
                && crates_io_sparse_but_git.is_registry()
        );
        assert!(
            crates_io_git.is_crates_io()
                && crates_io_sparse.is_crates_io()
                && crates_io_sparse_but_git.is_crates_io()
        );

        assert!(
            Source::from_metadata(
                "registry+https://my-own-my-precious.com/".to_owned(),
                empty_dir
            )
            .unwrap()
            .is_registry()
        );
        assert!(
            Source::from_metadata("sparse+https://my-registry.rs/".to_owned(), empty_dir)
                .unwrap()
                .is_registry()
        );

        let src = Source::from_metadata("git+https://github.com/EmbarkStudios/wasmtime?branch=v6.0.1-profiler#84b8cacceacb585ef53774c3790b2372ba080067".to_owned(), empty_dir).unwrap();

        assert!(src.is_git());
    }

    /// Sanity checks that the crates.io sparse registry still uses the same
    /// local directory. Really this should be doing a cargo invocation, but
    /// meh, we depend on tame-index to stay up to date
    #[test]
    fn validate_crates_io_sparse_dir_name() {
        let stable =
            tame_index::utils::cargo_version(None).unwrap() >= tame_index::Version::new(1, 85, 0);
        assert_eq!(
            tame_index::utils::url_to_local_dir(tame_index::CRATES_IO_HTTP_INDEX, stable)
                .unwrap()
                .dir_name,
            super::crates_io_sparse_dir(),
        );
    }

    #[test]
    fn inexact_match_fails_for_different_hosts() {
        let krate = Krate {
            source: Some(
                Source::from_metadata(
                    "git+ssh://git@repo1.test.org/path/test.git".to_owned(),
                    &PathBuf::new(),
                )
                .unwrap(),
            ),
            ..Krate::default()
        };
        let url = Url::parse("ssh://git@repo2.test.org:8000").unwrap();

        assert!(!krate.matches_url(&url, false));
    }

    #[test]
    fn inexact_match_passes_for_same_hosts() {
        let krate = Krate {
            source: Some(
                Source::from_metadata(
                    "git+ssh://git@repo1.test.org/path/test.git".to_owned(),
                    &PathBuf::new(),
                )
                .unwrap(),
            ),
            ..Krate::default()
        };
        let url = Url::parse("ssh://git@repo1.test.org:8000").unwrap();

        assert!(krate.matches_url(&url, false));
    }
}