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

Commit 0cd9dd38 authored by Rob Barnes's avatar Rob Barnes Committed by Gerrit Code Review
Browse files

Merge "Added LargeFinishInput test"

parents dca3711d f5fc7c64
Loading
Loading
Loading
Loading
+51 −0
Original line number Original line Diff line number Diff line
@@ -4604,6 +4604,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