// Copyright 2024 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.
//
////////////////////////////////////////////////////////////////////////////////

#include "tink/signature/ml_dsa_parameters.h"

#include <memory>
#include <utility>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "tink/parameters.h"
#include "tink/util/test_matchers.h"

namespace crypto {
namespace tink {
namespace {

using ::crypto::tink::test::IsOk;
using ::crypto::tink::test::StatusIs;
using ::testing::Eq;
using ::testing::TestWithParam;
using ::testing::Values;

struct VariantTestCase {
  MlDsaParameters::Instance instance;
  MlDsaParameters::Variant variant;
  bool has_id_requirement;
};

using MlDsaParametersTest = TestWithParam<VariantTestCase>;

// Note: for now only the Tink variant is supported, so this test suite should
// be extended if other variants (e.g. no-prefix) are added in the future.
INSTANTIATE_TEST_SUITE_P(
    MlDsaParametersTestSuite, MlDsaParametersTest,
    Values(VariantTestCase{MlDsaParameters::Instance::kMlDsa65,
                           MlDsaParameters::Variant::kNoPrefix,
                           /*has_id_requirement=*/false},
           VariantTestCase{MlDsaParameters::Instance::kMlDsa65,
                           MlDsaParameters::Variant::kTink,
                           /*has_id_requirement=*/true},
           VariantTestCase{MlDsaParameters::Instance::kMlDsa87,
                           MlDsaParameters::Variant::kNoPrefix,
                           /*has_id_requirement=*/false},
           VariantTestCase{MlDsaParameters::Instance::kMlDsa87,
                           MlDsaParameters::Variant::kTink,
                           /*has_id_requirement=*/true}));

TEST_P(MlDsaParametersTest, CreateMlDsaWorks) {
  VariantTestCase test_case = GetParam();

  absl::StatusOr<MlDsaParameters> parameters =
      MlDsaParameters::Create(test_case.instance, test_case.variant);
  ASSERT_THAT(parameters, IsOk());

  EXPECT_THAT(parameters->GetInstance(), Eq(test_case.instance));
  EXPECT_THAT(parameters->GetVariant(), Eq(test_case.variant));
  EXPECT_THAT(parameters->HasIdRequirement(), Eq(test_case.has_id_requirement));
}

TEST_P(MlDsaParametersTest, CreateWithInvalidVariantFails) {
  VariantTestCase test_case = GetParam();

  EXPECT_THAT(MlDsaParameters::Create(
                  test_case.instance,
                  MlDsaParameters::Variant::
                      kDoNotUseInsteadUseDefaultWhenWritingSwitchStatements)
                  .status(),
              StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST_P(MlDsaParametersTest, CreateWithInvalidInstanceFails) {
  VariantTestCase test_case = GetParam();

  EXPECT_THAT(MlDsaParameters::Create(
                  MlDsaParameters::Instance::
                      kDoNotUseInsteadUseDefaultWhenWritingSwitchStatements,
                  test_case.variant)
                  .status(),
              StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST(MlDsaParametersTest, CopyConstructor) {
  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, MlDsaParameters::Variant::kNoPrefix);
  ASSERT_THAT(parameters, IsOk());

  MlDsaParameters copy(*parameters);

  EXPECT_THAT(copy, Eq(*parameters));
}

TEST(MlDsaParametersTest, CopyAssignment) {
  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, MlDsaParameters::Variant::kNoPrefix);
  ASSERT_THAT(parameters, IsOk());

  absl::StatusOr<MlDsaParameters> copy = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa87, MlDsaParameters::Variant::kTink);
  ASSERT_THAT(copy, IsOk());

  *copy = *parameters;

  EXPECT_THAT(*copy, Eq(*parameters));
}

TEST(MlDsaParametersTest, MoveConstructor) {
  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, MlDsaParameters::Variant::kNoPrefix);
  ASSERT_THAT(parameters, IsOk());

  MlDsaParameters expected(*parameters);
  MlDsaParameters moved(std::move(*parameters));

  EXPECT_THAT(moved, Eq(expected));
}

TEST(MlDsaParametersTest, MoveAssignment) {
  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, MlDsaParameters::Variant::kNoPrefix);
  ASSERT_THAT(parameters, IsOk());

  absl::StatusOr<MlDsaParameters> moved = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa87, MlDsaParameters::Variant::kTink);
  ASSERT_THAT(moved, IsOk());

  MlDsaParameters expected(*parameters);
  *moved = std::move(*parameters);

  EXPECT_THAT(*moved, Eq(expected));
}

TEST_P(MlDsaParametersTest, ParametersEquals) {
  VariantTestCase test_case = GetParam();

  absl::StatusOr<MlDsaParameters> parameters =
      MlDsaParameters::Create(test_case.instance, test_case.variant);
  ASSERT_THAT(parameters, IsOk());

  absl::StatusOr<MlDsaParameters> other_parameters =
      MlDsaParameters::Create(test_case.instance, test_case.variant);
  ASSERT_THAT(other_parameters, IsOk());

  EXPECT_TRUE(*parameters == *other_parameters);
  EXPECT_TRUE(*other_parameters == *parameters);
  EXPECT_FALSE(*parameters != *other_parameters);
  EXPECT_FALSE(*other_parameters != *parameters);
}

TEST_P(MlDsaParametersTest, DifferentInstanceNotEqual) {
  VariantTestCase test_case = GetParam();

  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, test_case.variant);

  absl::StatusOr<MlDsaParameters> other_parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa87, test_case.variant);

  EXPECT_TRUE(*parameters != *other_parameters);
  EXPECT_FALSE(*parameters == *other_parameters);
}

TEST_P(MlDsaParametersTest, DifferentVariantNotEqual) {
  VariantTestCase test_case = GetParam();

  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      test_case.instance, MlDsaParameters::Variant::kNoPrefix);

  absl::StatusOr<MlDsaParameters> other_parameters = MlDsaParameters::Create(
      test_case.instance, MlDsaParameters::Variant::kTink);

  EXPECT_TRUE(*parameters != *other_parameters);
  EXPECT_FALSE(*parameters == *other_parameters);
}

TEST(MlDsaParametersTest, Clone) {
  absl::StatusOr<MlDsaParameters> parameters = MlDsaParameters::Create(
      MlDsaParameters::Instance::kMlDsa65, MlDsaParameters::Variant::kNoPrefix);
  ASSERT_THAT(parameters, IsOk());

  std::unique_ptr<Parameters> cloned_parameters = parameters->Clone();
  ASSERT_THAT(*cloned_parameters, Eq(*parameters));
}

}  // namespace
}  // namespace tink
}  // namespace crypto
