name: Power-failure during update - test with simulator target

on:
  push:
    branches: [ 'master', 'main', 'release/**' ]
  pull_request:
    branches: [ '*' ]

jobs:
  powerfail_simulator_tests:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - name: make clean
        run: |
          make distclean

      - name: Select config
        run: |
          cp config/examples/sim.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test with power failures
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh


      # TEST with NVM_FLASH_WRITEONCE enabled
      - name: make clean
        run: |
          make distclean

      - name: Select config with NVM_FLASH_WRITEONCE
        run: |
          cp config/examples/sim-nvm-writeonce.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test (NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test (NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test with power failures (NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh


      # TEST with NVM_FLASH_WRITEONCE AND FLAGS_HOME enabled
      - name: make clean
        run: |
          make distclean

      - name: Select config with NVM_FLASH_WRITEONCE and FLAGS_HOME
        run: |
          cp config/examples/sim-nvm-writeonce-flags-home.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test (NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test (NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update-revert test with power failures (NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh


      # TEST with NVM_FLASH_WRITEONCE AND FLAGS_HOME AND FLAGS_INVERT enabled
      - name: make clean
        run: |
          make distclean

      - name: Select config with NVM_FLASH_WRITEONCE and FLAGS_HOME and FLAGS_INVERT
        run: |
          cp config/examples/sim-nvm-writeonce-flags-home-invert.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-update

      - name: Run sunny day update test (NVM_FLASH_WRITEONCE FLAGS_HOME FLAGS_INVERT)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-update

      - name: Run update-revert test (NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-update

      - name: Run update-revert test with power failures (NVM_FLASH_WRITEONCE FLAGS_HOME FLAGS_INVERT)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh


      # TEST with DELTA updates
      - name: make clean
        run: |
          make distclean

      - name: Select config with DELTA updates
        run: |
          cp config/examples/sim-delta-update.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-delta-update

      - name: Run sunny day update test (DELTA)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-delta-update

      - name: Run update-revert test (DELTA)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-delta-update

      - name: Run update-revert test with power failures (DELTA)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild with wrong delta base version
        run: |
          make clean && make test-sim-internal-flash-with-wrong-delta-update

      - name: Run negative update test with wrong base version (DELTA)
        run: |
          tools/scripts/sim-delta-wrongversion-update.sh

     # TEST with encryption (aes128)
      - name: make clean
        run: |
          make distclean

      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-update.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run sunny day update test (AES128)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run update-revert test (AES128)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run update-revert test with power failures (AES128)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh

     # TEST with encryption (aes128) and delta updates
      - name: make clean
        run: |
          make distclean
      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-delta-update.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run sunny day update test (AES128 DELTA)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run update-revert test (AES128 DELTA)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run update-revert test with power failures (AES128 DELTA)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh


     # TEST with encryption (aes128) and NVM_FLASH_WRITEONCE
      - name: make clean
        run: |
          make distclean

      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-nvm-writeonce-update.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run sunny day update test (AES128 NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run update-revert test (AES128 NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run update-revert test with power failures (AES128 NVM_FLASH_WRITEONCE)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh

     # TEST with encryption (aes128) and NVM_FLASH_WRITEONCE and FLAGS_HOME

      - name: make clean
        run: |
          make keysclean && make -C tools/keytools clean && rm -f include/target.h
      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-nvm-writeonce-flags-home-update.config .config
      - name: Build key tools
        run: |
          make -C tools/keytools
      - name: Build bin assemble
        run: |
          make -C tools/bin-assemble
      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run sunny day update test (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-sunnyday-update.sh
      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run update-revert test (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-update-fallback.sh
      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run update-revert test with power failures (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh

     # TEST with encryption (aes128) and NVM_FLASH_WRITEONCE and FLAGS_HOME and FLAGS_INVERT

      - name: make clean
        run: |
          make keysclean && make -C tools/keytools clean && rm -f include/target.h
      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-nvm-writeonce-flags-home-invert-update.config .config
      - name: Build key tools
        run: |
          make -C tools/keytools
      - name: Build bin assemble
        run: |
          make -C tools/bin-assemble
      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run sunny day update test (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME FLAGS_INVERT)
        run: |
          tools/scripts/sim-sunnyday-update.sh
      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run update-revert test (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME FLAGS_INVERT)
        run: |
          tools/scripts/sim-update-fallback.sh
      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update
      - name: Run update-revert test with power failures (AES128 NVM_FLASH_WRITEONCE FLAGS_HOME FLAGS_INVERT)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-update

      - name: Run emergency fallback test
        run: |
          tools/scripts/sim-update-emergency-fallback.sh

     # TEST with encryption (aes128) and NVM_FLASH_WRITEONCE and DELTA updates
      - name: make clean
        run: |
          make distclean
      - name: Select config with encrypted updates
        run: |
          cp config/examples/sim-encrypt-delta-nvm-writeonce-update.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run sunny day update test (AES128 DELTA)
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run update-revert test (AES128 DELTA)
        run: |
          tools/scripts/sim-update-fallback.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-external-flash-with-enc-delta-update

      - name: Run update-revert test with power failures (AES128 DELTA)
        run: |
          tools/scripts/sim-update-powerfail-resume.sh

    # TEST with backup disabled
      - name: make clean
        run: |
          make distclean
      - name: Select config with backup disabled
        run: |
          cp config/examples/sim-nobackup.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update test with DISABLE_BACKUP and powefail
        run: |
          tools/scripts/sim-update-powerfail-resume-nobackup.sh

    # TEST with backup disabled + NVM_FLASH_WRITEONCE
      - name: make clean
        run: |
          make distclean
      - name: Select config with backup disabled + NVM WRITEONCE
        run: |
          cp config/examples/sim-nobackup-nvm-writeonce.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update test with DISABLE_BACKUP and powefail
        run: |
          tools/scripts/sim-update-powerfail-resume-nobackup.sh

    # TEST with backup disabled + FLAGS_HOME
      - name: make clean
        run: |
          make distclean
      - name: Select config with backup disabled + FLAGS_HOME
        run: |
          cp config/examples/sim-nobackup-flags-home.config .config

      - name: Build tools
        run: |
          make -C tools/keytools && make -C tools/bin-assemble

      - name: Build wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run sunny day update test
        run: |
          tools/scripts/sim-sunnyday-update.sh

      - name: Rebuild wolfboot.elf
        run: |
          make clean && make test-sim-internal-flash-with-update

      - name: Run update test with DISABLE_BACKUP and powefail
        run: |
          tools/scripts/sim-update-powerfail-resume-nobackup.sh
