// Copyright 2023 Google LLC
//
// 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.

#ifndef NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
#define NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_

#include "nearby_protocol.h"

// Plaintext advertisement bytes
constexpr std::array<uint8_t, 2> V0AdvEmptyBytes{0x00, 0x03};
constexpr std::array<uint8_t, 2> V1AdvEmptyBytes{0x00, 0x00};
constexpr std::array<uint8_t, 3> V0AdvPlaintextBytes{0x00, 0x15, 0x03};
constexpr std::array<uint8_t, 6> V0AdvPlaintextMultiDeBytes{
    0x00, 0x15, 0x05, 0x26, 0x40, 0x40,
};
constexpr std::array<uint8_t, 5> V1AdvPlaintextBytes{0x20,  // Version header
                                                     0x00,  // format
                                                     0x02,  // section len
                                                     0x15,  // Tx power value 3
                                                     0x03};
// V0 encrypted advertisement data - ripped out of np_adv/tests/examples_v0.rs
constexpr std::array<uint8_t, 19> V0AdvEncryptedBytes{
    0x04, 0x22, 0x22, 0xD8, 0x22, 0x12, 0xEF, 0x16, 0xDB, 0xF8,
    0x72, 0xF2, 0xA3, 0xA7, 0xC0, 0xFA, 0x52, 0x48, 0xEC};
inline std::vector<uint8_t> V0AdvEncryptedMetadata = {
    0x8D, 0xD4, 0x7A, 0x58, 0x47, 0x30, 0x65, 0x89, 0x06, 0x16, 0x04, 0xCA,
    0x68, 0xCE, 0xB4, 0x18, 0xA7, 0x40, 0xB8, 0x36, 0xB0, 0x94, 0xB4, 0xC0,
    0x6C, 0xAF, 0x7C, 0x4A, 0x3B, 0x68, 0x28, 0x54, 0x8F, 0x36, 0xD5, 0x68,
    0x12, 0x6C, 0xC2, 0x63, 0x31, 0x4A, 0x48, 0xE4, 0x3B, 0xB3, 0xCF, 0xD4,
    0x10, 0x49, 0x59, 0xE7, 0x2B, 0xDE, 0xEB, 0x78, 0x44, 0x68,
};
inline std::string ExpectedV0DecryptedMetadata(
    R"({"name":"Alice","email":"alice@gmail.com"})");
constexpr std::array<uint8_t, 32> V0AdvKeySeed = {
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
};
constexpr std::array<uint8_t, 32> V0AdvLegacyIdentityTokenHmac = {
    0x09, 0xFE, 0x9E, 0x81, 0xB7, 0x3E, 0x5E, 0xCC, 0x76, 0x59, 0x57,
    0x71, 0xE0, 0x1F, 0xFB, 0x34, 0x38, 0xE7, 0x5F, 0x24, 0xA7, 0x69,
    0x56, 0xA0, 0xB8, 0xEA, 0x67, 0xD1, 0x1C, 0x3E, 0x36, 0xFD};

// V1 encrypted advertisement data - ripped out of np_adv/tests/examples_v1.rs

constexpr std::array<uint8_t, 53> V1AdvEncryptedBytes = {
    0x20, 0x02, 0x11, 0x4E, 0xB8, 0x1D, 0x0B, 0xCF, 0x6D, 0xA9, 0xD3,
    0x19, 0x30, 0xF3, 0xC2, 0xD8, 0xB3, 0xD1, 0x03, 0xF2, 0x46, 0x98,
    0x2B, 0xAF, 0x4F, 0xC5, 0x65, 0x70, 0xD2, 0x2C, 0x4F, 0x7C, 0x3E,
    0x42, 0x12, 0xD6, 0xCE, 0x71, 0xD9, 0x92, 0x56, 0xF1, 0x3B, 0xB1,
    0xD6, 0x3B, 0x77, 0xE8, 0x0E, 0x74, 0xFD, 0x82, 0xE3};
constexpr std::array<uint8_t, 32> V1AdvKeySeed = {
    0xA0, 0xC4, 0x9E, 0xF2, 0x9D, 0xE3, 0xAF, 0x42, 0xCB, 0x71, 0xF5,
    0x01, 0x55, 0x7C, 0x9C, 0xD3, 0x3F, 0x76, 0x03, 0x91, 0xF8, 0xF2,
    0xC2, 0x72, 0xBC, 0x26, 0xEA, 0x4C, 0xF2, 0x77, 0xB8, 0xBC};
constexpr std::array<uint8_t, 32> V1AdvExpectedMicShortSaltIdentityTokenHmac = {
    0x06, 0x78, 0x21, 0x6D, 0x23, 0x30, 0x8D, 0xE8, 0xA8, 0xA4, 0x29,
    0x9D, 0x6E, 0x1B, 0x81, 0x29, 0x63, 0xDD, 0x96, 0xE4, 0xD3, 0xE4,
    0x3F, 0xF0, 0xA9, 0xB5, 0x61, 0xD4, 0x16, 0x41, 0x0E, 0xA7};
