/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2025,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mgmt/manager-base.hpp"

#include "manager-common-fixture.hpp"

namespace nfd::tests {

class TestCommandVoidParameters : public ndn::nfd::ControlCommand<TestCommandVoidParameters>
{
  NDN_CXX_CONTROL_COMMAND("test-module", "test-void");
};

const TestCommandVoidParameters::RequestFormat TestCommandVoidParameters::s_requestFormat;

class TestCommandRequireName : public ndn::nfd::ControlCommand<TestCommandRequireName>
{
  NDN_CXX_CONTROL_COMMAND("test-module", "test-name-required");
};

const TestCommandRequireName::RequestFormat TestCommandRequireName::s_requestFormat =
    RequestFormat()
    .required(ndn::nfd::CONTROL_PARAMETER_NAME);

class DummyManager final : public ManagerBase
{
public:
  explicit
  DummyManager(Dispatcher& dispatcher)
    : ManagerBase("test-module", dispatcher)
  {
  }

private:
  ndn::mgmt::Authorization
  makeAuthorization(const std::string&) final
  {
    return [] (const Name& prefix, const Interest& interest,
               const ndn::mgmt::ControlParametersBase* params,
               const ndn::mgmt::AcceptContinuation& accept,
               const ndn::mgmt::RejectContinuation& reject) {
      accept("requester");
    };
  }
};

class ManagerBaseFixture : public ManagerCommonFixture
{
protected:
  DummyManager m_manager{m_dispatcher};
};

BOOST_AUTO_TEST_SUITE(Mgmt)
BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)

BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
{
  bool wasHandlerCalled = false;
  auto handler = [&] (auto&&...) { wasHandlerCalled = true; };

  m_manager.registerCommandHandler<TestCommandVoidParameters>(handler);
  m_manager.registerCommandHandler<TestCommandRequireName>(handler);
  setTopPrefix();

  auto testRegisterCommandHandler = [&] (const Name& commandName) {
    wasHandlerCalled = false;
    receiveInterest(makeControlCommandRequest(commandName));
  };

  testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
  BOOST_CHECK(wasHandlerCalled);

  testRegisterCommandHandler("/localhost/nfd/test-module/test-name-required");
  BOOST_CHECK(!wasHandlerCalled);

  testRegisterCommandHandler(Name("/localhost/nfd/test-module/test-name-required")
                             .append(ControlParameters().setName("/foo").wireEncode()));
  BOOST_CHECK(wasHandlerCalled);
}

BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
{
  bool wasHandlerCalled = false;
  auto handler = [&] (auto&&...) { wasHandlerCalled = true; };

  m_manager.registerStatusDatasetHandler("test-status", handler);
  setTopPrefix();

  receiveInterest(*makeInterest("/localhost/nfd/test-module/test-status", true));
  BOOST_CHECK(wasHandlerCalled);
}

BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
{
  auto post = m_manager.registerNotificationStream("test-notification");
  setTopPrefix();

  post(Block({0x82, 0x01, 0x02}));
  advanceClocks(1_ms);

  BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
  BOOST_CHECK_EQUAL(m_responses[0].getName(),
                    Name("/localhost/nfd/test-module/test-notification").appendSequenceNumber(0));
}

BOOST_AUTO_TEST_CASE(ExtractSigner)
{
  std::string signer = ManagerBase::extractSigner(Interest("/test/interest/unsigned"));
  BOOST_CHECK_EQUAL(signer, ""); // missing SignatureInfo

  auto signedV03 = makeControlCommandRequest("/test/interest/signed", {},
                                             ndn::security::SignedInterestFormat::V03);
  signer = ManagerBase::extractSigner(signedV03);
  BOOST_CHECK_EQUAL(signer,
                    m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
                    .getDefaultKey().getDefaultCertificate().getName().toUri());

  signedV03.setSignatureInfo(ndn::SignatureInfo(tlv::SignatureSha256WithEcdsa));
  signer = ManagerBase::extractSigner(signedV03);
  BOOST_CHECK_EQUAL(signer, ""); // missing KeyLocator

  auto signedV02 = makeControlCommandRequest("/test/interest/signed", {},
                                             ndn::security::SignedInterestFormat::V02);
  signer = ManagerBase::extractSigner(signedV02);
  BOOST_CHECK_EQUAL(signer,
                    m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
                    .getDefaultKey().getDefaultCertificate().getName().toUri());
}

BOOST_AUTO_TEST_CASE(MakeRelPrefix)
{
  auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
  BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
}

BOOST_AUTO_TEST_SUITE_END() // TestManagerBase
BOOST_AUTO_TEST_SUITE_END() // Mgmt

} // namespace nfd::tests
