/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2024,  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/>.
 */

/** \file
 *  This test suite checks that a strategy returns Nack-NoRoute when there are
 *  no usable FIB nexthops.
 */

// Strategies returning Nack-NoRoute when there are no usable FIB nexthops,
// sorted alphabetically.
#include "fw/asf-strategy.hpp"
#include "fw/best-route-strategy.hpp"
#include "fw/random-strategy.hpp"

#include "tests/test-common.hpp"
#include "tests/daemon/face/dummy-face.hpp"
#include "choose-strategy.hpp"
#include "strategy-tester.hpp"

#include <boost/mp11/list.hpp>

namespace nfd::tests {

using namespace nfd::fw;

template<typename S>
class StrategyNoRouteFixture : public GlobalIoTimeFixture
{
public:
  StrategyNoRouteFixture()
    : limitedIo(this)
    , forwarder(faceTable)
    , strategy(choose<StrategyTester<S>>(forwarder))
    , fib(forwarder.getFib())
    , pit(forwarder.getPit())
    , face1(make_shared<DummyFace>())
    , face2(make_shared<DummyFace>())
  {
    faceTable.add(face1);
    faceTable.add(face2);
  }

public:
  LimitedIo limitedIo;

  FaceTable faceTable;
  Forwarder forwarder;
  StrategyTester<S>& strategy;
  Fib& fib;
  Pit& pit;

  shared_ptr<Face> face1;
  shared_ptr<Face> face2;
};

BOOST_AUTO_TEST_SUITE(Fw)
BOOST_AUTO_TEST_SUITE(TestStrategyNoRoute)

template<typename S>
struct EmptyNextHopList
{
  static Name
  getInterestName()
  {
    return "/P";
  }

  static void
  insertFibEntry(StrategyNoRouteFixture<S>* fixture)
  {
    fixture->fib.insert(Name());
  }
};

template<typename S>
struct NextHopIsDownstream
{
  static Name
  getInterestName()
  {
    return "/P";
  }

  static void
  insertFibEntry(StrategyNoRouteFixture<S>* fixture)
  {
    fib::Entry* entry = fixture->fib.insert(Name()).first;
    fixture->fib.addOrUpdateNextHop(*entry, *fixture->face1, 10);
  }
};

template<typename S>
struct NextHopViolatesScope
{
  static Name
  getInterestName()
  {
    return "/localhop/P";
  }

  static void
  insertFibEntry(StrategyNoRouteFixture<S>* fixture)
  {
    fib::Entry* entry = fixture->fib.insert("/localhop").first;
    fixture->fib.addOrUpdateNextHop(*entry, *fixture->face2, 10);
    // face1 and face2 are both non-local; Interest from face1 cannot be forwarded to face2
  }
};

using Tests = boost::mp11::mp_list<
  boost::mp11::mp_list<AsfStrategy, EmptyNextHopList<AsfStrategy>>,
  boost::mp11::mp_list<AsfStrategy, NextHopIsDownstream<AsfStrategy>>,
  boost::mp11::mp_list<AsfStrategy, NextHopViolatesScope<AsfStrategy>>,

  boost::mp11::mp_list<BestRouteStrategy, EmptyNextHopList<BestRouteStrategy>>,
  boost::mp11::mp_list<BestRouteStrategy, NextHopIsDownstream<BestRouteStrategy>>,
  boost::mp11::mp_list<BestRouteStrategy, NextHopViolatesScope<BestRouteStrategy>>,

  boost::mp11::mp_list<RandomStrategy, EmptyNextHopList<RandomStrategy>>,
  boost::mp11::mp_list<RandomStrategy, NextHopIsDownstream<RandomStrategy>>,
  boost::mp11::mp_list<RandomStrategy, NextHopViolatesScope<RandomStrategy>>
>;

BOOST_FIXTURE_TEST_CASE_TEMPLATE(IncomingInterest,
                                 T, Tests, StrategyNoRouteFixture<boost::mp11::mp_first<T>>)
{
  using Scenario = boost::mp11::mp_second<T>;
  Scenario::insertFibEntry(this);

  auto interest = makeInterest(Scenario::getInterestName());
  auto pitEntry = this->pit.insert(*interest).first;
  pitEntry->insertOrUpdateInRecord(*this->face1, *interest);

  auto f = [&] {
    this->strategy.afterReceiveInterest(*interest, FaceEndpoint(*this->face1), pitEntry);
  };
  BOOST_REQUIRE(this->strategy.waitForAction(f, this->limitedIo, 2));

  BOOST_REQUIRE_EQUAL(this->strategy.rejectPendingInterestHistory.size(), 1);
  BOOST_CHECK_EQUAL(this->strategy.rejectPendingInterestHistory[0].pitInterest.wireEncode(),
                    pitEntry->getInterest().wireEncode());

  BOOST_REQUIRE_EQUAL(this->strategy.sendNackHistory.size(), 1);
  BOOST_CHECK_EQUAL(this->strategy.sendNackHistory[0].pitInterest.wireEncode(),
                    pitEntry->getInterest().wireEncode());
  BOOST_CHECK_EQUAL(this->strategy.sendNackHistory[0].outFaceId, this->face1->getId());
  BOOST_CHECK_EQUAL(this->strategy.sendNackHistory[0].header.getReason(), lp::NackReason::NO_ROUTE);
}

BOOST_AUTO_TEST_SUITE_END() // TestStrategyNoRoute
BOOST_AUTO_TEST_SUITE_END() // Fw

} // namespace nfd::tests
