/******************************************************************************/
// @copyright   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.
//   Trademark 2010, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC).
//   All rights reserved.
// @version: IWD_10wk37
// $Date: 2010-08-18 12:20:57 +0200 (Wed, 18 Aug 2010) $
// $Rev: 3692 $
/******************************************************************************/
module PDCP_Security {
//=========================================================================
// Import Part
//=========================================================================

  import from EUTRA_CellInfo all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_LoopBack all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_Component all;
  import from EUTRA_SecuritySteps  all;
  import from EUTRA_ConfigurationSteps all;
  import from L2_CommonTemplates all;
  import from EUTRA_RRC_Templates all;// R5-101113
  import from EUTRA_SRB_Templates all;// R5-101113
  //import from EUTRA_CommonDefs all;// R5-101113
  import from EUTRA_DRB_Templates all;

  //=========================================================================
  // Test case main functions
  //=========================================================================

  // Functions for Test cases
  group Local_Functions {

  /*
   * @desc      Generic function suitable as the test body for tests 7.3.3.1 or 7.3.3.3
   * @param     p_CellId
   * @param     p_AS_CipheringAlgorithm
   * @status    APPROVED
   */
  function fl_Test_7_3_3_1Or7_3_3_3 (CellId_Type p_CellId,
                                     CipheringAlgorithm_Type p_AS_CipheringAlgorithm) runs on EUTRA_PTC
  {
    ///////////////////////////////////////////////////////////////////////////////////
    //      PREAMBLE
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_Init (c1);
    f_EUTRA_CellConfig_Def (eutra_Cell1);
    f_EUTRA_AS_CipheringAlgorithm_Set (p_AS_CipheringAlgorithm); // set snow 3G or AES  as Ciphering algorithm.
    f_EUTRA_Preamble ( p_CellId,  STATE2_IDLEUPDATE  );       // creates cell and performs registration
    ///////////////////////////////////////////////////////////////////////////////////
    //      Test Body
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_TestBody_Set(true);

    //@siclog "Step 1" siclog@
    f_EUTRA_RbEst_Def(p_CellId);
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "UE has transmitted UE Capability Information with valid Ciphering protected");
    //@siclog "Steps 2 - 3" siclog@
    //fl_CheckUECapability(p_CellId)
    f_EUTRA_TestBody_Set(false);
    //test body ends

    ///////////////////////////////////////////////////////////////////////////////////
    //      Post Amble
    //////////////////////////////////////////////////////////////////////////////////

    //Switch off UE and expect a DETACH REQUEST message
    f_EUTRA_Postamble ( p_CellId, E2_CONNECTED );//@sic R5-103875 sic@
  };// end of fl_Test_7_3_3_1Or7_3_3_3

