TEST_F(PluginCollectionTest, testConstruction)
{
  // Test invalid plugin directory.
  try
  {
    QS::PluginCollection collection("");
    FAIL() << "Unexpectedly did not throw an exception.";
  }
  catch (const std::runtime_error &exception)
  {
    std::string expectedError{
      "Failed to open plugin configuration file base directory '"};
    expectedError += "': No such file or directory.";
    EXPECT_EQ(expectedError, exception.what());
  }
  catch (...)
  {
    FAIL();
  }

  std::string pluginDir{myPluginBaseDir + "/TestPlugin"};
  auto mkdirStatus = ::mkdir(pluginDir.c_str(), 0);
  auto thisErrno = errno;
  ASSERT_EQ(0, mkdirStatus) << "On mkdir " << pluginDir << ": "
                            << std::strerror(thisErrno);

  // Test error on directory which cannot be opened.
  try
  {
    QS::PluginCollection collection(myPluginBaseDir);
  }
  catch (const std::runtime_error &exception)
  {
    std::string expectedError{
      "Failed to open plugin directory '"};
    expectedError += pluginDir + "': Permission denied.";
    EXPECT_EQ(expectedError, exception.what());
  }
  catch (...)
  {
    FAIL();
  }

  auto chmodStatus = ::chmod(pluginDir.c_str(), 0700);
  thisErrno = errno;
  ASSERT_EQ(0, chmodStatus) << "On chmod 0700 " << pluginDir << ": "
                            << std::strerror(thisErrno);
  

  // Test no XML files in plugin directory
  try
  {
    QS::PluginCollection collection(myPluginBaseDir);
  }
  catch (const std::runtime_error &exception)
  {
    std::string expectedError{
      "No plugin configuration file found in '"};
    expectedError += pluginDir + "'.";
    EXPECT_EQ(expectedError, exception.what());
  }
  catch (...)
  {
    FAIL();
  }

  std::ofstream xmlFile1(pluginDir + "/File1.xml");
  ASSERT_TRUE(xmlFile1.is_open());
  std::ofstream xmlFile2(pluginDir + "/File2.xml");
  ASSERT_TRUE(xmlFile2.is_open());

  // Test too many XMLs files in plugin directory
  try
  {
    QS::PluginCollection collection(myPluginBaseDir);
  }
  catch (const std::runtime_error &exception)
  {
    std::string expectedError{
      "Too many XML files found in plugin configuration file directory '"};
    expectedError += pluginDir + "'. There can be only one.";
    EXPECT_EQ(expectedError, exception.what());
  }
  catch (...)
  {
    FAIL();
  }

  std::string unlinkFile{pluginDir + "/File2.xml"};
  auto unlinkStatus = ::unlink(unlinkFile.c_str());
  thisErrno = errno;
  ASSERT_EQ(0, unlinkStatus) << "On unlink " << unlinkFile << ": "
                             << std::strerror(thisErrno);

  // Test invalid file (just making sure the PluginReader class is called,
  // more extensive tests for that class are in its test file).
  EXPECT_THROW(QS::PluginCollection collection(myPluginBaseDir),
               std::logic_error);

}
Beispiel #2
0
void SessionSaver::Save_Session()
{

    QFile xmlFile2(filename_);

    // We use template file to create the final xml.
    QFile xmlFile("UDP_session_template.xml");
    xmlFile.open(QIODevice::ReadWrite);

    // Make dom document from the template
    QByteArray xmlData(xmlFile.readAll());
    QDomDocument doc;
    doc.setContent(xmlData);

    QDomElement root = doc.documentElement();
    QDomElement sess_var_node = root.firstChildElement("RUGE_SESSION_VARIABLES");
    QList<rugeVariable>* sessionVariables = session_->getVariables();

    // Add the session variables to the RUGE_SESSION_VARIABLES
    for(int i = 0; i < sessionVariables->size(); i++){
       QDomElement ui = doc.createElement("RUGE_SESSION_VARIABLE ");
       ui.setAttribute("MAX", sessionVariables->at(i).max);
       ui.setAttribute("MIN", sessionVariables->at(i).min);
       ui.setAttribute("TYPE",sessionVariables->at(i).type);
       ui.setAttribute("LOOP_INCREMENT",sessionVariables->at(i).loopIncrement);
       ui.setAttribute("DEFAULT",sessionVariables->at(i).value);
       ui.setAttribute("INCREMENT",sessionVariables->at(i).increment);
       ui.setAttribute("SIZE",sessionVariables->at(i).size);
       ui.setAttribute("VARIABLE",sessionVariables->at(i).name);
       sess_var_node.appendChild(ui);
    }

    QDomElement sess_cont_msgs = root.firstChildElement("RUGE_SESSION_CONTROL_MESSAGES");
    QDomElement sess_cont_mes = sess_cont_msgs.firstChildElement("RUGE_SESSION_CONTROL_MESSAGE");

    // Save the ip version and protocol
    QDomElement sess_cont_mesg_head_stk = sess_cont_mes.firstChildElement("RUGE_SESSION_CONTROL_MESSAGE_HEADERS_STACK");
    sess_cont_mesg_head_stk.setAttribute("Protocol_1",session_->getipVersion());
    sess_cont_mesg_head_stk.setAttribute("Protocol_2",session_->getProtocol());

    // Save the RUGE_SESSION_CONTROL_MESSAGE_HEADER_VARIABLES
    for(int i = 0; i < sessionVariables->size(); i++){
        QDomElement ui = doc.createElement("RUGE_SESSION_CONTROL_MESSAGE_HEADER_VARIABLES");
        ui.setAttribute("OFFSET", sessionVariables->at(i).offset);
        ui.setAttribute("VARIABLE", sessionVariables->at(i).name);
        sess_cont_mes.appendChild(ui);
    }


    // Save the payload.
    QDomElement ref_data = sess_cont_mes.firstChildElement("RUGE_SESSION_CONTROL_MESSAGE_HEADER_REFERENCE_DATA");
    QString s=ref_data.attribute("VALUE");
    QString value = s + QString::fromLatin1(session_->getPayload().toLatin1().toHex());
    ref_data.setAttribute("LENGTH",value.length());
    ref_data.setAttribute("VALUE",  value);

    QDomElement sess_interfaces = root.firstChildElement("RUGE_SESSION_INTERFACES");
    QDomElement sess_interface = sess_interfaces.firstChildElement("RUGE_SESSION_INTERFACE");

    // Save this file to the filename.
    xmlFile2.open(QIODevice::ReadWrite);
    xmlFile2.resize(0);
    QTextStream stream;
    stream.setDevice(&xmlFile2);
    doc.save(stream, 4);
    xmlFile.close();
    xmlFile2.close();
}