Commit d97fc760 authored by garciay's avatar garciay
Browse files

Create first ECRF test

parent dfc2f6db
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -59,13 +59,13 @@ int held_codec::decode (const OCTETSTRING& p_data, LibItsHttp__XmlMessageBodyTyp
  }
  
  if (it->second.find("<locationRequest") != std::string::npos) {
    urn__ietf__params__xml__ns__geopriv__held::LocationResponseType locationResponse;
    locationResponse.decode(urn__ietf__params__xml__ns__geopriv__held::LocationResponse_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.locationResponse() = locationResponse;
    urn__ietf__params__xml__ns__geopriv__held::LocationRequestType location_request;
    location_request.decode(urn__ietf__params__xml__ns__geopriv__held::LocationRequest_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.locationRequest() = location_request;
  } else if (it->second.find("<locationResponse") != std::string::npos) {
    urn__ietf__params__xml__ns__geopriv__held::LocationResponseType locationResponse;
    locationResponse.decode(urn__ietf__params__xml__ns__geopriv__held::LocationResponse_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.locationResponse() = locationResponse;
    urn__ietf__params__xml__ns__geopriv__held::LocationResponseType location_response;
    location_response.decode(urn__ietf__params__xml__ns__geopriv__held::LocationResponse_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.locationResponse() = location_response;
  } else if (it->second.find("<error") != std::string::npos) {
    urn__ietf__params__xml__ns__geopriv__held::Error error;
    error.decode(urn__ietf__params__xml__ns__geopriv__held::Error_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
+31 −5
Original line number Diff line number Diff line
@@ -332,6 +332,16 @@ int http_codec::encode_body(const LibItsHttp__MessageBodyTypes::HttpMessageBody&
    const LibItsHttp__XmlMessageBodyTypes::XmlBody& xml_body = p_message_body.xml__body();
    if (xml_body.ischosen(LibItsHttp__XmlMessageBodyTypes::XmlBody::ALT_raw)) {
      p_encoding_buffer = OCTETSTRING(xml_body.raw().lengthof(), (unsigned char*)static_cast<const char*>(xml_body.raw()));
    } else if (xml_body.ischosen(LibItsHttp__XmlMessageBodyTypes::XmlBody::ALT_findServiceRequest)) {
      std::map<std::string, std::unique_ptr<codec<Record_Type, Record_Type> > >::const_iterator it = _codecs.find("lost");
      if (it != _codecs.cend()) {
        loggers::get_instance().log("http_codec::encode_body: Call 'lost_codec'");
        _codecs["lost"]->encode((Record_Type&)xml_body, p_encoding_buffer);
      } else {
        loggers::get_instance().warning("http_codec::encode_body: Unsupported HTTP codec, use raw field as default");
        p_encoding_buffer = OCTETSTRING(xml_body.raw().lengthof(), (unsigned char*)static_cast<const char*>(xml_body.raw()));
        // TODO Add new HTTP message codec here
      }
    } else {
      std::map<std::string, std::unique_ptr<codec<Record_Type, Record_Type> > >::const_iterator it = _codecs.find("held");
      if (it != _codecs.cend()) {
@@ -432,6 +442,22 @@ int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBod
           xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
         }
         message_body.xml__body() = xml_body;
       } else if (p["decode_str"].find("xmlns=\"urn:ietf:params:xml:ns:lost1\">") != std::string::npos) {
         loggers::get_instance().log("http_codec::decode_body: Find 'urn:ietf:params:xml:ns:loat1'");
         if (_codecs["lost"].get() != nullptr) {
           loggers::get_instance().log("http_codec::decode_body: Call 'lost_codec'");
           if (_codecs["lost"]->decode(body, (Record_Type&)xml_body, &p) == -1) {
             loggers::get_instance().warning("http_codec::decode_body: Failed to decode LOST message");
             xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
           } else {
             loggers::get_instance().log_msg("http_codec::decode_body: Decoded message:", xml_body);
             message_body.xml__body() = xml_body;
           }
         } else {
           loggers::get_instance().warning("http_codec::decode_body: No codec for LOST");
           xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
         }
         message_body.xml__body() = xml_body;
      }
    } else if (p["decode_str"].find("<html>") != std::string::npos) { // Try to identify HTML
      loggers::get_instance().log("http_codec::decode_body: Find html message");
@@ -494,17 +520,17 @@ void http_codec::set_payload_codecs(const std::string& p_codecs) {
  
  // Extract codecs
  try {
    std::regex rgx("(\\w+)(:(\\w+))*(;((\\w+)(:(\\w+))*))*");
    std::regex rgx("(\\w+):(\\w+)(;(\\w+):(\\w+))*");
    std::sregex_iterator begin(p_codecs.cbegin(), p_codecs.cend(), rgx);
    std::sregex_iterator end = std::sregex_iterator();
    // E.g. 9 - xml - :held_codec - held_codec - ;html:html_codec - html:html_codec - html - :html_codec - html_codec
    for (std::sregex_iterator it = begin; it != end; ++it) {
      std::smatch m = *it;
      loggers::get_instance().log("http_codec::set_payload_codecs: %d - %s - %s - %s - %s - %s - %s - %s - %s", m.size(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str(), m[4].str().c_str(), m[5].str().c_str(), m[6].str().c_str(), m[7].str().c_str(), m[8].str().c_str());
      for (unsigned int j = 1; j < m.size() - 1; j += 5) { // Exclude m[0]
        loggers::get_instance().log("http_codec::set_payload_codecs: insert (%s, %s), j = %d", m[j].str().c_str(), m[j + 2].str().c_str(), j);
      for (unsigned int j = 1; j < m.size() - 1; j += 3) { // Exclude m[0]
        loggers::get_instance().log("http_codec::set_payload_codecs: insert (%s, %s), j = %d", m[j].str().c_str(), m[j + 1].str().c_str(), j);
        std::string key(m[j].str());
        _codecs.insert(std::make_pair(key, std::unique_ptr<codec<Record_Type, Record_Type> >(codec_stack_builder::get_instance()->get_codec(m[j + 2].str().c_str()))));
        _codecs.insert(std::make_pair(key, std::unique_ptr<codec<Record_Type, Record_Type> >(codec_stack_builder::get_instance()->get_codec(m[j + 1].str().c_str()))));
      } // End of 'for' statement
    } // End of 'for' statement
    //loggers::get_instance().log("http_codec::set_payload_codecs: _codecs length=%d", _codecs.size());
+78 −0
Original line number Diff line number Diff line
#include <stdexcept>
#include <regex>
#include <string>

#include "lost_codec_factory.hh"

#include "loggers.hh"

#include "LibItsHttp_XmlMessageBodyTypes.hh"

#include "urn_ietf_params_xml_ns_lost1.hh"

int lost_codec::encode (const LibItsHttp__XmlMessageBodyTypes::XmlBody& msg, OCTETSTRING& data)
{
  loggers::get_instance().log_msg(">>> lost_codec::encode: ", (const Base_Type&)msg);

  TTCN_EncDec::clear_error();
  TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
  TTCN_Buffer encoding_buffer;

  if (msg.ischosen(LibItsHttp__XmlMessageBodyTypes::XmlBody::ALT_findServiceRequest)) {
    const urn__ietf__params__xml__ns__lost1::FindService& find_service = msg.findServiceRequest();
    loggers::get_instance().log_msg("lost_codec::encode: Process FindService", (const Base_Type&)find_service);
    find_service.encode(urn__ietf__params__xml__ns__lost1::FindService_descr_, encoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
  } else if  (msg.ischosen(LibItsHttp__XmlMessageBodyTypes::XmlBody::ALT_findServiceResponse)) {
    const urn__ietf__params__xml__ns__lost1::FindServiceResponse& find_service_response = msg.findServiceResponse();
    loggers::get_instance().log_msg("lost_codec::encode: Process FindServiceResponse", (const Base_Type&)find_service_response);
    find_service_response.encode(urn__ietf__params__xml__ns__lost1::FindServiceResponse_descr_, encoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
  } else {
    loggers::get_instance().warning("lost_codec::encode: Unsupported variant");
    return -1;
  }
  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
  loggers::get_instance().log_msg("lost_codec::decode: After encoding: ", data);

  loggers::get_instance().log("<<< lost_codec::encode");
  return 0;
}

int lost_codec::decode (const OCTETSTRING& p_data, LibItsHttp__XmlMessageBodyTypes::XmlBody& msg, params* p_params)
{
  loggers::get_instance().log_msg(">>> lost_codec::decode: p_data=", p_data);

  TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
  TTCN_EncDec::clear_error();
  TTCN_Buffer decoding_buffer(p_data);

  // Sanity checks
  params::const_iterator it;
  if (p_params == nullptr) {
    loggers::get_instance().warning("lost_codec::decode: Failed to access p_params (null pointer)");
    return -1;
  } else {
    it = p_params->find("decode_str");
    if (it == p_params->cend()) {
      loggers::get_instance().warning("lost_codec::decode: Failed to access p_params item (decode_str)");
      return -1;
    }
  }
  
  if (it->second.find("<findService") != std::string::npos) {
    urn__ietf__params__xml__ns__lost1::FindService find_service;
    find_service.decode(urn__ietf__params__xml__ns__lost1::FindService_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.findServiceRequest() = find_service;
  } else if (it->second.find("<findServiceResponse") != std::string::npos) {
    urn__ietf__params__xml__ns__lost1::FindServiceResponse find_service_response;
    find_service_response.decode(urn__ietf__params__xml__ns__lost1::FindServiceResponse_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
    msg.findServiceResponse() = find_service_response;
  } else {
    loggers::get_instance().warning("lost_codec::decode: Unsupported variant");
    return -1;
  }
  
  loggers::get_instance().log_msg("<<< lost_codec::decode: ", (const Base_Type&)msg);
  return 0;
}

lost_codec_factory lost_codec_factory::_f;
+23 −0
Original line number Diff line number Diff line
#pragma once

#include "codec.hh"
#include "params.hh"

class Base_Type;
class TTCN_Typedescriptor_t;
class TTCN_Buffer;

namespace LibItsHttp__XmlMessageBodyTypes {
  class XmlBody;
}

class lost_codec: public codec <LibItsHttp__XmlMessageBodyTypes::XmlBody, LibItsHttp__XmlMessageBodyTypes::XmlBody>
{
public:
  explicit lost_codec() : codec<LibItsHttp__XmlMessageBodyTypes::XmlBody, LibItsHttp__XmlMessageBodyTypes::XmlBody>() { };
  virtual ~lost_codec() { };

  virtual int encode (const LibItsHttp__XmlMessageBodyTypes::XmlBody&, OCTETSTRING& data);
  virtual int decode (const OCTETSTRING& p_data, LibItsHttp__XmlMessageBodyTypes::XmlBody&, params* p_params = NULL);

}; // End of class lost_codec
+46 −0
Original line number Diff line number Diff line
/*!
 * \file      lost_codec_factory.hh
 * \brief     Header file for ITS LOST/IP protocol codec factory.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

#include "codec_stack_builder.hh"

#include "lost_codec.hh"

class Record_Type; //! TITAN forward declaration

/*!
 * \class lost_codec_factory
 * \brief  This class provides a factory class to create an lost_codec class instance
 */
class lost_codec_factory: public codec_factory {
  static lost_codec_factory _f; //! Reference to the unique instance of this class
public: //! \publicsection
  /*!
   * \brief Default constructor
   *        Create a new instance of the lost_codec_factory class
   * \remark The LOST/IP codec identifier is LOST
   */
  lost_codec_factory() {
    // register factory
    codec_stack_builder::register_codec_factory("lost_codec", this);
  };
  /*!
   * \fn codec* create_codec(const std::string & type, const std::string & param);
   * \brief  Create the codecs stack based on the provided codecs stack description
   * \param[in] p_type The provided codecs stack description
   * \param[in] p_params Optional parameters
   * \return 0 on success, -1 otherwise
   * \inline
   */
  inline virtual codec<Record_Type, Record_Type>* create_codec() {
    return (codec<Record_Type, Record_Type>*)new lost_codec();
  };
}; // End of class lost_codec_factory
Loading