constexpr std::array<uint8_t, 32>
    V1AdvExpectedMicExtendedSaltIdentityTokenHmac = {
        0x1C, 0x36, 0xE9, 0x0B, 0x8D, 0xCB, 0xFB, 0x18, 0x34, 0x7D, 0xA9,
        0xE2, 0x89, 0x39, 0xCE, 0x88, 0x33, 0xCD, 0xBD, 0x9A, 0x0F, 0x53,
        0x75, 0xF1, 0x02, 0xF7, 0x96, 0x1A, 0x59, 0x55, 0x7C, 0x09};
inline std::vector<uint8_t> V1AdvEncryptedMetadata = {
    0x6D, 0x60, 0x84, 0xCD, 0xE2, 0x81, 0x6B, 0xB0, 0xE6, 0x69, 0x79,
    0x80, 0x4D, 0xEE, 0x8F, 0xBD, 0x37, 0x79, 0xE7, 0xB8, 0xA3, 0xF2,
    0x8A, 0x8A, 0xB4, 0x0F, 0x42, 0x51, 0x33, 0xB1, 0x0A, 0x1A, 0x3B,
    0x9C, 0xE3, 0x9C, 0xFA, 0x64, 0x4C, 0x77, 0x82, 0x94, 0x34, 0x36,
    0x5E, 0xE1, 0x9B, 0x18, 0xF7, 0xBF, 0x51, 0x24, 0xF5, 0x90, 0xD4,
    0xF1, 0x58, 0x09, 0xB2, 0x48, 0x19, 0x5F, 0x0C, 0xE9, 0x27, 0xB4,
    0xA1, 0xEE, 0x0C, 0x36, 0x6B, 0x02, 0x52, 0x45, 0xAE, 0x76, 0x60,
    0x0E, 0x8B, 0x34, 0xC2, 0xD7, 0x66, 0xCD, 0x93, 0xD9, 0xFD, 0x88,
    0x3F, 0xBC, 0x28, 0x49, 0x9C, 0x39, 0x70, 0xF1, 0x1F, 0x16, 0xD1,
    0xD3, 0x61, 0x0C, 0xEE, 0x2F, 0xF4, 0xF4, 0xFD, 0x29, 0x85, 0x2A};
inline std::string ExpectedV1DecryptedMetadata =
    "{\"uuid\":\"378845e1-2616-420d-86f5-674177a7504d\",\"display_name\":"
    "\"Alice\",\"location\":\"Wonderland\"}";
constexpr std::array<uint8_t, 16> ExpectedV1DerivedSalt = {
    0xFF, 0x85, 0xFF, 0x27, 0xD5, 0xE2, 0xEE, 0x77,
    0x06, 0x11, 0x0E, 0xD0, 0xF4, 0x2A, 0x19, 0x35};

// Data suitable for use directly through the C API (bypassing the C++ wrapper)
constexpr np_ffi::internal::RawAdvertisementPayload V0AdvMultiDeInternals{
    {7, {0x00, 0x03, 0x15, 0x05, 0x26, 0x00, 0x46}}};

// Wrapper types directly usable by C++ API
constexpr nearby_protocol::ByteBuffer<255> V0AdvEmptyBuffer(V0AdvEmptyBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvEmpty(V0AdvEmptyBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextBuffer(
    V0AdvPlaintextBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintext(
    V0AdvPlaintextBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextMultiDeBuffer(
    V0AdvPlaintextMultiDeBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintextMultiDe(
    V0AdvPlaintextMultiDeBuffer);

constexpr nearby_protocol::ByteBuffer<255> V1AdvPlaintextBuffer(
    V1AdvPlaintextBytes);
constexpr nearby_protocol::RawAdvertisementPayload V1AdvPlaintext(
    V1AdvPlaintextBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvEncryptedBuffer(
    V0AdvEncryptedBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvEncryptedPayload(
    V0AdvEncryptedBuffer);

constexpr nearby_protocol::ByteBuffer<255> V1AdvEncryptedBuffer(
    V1AdvEncryptedBytes);
constexpr nearby_protocol::RawAdvertisementPayload V1AdvEncrypted(
    V1AdvEncryptedBuffer);

void test_panic_handler(nearby_protocol::PanicReason reason);

nearby_protocol::V0MatchableCredential GenerateRandomCredentialV0();

nearby_protocol::V1MatchableCredential GenerateRandomCredentialV1();

template <uint32_t N>
std::array<uint8_t, N> create_random_array() {
  std::array<uint8_t, N> array{};
  std::generate(array.begin(), array.end(), []() { return rand(); });
  return array;
}

std::string generate_hex_string(size_t length);

#endif  // NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
