178 lines
6.3 KiB
Rust
178 lines
6.3 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn each_channel_gets_its_newest_release_above_the_installed_version() {
|
|
let key = key();
|
|
let (url, _) = serve(
|
|
vec![
|
|
publish(&key, "stable", "1.0.0"),
|
|
publish(&key, "beta", "1.2.0-beta.10"),
|
|
publish(&key, "stable", "1.1.0"),
|
|
publish(&key, "beta", "1.2.0-beta.2"),
|
|
publish(&key, "stable", "1.0.1"),
|
|
publish(&key, "stable", "2.0"),
|
|
],
|
|
false,
|
|
);
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check =
|
|
|channel, installed| download_update(&url, dir.path(), public, channel, installed).unwrap();
|
|
|
|
assert_eq!(
|
|
check(UpdateChannel::Stable, "1.0.0").as_deref(),
|
|
Some("1.1.0")
|
|
);
|
|
assert_eq!(kept(dir.path()), ["stable-1.1.0"]);
|
|
let ready = dir.path().join("stable-1.1.0");
|
|
assert!(verify_release(&ready, public, "stable", "1.1.0").is_ok());
|
|
// The same version or an older one is ignored, and nothing stays pending.
|
|
assert_eq!(check(UpdateChannel::Stable, "1.1.0"), None);
|
|
assert_eq!(check(UpdateChannel::Stable, "1.2.0"), None);
|
|
assert!(kept(dir.path()).is_empty());
|
|
|
|
// beta.10 comes after beta.2, and the beta channel never takes a stable.
|
|
assert_eq!(
|
|
check(UpdateChannel::Beta, "1.1.0-beta.1").as_deref(),
|
|
Some("1.2.0-beta.10")
|
|
);
|
|
assert_eq!(
|
|
check(UpdateChannel::Beta, "1.0.0").as_deref(),
|
|
Some("1.2.0-beta.10")
|
|
);
|
|
assert_eq!(kept(dir.path()), ["beta-1.2.0-beta.10"]);
|
|
assert_eq!(check(UpdateChannel::Beta, "1.2.0-beta.10"), None);
|
|
assert!(kept(dir.path()).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn back_to_stable_from_a_newer_beta_waits_for_a_newer_stable() {
|
|
let key = key();
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check = |url: &str| {
|
|
download_update(
|
|
url,
|
|
dir.path(),
|
|
public,
|
|
UpdateChannel::Stable,
|
|
"1.2.0-beta.3",
|
|
)
|
|
.unwrap()
|
|
};
|
|
let older = vec![
|
|
publish(&key, "stable", "1.1.0"),
|
|
publish(&key, "beta", "1.2.0-beta.4"),
|
|
];
|
|
assert_eq!(check(&serve(older, false).0), None);
|
|
let newer = vec![
|
|
publish(&key, "stable", "1.1.0"),
|
|
publish(&key, "stable", "1.2.0"),
|
|
];
|
|
assert_eq!(check(&serve(newer, false).0).as_deref(), Some("1.2.0"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_kept_package_is_verified_again_and_downloaded_again_once_altered() {
|
|
let key = key();
|
|
let (url, downloads) = serve(vec![publish(&key, "stable", "1.1.0")], false);
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check = || download_update(&url, dir.path(), public, UpdateChannel::Stable, "1.0.0");
|
|
assert_eq!(check().unwrap().as_deref(), Some("1.1.0"));
|
|
assert_eq!(check().unwrap().as_deref(), Some("1.1.0"));
|
|
assert_eq!(downloads.load(Ordering::SeqCst), 1);
|
|
|
|
let package = dir
|
|
.path()
|
|
.join("stable-1.1.0/ArenaTracker-1.1.0-stable-full.nupkg");
|
|
fs::write(&package, b"altered").unwrap();
|
|
assert_eq!(check().unwrap().as_deref(), Some("1.1.0"));
|
|
assert_eq!(downloads.load(Ordering::SeqCst), 2);
|
|
assert_eq!(fs::read(&package).unwrap(), b"package 1.1.0");
|
|
}
|
|
|
|
#[test]
|
|
fn a_channel_change_discards_the_other_channels_package_even_offline() {
|
|
let key = key();
|
|
let (url, _) = serve(
|
|
vec![
|
|
publish(&key, "stable", "1.1.0"),
|
|
publish(&key, "beta", "1.2.0-beta.1"),
|
|
],
|
|
false,
|
|
);
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check = |url: &str, channel| download_update(url, dir.path(), public, channel, "1.0.0");
|
|
assert!(check(&url, UpdateChannel::Stable).unwrap().is_some());
|
|
|
|
// A late stable package goes as soon as a beta check starts, before any network access.
|
|
let offline = unreachable();
|
|
assert!(matches!(
|
|
check(&offline, UpdateChannel::Beta),
|
|
Err(Error::Source(_))
|
|
));
|
|
assert!(kept(dir.path()).is_empty());
|
|
assert_eq!(
|
|
check(&url, UpdateChannel::Beta).unwrap().as_deref(),
|
|
Some("1.2.0-beta.1")
|
|
);
|
|
// Offline, the channel's own pending package stays for a later launch.
|
|
assert!(check(&offline, UpdateChannel::Beta).is_err());
|
|
assert_eq!(kept(dir.path()), ["beta-1.2.0-beta.1"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_newer_download_keeps_the_older_pending_package() {
|
|
let key = key();
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check = |url: &str| download_update(url, dir.path(), public, UpdateChannel::Beta, "1.0.0");
|
|
let first = vec![publish(&key, "beta", "1.1.0-beta.1")];
|
|
assert!(check(&serve(first, false).0).is_ok());
|
|
let newer = || {
|
|
vec![
|
|
publish(&key, "beta", "1.1.0-beta.1"),
|
|
publish(&key, "beta", "1.1.0-beta.2"),
|
|
]
|
|
};
|
|
assert!(check(&serve(newer(), true).0).is_err());
|
|
assert_eq!(kept(dir.path()), ["beta-1.1.0-beta.1"]);
|
|
let ready = check(&serve(newer(), false).0).unwrap();
|
|
assert_eq!(ready.as_deref(), Some("1.1.0-beta.2"));
|
|
assert_eq!(kept(dir.path()), ["beta-1.1.0-beta.2"]);
|
|
}
|
|
|
|
#[test]
|
|
fn network_and_signature_failures_leave_no_update_ready() {
|
|
let key = key();
|
|
let other = Ed25519KeyPair::from_seed_unchecked(&[8; 32]).unwrap();
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let public = key.public_key().as_ref();
|
|
let check =
|
|
|url: &str| download_update(url, dir.path(), public, UpdateChannel::Stable, "1.0.0");
|
|
|
|
assert!(matches!(check(&unreachable()), Err(Error::Source(_))));
|
|
let (cut, _) = serve(vec![publish(&key, "stable", "1.1.0")], true);
|
|
assert!(check(&cut).is_err());
|
|
assert!(kept(dir.path()).is_empty());
|
|
|
|
let (forged, _) = serve(vec![publish(&other, "stable", "1.1.0")], false);
|
|
assert!(matches!(check(&forged), Err(Error::InvalidUpdate(_))));
|
|
assert!(kept(dir.path()).is_empty());
|
|
|
|
let mut altered = publish(&key, "stable", "1.1.0");
|
|
altered.files[2].1 = b"package 6.6.6".to_vec();
|
|
assert!(matches!(
|
|
check(&serve(vec![altered], false).0),
|
|
Err(Error::InvalidUpdate(_))
|
|
));
|
|
assert!(kept(dir.path()).is_empty());
|
|
|
|
// A release without its signature is not offered at all.
|
|
let mut unsigned = publish(&key, "stable", "1.1.0");
|
|
unsigned.files.remove(0);
|
|
assert_eq!(check(&serve(vec![unsigned], false).0).unwrap(), None);
|
|
}
|