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

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  powerfail_simulator_tests:
    runs-on: ubuntu-20.04

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

      - name: make clean
        run: |
          make keysclean && make -C tools/keytools clean && rm -f include/target.h

      - name: Select config
        run: |
          cp config/examples/sim.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 && 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 test-sim-internal-flash-with-update

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

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

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


      # TEST with NVM_WRITEONCE enabled

      - name: make clean
        run: |
          make keysclean && make -C tools/keytools clean && rm -f include/target.h

      - name: Select config with NVM_WRITEONCE
        run: |
          cp config/examples/sim-nvm-writeonce.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 && make test-sim-internal-flash-with-update

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

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

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

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

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

      # TEST with DELTA updates

      - name: make clean
        run: |
          make keysclean && make -C tools/keytools clean && rm -f include/target.h

      - name: Select config with DELTA updates
        run: |
          cp config/examples/sim-delta-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 && 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 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 test-sim-internal-flash-with-delta-update

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


     # TEST with encryption (aes128)

      - 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-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 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

     # TEST with encryption (aes128) and delta updates

      - 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-delta-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 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

