# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file lists tests that run with a full Android userspace present:
[
    include("trusty/kernel/build-config-kerneltests"),
    include("trusty/user/base/build-config-usertests"),

    # Test automatic clearing of td filesystem when userdata is cleared
    # - Stage 1
    # -- Wipe user-data and reboot
    # -- Create a valid filesystem (with both superblocks written)
    #
    # - Stage 2
    # -- Wipe user-data and reboot
    # -- Create a valid filesystem (with a single committed superblock)
    # -- Reboot (keep all data)
    # -- Check that filesystem is accessible (with a small uncommitted
    #    transaction to more avoid super block updates)
    #
    #    If only one super block was written, it could have used the
    #    wrong version. If the new filesystem always writes superblock
    #    version 1, then it will fail if the old version was 2 or 3 as
    #    those two starting points have version 2 in the first
    #    superblock. Stage one will leave the filesystem at version 2 if
    #    b/190109878 has not been fixed or at version 3 if it has been
    #    partially fixed.
    #
    # - Stage 3
    # -- Wipe user-data and reboot
    # -- Write to the filesystem without commiting anything
    # -- Reboot (Should trigger cleanup path for b/190109878
    #    bugfix as generated initial superblock is not needed)
    #
    # - Stage 4
    # -- Write a large transaction to the filesystem without commiting
    #    anything
    # -- Reboot
    # -- Check that filesystem is accessible. If superblock was not
    #    written (b/190109878) this step would fail as the data file is
    #    no longer empty, but the old super blocks refer to data in the
    #    previous deleted file.
    # -- Trigger cleanup in test app.
    compositetest(
        name="storage-td-clear-test",
        sequence=[
            print("[ -------- ] Stage 1 - 2 commit setup"),
            reboot(mode=RebootMode.FACTORY_RESET),
            porttest("com.android.storage-unittest.td.init"),
            porttest("com.android.storage-unittest.td.init"),
            print("[ -------- ] Stage 2 - 1 commit setup"),
            reboot(mode=RebootMode.FACTORY_RESET),
            porttest("com.android.storage-unittest.td.init"),
            reboot(mode=RebootMode.REGULAR),
            porttest("com.android.storage-unittest.td.initnocommitsmall"),
            print("[ -------- ] Stage 3 - no commit small"),
            reboot(mode=RebootMode.FACTORY_RESET),
            porttest("com.android.storage-unittest.td.initnocommitsmall"),
            reboot(mode=RebootMode.REGULAR),
            print("[ -------- ] Stage 4 - no commit large"),
            porttest("com.android.storage-unittest.td.initnocommitlarge"),
            reboot(mode=RebootMode.REGULAR),
            porttest("com.android.storage-unittest.td.initnocommitsmall"),
            porttest("com.android.storage-unittest.td.initnocommitcleanup"),
        ]
    ).needs(storage_full=True),

    # Trusty linux driver tests. Unbind and bind to trigger remove and
    # probe function.
    androidtest(name="irq-driver",
                command="TRUSTY_DEV=$(basename /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core):irq)"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-irq/unbind"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-irq/bind"),

    androidtest(name="log-driver",
                # logd currently goes into a busy loop if the trusty
                # log driver starts returning errors from poll and
                # read. Additionally, the trusty-log driver does not
                # wake up any waiters in unbind. This happens later
                # when there is output from trusty ready for them to
                # read. Stop logd while unbinding to work around these
                # bugs for now and avoid filling the kernel log with
                # error messages. (b/397453308)
                command="TRUSTY_DEV=$(basename /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core):log)"
                        "&&"
                        "stop logd"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-log/unbind"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-log/bind"
                        "&&"
                        "start logd"),

    androidtest(name="virtio-driver",
                # virtio remove currently hangs (bug: 142275662).
                # Disable test until fixed
                enabled=False,
                command="TRUSTY_DEV=$(basename /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core):virtio)"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-virtio/unbind"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "/sys/bus/platform/drivers/trusty-virtio/bind"),

    androidtest(name="trusty-driver",
                # virtio remove currently hangs (bug: 142275662).
                # Disable affected test until fixed
                enabled=False,
                command="TRUSTY_DEV=$(basename /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core))"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "$(echo /sys/bus/platform/drivers/trusty?(-core)/unbind)"
                        "&&"
                        "echo $TRUSTY_DEV >"
                        "$(echo /sys/bus/platform/drivers/trusty?(-core)/bind)"),

    # test that trusty driver started and got a version string
    androidtest(name="trusty-driver-version",
                command="TRUSTY_VERSION=$(cat /sys/bus/platform/"
                        "devices/?(*:)trusty?(-core)/trusty_version)"
                        "&&"
                        "echo Trusty version: ${TRUSTY_VERSION}"
                        "&&"
                        "if [[ \"${TRUSTY_VERSION}\" != \"Project:\"* ]];"
                        "then "
                        "echo Unexpected trusty version string;"
                        "exit 1;"
                        "fi"),

    # Check whether the kernel has been tainted, ignoring
    # certain causes like out-of-tree and unsigned modules.
    androidtest(name="untainted-linux",
                command="TAINTED=$(cat /proc/sys/kernel/tainted)"
                        "&&"
                        "if (( ${TAINTED} & ~0x3000 ));"
                        "then "
                        "echo Linux kernel tainted ${TAINTED};"
                        "exit 1;"
                        "fi"),

    # stdcall test with shared memory buffers.
    # Each test run takes up to 4 arguments:
    # <obj_size>,<obj_count=1>,<repeat_share=1>,<repeat_access=3>
    #
    # Test single 4K shared memory object.
    # Test 10 8MB objects, shared twice, each accessed twice. (8MB non-
    # contiguous object is large enough to need several 4KB messages to
    # describe)
    # Test sharing 2 8MB objects 100 times without accessing it.
    # Test 10 4K shared memory objects, shared 10 times, each accessed
    # 10 times.
    androidtest(name="stdcalltest",
                command="echo '0x1000 "
                        "0x800000,10,2,2 "
                        "0x800000,2,100,0 "
                        "0x1000,10,10,10' >"
                        "$(echo /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core):test/trusty_test_run)"),

    androidtest(name="fpsimdtest",
                command="echo 'fpsimd:1000' >"
                        "$(echo /sys/bus/platform/devices/"
                        "?(*:)trusty?(-core):test/trusty_test_run)"),

    # TIPC tests
    androidtest(name="tipc:ta2ta",
                command="/data/nativetest64/vendor/tipc-test/tipc-test "
                        "-t ta2ta-ipc"),

    # TIPC linux to trusty echo test
    androidtest(name="tipc:echo",
                command="/data/nativetest64/vendor/tipc-test/tipc-test "
                        "-t echo -r 100"),

    # TIPC linux to trusty echo test repeat so 16 bit virtio index wraps
    # Timeout is set to 15 minutes due to b/258851590.
    androidtest(name="tipc:echo-long",
                command="/data/nativetest64/vendor/tipc-test/tipc-test "
                        "-t echo -r 65537",
                timeout=(15 * 60)),

    # NS shares NS memory with S
    androidtest(name="tipc:send-fd",
                command="/data/nativetest64/vendor/tipc-test/tipc-test "
                        "-t send-fd"),

    # Storage proxy restart test. The keymaster storage wrapper keeps a
    # persistent connection to the storage server. Test that this
    # connection gets re-established when the storage proxy restarts.
    androidtest(name="storage-proxy-restart",
                command="/vendor/bin/trusty-ut-ctrl "
                        "com.android.keymaster-unittest"
                        "&&"
                        "stop storageproxyd"
                        "&&"
                        "("
                        "sleep 5"
                        "&&"
                        "start storageproxyd"
                        "&"
                        "/vendor/bin/trusty-ut-ctrl "
                        "com.android.keymaster-unittest"
                        "&&"
                        "wait"
                        ")"
                        "&&"
                        "/vendor/bin/trusty-ut-ctrl "
                        "com.android.keymaster-unittest"
                ).needs(storage_boot=True),

    # Test that storage sessions behave correctly when proxy restarts
    androidtest(
        name="com.android.storage-reconnect-test.tp.reconnect",
        command="/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tp.before "
                "&&"
                "stop storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tp.during "
                "&&"
                "start storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tp.after "
    ).needs(storage_tp=True),
    androidtest(
        name="com.android.storage-reconnect-test.td.reconnect",
        command="/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.td.before "
                "&&"
                "stop storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.td.during "
                "&&"
                "start storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.td.after "
    ).needs(storage_full=True),
    androidtest(
        name="com.android.storage-reconnect-test.tdp.reconnect",
        command="/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tdp.before "
                "&&"
                "stop storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tdp.during "
                "&&"
                "start storageproxyd"
                "&&"
                "/vendor/bin/trusty-ut-ctrl "
                "com.android.storage-reconnect-test.tdp.after "
    ).needs(storage_full=True),

    # Test confirmation UI
    androidtest(name="vts:confirmationui@1.0",
                command="/data/nativetest64/"
                        "VtsHalConfirmationUIV1_0TargetTest/"
                        "VtsHalConfirmationUIV1_0TargetTest"),

    # Test gatekeeper
    androidtest(name="vts:gatekeeper@1.0",
                command="/data/nativetest64/"
                        "VtsHalGatekeeperV1_0TargetTest/"
                        "VtsHalGatekeeperV1_0TargetTest"),

    # Test RKP
    # TODO(b/235265072): re-enable once Android changes propagate to
    # Trusty repo.
    #androidtest(name="vts:rkp",
    #            command="/data/nativetest64/"
    #                    "VtsHalRemotelyProvisionedComponentTargetTest/"
    #                    "VtsHalRemotelyProvisionedComponentTargetTest"
    #                    " --gtest_filter=\""
    #                    "-"
    #                    # We can not satisfy the requirements of
    #                    # prodMode attestation.
    #                    "*.generateAndUseEcdsaP256Key_prodMode/*:"
    #                    # TODO:
    #                    # bootloader/test-runner/test-runner.c in
    #                    # trusty/external/trusty needs to properly set
    #                    # the boot flags for the following tests to pass
    #                    "*.EmptyRequest_testMode/*:"
    #                    "*.NewKeyPerCallInTestMode/*:"
    #                    "*.NonEmptyRequest_testMode/*"
    #                    "\""),

    # Test keymint attestation key provisioning
    androidtest(name="keymint-set-attestation-keys",
                command="/vendor/bin/"
                        "trusty_keymaster_set_attestation_key "
                        "/vendor/etc/"
                        "keymaster_soft_attestation_keys.xml"),

    # Test keymint wrapped attestation key provisioning
    androidtest(name="keymint-wrapped-vts",
                command="/vendor/bin/"
                        "trusty_keymaster_set_attestation_key "
                        "/vendor/etc/"
                        "keymaster_soft_wrapped_attestation_keys.xml"
                        "&&"
                        "/data/nativetest64/"
                        "VtsAidlKeyMintTargetTest/"
                        "VtsAidlKeyMintTargetTest"
                        " --gtest_filter=\""
                        "*/AttestKeyTest.*:"
                        "-"
                        "*/AttestKeyTest.AllRsaSizes*:"
                        "*/AttestKeyTest.RsaAttestedAttestKeys*:"
                        "*/AttestKeyTest.RsaAttestKeyChaining*:"
                        "*/AttestKeyTest.EcAttestKeyChaining*:"
                        "*/AttestKeyTest.AlternateAttestKeyChaining*:"
                        "*/AttestKeyTest.AllEcCurves*:"
                        "*/AttestKeyTest.EcdsaAttestationID*"
                        "\""),

    # Test keymint
    #
    # ClearOperationsTest.TooManyOperations is excluded from testing
    # because KM4 VTS leaks operation slots on Trusty (b/146083990).
    #
    # EarlyBootKeyTest.UseEarlyBootKeyFailure and
    # EarlyBootKeyTest.ImportEarlyBootKeyFailure test cases are exluded
    # because keystore (not present on Trusty emulator image) is needed
    # to propagate earlyBootEnded signal.
    #
    # TODO(b/208872187): Remove --skip_boot_pl_check once we have boot
    # patchlevel configured.
    androidtest(name="vts:keymint",
                command="/data/nativetest64/"
                        "VtsAidlKeyMintTargetTest/"
                        "VtsAidlKeyMintTargetTest"
                        " --skip_boot_pl_check"
                        " --gtest_filter=\""
                        "-"
                        "*/ClearOperationsTest.TooManyOperations/*:"
                        "*/EarlyBootKeyTest.UseEarlyBootKeyFailure/*:"
                        "*/EarlyBootKeyTest.ImportEarlyBootKeyFailure/*:"
                        "*SecureElementProvisioningTest.TeeOnly*:"
                        "*/AttestKeyTest.AllRsaSizes*:"
                        "*/AttestKeyTest.RsaAttestedAttestKeys*:"
                        "*/AttestKeyTest.RsaAttestKeyChaining*:"
                        "*/AttestKeyTest.EcAttestKeyChaining*:"
                        "*/AttestKeyTest.AlternateAttestKeyChaining*:"
                        "*/AttestKeyTest.AllEcCurves*:"
                        "*/AttestKeyTest.EcdsaAttestationID*:"
                        # (b/480194313): DeviceIdAttestationDisabled depends on
                        # the package_native service which is not available
                        # in the qemu_trusty_arm64 android target.
                        "*/NewKeyGenerationTest.DeviceIdAttestationDisabled*:"
                        "*/NewKeyGenerationTest.RsaWithAttestation*:"
                        "*/NewKeyGenerationTest.RsaEncryptionWithAttestation*:"
                        "*/NewKeyGenerationTest.LimitedUsageRsaWithAttestation*:"
                        "*/NewKeyGenerationTest.EcdsaAttestation*:"
                        "*/NewKeyGenerationTest.EcdsaAttestationCurve25519*:"
                        "*/NewKeyGenerationTest.EcdsaAttestationTags*:"
                        "*/NewKeyGenerationTest.EcdsaAttestationIdTags*:"
                        "*/NewKeyGenerationTest.EcdsaAttestationUniqueId*:"
                        "*/NewKeyGenerationTest.EcdsaAttestationTagNoApplicationId*:"
                        "*/NewKeyGenerationTest.AttestationApplicationIDLengthProperlyEncoded*:"
                        # TODO(b/463979556): Remove once fixed.
                        "*/MlDsaTest.AttestToEcdsaKey*:"
                        "*/MlDsaTest.GenerateWithAttestation*:"
                        # TODO(b/463979556): End to remove.
                        "*/BootloaderStateTest.VbStateIsUnverified*:"
                        "*/BootloaderStateTest.VbmetaDigest*:"
                        "*InstanceTest.AidlVersionInFeature*:"
                        "*InstanceTest.FeatureVersionInAidl*:"
                        "\"",
                timeout=(60 * 60)),

    # Busy test validating that linux acts upon the
    # trusty thread priority or the high-priority workqueue setting
    androidtest(name="busy-test-high-to-low-priority-workqueue",
                command="nice -n -20 top -b -m 5 -n 11 -d 2 &"
                        "echo 0 >/sys/module/trusty_core/parameters/use_high_wq"
                        ";"
                        "timeout 10 trusty-ut-ctrl com.android.kernel.busy-test"
                        ";"
                        "echo 1 > /sys/module/trusty_core/parameters/use_high_wq"
                        ";"
                        "timeout 10 trusty-ut-ctrl com.android.kernel.busy-test"
                        ";"
                        "echo 0 >/sys/module/trusty_core/parameters/use_high_wq"
                        "&&"
                        "wait"),

    # Test Binder RPC between Android and Trusty
    androidtest(name="binder-rpc-to-trusty-test",
                enabled=False,
                command="/data/nativetest64/vendor/"
                        "binderRpcToTrustyTest/"
                        "binderRpcToTrustyTest64"),
]
