Tests/esapi: Add FieldUpgrade

Signed-off-by: Juergen Repp <juergen.repp@sit.fraunhofer.de>
Signed-off-by: Andreas Fuchs <andreas.fuchs@sit.fraunhofer.de>
diff --git a/Makefile-test.am b/Makefile-test.am
index e7b2ea9..a3f24e3 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -132,6 +132,7 @@
     test/integration/esys-encrypt-decrypt.int \
     test/integration/esys-event-sequence-complete.int \
     test/integration/esys-evict-control-serialization.int \
+    test/integration/esys-field-upgrade.int \
     test/integration/esys-get-capability.int \
     test/integration/esys-get-random.int \
     test/integration/esys-hashsequencestart.int \
@@ -531,6 +532,13 @@
     test/integration/esys-evict-control-serialization.int.c \
     test/integration/main-esapi.c test/integration/test-esapi.h
 
+test_integration_esys_field_upgrade_int_CFLAGS  = $(TESTS_CFLAGS)
+test_integration_esys_field_upgrade_int_LDADD   = $(TESTS_LDADD)
+test_integration_esys_field_upgrade_int_LDFLAGS = $(TESTS_LDFLAGS) -lgcrypt
+test_integration_esys_field_upgrade_int_SOURCES = \
+    test/integration/esys-field-upgrade.int.c \
+    test/integration/main-esapi.c test/integration/test-esapi.h
+
 test_integration_esys_get_capability_int_CFLAGS  = $(TESTS_CFLAGS)
 test_integration_esys_get_capability_int_LDADD   = $(TESTS_LDADD)
 test_integration_esys_get_capability_int_LDFLAGS = $(TESTS_LDFLAGS) -lgcrypt
diff --git a/test/integration/esys-field-upgrade.int.c b/test/integration/esys-field-upgrade.int.c
new file mode 100644
index 0000000..42c7ff9
--- /dev/null
+++ b/test/integration/esys-field-upgrade.int.c
@@ -0,0 +1,86 @@
+/*******************************************************************************
+ * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *******************************************************************************/
+
+#include "tss2_esys.h"
+
+#include "esys_iutil.h"
+#define LOGMODULE test
+#include "util/log.h"
+
+/* Test the ESAPI function Esys_FieldUpgradeStart and   Esys_FieldUpgradeData */
+int
+test_invoke_esapi(ESYS_CONTEXT * esys_context)
+{
+    uint32_t r = 0;
+
+    TPM2B_MAX_BUFFER fuData = {
+        .size = 20,
+        .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
+                   11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
+    };
+    TPMT_HA *nextDigest;
+    TPMT_HA *firstDigest;
+
+    r = Esys_FieldUpgradeData(
+        esys_context,
+        ESYS_TR_NONE,
+        ESYS_TR_NONE,
+        ESYS_TR_NONE,
+        &fuData,
+        &nextDigest,
+        &firstDigest);
+    if (r == TPM2_RC_COMMAND_CODE) {
+        LOG_INFO("Command TPM2_FieldUpgradeData not supported by TPM.");
+        r = 77; /* Skip */
+        goto error;
+    }
+
+    goto_if_error(r, "Error: FieldUpgradeData", error);
+
+    /* TODO test has to be adapted if FieldUpgrade commands are available */
+    /*
+    ESYS_TR authorization_handle = ESYS_TR_NONE;
+    ESYS_TR keyHandle_handle = ESYS_TR_NONE;
+    TPM2B_DIGEST fuDigest;
+    TPMT_SIGNATURE manifestSignature;
+
+    r = Esys_FieldUpgradeStart(
+        esys_context,
+        authorization_handle,
+        keyHandle_handle,
+        ESYS_TR_PASSWORD,
+        ESYS_TR_NONE,
+        ESYS_TR_NONE,
+        &fuDigest,
+        &manifestSignature);
+    goto_if_error(r, "Error: FieldUpgradeStart", error);
+    */
+    return 0;
+
+ error:
+    return r;
+}