LibMec_Functions.ttcn 15.2 KB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
1
2
3
module LibMec_Functions {
  
  // Libcommon
4
  import from LibCommon_BasicTypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
5
6
  import from LibCommon_Sync all;
  
7
8
  // LibMec
  import from LibMec_Pixits all;
YannGarcia's avatar
YannGarcia committed
9
  import from LibMec_Pics all;
10
11
12
13
14
15
  
   // LibMec_UEidentityAPI
  import from UEidentityAPI_TypesAndValues all;
  import from UEidentityAPI_Templates all;
  import from UEidentityAPI_Pixits all;
  
Yann Garcia's avatar
Yann Garcia committed
16
17
  // LibHttp
  import from LibItsHttp_TypesAndValues all;
18
  import from LibItsHttp_JsonMessageBodyTypes all;
Yann Garcia's avatar
Yann Garcia committed
19
20
  import from LibItsHttp_Templates all;
  import from LibItsHttp_TestSystem all;
21
22
  import from LibItsHttp_Templates all;
  import from LibItsHttp_JsonTemplates all;
23
  import from LibItsHttp_Pics all;
Yann Garcia's avatar
Yann Garcia committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
  group preambles {
    
    /**
     * @desc Setup HTTP pprotocol port
     */
    function f_cf_01_http_up() runs on HttpComponent {
      
      // Map ports
      map(self:httpPort, system:httpPort);
      
      // Connect
      f_connect4SelfOrClientSync();
      
      activate(a_cf_01_http_down());
      activate(a_default_requests());
      activate(a_default_responses());
      
    } // End of function f_cf_01_http_up
    
YannGarcia's avatar
YannGarcia committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    /**
     * @desc Setup HTTP pprotocol port
     */
    function f_cf_01_http_notif_up() runs on HttpComponent {
      
      // Map ports
      map(self:httpPort_notif, system:httpPort_notif);
      
      // Connect
      f_connect4SelfOrClientSync();
      
      activate(a_cf_01_http_notif_down());
      activate(a_default_requests());
      activate(a_default_responses());
      
    } // End of function f_cf_01_http_notif_up
    
Yann Garcia's avatar
Yann Garcia committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  } // End of group preambles
  
