Fix test/revert upgrade flash count

When doing a test with fails, the total number of flash accesses is
first calculated doing an upgrade without fails, which is then used to
fail/resume at all test points. The count was always considering the
setting of a permanent upgrade, which added 1 to the total count in a
non-permanent upgrade. This amount was being discounted when running
the test/revert with fails, although the discount was only ok for single
images. This adds a new image constructor that does not run a permanent
upgrade and thus gets the correct number of flash accesses for a
test/revert no matter how many images are being tested.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/sim/src/image.rs b/sim/src/image.rs
index 44cfaaa..90d3a77 100644
--- a/sim/src/image.rs
+++ b/sim/src/image.rs
@@ -155,15 +155,14 @@
         }
     }
 
-    /// Construct an `Images` for normal testing.
-    pub fn make_image(self) -> Images {
+    fn make_image_with_permanent(self, permanent: bool) -> Images {
         let mut images = self.make_no_upgrade_image();
         for image in &images.images {
             mark_upgrade(&mut images.flash, &image.slots[1]);
         }
 
         // upgrades without fails, counts number of flash operations
-        let total_count = match images.run_basic_upgrade() {
+        let total_count = match images.run_basic_upgrade(permanent) {
             Ok(v)  => v,
             Err(_) => {
                 panic!("Unable to perform basic upgrade");
@@ -174,6 +173,16 @@
         images
     }
 
+    /// Construct an `Images` for normal testing with perm upgrade.
+    pub fn make_image(self) -> Images {
+        self.make_image_with_permanent(true)
+    }
+
+    /// Construct an `Images` for normal testing with test upgrade.
+    pub fn make_non_permanent_image(self) -> Images {
+        self.make_image_with_permanent(false)
+    }
+
     pub fn make_bad_secondary_slot_image(self) -> Images {
         let mut bad_flash = self.flash;
         let images = self.slots.into_iter().map(|slots| {
@@ -304,8 +313,8 @@
     ///
     /// Returns the number of flash operations which can later be used to
     /// inject failures at chosen steps.
-    pub fn run_basic_upgrade(&self) -> Result<i32, ()> {
-        let (flash, total_count) = self.try_upgrade(None);
+    pub fn run_basic_upgrade(&self, permanent: bool) -> Result<i32, ()> {
+        let (flash, total_count) = self.try_upgrade(None, permanent);
         info!("Total flash operation count={}", total_count);
 
         if !self.verify_images(&flash, 0, 1) {
@@ -345,7 +354,7 @@
         // Let's try an image halfway through.
         for i in 1 .. total_flash_ops {
             info!("Try interruption at {}", i);
-            let (flash, count) = self.try_upgrade(Some(i));
+            let (flash, count) = self.try_upgrade(Some(i), true);
             info!("Second boot, count={}", count);
             if !self.verify_images(&flash, 0, 1) {
                 warn!("FAIL at step {} of {}", i, total_flash_ops);
@@ -431,7 +440,7 @@
         let mut fails = 0;
 
         if Caps::SwapUpgrade.present() {
-            for i in 1 .. (self.total_count.unwrap() - 1) {
+            for i in 1 .. self.total_count.unwrap() {
                 info!("Try interruption at {}", i);
                 if self.try_revert_with_fail_at(i) {
                     error!("Revert failed at interruption {}", i);
@@ -779,11 +788,13 @@
 
     /// Test a boot, optionally stopping after 'n' flash options.  Returns a count
     /// of the number of flash operations done total.
-    fn try_upgrade(&self, stop: Option<i32>) -> (SimMultiFlash, i32) {
+    fn try_upgrade(&self, stop: Option<i32>, permanent: bool) -> (SimMultiFlash, i32) {
         // Clone the flash to have a new copy.
         let mut flash = self.flash.clone();
 
-        self.mark_permanent_upgrades(&mut flash, 1);
+        if permanent {
+            self.mark_permanent_upgrades(&mut flash, 1);
+        }
 
         let mut counter = stop.unwrap_or(0);
 
diff --git a/sim/tests/core.rs b/sim/tests/core.rs
index 5aa29ae..3253ddf 100644
--- a/sim/tests/core.rs
+++ b/sim/tests/core.rs
@@ -21,7 +21,7 @@
 sim_test!(bad_secondary_slot, make_bad_secondary_slot_image, run_signfail_upgrade);
 sim_test!(norevert_newimage, make_no_upgrade_image, run_norevert_newimage);
 sim_test!(basic_revert, make_image, run_basic_revert);
-sim_test!(revert_with_fails, make_image, run_revert_with_fails);
+sim_test!(revert_with_fails, make_non_permanent_image, run_revert_with_fails);
 sim_test!(perm_with_fails, make_image, run_perm_with_fails);
 sim_test!(perm_with_random_fails, make_image, run_perm_with_random_fails_5);
 sim_test!(norevert, make_image, run_norevert);