/*
 * @author
 *         
 * @version
 *         1.0
 * @desc
 *         
 * @remark
 *         
 * @see
 *         
 */ 
module TestCodec_SecuredMessages {
    
  // LibCommon
  import from LibCommon_BasicTypesAndValues all;
  import from LibCommon_DataStrings all;
    
  // LibIts
  import from IEEE1609dot2BaseTypes language "ASN.1:1997" all;
  import from IEEE1609dot2 language "ASN.1:1997" all;
  import from EtsiTs103097Module language "ASN.1:1997" all;
    
  // LibItsGeoNetworking
  import from LibItsGeoNetworking_EncdecDeclarations all;
  import from LibItsGeoNetworking_TypesAndValues all;
  import from LibItsGeoNetworking_Templates all;
    
  // LibItsSecurity
  import from LibItsSecurity_EncdecDeclarations all;
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Pixits all;
    
  // TestCodec
  import from TestCodec_TestAndSystem all;
    
  testcase tc_ssp_cam_1() runs on TCType system TCType {
    var SspCAM v_sspCAM := valueof(m_sspCAMContainer_sign_all);
    var bitstring v_encMsg;
        
  v_encMsg := encvalue(valueof(v_sspCAM));
    if (lengthof(v_encMsg) != 32) {
      setverdict(fail);
    } else {
      var SspCAM v_sspCAM_dec;
      if (decvalue(v_encMsg, v_sspCAM_dec) != 0) {
        setverdict(fail);
      } else if (not(match(v_sspCAM_dec, v_sspCAM))) {
        setverdict(fail);
      } else {
        setverdict(pass);
      }
    }
  } // End of testcase tc_ssp_cam_1
    
  testcase tc_ssp_cam_2() runs on TCType system TCType {
    var SspCAM v_sspCAM := valueof(m_sspCAMContainer_sign_all);
    var bitstring v_encMsg;

  v_sspCAM.publicTransport := '0'B;
  v_sspCAM.specialTransport := '0'B;
  v_sspCAM.dangerousGoods := '0'B;
  v_sspCAM.roadwork := '0'B;
  v_sspCAM.rescue := '0'B;
  v_encMsg := encvalue(valueof(v_sspCAM));
    if (lengthof(v_encMsg) != 32) {
      setverdict(fail);
    } else {
      var SspCAM v_sspCAM_dec;
      if (decvalue(v_encMsg, v_sspCAM_dec) != 0) {
        setverdict(fail);
      } else if (not(match(v_sspCAM_dec, v_sspCAM))) {
        setverdict(fail);
      } else {
        setverdict(pass);
      }
    }
  } // End of testcase tc_ssp_cam_2
    
  testcase tc_ssp_denm() runs on TCType system TCType {
    var SspDENM v_sspDENM := valueof(m_sspDENMContainer_sign_all);
    var bitstring v_encMsg;
        
  v_encMsg := encvalue(valueof(v_sspDENM));
    if (lengthof(v_encMsg) != 32) {
      setverdict(fail);
    } else {
      var SspDENM v_sspDENM_dec;
      if (decvalue(v_encMsg, v_sspDENM_dec) != 0) {
        setverdict(fail);
      } else if (not(match(v_sspDENM_dec, v_sspDENM))) {
        setverdict(fail);
      } else {
        setverdict(pass);
      }
    }
  } // End of testcase tc_ssp_denm
    
  testcase tc_secured_message_unsecured() runs on TCType system TCType {
    var template (value) EtsiTs103097Data v_secured_data;
    var EtsiTs103097Data v_secured_data_dec;
    var bitstring v_encMsg;
        
  v_secured_data := m_etsiTs103097Data_unsecured(
                                                 'CAFEDECA'O
                                                 );
  v_encMsg := encvalue(valueof(v_secured_data));
    setverdict(pass, "Encoding passed.");
    if (decvalue(v_encMsg, v_secured_data_dec) != 0) {
      setverdict(fail);
      stop;
    } else if (not(match(valueof(v_secured_data), v_secured_data_dec))) {
      setverdict(fail);
      stop;
    }
    setverdict(pass, "Decoding passed.");
      
  } // End of testcase tc_secured_message_unsecured
    