  group postambles {
    
    /**
     * @desc Shutdown HTTP pprotocol port
     */
    function f_cf_01_http_down() runs on HttpComponent {
      
      // Unmap ports
      unmap(self:httpPort, system:httpPort);
      
      // Disconnect ports
      f_disconnect4SelfOrClientSync();
      
      deactivate;
    } // End of function f_cf_01_http_down
    
YannGarcia's avatar
YannGarcia committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    /**
     * @desc Shutdown HTTP pprotocol port
     */
    function f_cf_01_http_notif_down() runs on HttpComponent {
      
      // Unmap ports
      unmap(self:httpPort_notif, system:httpPort_notif);
      
      // Disconnect ports
      f_disconnect4SelfOrClientSync();
      
      deactivate;
    } // End of function f_cf_01_http_notif_down
    
Yann Garcia's avatar
Yann Garcia committed
93
94
95
96
97
98
99
100
101
102
103
    /**
     * @desc Default handling cf01 de-initialisation.
     */
    altstep a_cf_01_http_down() runs on HttpComponent {
      [] a_shutdown() {
        f_cf_01_http_down();
        log("*** a_cf_01_http_down: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
    } // End of altstep a_cf_01_http_down
    
YannGarcia's avatar
YannGarcia committed
104
105
106
107
108
109
110
111
112
113
114
    /**
     * @desc Default handling cf01 de-initialisation.
     */
    altstep a_cf_01_http_notif_down() runs on HttpComponent {
      [] a_shutdown() {
        f_cf_01_http_notif_down();
        log("*** a_cf_01_http_notif_down: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
    } // End of altstep a_cf_01_http_notif_down
    
Yann Garcia's avatar
Yann Garcia committed
115
116
  } // End of group postambles
  
117
118
119
120
121
122
123
124
125
126
  group external_functions {
    
    /**
     * @desc    Gets the current time since 01/01/1970 in UTC format
     * @return  The current time since 01/01/1970 in UTC format
     */
    external function fx_get_current_time_utc() return UInt64;
    
  } // End of group external_functions
  
127
128
  group helpers {
    
129
130
131
132
133
134
135
136
    /**
     * @desc Check HTTP response headers
     * @param p_headers The HTTP headers
     * @param p_value the expected va;ue
     * @return true on success, false otherwise
     */
    function f_check_headers(
                             in HeaderLines p_headers,
YannGarcia's avatar
YannGarcia committed
137
                             in charstring p_header_name := "Location",
138
139
140
141
142
143
                             in template (present) charstring p_value := ?
    ) return boolean {
      // Local variables
      var boolean v_header_matched := false;
      
      for (var integer v_idx := 0; v_idx < lengthof(p_headers); v_idx := v_idx + 1) {
YannGarcia's avatar
YannGarcia committed
144
        if (p_headers[v_idx].header_name == p_header_name) {
145
146
147
148
149
150
151
152
153
154
          if (match(p_headers[v_idx].header_value[0], p_value) == true) { 
            v_header_matched := true;
          } // else, nothing to do
          break;
        }
      } // End of 'for' statement
      
      return v_header_matched;
    }
    
155
156
    /**
     * @desc Check if the user is registered or not
157
158
     * @param p_ue_identity_tag The UE identity tag
     * @param p_headers The HTTP headers
159
160
161
162
163
164
165
166
167
     * @return 0 if unregistered, 1 if registered, -1 otherwise
     */
    function f_check_user_register_state(
                                         in UeIdentityTag p_ue_identity_tag := PX_UE_IDENTITY_TAG,
                                         in HeaderLines p_headers
    ) runs on HttpComponent return integer {
      var HttpMessage v_response;
      var integer v_result := -1;
      
168
      // Send request
169
170
171
      httpPort.send(
                    m_http_request(
                                   m_http_request_get(
YannGarcia's avatar
YannGarcia committed
172
                                                      "/" & PICS_ROOT_API & PX_ME_APP_Q_UE_IDENTITY_ID_URI & oct2char(unichar2oct(PX_APP_INSTANCE_ID, "UTF-8")) & "/ue_identity_tag_info?ueIdentityTag=" & oct2char(unichar2oct(p_ue_identity_tag, "UTF-8")),
173
174
175
176
                                                      p_headers
                                                      )
                                   )
                    );
177
      // Await for the response
178
179
180
181
182
183
184
185
186
187
      tc_ac.start;
      alt {
        [] httpPort.receive(
                            mw_http_response(
                                             mw_http_response_ok(
                                                                 mw_http_message_body_json(
                                                                                           mw_body_json_ue_identity_tag_info(
                                                                                                                             mw_ue_identity_tag_info(
                                                                                                                                                     { *, mw_ue_identity_tag_item(PX_UE_IDENTITY_TAG), * }
                                                                                                                                                     )))))) -> value v_response {
YannGarcia's avatar
YannGarcia committed
188
189
          tc_ac.stop;
          
190
191
          for (var integer v_i := 0; v_i < lengthof(v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags); v_i := v_i + 1) {
            if (v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].ueIdentityTag == p_ue_identity_tag) {
YannGarcia's avatar
YannGarcia committed
192
193
              //log("Processing ", v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].ueIdentityTag);
              //log("State      ", v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].state);
194
195
              if (v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].state == UNREGISTERED) {
                v_result := 0;
YannGarcia's avatar
YannGarcia committed
196
              } else if (v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].state == REGISTERED) {
197
                v_result := 1;
YannGarcia's avatar
YannGarcia committed
198
199
              } else if (v_response.response.body.json_body.ueIdentityTagInfo.ueIdentityTags[v_i].state == INVALID_STATE) {
                v_result := 2;
200
201
202
203
204
205
206
207
208
              }
              break;
            }
          }
        }
        [] tc_ac.timeout {
        }
      } // End of 'alt' statement
      
YannGarcia's avatar
YannGarcia committed
209
      //log("<<< f_check_user_register_state: v_result: ", v_result);
210
211
212
      return v_result;
    } // End of function f_check_user_register_state
    
YannGarcia's avatar
YannGarcia committed
213
214
215
216
    function f_get_current_time_utc() return UInt64 {
      return fx_get_current_time_utc();
    } // End of function f_get_current_time_utc
    
217
218
    function f_get_current_timestamp_utc() return UInt64 {
      return fx_get_current_time_utc();
YannGarcia's avatar
YannGarcia committed
219
    } // End of function f_get_current_timestamp_utc
220
    
221
222
  } // End of group helpers 
  
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  group registration {
    
    function f_register_for_notification(
                                         in charstring p_uri,
                                         in HeaderLines p_headers,
                                         in template (value) JsonBody p_json_body,
                                         out JsonBody p_notification
    ) runs on HttpComponent return integer {
      var integer v_result := -1;
      var HttpMessage v_response;
      
      // Send the request
      httpPort.send(
                    m_http_request(
                                   m_http_request_get(
                                                      p_uri,
                                                      p_headers,
                                                      m_http_message_body_json(p_json_body)
                    )));
      // Await for the response
      tc_ac.start;
      alt {
        [] httpPort.receive(
                            mw_http_response(
                                             mw_http_response_201_created(
                                                                          mw_http_message_body_json
                            ))) -> value v_response {
          tc_ac.stop;
          
          p_notification := v_response.response.body.json_body;
          v_result := 0;
          log("*** f_register_for_notification: INFO: Regustration for notification succeed ***");
        }
        [] tc_ac.timeout {
          log("*** f_register_for_notification: INCONC: Expected message not received ***");
        }
      } // End of 'alt' statement
      
      //log("<<< f_register_for_notification: v_result: ", v_result);
      return v_result;
    } // End of fiunction f_register_for_notification

    function f_unregister_for_notification(
                                           in charstring p_uri,
                                           in HeaderLines p_headers
    ) runs on HttpComponent return integer {
      var HttpMessage v_response;
      var integer v_result := -1;
      
      // Send the request
      httpPort.send(
                    m_http_request(
                                   m_http_request_delete(
                                                         p_uri,
                                                         p_headers
                    )));
      // Await for the response
      tc_ac.start;
      alt {
        [] httpPort.receive(
                            mw_http_response(
                                             mw_http_response_ok
                                             )) -> value v_response {
          tc_ac.stop;
          
          v_result := 0;
          log("*** f_unregister_for_notification: INFO: Regustration for notification succeed ***");
        }
        [] tc_ac.timeout {
          log("*** f_unregister_for_notification: INCONC: Expected message not received ***");
        }
      } // End of 'alt' statement
      
      //log("<<< f_unregister_for_notification: v_result: ", v_result);
      return v_result;
    } // End of fiunction f_unregister_for_notification
    
  } // End of group registration 
  
Yann Garcia's avatar
Yann Garcia committed
302
303
304
  group altsteps {

    altstep a_default_requests() runs on HttpComponent {
Yann Garcia's avatar
Yann Garcia committed
305
306
      [] httpPort.receive(mw_http_request) {
        tc_ac.stop;
YannGarcia's avatar
YannGarcia committed
307
308
309
310
311
312
        log("*** " & testcasename() & ": FAIL: Server error: Receive request instead of response on httpPort ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [] httpPort_notif.receive(mw_http_request) {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: Receive unsollicited request on httpPort_notif ***");
Yann Garcia's avatar
Yann Garcia committed
313
314
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
315
316
317
    } // End of altstep a_default_requests
    
    altstep a_default_responses() runs on HttpComponent {
Yann Garcia's avatar
Yann Garcia committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
      var HttpMessage v_response;
      
      [] httpPort.receive(
                          mw_http_response(
                                           mw_http_response_ok(
                                                               mw_http_message_body_xml
                                                               ))) {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Unexpected XML response ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [] httpPort.receive(
                          mw_http_response(
                                           mw_http_response_ok(
                                                               mw_http_message_body_json
                                                               ))) {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Unexpected JSON response ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [] httpPort.receive(
                          mw_http_response(
                                           mw_http_response_ok(
                                                               mw_http_message_body_binary
                                                               ))) {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Unexpected binary response ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
347
348
349
350
351
352
353
354
      [PICS_USE_TOKEN_HEADER == false] httpPort.receive(
                                                        mw_http_response(
                                                                         mw_http_response_401_unauthorized
                                                                         ))  -> value v_response {
        tc_ac.stop;
        log("*** " & testcasename() & ": PASS: Received HTTP error message in case of non authenticated HTTP request ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
355
356
357
358
359
360
      [] httpPort.receive(
                          mw_http_response(
                                           mw_http_response_ko
                                           ))  -> value v_response {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: " & int2str(v_response.response.statuscode) & "/" & v_response.response.statustext & " ***");
361
362
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_success);
        // Do not repeat!
Yann Garcia's avatar
Yann Garcia committed
363
364
365
366
367
368
      }
      [] httpPort.receive(mw_http_response) -> value v_response {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: " & int2str(v_response.response.statuscode) & "/" & v_response.response.statustext & " ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
YannGarcia's avatar
YannGarcia committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
      [] httpPort.receive {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: Receive unsollicited message on httpPort ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [] httpPort_notif.receive(mw_http_response) -> value v_response {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: " & int2str(v_response.response.statuscode) & "/" & v_response.response.statustext & " on httpPort_notif ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [] httpPort_notif.receive {
        tc_ac.stop;
        log("*** " & testcasename() & ": FAIL: Server error: Receive unsollicited message on httpPort_notif ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
384
385
386
387
388
    } // End of altstep a_default_responses
    
  } // end of group altsteps
  
} // End of module LibMec_Functions