module ItsGencert_TestCases {
  
  // 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_Functions all;
  import from LibItsSecurity_Pixits all;
  
  // AtsGenCert
  import from ItsGenCert_TypeAndValues all;
  import from ItsGenCert_Functions all;
  import from ItsGenCert_TestSystem all;
  import from ItsGenCert_Pics all;
  
  testcase TC_GEN_CERT() runs on TCType system TCType {
    var certificate_details_list v_certificate_details_list;
    var integer v_idx := 0;
  
    for (var integer v_counter := 0; v_counter < lengthof(PICS_CERTFICATES); v_counter := v_counter + 1) {
      var certificate_details v_details;
      var template (value) EtsiTs103097Certificate v_certificate;
      var certificate_params v_certificate_params;
      
      // Setup
      v_certificate_params := PICS_CERTFICATES[v_counter];
      v_details := { "", {}, ''O, ''O, ''O, ''O, '0000000000000000'O, '0000000000000000'O };
      v_certificate_details_list[v_idx] := v_details;
      v_details.certificate_id := v_certificate_params.certificate_id;
      
      // Generate Private/Public keys for the certificate
      if (f_generate_signing_keys(v_certificate_params, v_details) == -1) {
        setverdict(fail, "Unsupported curve");
        stop;
      }
      
      // Build the certificate templates
      if (f_fill_certificate(v_certificate_params, v_details) == -1) {
        setverdict(fail, "Failed to build the certificate templates");
        stop;
      }
      
      // Generate the certificate
      if (f_generate_certificate(v_certificate_params, v_details) == -1) {
        setverdict(fail, "Failed to generate the certificate");
        stop;
      }
      
      // Fianlyse certificate
      if (f_finalyse_certificate(v_certificate_params, v_certificate_details_list, v_details) == -1) {
        setverdict(fail, "Failed to finalyse the certificate");
        stop;
      }
      
      v_idx := v_idx + 1;
    } // End of 'for' statement
    
    log("v_certificate_details_list = ", v_certificate_details_list);
    
    // Store them
    if (f_store_certificates(v_certificate_details_list) == -1) {
      setverdict(fail, "Failed to finalyse the certificate");
      stop;
    } else {
      setverdict(pass);
    }
    
  } // End of testcase TC_GEN_CERT
  
  testcase TC_VALIDATE_CERT() runs on TCType system TCType {

    for (var integer v_counter := 0; v_counter < lengthof(PICS_CERTFICATES); v_counter := v_counter + 1) {
      var EtsiTs103097Certificate v_certificate;

      fx_loadCertificates(PX_CERTIFICATE_POOL_PATH, "certificates");
      if (f_readCertificate(PICS_CERTFICATES[v_counter].certificate_id, v_certificate) == false) {
        setverdict(fail, "Failed to read certificate ", PICS_CERTFICATES[v_counter].certificate_id);
        stop;
      }
    } // End of 'for' statement

    setverdict(pass);
  } // End of testcase TC_VALIDATE_CERT
  
  control {
    execute(TC_GEN_CERT(), 1.0);
    execute(TC_VALIDATE_CERT(), 1.0);
  } // End of 'control' statement
  
} // End of module ItsGencert_TestCases
