#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sstream>

#include <cppunit/extensions/HelperMacros.h>

#include <arc/message/PayloadSOAP.h>
#include <arc/delegation/DelegationInterface.h>
 	
class DelegationInterfaceTest
  : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(DelegationInterfaceTest);
  CPPUNIT_TEST(TestDelegationInterfaceDELEGATEARC);
  CPPUNIT_TEST(TestDelegationInterfaceDELEGATEGDS20);
  CPPUNIT_TEST(TestDelegationInterfaceDELEGATEEMIES);
  CPPUNIT_TEST(TestDelegationInterfaceDELEGATEEMIDS);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp();
  void tearDown();

  void TestDelegationInterfaceDELEGATEARC();
  void TestDelegationInterfaceDELEGATEGDS20();
  void TestDelegationInterfaceDELEGATEEMIES();
  void TestDelegationInterfaceDELEGATEEMIDS();

private:
  std::string credentials;
};

class DirectMCC: public Arc::MCCInterface {
  private:
    Arc::DelegationContainerSOAP& container_;
  public:
    DirectMCC(Arc::DelegationContainerSOAP& container,Arc::PluginArgument* parg):Arc::MCCInterface(parg),container_(container) {};
    Arc::MCC_Status process(Arc::Message& in,Arc::Message& out);
};

Arc::MCC_Status DirectMCC::process(Arc::Message& in,Arc::Message& out) {
  if(!in.Payload()) return Arc::MCC_Status();
  Arc::PayloadSOAP* in_payload = NULL;
  try {
    in_payload = dynamic_cast<Arc::PayloadSOAP*>(in.Payload());
  } catch(std::exception& e) { };
  if(!in_payload) return Arc::MCC_Status();
  Arc::MCC_Status r;
  Arc::NS ns;
  Arc::PayloadSOAP* out_payload = new Arc::PayloadSOAP(ns);
  out.Payload(out_payload);
  if(!container_.Process(*in_payload,*out_payload,"")) return Arc::MCC_Status();
  //  if(cred.empty()) return Arc::MCC_Status();
  return Arc::MCC_Status(Arc::STATUS_OK);
}

void DelegationInterfaceTest::setUp() {
  credentials.assign("\
-----BEGIN CERTIFICATE-----\n\
MIIBxjCCAS8CARAwDQYJKoZIhvcNAQEFBQAwKzENM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\
jjnSUl5TgbqAtE4LXgnOgVl+Sazq3dVHBBhaFTzFYfa57YlvN8i6nYF8SfegpFJa\n\
Pt7BdSFlAkEA3PUrZgZDQDdrIFrqk12tW7P4YGqPkSjGrbuLTwGxhTiWhPL7Tej7\n\
Up/z8zpahDbGEXhNUgKKw0AOwHtZ2wssAwJBAJyPr2jyCRgApH4U2h4kLWffPH8Y\n\
7kq16HqTlNirqyKhV08cqllwEnH7+rGwFImlq2Xsz7Cfsr0u6I3SmRJT7GkCQQCJ\n\
v8q7gyH/8cy2Uhe1oYwHBI7OxQAV9f7OpoH10k9yh1HHNpgW/S1ZWGDEfNebX25h\n\
y8cgXndVvCS2OPBOz4szAkBWx+6KgpQ+Xdx5Jv7IoGRdE9GdIGMtTaHOnUxSsdlj\n\
buEHRt+0Gp5Rod9S6w9Ppl6CphSPq5HRCo49SBBRgAWm\n\
-----END RSA PRIVATE KEY-----\n\
");
}

void DelegationInterfaceTest::tearDown() {
}

void DelegationInterfaceTest::TestDelegationInterfaceDELEGATEARC() {
  Arc::DelegationContainerSOAP c;
  Arc::DelegationProviderSOAP p(credentials);
  DirectMCC m(c,NULL);
  Arc::MessageContext context;

  CPPUNIT_ASSERT((bool)p.DelegateCredentialsInit(m,&context,Arc::DelegationProviderSOAP::ARCDelegation));
  CPPUNIT_ASSERT((bool)p.UpdateCredentials(m,&context,Arc::DelegationRestrictions(),Arc::DelegationProviderSOAP::ARCDelegation));
}

void DelegationInterfaceTest::TestDelegationInterfaceDELEGATEGDS20() {
  Arc::DelegationContainerSOAP c;
  Arc::DelegationProviderSOAP p(credentials);
  DirectMCC m(c,NULL);
  Arc::MessageContext context;

  CPPUNIT_ASSERT((bool)p.DelegateCredentialsInit(m,&context,Arc::DelegationProviderSOAP::GDS20));
  CPPUNIT_ASSERT((bool)p.UpdateCredentials(m,&context,Arc::DelegationRestrictions(),Arc::DelegationProviderSOAP::GDS20));
}

void DelegationInterfaceTest::TestDelegationInterfaceDELEGATEEMIES() {
  Arc::DelegationContainerSOAP c;
  Arc::DelegationProviderSOAP p(credentials);
  DirectMCC m(c,NULL);
  Arc::MessageContext context;

  CPPUNIT_ASSERT((bool)p.DelegateCredentialsInit(m,&context,Arc::DelegationProviderSOAP::EMIES));
  CPPUNIT_ASSERT((bool)p.UpdateCredentials(m,&context,Arc::DelegationRestrictions(),Arc::DelegationProviderSOAP::EMIES));
}

void DelegationInterfaceTest::TestDelegationInterfaceDELEGATEEMIDS() {
  Arc::DelegationContainerSOAP c;
  Arc::DelegationProviderSOAP p(credentials);
  DirectMCC m(c,NULL);
  Arc::MessageContext context;

  CPPUNIT_ASSERT((bool)p.DelegateCredentialsInit(m,&context,Arc::DelegationProviderSOAP::EMIDS));
  CPPUNIT_ASSERT((bool)p.UpdateCredentials(m,&context,Arc::DelegationRestrictions(),Arc::DelegationProviderSOAP::EMIDS));
}

CPPUNIT_TEST_SUITE_REGISTRATION(DelegationInterfaceTest);