  /*
   * @desc      Generic function suitable as the test body for tests 7.3.3.2 or 7.3.3.4
   * @param     p_CellId
   * @param     p_AS_CipheringAlgorithm
   * @status    APPROVED
   */
  function fl_TC_7_3_3_2_TC_7_3_3_4 (CellId_Type p_CellId,
                                     CipheringAlgorithm_Type p_AS_CipheringAlgorithm)runs on EUTRA_PTC
  {
    timer t_WaitTimer := 5.0;              // Local wait Timer
    ///////////////////////////////////////////////////////////////////////////////////
    //      PREAMBLE
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_Init (c1);
    f_EUTRA_CellConfig_Def (eutra_Cell1);
    f_EUTRA_AS_CipheringAlgorithm_Set (p_AS_CipheringAlgorithm); // set snow 3G or AES as Ciphering algorithm.
    f_EUTRA_Preamble ( p_CellId, STATE2A_TESTLOOP_ModeA );
    // creates cell and performs registration
    f_EUTRA_LoopBackActivation_State4_Def(eutra_Cell1, cs_UE_TestLoopModeA_LB_Setup_NoScaling );
    ///////////////////////////////////////////////////////////////////////////////////
    //      Test Body
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_TestBody_Set(true,layer2); // @sic R5-101050.

    //@siclog "Step 1" siclog@
    DRB.send (cas_DRB_COMMON_REQ_PDCP_SDU (p_CellId,tsc_RbId_DRB1, cs_TimingInfo_Now, cs_PDCP_SDUList_1 (crs_PDCP_SDU_16B)));
    t_WaitTimer.start;
    //@siclog "Step 2" siclog@
    // Proper application of Ciphering is implicitly tested.
    alt {
      [] DRB.receive (car_DRB_COMMON_IND_PDCP_SDUList (p_CellId,tsc_RbId_DRB1, ?, cr_PDCP_SDUList_1(crs_PDCP_SDU_16B)))
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, " UE has loop backed Data");
          t_WaitTimer.stop;
        }
      [] t_WaitTimer.timeout
        {
          f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "UE is not sending loop back data");
        }
    };
    f_EUTRA_TestBody_Set(false);
    //test body ends

    ///////////////////////////////////////////////////////////////////////////////////
    //      Post Amble

    //////////////////////////////////////////////////////////////////////////////////

    //Switch off UE and expect a DETACH REQUEST message
    f_EUTRA_Postamble ( p_CellId, E3_TESTMODE );//@sic R5-103875 sic@

  }; // end of fl_TC_7_3_3_2_TC_7_3_3_4
   
  /*
   * @desc     Function to transmit UE Capability Enquiry message and receive UE Capability Information message
   * @param     p_CellId
   * @status    APPROVED
   */
   function fl_CheckUECapability (CellId_Type p_CellId) runs on EUTRA_PTC
   {

      //@sic R5-1001113
    //* step 2 Transmit UE Capability Enquiry message to ask for EUTRA Capability information
    timer t_Watchdog := 0.4;// 400 ms should be sufficient to receive HARQ ACK/NACK
    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId, cs_TimingInfo_Now, cs_508_UeCapabilityEnquiry ( tsc_RRC_TI_Def ) ) );
    t_Watchdog.start;
    //* step 3 Receive UE Capability Information message for EUTRA capabilities
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_508_UeCapabilityInformation ( tsc_RRC_TI_Def, cr_EutraCapInfo )));
    t_Watchdog.stop;

    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "UE has transmitted UE Capability Information with valid Integrity protected");
  }

  /*
   * @desc      Generic function suitable as the test body for tests 7.3.4.1 or 7.3.4.2
   * @param     p_CellId
   * @param     p_AS_IntegrityProtAlgorithm
   * @status    APPROVED
   */
  function fl_Test_7_3_4_1Or7_3_4_2 (CellId_Type p_CellId,
                                     IntegrityProtAlgorithm_Type p_AS_IntegrityProtAlgorithm) runs on EUTRA_PTC
  {
    ///////////////////////////////////////////////////////////////////////////////////
    //      PREAMBLE
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_Init (c1);
    f_EUTRA_CellConfig_Def (eutra_Cell1);
    f_EUTRA_AS_IntegrityAlgorithm_Set (p_AS_IntegrityProtAlgorithm); // set snow 3G or AES  as Ciphering algorithm.
    f_EUTRA_Preamble ( p_CellId,STATE2_IDLEUPDATE  );
    // creates cell and performs registration
    ///////////////////////////////////////////////////////////////////////////////////
    //      Test Body
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_TestBody_Set(true);

    //@siclog "Step 1" siclog@
    f_EUTRA_RbEst_Def(p_CellId);
    //@siclog "Steps 2 - 3" siclog@
    fl_CheckUECapability(p_CellId);
    f_EUTRA_TestBody_Set(false);
    //test body ends

    ///////////////////////////////////////////////////////////////////////////////////
    //      Post Amble
    //////////////////////////////////////////////////////////////////////////////////

    //Switch off UE and expect a DETACH REQUEST message
    f_EUTRA_Postamble ( p_CellId, E2_CONNECTED );//@sic R5-103875 sic@

  };// end of fl_Test_7_3_4_1Or7_3_4_2

  } // end of group  Local_Functions

  //==========================================================
  // L2/PDCP/7.3.3 PDCP Ciphering and Deciphering
  //==========================================================

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.3.1
   * @status    APPROVED
   */
  function f_TC_7_3_3_1_EUTRA ( ) runs on EUTRA_PTC
  { /* L2/PDCP/
       7.3.3.1 : Ciphering and Deciphering: Correct functionality of EPS AS encription algorithms (SNOW 3G)
    */
    fl_Test_7_3_3_1Or7_3_3_3(eutra_Cell1,eea1); // set snow 3G as Ciphering algorithm.
  };

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.3.2
   * @status    APPROVED
   */
  function f_TC_7_3_3_2_EUTRA ( ) runs on EUTRA_PTC
  { /* L2/PDCP/
       7.3.3.2 : Ciphering and Deciphering: Correct functionality of EPS AS encription algorithms (SNOW 3G)
    */
    fl_TC_7_3_3_2_TC_7_3_3_4 (eutra_Cell1, eea1); // set snow 3G as Ciphering algorithm.
  };

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.3.3
   * @status    APPROVED
   */
  function f_TC_7_3_3_3_EUTRA ( ) runs on EUTRA_PTC
  { /* L2/PDCP/
       7.3.3.3 : Ciphering and Deciphering: Correct functionality of EPS AS encription algorithms (AES)
    */
    fl_Test_7_3_3_1Or7_3_3_3(eutra_Cell1,eea2); // set AES as Ciphering algorithm.
  };

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.3.4
   * @status    APPROVED
   */
  function f_TC_7_3_3_4_EUTRA ( ) runs on EUTRA_PTC
  {  /* L2/PDCP/
        7.3.3.4 : Ciphering and Deciphering: Correct functionality of EPS AS encription algorithms (AES)
     */
    fl_TC_7_3_3_2_TC_7_3_3_4 (eutra_Cell1, eea2); // set AES as Ciphering algorithm.
  };

  //==========================================================
  // L2/PDCP/7.3.4 PDCP Integrity Protection
  //==========================================================
  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.4.1
   * @status    APPROVED
   */
  function f_TC_7_3_4_1_EUTRA ( ) runs on EUTRA_PTC
  { /* L2/PDCP/
       7.3.4.1 : Integrity protection: Correct functionality of EPS AS integrity algorithms (SNOW3G)
    */
    fl_Test_7_3_4_1Or7_3_4_2(eutra_Cell1, eia1); //set snow 3G as integrity algorithm.
  };

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.3.4.2
   * @status    APPROVED
   */
  function f_TC_7_3_4_2_EUTRA ( ) runs on EUTRA_PTC
  { /* L2/PDCP/
       7.3.4.2 : Integrity protection: Correct functionality of EPS AS integrity algorithms (AES)
    */
    fl_Test_7_3_4_1Or7_3_4_2(eutra_Cell1, eia2); //set AES as integrity algorithm.
  };
} // module
