Skip to content

Commit

Permalink
Merge 47554f9 into 01961ff
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathas-Conceicao committed Aug 25, 2021
2 parents 01961ff + 47554f9 commit a4ced0a
Show file tree
Hide file tree
Showing 33 changed files with 872 additions and 222 deletions.
366 changes: 365 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions updatehub-package-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod flash;
mod imxkobs;
mod mender;
mod raw;
mod raw_delta;
mod tarball;
mod test;
mod ubifs;
Expand All @@ -20,8 +21,8 @@ pub mod definitions;
/// Objects representing each possible install mode
pub mod objects {
pub use crate::{
copy::Copy, flash::Flash, imxkobs::Imxkobs, mender::Mender, raw::Raw, tarball::Tarball,
test::Test, ubifs::Ubifs, uboot_env::UbootEnv, zephyr::Zephyr,
copy::Copy, flash::Flash, imxkobs::Imxkobs, mender::Mender, raw::Raw, raw_delta::RawDelta,
tarball::Tarball, test::Test, ubifs::Ubifs, uboot_env::UbootEnv, zephyr::Zephyr,
};
}
pub use update_package::{SupportedHardware, UpdatePackage};
Expand All @@ -38,6 +39,8 @@ pub enum Object {
Imxkobs(Box<objects::Imxkobs>),
Mender(Box<objects::Mender>),
Raw(Box<objects::Raw>),
#[serde(rename = "raw-delta")]
RawDelta(Box<objects::RawDelta>),
Tarball(Box<objects::Tarball>),
Test(Box<objects::Test>),
Ubifs(Box<objects::Ubifs>),
Expand Down
46 changes: 46 additions & 0 deletions updatehub-package-schema/src/raw_delta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2021 O.S. Systems Sofware LTDA
//
// SPDX-License-Identifier: Apache-2.0

use crate::definitions::{ChunkSize, TargetType};
use serde::Deserialize;

#[derive(Deserialize, PartialEq, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct RawDelta {
pub filename: String,
pub sha256sum: String,
#[serde(flatten)]
pub target: TargetType,
pub size: u64,
#[serde(default)]
pub chunk_size: ChunkSize,
#[serde(default)]
pub seek: u64,
}

#[test]
fn deserialize() {
use pretty_assertions::assert_eq;
use serde_json::json;

assert_eq!(
RawDelta {
filename: "etc/passwd".to_string(),
sha256sum: "cfe2be1c64b0387500853de0f48303e3de7b1c6f1508dc719eeafa0d41c36722"
.to_string(),
target: TargetType::Device(std::path::PathBuf::from("/dev/sda1")),
chunk_size: ChunkSize::default(),
seek: 0,
size: 1024,
},
serde_json::from_value::<RawDelta>(json!({
"filename": "etc/passwd",
"sha256sum": "cfe2be1c64b0387500853de0f48303e3de7b1c6f1508dc719eeafa0d41c36722",
"target-type": "device",
"target": "/dev/sda1",
"size": 1024,
}))
.unwrap()
);
}
7 changes: 6 additions & 1 deletion updatehub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ required-features = ["test-env"]
[dependencies]
argh = "0.1.3"
async-ctrlc = { version = "1", optional = true }
async-std = { version = "1", features = ["unstable"] }
async-std = { version = "1", features = ["unstable", "tokio1"] }
async-trait = "0.1"
async-lock = "2"
chrono = { version = "0.4", default-features = false, features = ["serde"] }
Expand Down Expand Up @@ -68,6 +68,11 @@ timeout-readwrite = "0.3"
toml = "0.5"
walkdir = "2"

bitar = { version = "0.8", git = "https://github.com/oll3/bita" }
futures-util = { version = "0.3", default-features = false }
tokio = { version = "1", default-features = false, features = ["fs"] }
url = "2"

[build-dependencies]
git-version = "0.3"

