Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f5fc7c64 authored by Rob Barnes's avatar Rob Barnes
Browse files

Added LargeFinishInput test

This tests passing a large input to finish. This should either succeed
or fail with the right error code.

Test: Run new VTS test
Change-Id: Ic4ef90adc6274317796bbe752f95fc9efa5fdb07
parent 9854f58e
Loading
Loading
Loading
Loading
+51 −0
Original line number Original line Diff line number Diff line
@@ -4600,6 +4600,57 @@ TEST_F(ClearOperationsTest, ServiceDeath) {
    }
    }
}
}


typedef KeymasterHidlTest TransportLimitTest;

/*
 * TransportLimitTest.LargeFinishInput
 *
 * Verifies that passing large input data to finish either succeeds or fails as expected.
 */
TEST_F(TransportLimitTest, LargeFinishInput) {
    ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder()
                                                 .Authorization(TAG_NO_AUTH_REQUIRED)
                                                 .AesEncryptionKey(128)
                                                 .BlockMode(BlockMode::ECB)
                                                 .Padding(PaddingMode::NONE)));

    for (int msg_size = 10 /*1KB*/; msg_size <= 17 /*128KB*/; msg_size++) {
        auto cipher_params =
                AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE);

        AuthorizationSet out_params;
        EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, cipher_params, &out_params));

        string plain_message = std::string(1 << msg_size, 'x');
        string encrypted_message;
        auto rc = Finish(plain_message, &encrypted_message);

        if (rc == ErrorCode::OK) {
            EXPECT_EQ(plain_message.size(), encrypted_message.size())
                    << "Encrypt finish returned OK, but did not consume all of the given input";
        } else {
            EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, rc)
                    << "Encrypt finish failed in an unexpected way when given a large input";
            continue;
        }
        cipher_params.push_back(out_params);

        EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, cipher_params));

        string decrypted_message;
        rc = Finish(encrypted_message, &decrypted_message);

        if (rc == ErrorCode::OK) {
            EXPECT_EQ(plain_message.size(), decrypted_message.size())
                    << "Decrypt finish returned OK, did not consume all of the given input";
        } else {
            EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, rc)
                    << "Encrypt finish failed in an unexpected way when given a large input";
        }
    }

    CheckedDeleteKey();
}


}  // namespace test
}  // namespace test
}  // namespace V4_0
}  // namespace V4_0