Skip to content

Commit f2fafd2

Browse files
committed
chore: switched to unit testing
Signed-off-by: Adrian Lungu <lunguadrian30@gmail.com>
1 parent 16e88fe commit f2fafd2

4 files changed

Lines changed: 271 additions & 272 deletions

File tree

tockloader-lib/src/command_impl/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl CommandInstall for TockloaderConnection {
1414
let app_attributes_list: Vec<AppAttributes> = self.list().await?;
1515
let mut tock_app_list = app_attributes_list
1616
.iter()
17-
.map(|app| TockApp::from_app_attributes(app))
17+
.map(TockApp::from_app_attributes)
1818
.collect::<Vec<TockApp>>();
1919
log::info!("tock apps len {:?}", tock_app_list.len());
2020

tockloader-lib/src/command_impl/reshuffle_apps.rs

Lines changed: 262 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl TockApp {
9696
let header =
9797
parse_tbf_header(&binary[0..header_len as usize], tbf_version).expect("invalid header");
9898

99-
if let Some(_) = header.get_fixed_address_flash() {
99+
if header.get_fixed_address_flash().is_some() {
100100
// addr = align_down(addr as u64) as u32;
101101
// if addr < settings.start_address as u32 {
102102
// // this rust app should not be here
@@ -481,29 +481,277 @@ pub fn create_pkt(
481481
) -> Vec<u8> {
482482
let mut pkt: Vec<u8> = Vec::new();
483483
for item in configuration.iter() {
484-
if item.idx.is_none() {
485-
// write padding binary
486-
let mut buf = create_padding(item.size as u32);
487-
pkt.append(&mut buf);
488-
} else {
484+
if let Some(idx) = item.idx {
489485
match &item.installed {
490-
true => pkt.append(&mut app_binaries[item.idx.unwrap()]),
486+
true => pkt.append(&mut app_binaries[idx]),
491487
false => {
492488
let mut arch: String = settings.arch.clone().unwrap();
493489
// if ram is set, this is a rust app, we need to reconstruct the arch and then
494490
// read the binary from the tab
495-
if item.ram_address.is_some() {
496-
arch = format!(
497-
"{}.0x{:08x}.0x{:08x}",
498-
arch,
499-
item.address,
500-
item.ram_address.unwrap()
501-
);
491+
if let Some(ram_address) = item.ram_address {
492+
arch = format!("{}.0x{:08x}.0x{:08x}", arch, item.address, ram_address);
502493
}
503494
pkt.append(&mut tab.as_ref().unwrap().extract_binary(arch).unwrap());
504495
}
505496
}
497+
} else {
498+
// write padding binary
499+
let mut buf = create_padding(item.size as u32);
500+
pkt.append(&mut buf);
506501
}
507502
}
508503
pkt
509504
}
505+
506+
#[cfg(test)]
507+
mod tests {
508+
use super::*;
509+
510+
#[test]
511+
fn install_new_c_app() {
512+
let settings: &BoardSettings = &BoardSettings {
513+
arch: Some("cortex-m4".to_string()),
514+
start_address: 0x00040000,
515+
page_size: 512,
516+
ram_start_address: 0x20000000,
517+
};
518+
519+
let c_app_1: TockApp = TockApp::Flexible(FlexibleApp {
520+
installed: true,
521+
idx: None,
522+
size: 0x2000,
523+
});
524+
525+
let c_app_2: TockApp = TockApp::Flexible(FlexibleApp {
526+
installed: true,
527+
idx: None,
528+
size: 0x2000,
529+
});
530+
531+
let c_app_3: TockApp = TockApp::Flexible(FlexibleApp {
532+
installed: false,
533+
idx: None,
534+
size: 0x2000,
535+
});
536+
537+
let apps: Vec<TockApp> = vec![c_app_1, c_app_2, c_app_3];
538+
539+
let reshuffled_apps = reshuffle_apps(settings, apps);
540+
541+
let correct_config: Option<Vec<Index>> = Some(vec![
542+
Index {
543+
installed: true,
544+
idx: Some(0x0),
545+
ram_address: None,
546+
address: 0x40000,
547+
size: 0x2000,
548+
},
549+
Index {
550+
installed: true,
551+
idx: Some(0x1),
552+
ram_address: None,
553+
address: 0x42000,
554+
size: 0x2000,
555+
},
556+
Index {
557+
installed: false,
558+
idx: Some(0x2),
559+
ram_address: None,
560+
address: 0x44000,
561+
size: 0x2000,
562+
},
563+
]);
564+
assert_eq!(reshuffled_apps, correct_config);
565+
}
566+
567+
#[test]
568+
fn install_new_rust_app() {
569+
let settings: &BoardSettings = &BoardSettings {
570+
arch: Some("cortex-m4".to_string()),
571+
start_address: 0x00040000,
572+
page_size: 512,
573+
ram_start_address: 0x20000000,
574+
};
575+
576+
let rust_app: TockApp = TockApp::Fixed(FixedApp {
577+
installed: false,
578+
idx: None,
579+
compatible_addresses: vec![
580+
Some((0x40000, 0x20008000)),
581+
Some((0x42000, 0x2000a000)),
582+
Some((0x48000, 0x20010000)),
583+
Some((0x80000, 0x20006000)),
584+
Some((0x88000, 0x2000e000)),
585+
],
586+
size: 0x2000,
587+
});
588+
589+
let apps: Vec<TockApp> = vec![rust_app];
590+
591+
let reshuffled_apps = reshuffle_apps(settings, apps);
592+
593+
let correct_config: Option<Vec<Index>> = Some(vec![Index {
594+
installed: false,
595+
idx: Some(0x0),
596+
ram_address: Some(0x20008000),
597+
address: 0x40000,
598+
size: 0x2000,
599+
}]);
600+
assert_eq!(reshuffled_apps, correct_config);
601+
}
602+
603+
#[test]
604+
fn install_more_rust_apps() {
605+
let settings: &BoardSettings = &BoardSettings {
606+
arch: Some("cortex-m4".to_string()),
607+
start_address: 0x00040000,
608+
page_size: 512,
609+
ram_start_address: 0x20000000,
610+
};
611+
612+
let rust_app_1: TockApp = TockApp::Fixed(FixedApp {
613+
installed: true,
614+
idx: None,
615+
compatible_addresses: vec![Some((0x40000, 0x20008000))],
616+
size: 0x2000,
617+
});
618+
619+
let rust_app_2: TockApp = TockApp::Fixed(FixedApp {
620+
installed: true,
621+
idx: None,
622+
compatible_addresses: vec![Some((0x42000, 0x2000a000))],
623+
size: 0x2000,
624+
});
625+
626+
let rust_app_3: TockApp = TockApp::Fixed(FixedApp {
627+
installed: false,
628+
idx: None,
629+
compatible_addresses: vec![
630+
Some((0x40000, 0x20008000)),
631+
Some((0x42000, 0x2000a000)),
632+
Some((0x48000, 0x20010000)),
633+
Some((0x80000, 0x20006000)),
634+
Some((0x88000, 0x2000e000)),
635+
],
636+
size: 0x2000,
637+
});
638+
639+
let apps: Vec<TockApp> = vec![rust_app_1, rust_app_2, rust_app_3];
640+
641+
let reshuffled_apps = reshuffle_apps(settings, apps);
642+
643+
let correct_config: Option<Vec<Index>> = Some(vec![
644+
Index {
645+
installed: true,
646+
idx: Some(0x0),
647+
ram_address: Some(0x20008000),
648+
address: 0x40000,
649+
size: 0x2000,
650+
},
651+
Index {
652+
installed: true,
653+
idx: Some(0x1),
654+
ram_address: Some(0x2000a000),
655+
address: 0x42000,
656+
size: 0x2000,
657+
},
658+
// padding
659+
Index {
660+
installed: false,
661+
idx: None,
662+
ram_address: None,
663+
address: 0x44000,
664+
size: 0x4000,
665+
},
666+
Index {
667+
installed: false,
668+
idx: Some(0x2),
669+
ram_address: Some(0x20010000),
670+
address: 0x48000,
671+
size: 0x2000,
672+
},
673+
]);
674+
assert_eq!(reshuffled_apps, correct_config);
675+
}
676+
677+
#[test]
678+
fn insert_c_app_between_rust_apps() {
679+
let settings: &BoardSettings = &BoardSettings {
680+
arch: Some("cortex-m4".to_string()),
681+
start_address: 0x00040000,
682+
page_size: 512,
683+
ram_start_address: 0x20000000,
684+
};
685+
686+
let rust_app_1: TockApp = TockApp::Fixed(FixedApp {
687+
installed: true,
688+
idx: None,
689+
compatible_addresses: vec![Some((0x40000, 0x20008000))],
690+
size: 0x2000,
691+
});
692+
693+
let rust_app_2: TockApp = TockApp::Fixed(FixedApp {
694+
installed: true,
695+
idx: None,
696+
compatible_addresses: vec![Some((0x42000, 0x2000a000))],
697+
size: 0x2000,
698+
});
699+
700+
let rust_app_3: TockApp = TockApp::Fixed(FixedApp {
701+
installed: true,
702+
idx: None,
703+
compatible_addresses: vec![Some((0x48000, 0x20010000))],
704+
size: 0x2000,
705+
});
706+
707+
let c_app_1: TockApp = TockApp::Flexible(FlexibleApp {
708+
installed: false,
709+
idx: None,
710+
size: 0x2000,
711+
});
712+
713+
let apps: Vec<TockApp> = vec![rust_app_1, rust_app_2, rust_app_3, c_app_1];
714+
715+
let reshuffled_apps = reshuffle_apps(settings, apps);
716+
717+
let correct_config: Option<Vec<Index>> = Some(vec![
718+
Index {
719+
installed: true,
720+
idx: Some(0x0),
721+
ram_address: Some(0x20008000),
722+
address: 0x40000,
723+
size: 0x2000,
724+
},
725+
Index {
726+
installed: true,
727+
idx: Some(0x1),
728+
ram_address: Some(0x2000a000),
729+
address: 0x42000,
730+
size: 0x2000,
731+
},
732+
Index {
733+
installed: false,
734+
idx: Some(0x3),
735+
ram_address: None,
736+
address: 0x44000,
737+
size: 0x2000,
738+
},
739+
// padding
740+
Index {
741+
installed: false,
742+
idx: None,
743+
ram_address: None,
744+
address: 0x46000,
745+
size: 0x2000,
746+
},
747+
Index {
748+
installed: true,
749+
idx: Some(0x2),
750+
ram_address: Some(0x20010000),
751+
address: 0x48000,
752+
size: 0x2000,
753+
},
754+
]);
755+
assert_eq!(reshuffled_apps, correct_config);
756+
}
757+
}

tockloader-lib/src/tabs/tab.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ impl Tab {
9393
let (arch, flash, ram) = Self::split_arch(file.filename.to_string());
9494
// check if we have the same arch
9595
// check if flash and ram fit
96-
if flash != 0 && ram != 0 {
97-
if arch.starts_with(settings.arch.as_ref().unwrap())
98-
&& flash >= settings.start_address
99-
&& ram >= settings.ram_start_address
100-
{
101-
log::info!("rust, pushed arch {arch}, flash {flash:#x}, ram {ram:#x}");
102-
compatible_tbfs.push(Some((flash, ram)));
103-
}
96+
if flash != 0
97+
&& ram != 0
98+
&& arch.starts_with(settings.arch.as_ref().unwrap())
99+
&& flash >= settings.start_address
100+
{
101+
log::info!("rust, pushed arch {arch}, flash {flash:#x}, ram {ram:#x}");
102+
compatible_tbfs.push(Some((flash, ram)));
104103
}
104+
105105
// how about we don't do anything on else?
106106
// } else if arch.starts_with(settings.arch.as_ref().unwrap()) {
107107
// // this happens for C apps, we'll have

0 commit comments

Comments
 (0)