  testcase tc_secured_message_signed_1() runs on TCType system TCType {
    var template (value) EtsiTs103097Data v_secured_data;
    var EtsiTs103097Data v_secured_data_dec;
    var octetstring v_raw_payload_to_be_signed := 'CAFFEDECA0000001'O;
    var HashedId8 v_digest := '0000000000000000'O;
    var bitstring v_encMsg;
        
  v_secured_data := m_etsiTs103097Data_signed(
                                              m_signedData(
                                                           sha256,
                                                           m_toBeSignedData(
                                                                            m_signedDataPayload(
                                                                                                m_etsiTs103097Data_unsecured(v_raw_payload_to_be_signed)
                                                                                                ),
                                                                            m_headerInfo_gn(
                                                                                            -,
                                                                                            12345
                                                                                            )
                                                                            ),
                                                           { digest := v_digest },
                                                           m_signature_ecdsaNistP256(
                                                                                     m_ecdsaP256Signature(
                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                     '08B2030104020A0D010C0105C0F80BB1460239348D17405C1A845151D4061200'O
                                                                                                                                     ),
                                                                                                          '2617CF4E6B25097F03F502AD0C6F2F125974700D31A60FD1EF12040E4D8231AB'O
                                                                                                          )
                                                                                     )
                                                           )
                                              );
    log("v_secured_data = ", v_secured_data);
  v_encMsg := encvalue(valueof(v_secured_data));
    setverdict(pass, "Encoding passed.");
    if (decvalue(v_encMsg, v_secured_data_dec) != 0) {
      setverdict(fail);
      stop;
    } else if (not(match(valueof(v_secured_data), v_secured_data_dec))) {
      setverdict(fail);
      stop;
    }
    setverdict(pass, "Decoding passed.");
  } // End of testcase tc_secured_message_signed_1
    
  testcase tc_secured_message_signed_2() runs on TCType system TCType {
    var template (value) EtsiTs103097Data v_secured_data;
    var EtsiTs103097Data v_secured_data_dec;
    var template (value) GeoNetworkingPdu v_gnPacket;
    var octetstring v_raw_payload_to_be_signed;
    var HashedId8 v_digest := '0000000000000000'O;
    var bitstring v_encMsg;

  v_gnPacket := m_geoNwPdu(
                           m_geoNwShbPacket(
                                            LongPosVector: {
                                            gnAddr := {
                                              typeOfAddress := e_manual,
                                              stationType := e_roadSideUnit,
                                              stationCountryCode := 33,
                                              mid := 'a4fedecabeef'O
                                            },
                                              timestamp_ := 123456,
                                              latitude := 4856,
                                              longitude := 675,
                                              pai := '1'B,
                                              speed := 55,
                                              heading := 9876
                                              }
                                            )
                           );
  v_raw_payload_to_be_signed := bit2oct(encvalue(m_toBeSignedData(
                                                                    m_signedDataPayload(
                                                                                        m_etsiTs103097Data_unsecured(v_raw_payload_to_be_signed)
                                                                                        ),
                                                                    m_headerInfo_gn(
                                                                                    -,
                                                                                    12345
                                                                                    )
                                                                    )));
  v_secured_data := m_etsiTs103097Data_signed(
                                              m_signedData(
                                                           sha256,
                                                           m_toBeSignedData(
                                                                    m_signedDataPayload(
                                                                                        m_etsiTs103097Data_unsecured(v_raw_payload_to_be_signed)
                                                                                        ),
                                                                    m_headerInfo_gn(
                                                                                    -,
                                                                                    12345
                                                                                    )
                                                                    ),
                                                           { digest := v_digest },
                                                           m_signature_ecdsaNistP256(
                                                                                     m_ecdsaP256Signature(
                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                     '08B2030104020A0D010C0105C0F80BB1460239348D17405C1A845151D4061200'O
                                                                                                                                     ),
                                                                                                          '2617CF4E6B25097F03F502AD0C6F2F125974700D31A60FD1EF12040E4D8231AB'O
                                                                                                          )
                                                                                     )
                                                           )
                                              );
    log("v_secured_data = ", v_secured_data);
  v_encMsg := encvalue(valueof(v_secured_data));
    setverdict(pass, "Encoding passed.");
    if (decvalue(v_encMsg, v_secured_data_dec) != 0) {
      setverdict(fail);
      stop;
    } else if (not(match(valueof(v_secured_data), v_secured_data_dec))) {
      setverdict(fail);
      stop;
    }
    setverdict(pass, "Decoding passed.");
  } // End of testcase tc_secured_message_signed_2
    
  control {
    execute(tc_ssp_cam_1());
    execute(tc_ssp_cam_2());
    execute(tc_ssp_denm());
    execute(tc_secured_message_unsecured());
    execute(tc_secured_message_signed_1());
    execute(tc_secured_message_signed_2());
  }
    
} // End of module TestCodec_SecuredMessages
