Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tuftool: Allow specifying version in root init #646

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions tuftool/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub(crate) enum Command {
Init {
/// Path to new root.json
path: PathBuf,
/// Initial metadata file version
#[clap(long)]
version: Option<u64>,
},
/// Increment the version
BumpVersion {
Expand Down Expand Up @@ -131,7 +134,7 @@ macro_rules! role_keys {
impl Command {
pub(crate) fn run(self) -> Result<()> {
match self {
Command::Init { path } => Command::init(&path),
Command::Init { path, version } => Command::init(&path, version),
Command::BumpVersion { path } => Command::bump_version(&path),
Command::Expire { path, time } => Command::expire(&path, &time),
Command::SetThreshold {
Expand Down Expand Up @@ -162,14 +165,15 @@ impl Command {
}
}

fn init(path: &Path) -> Result<()> {
fn init(path: &Path, version: Option<u64>) -> Result<()> {
let init_version = version.unwrap_or(1);
write_file(
path,
&Signed {
signed: Root {
spec_version: crate::SPEC_VERSION.to_owned(),
consistent_snapshot: true,
version: NonZeroU64::new(1).unwrap(),
version: NonZeroU64::new(init_version).unwrap(),
expires: round_time(Utc::now()),
keys: HashMap::new(),
roles: hashmap! {
Expand Down
44 changes: 42 additions & 2 deletions tuftool/tests/root_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,46 @@ fn create_root() {
assert_eq!(get_sign_len(root_json.to_str().unwrap()), 2);
}

#[test]
fn create_root_to_version() {
let out_dir = TempDir::new().unwrap();
let root_json = out_dir.path().join("root.json");
let version = NonZeroU64::new(99).unwrap();

Command::cargo_bin("tuftool")
.unwrap()
.args([
"root",
"init",
root_json.to_str().unwrap(),
"--version",
"99",
])
.assert()
.success();

// validate version number
assert_eq!(get_version(root_json.to_str().unwrap()), version);
}

#[test]
fn create_root_invalid_version() {
let out_dir = TempDir::new().unwrap();
let root_json = out_dir.path().join("root.json");

Command::cargo_bin("tuftool")
.unwrap()
.args([
"root",
"init",
root_json.to_str().unwrap(),
"--version",
"0",
])
.assert()
.failure();
}

#[test]
// Ensure creating an unstable root throws error
fn create_unstable_root() {
Expand Down Expand Up @@ -351,13 +391,13 @@ fn set_version_root() {
initialize_root_json(root_json.to_str().unwrap());
let version = NonZeroU64::new(5).unwrap();

//set version to 5
// set version to 5
Command::cargo_bin("tuftool")
.unwrap()
.args(["root", "set-version", root_json.to_str().unwrap(), "5"])
.assert()
.success();

//validate version number
// validate version number
assert_eq!(get_version(root_json.to_str().unwrap()), version);
}