Expand Down
Binary file added updatehub/fixtures/message.bita
Binary file not shown.
11 changes: 9 additions & 2 deletions updatehub/src/object/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use super::Result;
use crate::utils;
use openssl::sha::Sha256;
use pkg_schema::{
objects::{Copy, Flash, Imxkobs, Mender, Raw, Tarball, Test, Ubifs, UbootEnv, Zephyr},
objects::{
Copy, Flash, Imxkobs, Mender, Raw, RawDelta, Tarball, Test, Ubifs, UbootEnv, Zephyr,
},
Object,
};
use std::{
Expand All @@ -23,6 +25,7 @@ pub(crate) enum Status {
Ready,
}

impl_remote_object_info!(RawDelta);
impl_compressed_object_info!(Copy);
impl_compressed_object_info!(Raw);
impl_compressed_object_info!(Ubifs);
Expand All @@ -35,7 +38,7 @@ impl_object_info!(UbootEnv);
impl_object_info!(Zephyr);

impl_object_for_object_types!(
Copy, Flash, Imxkobs, Mender, Tarball, Ubifs, Raw, Test, UbootEnv, Zephyr
RawDelta, Copy, Flash, Imxkobs, Mender, Tarball, Ubifs, Raw, Test, UbootEnv, Zephyr
);

pub(crate) trait Info {
Expand Down Expand Up @@ -69,6 +72,10 @@ pub(crate) trait Info {
Ok(Status::Ready)
}

fn allow_remote_install(&self) -> bool {
false
}

fn mode(&self) -> String;
fn filename(&self) -> &str;
fn len(&self) -> u64;
Expand Down
45 changes: 26 additions & 19 deletions updatehub/src/object/installer/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use std::{
os::unix::fs::PermissionsExt,
};

#[async_trait::async_trait(?Send)]
impl Installer for objects::Copy {
fn check_requirements(&self) -> Result<()> {
async fn check_requirements(&self, _: &Context) -> Result<()> {
info!("'copy' handle checking requirements");

if let definitions::TargetType::Device(dev) = self.target_type.valid()? {
Expand All @@ -27,7 +28,7 @@ impl Installer for objects::Copy {
Err(Error::InvalidTargetType(self.target_type.clone()))
}

fn install(&self, context: &Context) -> Result<()> {
async fn install(&self, context: &Context) -> Result<()> {
info!("'copy' handler Install {} ({})", self.filename, self.sha256sum);

let device = self.target_type.get_target()?;
Expand Down Expand Up @@ -112,7 +113,7 @@ mod tests {
const ORIGINAL_BYTE: u8 = 0xA;
const FILE_SIZE: usize = 2048;

fn exec_test_with_copy<F>(
async fn exec_test_with_copy<F>(
mut f: F,
original_permissions: Option<definitions::TargetPermissions>,
compressed: bool,
Expand Down Expand Up @@ -189,8 +190,8 @@ mod tests {
f(&mut obj);

// Peform Install
obj.check_requirements()?;
obj.install(&Context::default())?;
obj.check_requirements(&Context::default()).await?;
obj.install(&Context::default()).await?;

// Validade File
#[allow(clippy::redundant_clone)]
Expand Down Expand Up @@ -237,21 +238,23 @@ mod tests {
Ok(())
}

#[test]
#[async_std::test]
#[ignore]
fn copy_compressed_file() {
exec_test_with_copy(|obj| obj.compressed = true, None, true).unwrap();
async fn copy_compressed_file() {
exec_test_with_copy(|obj| obj.compressed = true, None, true).await.unwrap();
}

#[test]
#[async_std::test]
#[ignore]
fn copy_over_formated_partion() {
exec_test_with_copy(|obj| obj.target_format.should_format = true, None, false).unwrap();
async fn copy_over_formated_partion() {
exec_test_with_copy(|obj| obj.target_format.should_format = true, None, false)
.await
.unwrap();
}

#[test]
#[async_std::test]
#[ignore]
fn copy_over_existing_file() {
async fn copy_over_existing_file() {
exec_test_with_copy(
|_| (),
Some(definitions::TargetPermissions {
Expand All @@ -261,12 +264,13 @@ mod tests {
}),
false,
)
.await
.unwrap();
}

#[test]
#[async_std::test]
#[ignore]
fn copy_change_uid() {
async fn copy_change_uid() {
exec_test_with_copy(
|obj| {
obj.target_permissions.target_uid =
Expand All @@ -275,12 +279,13 @@ mod tests {
None,
false,
)
.await
.unwrap();
}

#[test]
#[async_std::test]
#[ignore]
fn copy_change_gid() {
async fn copy_change_gid() {
exec_test_with_copy(
|obj| {
obj.target_permissions.target_gid =
Expand All @@ -293,12 +298,13 @@ mod tests {
}),
false,
)
.await
.unwrap();
}

#[test]
#[async_std::test]
#[ignore]
fn copy_change_mode() {
async fn copy_change_mode() {
exec_test_with_copy(
|obj| obj.target_permissions.target_mode = Some(0o444),
Some(definitions::TargetPermissions {
Expand All @@ -308,6 +314,7 @@ mod tests {
}),
false,
)
.await
.unwrap();
}
}
37 changes: 18 additions & 19 deletions updatehub/src/object/installer/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::{
use pkg_schema::{definitions, objects};
use slog_scope::info;

#[async_trait::async_trait(?Send)]
impl Installer for objects::Flash {
fn check_requirements(&self) -> Result<()> {
async fn check_requirements(&self, _: &Context) -> Result<()> {
info!("'flash' handle checking requirements");
utils::fs::is_executable_in_path("nandwrite")?;
utils::fs::is_executable_in_path("flashcp")?;
Expand All @@ -31,7 +32,7 @@ impl Installer for objects::Flash {
}
}

fn install(&self, context: &Context) -> Result<()> {
async fn install(&self, context: &Context) -> Result<()> {
info!("'flash' handler Install {} ({})", self.filename, self.sha256sum);

let target = self.target.get_target()?;
Expand Down Expand Up @@ -76,54 +77,52 @@ mod tests {
}
}

#[test]
fn check_requirements_with_missing_binaries() {
#[async_std::test]
async fn check_requirements_with_missing_binaries() {
let flash_obj = fake_flash_obj("system0");
let context = Context::default();

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["flash_erase"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["flashcp"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["nandwrite"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["flash_erase", "nandwrite"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["flash_erase", "flashcp"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());

env::set_var("PATH", "");
let (_handle, _) = create_echo_bins(&["nandwrite", "nandwrite"]).unwrap();
assert!(flash_obj.check_requirements().is_err());
assert!(flash_obj.check_requirements(&context).await.is_err());
}

#[test]
#[async_std::test]
#[ignore]
fn install_nor() {
async fn install_nor() {
let _mtd_lock = SERIALIZE.lock();
let mtd = FakeMtd::new(&["system0"], MtdKind::Nor).unwrap();
let target = &mtd.devices[0];
let flash_obj = fake_flash_obj("system0");
let download_dir = tempfile::tempdir().unwrap();
let source = download_dir.path().join(&flash_obj.sha256sum);
let context =
Context { download_dir: download_dir.path().to_owned(), ..Context::default() };

let (_handle, calls) = create_echo_bins(&["flash_erase", "flashcp", "nandwrite"]).unwrap();

flash_obj.check_requirements().unwrap();
flash_obj
.install(&Context {
download_dir: download_dir.path().to_owned(),
..Context::default()
})
.unwrap();
flash_obj.check_requirements(&context).await.unwrap();
flash_obj.install(&context).await.unwrap();

let expected = format!(
"flash_erase {} 0 0\nflashcp {} {}\n",
Expand Down
Loading

0 comments on commit a4ced0a

Please sign in to comment.