Test: Add BL1_2 integration testsuite

Change-Id: Ifad9e24fdf680c9ef72ef935ff67fb54c29c7f77
diff --git a/test/bl1/bl1_2/CMakeLists.txt b/test/bl1/bl1_2/CMakeLists.txt
index 6b97d00..7e5464f 100644
--- a/test/bl1/bl1_2/CMakeLists.txt
+++ b/test/bl1/bl1_2/CMakeLists.txt
@@ -9,6 +9,8 @@
     return()
 endif()
 
+add_subdirectory(suites/integration)
+
 add_library(bl1_2_tests STATIC)
 
 target_sources(bl1_2_tests
@@ -26,4 +28,5 @@
         tfm_test_framework_common
         platform_bl1_interface
         bl1_2_lib
+        bl1_2_test_suite_integration
 )
diff --git a/test/bl1/bl1_2/bl1_2_suites.c b/test/bl1/bl1_2/bl1_2_suites.c
index 3d7c835..5330224 100644
--- a/test/bl1/bl1_2/bl1_2_suites.c
+++ b/test/bl1/bl1_2/bl1_2_suites.c
@@ -9,8 +9,12 @@
 #include "test_framework_integ_test_helper.h"
 #include "test_framework.h"
 
+#include "bl1_2_integration_tests.h"
+
 static struct test_suite_t test_suites[] = {
 
+    {&register_testsuite_bl1_2_integration, 0, 0, 0},
+
     /* End of test suites */
     {0, 0, 0, 0}
 };
diff --git a/test/bl1/bl1_2/suites/integration/CMakeLists.txt b/test/bl1/bl1_2/suites/integration/CMakeLists.txt
new file mode 100644
index 0000000..69eb9f0
--- /dev/null
+++ b/test/bl1/bl1_2/suites/integration/CMakeLists.txt
@@ -0,0 +1,26 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+add_library(bl1_2_test_suite_integration)
+
+target_sources(bl1_2_test_suite_integration
+    PRIVATE
+        ./bl1_2_integration_tests.c
+)
+
+target_include_directories(bl1_2_test_suite_integration
+    PUBLIC
+        .
+)
+
+target_link_libraries(bl1_2_test_suite_integration
+    PRIVATE
+        tfm_test_framework_common
+        bl1_2_lib
+        bl1_1_shared_lib_interface
+        platform_region_defs
+)
diff --git a/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.c b/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.c
new file mode 100644
index 0000000..4e8ec17
--- /dev/null
+++ b/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "bl1_2_integration_tests.h"
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "image.h"
+#include "fih.h"
+#include "region_defs.h"
+#include "test_framework_helpers.h"
+
+static int setup_test_image(struct bl1_2_image_t **image)
+{
+    fih_int fih_rc;
+    *image = (struct bl1_2_image_t*)BL2_IMAGE_START;
+
+    FIH_CALL(copy_and_decrypt_image, fih_rc, 0);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        FIH_CALL(validate_image_at_addr, fih_rc, *image);
+        if (fih_eq(fih_rc, FIH_SUCCESS)) {
+            return 0;
+        }
+    }
+
+    FIH_CALL(copy_and_decrypt_image, fih_rc, 1);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        FIH_CALL(validate_image_at_addr, fih_rc, *image);
+        if (fih_eq(fih_rc, FIH_SUCCESS)) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+static void tfm_bl1_integration_test_5001(struct test_result_t *ret)
+{
+    fih_int fih_rc;
+    struct bl1_2_image_t *image;
+
+    if(setup_test_image(&image)) {
+        TEST_FAIL("Test image setup failed");
+        return;
+    }
+
+    memset(image, 0, sizeof(struct bl1_2_image_t));
+
+    FIH_CALL(validate_image_at_addr, fih_rc, image);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        TEST_FAIL("Bad image was successfully validated");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+    return;
+}
+
+static void tfm_bl1_integration_test_5002(struct test_result_t *ret)
+{
+    fih_int fih_rc;
+    struct bl1_2_image_t *image;
+
+    if(setup_test_image(&image)) {
+        TEST_FAIL("Test image setup failed");
+        return;
+    }
+
+    image->protected_values.encrypted_data.data[0] ^= 0xFF;
+
+    FIH_CALL(validate_image_at_addr, fih_rc, image);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        TEST_FAIL("Bad image was successfully validated");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+    return;
+}
+
+static void tfm_bl1_integration_test_5003(struct test_result_t *ret)
+{
+    fih_int fih_rc;
+    struct bl1_2_image_t *image;
+
+    if(setup_test_image(&image)) {
+        TEST_FAIL("Test image setup failed");
+        return;
+    }
+
+    image->protected_values.encrypted_data.data[IMAGE_BL2_CODE_SIZE - 1] ^= 0xFF;
+
+    FIH_CALL(validate_image_at_addr, fih_rc, image);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        TEST_FAIL("Bad image was successfully validated");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+    return;
+}
+
+
+static void tfm_bl1_integration_test_5004(struct test_result_t *ret)
+{
+    fih_int fih_rc;
+    struct bl1_2_image_t *image;
+
+    if(setup_test_image(&image)) {
+        TEST_FAIL("Test image setup failed");
+        return;
+    }
+
+    image->protected_values.security_counter = 0;
+
+    FIH_CALL(validate_image_at_addr, fih_rc, image);
+    if (fih_eq(fih_rc, FIH_SUCCESS)) {
+        TEST_FAIL("Bad image was successfully validated");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+    return;
+}
+
+static struct test_t integration_tests[] = {
+    {&tfm_bl1_integration_test_5001, "TFM_BL1_2_INTEGRATION_TEST_5001",
+     "INTEGRATION zeroed image test" },
+    {&tfm_bl1_integration_test_5002, "TFM_BL1_2_INTEGRATION_TEST_5002",
+     "INTEGRATION bit-flipped first byte test" },
+    {&tfm_bl1_integration_test_5003, "TFM_BL1_2_INTEGRATION_TEST_5003",
+     "INTEGRATION bit-flipped last byte test" },
+    {&tfm_bl1_integration_test_5004, "TFM_BL1_2_INTEGRATION_TEST_5003",
+     "INTEGRATION bad security counter test" },
+};
+
+void register_testsuite_bl1_2_integration(struct test_suite_t *p_test_suite)
+{
+    uint32_t list_size = (sizeof(integration_tests) / sizeof(integration_tests[0]));
+
+    set_testsuite("INTEGRATION test (TFM_BL1_2_INTEGRATION_TEST_5XXX)",
+                  integration_tests, list_size, p_test_suite);
+}
+
diff --git a/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.h b/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.h
new file mode 100644
index 0000000..2d64189
--- /dev/null
+++ b/test/bl1/bl1_2/suites/integration/bl1_2_integration_tests.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef BL1_2_INTEGRATION_TESTS_H
+#define BL1_2_INTEGRATION_TESTS_H
+
+#include "test_framework.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void register_testsuite_bl1_2_integration(struct test_suite_t *p_test_suite);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BL1_2_INTEGRATION_TESTS_H */