/*
 * Copyright (c) 2024, stelar7  <dudedbz@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCrypto/Hash/MGF.h>
#include <LibCrypto/Hash/SHA1.h>
#include <LibCrypto/Hash/SHA2.h>
#include <LibTest/TestCase.h>

static ByteBuffer operator""_b(char const* string, size_t length)
{
    return ByteBuffer::copy(string, length).release_value();
}

TEST_CASE(test_mgf1_short)
{
    u8 expected_result[3] {
        0x1a, 0xc9, 0x07
    };
    auto expected = ReadonlyBytes { expected_result, 3 };

    ByteBuffer seed = { "foo"_b };
    auto length = 3;
    ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1<Crypto::Hash::SHA1>(seed, length));

    EXPECT_EQ(expected, result);
}

TEST_CASE(test_mgf1_long)
{
    u8 expected_result[50] {
        0xbc, 0x0c, 0x65, 0x5e, 0x01, 0x6b, 0xc2, 0x93, 0x1d, 0x85, 0xa2, 0xe6, 0x75, 0x18, 0x1a, 0xdc,
        0xef, 0x7f, 0x58, 0x1f, 0x76, 0xdf, 0x27, 0x39, 0xda, 0x74, 0xfa, 0xac, 0x41, 0x62, 0x7b, 0xe2,
        0xf7, 0xf4, 0x15, 0xc8, 0x9e, 0x98, 0x3f, 0xd0, 0xce, 0x80, 0xce, 0xd9, 0x87, 0x86, 0x41, 0xcb,
        0x48, 0x76
    };
    auto expected = ReadonlyBytes { expected_result, 50 };

    ByteBuffer seed = { "bar"_b };
    auto length = 50;
    ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1<Crypto::Hash::SHA1>(seed, length));

    EXPECT_EQ(expected, result);
}

TEST_CASE(test_mgf1_long_sha256)
{
    u8 expected_result[50] {
        0x38, 0x25, 0x76, 0xa7, 0x84, 0x10, 0x21, 0xcc, 0x28, 0xfc, 0x4c, 0x09, 0x48, 0x75, 0x3f, 0xb8,
        0x31, 0x20, 0x90, 0xce, 0xa9, 0x42, 0xea, 0x4c, 0x4e, 0x73, 0x5d, 0x10, 0xdc, 0x72, 0x4b, 0x15,
        0x5f, 0x9f, 0x60, 0x69, 0xf2, 0x89, 0xd6, 0x1d, 0xac, 0xa0, 0xcb, 0x81, 0x45, 0x02, 0xef, 0x04,
        0xea, 0xe1
    };
    auto expected = ReadonlyBytes { expected_result, 50 };

    ByteBuffer seed = { "bar"_b };
    auto length = 50;
    ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1<Crypto::Hash::SHA256>(seed, length));

    EXPECT_EQ(expected, result);
}
