Skip to content

Commit 2179665

Browse files
committed
Ensure hash and stable_hash of SourceId are stable
1 parent a803a75 commit 2179665

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,4 +809,59 @@ mod tests {
809809
let crates_io = SourceId::crates_io(&config).unwrap();
810810
assert_eq!(crate::util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
811811
}
812+
813+
// See the comment in `test_cratesio_hash`.
814+
//
815+
// Only test on non-Windows as paths on Windows will get different hashes.
816+
#[test]
817+
#[cfg(all(target_endian = "little", target_pointer_width = "64", not(windows)))]
818+
fn test_stable_hash() {
819+
use std::hash::Hasher;
820+
use std::path::Path;
821+
822+
let gen_hash = |source_id: SourceId| {
823+
let mut hasher = std::collections::hash_map::DefaultHasher::new();
824+
source_id.stable_hash(Path::new("/tmp/ws"), &mut hasher);
825+
hasher.finish()
826+
};
827+
828+
let url = "https://my-crates.io".into_url().unwrap();
829+
let source_id = SourceId::for_registry(&url).unwrap();
830+
assert_eq!(gen_hash(source_id), 18108075011063494626);
831+
assert_eq!(crate::util::hex::short_hash(&source_id), "fb60813d6cb8df79");
832+
833+
let url = "https://your-crates.io".into_url().unwrap();
834+
let source_id = SourceId::for_alt_registry(&url, "alt").unwrap();
835+
assert_eq!(gen_hash(source_id), 12862859764592646184);
836+
assert_eq!(crate::util::hex::short_hash(&source_id), "09c10fd0cbd74bce");
837+
838+
let url = "sparse+https://my-crates.io".into_url().unwrap();
839+
let source_id = SourceId::for_registry(&url).unwrap();
840+
assert_eq!(gen_hash(source_id), 8763561830438022424);
841+
assert_eq!(crate::util::hex::short_hash(&source_id), "d1ea0d96f6f759b5");
842+
843+
let url = "sparse+https://your-crates.io".into_url().unwrap();
844+
let source_id = SourceId::for_alt_registry(&url, "alt").unwrap();
845+
assert_eq!(gen_hash(source_id), 5159702466575482972);
846+
assert_eq!(crate::util::hex::short_hash(&source_id), "135d23074253cb78");
847+
848+
let url = "file:///tmp/ws/crate".into_url().unwrap();
849+
let source_id = SourceId::for_git(&url, GitReference::DefaultBranch).unwrap();
850+
assert_eq!(gen_hash(source_id), 15332537265078583985);
851+
assert_eq!(crate::util::hex::short_hash(&source_id), "73a808694abda756");
852+
853+
let path = Path::new("/tmp/ws/crate");
854+
855+
let source_id = SourceId::for_local_registry(path).unwrap();
856+
assert_eq!(gen_hash(source_id), 18446533307730842837);
857+
assert_eq!(crate::util::hex::short_hash(&source_id), "52a84cc73f6fd48b");
858+
859+
let source_id = SourceId::for_path(path).unwrap();
860+
assert_eq!(gen_hash(source_id), 8764714075439899829);
861+
assert_eq!(crate::util::hex::short_hash(&source_id), "e1ddd48578620fc1");
862+
863+
let source_id = SourceId::for_directory(path).unwrap();
864+
assert_eq!(gen_hash(source_id), 17459999773908528552);
865+
assert_eq!(crate::util::hex::short_hash(&source_id), "6568fe2c2fab5bfe");
866+
}
812867
}

0 commit comments

Comments
 (0)