tf_fuzz: fix sst `check ... except fail` calls

Fix the error code modelling of sst calls with data value checks (`check
...`), and except fail.

Currently, demo/8.test erroneously fails:

----
set sst name snortwaggle data *;
read sst name snortwaggle check "almost certainly not *this*" expect fail;
----

When reading `expect fail`, the sst read call is marked as "should
fail". However, the intended behaviour is for the read to succeed and
the check to fail.

To fix this, this patch adds a template variable, $check_expect, that
can be used to modify the expected value of the check (similar to how
$expect is used for expected values of calls). Using this, separate
pass/fail values can be set for the check as well as the call itself.

Change-Id: Ia55b6f2fef5737d67540e98ac6fc5e7695a8515f
Signed-off-by: Nik Dewally <Nik.Dewally@arm.com>
diff --git a/tf_fuzz/tfz-cpp/calls/psa_call.cpp b/tf_fuzz/tfz-cpp/calls/psa_call.cpp
index 46af409..0e1680b 100644
--- a/tf_fuzz/tfz-cpp/calls/psa_call.cpp
+++ b/tf_fuzz/tfz-cpp/calls/psa_call.cpp
@@ -88,51 +88,104 @@
 {
     string formalized;  // "proper" result string
 
-    if (!exp_data.pf_nothing) {
-        if (exp_data.pf_pass) {
-            find_replace_all ("$expect",
-                              test_state->bplate->bplate_string[sst_pass_string],
-                              check_code);
-        } else if (exp_data.pf_fail) {
-            // Check for not-success:
-            find_replace_1st ("!=", "==",
-                              check_code);
-            find_replace_all ("$expect",
-                              test_state->bplate->bplate_string[sst_pass_string],
-                              check_code);
-            find_replace_1st ("expected ", "expected not ",
-                              check_code);
+    if (exp_data.pf_nothing) { // do not generate result checks
+        return;
+
+    }
+    if (exp_data.pf_pass) { // expect pass
+        find_replace_all ("$expect",
+                          test_state->bplate->bplate_string[sst_pass_string],
+                          check_code);
+
+        // `check "foo"`
+        find_replace_all ("$check_expect",
+                          "0",
+                          check_code);
+
+    } else if (exp_data.pf_fail) { // expect fail
+
+      // If the command is `... check "foo" expect fail;`, the fail
+      // binds to the check, not the command itself.
+      if (exp_data.data_specified) {
+        // expect a pass for the sst call itself.
+        find_replace_all ("$expect",
+                          test_state->bplate->bplate_string[sst_pass_string],
+                          check_code);
+
+        // expect a failure for the check.
+        find_replace_all ("!= $check_expect",
+                          "== 0",
+                          check_code);
+
+        find_replace_1st ("should be equal", "should not be equal",
+                          check_code);
         } else {
-            if (exp_data.pf_specified) {
-                formalized = formalize (exp_data.pf_result_string, "PSA_ERROR_");
-                find_replace_all ("$expect", formalized, check_code);
-            } else {
-                // Figure out what the message should read:
-                switch (asset_info.how_asset_found) {
-                    case asset_search::found_active:
-                    case asset_search::created_new:
-                        find_replace_all ("$expect",
-                                          test_state->bplate->
-                                              bplate_string[sst_pass_string],
-                                          check_code);
-                        break;
-                    case asset_search::found_deleted:
-                    case asset_search::not_found:
-                        find_replace_all ("$expect",
-                                          test_state->bplate->
-                                              bplate_string[sst_fail_removed],
-                                          check_code);
-                        break;
-                    default:
-                        find_replace_1st ("!=", "==",
-                                          check_code);  // like "fail", just make sure...
-                        find_replace_all ("$expect",
-                                          test_state->bplate->
-                                              bplate_string[sst_pass_string],
-                                          check_code);  // ... it's *not* PSA_SUCCESS
-                        break;
-                }
-            }
+        // Check for not-success:
+        find_replace_1st ("!=", "==",
+                          check_code);
+        find_replace_all ("$expect",
+                          test_state->bplate->bplate_string[sst_pass_string],
+                          check_code);
+        find_replace_1st ("expected ", "expected not ",
+                          check_code);
+        }
+    }
+
+    else if (exp_data.pf_specified) { // expect <PSA_ERROR_CODE>
+        formalized = formalize (exp_data.pf_result_string, "PSA_ERROR_");
+        find_replace_all ("$expect", formalized, check_code);
+
+        // NOTE: Assumes that the variable used to store the actual
+        // value initialised to a different value than the expected
+        // value.
+        find_replace_all ("!= $check_expect","== 0",check_code);
+        find_replace_1st ("should be equal", "should not be equal",
+                          check_code);
+    }
+
+    else { // no explicit expect -- simulate
+
+        // Figure out what the message should read:
+        switch (asset_info.how_asset_found) {
+            case asset_search::found_active:
+            case asset_search::created_new:
+                find_replace_all ("$expect",
+                                  test_state->bplate->
+                                      bplate_string[sst_pass_string],
+                                  check_code);
+
+                find_replace_all ("$check_expect","0",check_code);
+                break;
+            case asset_search::found_deleted:
+            case asset_search::not_found:
+                find_replace_all ("$expect",
+                                  test_state->bplate->
+                                      bplate_string[sst_fail_removed],
+                                  check_code);
+
+                // NOTE: Assumes that the variable used to store the actual
+                // value initialised to a different value than the expected
+                // value.
+                find_replace_all ("!= $check_expect","== 0",check_code);
+                find_replace_1st ("should be equal", "should not be equal",
+                                  check_code);
+                break;
+            default:
+                find_replace_1st ("!=", "==",
+                                  check_code);  // like "fail", just make sure...
+
+                find_replace_all ("$expect",
+                                  test_state->bplate->
+                                      bplate_string[sst_pass_string],
+                                  check_code);  // ... it's *not* PSA_SUCCESS
+
+                // NOTE: Assumes that the variable used to store the actual
+                // value initialised to a different value than the expected
+                // value.
+                find_replace_all ("!= $check_expect","== 0",check_code);
+                find_replace_1st ("should be equal", "should not be equal",
+                                  check_code);
+                break;
         }
     }
 }