1 /******************************************************************************
2 * Copyright (c) 2000-2015 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
12 import from PIPEasp_Types all;
13 import from PIPEasp_PortType all;
14 import from PIPEasp_Templates all;
17 modulepar float tsp_shellCmdTimeout := 20.0;
19 type component PIPE_CT {
20 port PIPEasp_PT PIPE_PCO;
21 var ASP_PExecute v_ASP_PExecute;
22 var ASP_PResult v_ASP_PResult;
23 var ASP_PExecuteBinary v_ASP_PExecuteBinary;
24 var ASP_PResultBinary v_ASP_PResultBinary;
25 var ASP_PExecuteBackground v_ASP_PExecuteBackground;
26 var ASP_PStdin v_ASP_PStdin;
27 var ASP_PStdout v_ASP_PStdout;
28 var ASP_PStderr v_ASP_PStderr;
29 var ASP_PStdinBinary v_ASP_PStdinBinary;
30 var ASP_PStdoutBinary v_ASP_PStdoutBinary;
31 var ASP_PStderrBinary v_ASP_PStderrBinary;
32 var ASP_PKill v_ASP_PKill;
33 var ASP_PExit v_ASP_PExit;
34 var ASP_PLineMode v_ASP_PLineMode;
35 var ASP_PError v_ASP_PError;
38 type component Shell_CT extends PIPE_CT {
39 var boolean v_initialized:=false;
42 type component mtc_CT {}
44 //=========================================================================
46 //=========================================================================
48 const integer c_shell_successWithoutWarningAndError:=0;
49 const integer c_shell_success := 0;
50 const integer c_shell_successWithWarning:=1;
51 const integer c_shell_successWithoutError:=2;
52 const integer c_shell_error:=256;
53 const integer c_shell_error_noSuchFileOrDirectory:=512;
55 //Expected and accepted diffs:
56 // Line "Copyright Ericsson AB 2013" - 4 diffs
57 // Line "XSD to TTCN-3 Translator version:" - 4 diffs
58 // Line "File" - 4 diffs
59 // Line "Updated: " - 4 diffs
60 // Line "module www_" - 4 diffs
61 // Line "ETSI ES 201 873-9 V4.1.2" - 2 diffs ???? << Fix it !
62 // Line "variant \"namespace as" - 4 diffs
63 // Script counts the strings "\n" thus N different lines mean N-1 numOfDiff
64 //Possible values: 19,21,23,25 but 21 and 25 should be eliminated!
66 const integer c_numOfDiff_headerAndModuleName := 19;
67 const integer c_numOfDiff_headerModNameAndNamespace := 23;
68 const integer c_numOfDiff_headerModNameAndImport := 23;
70 function f_countDelimiters(in charstring pl_string, in charstring pl_delimiter, inout integer pl_counter) {
73 var integer vl_size:=lengthof(pl_string);
74 var integer vl_delimsize:=lengthof(pl_delimiter);
76 if( substr(pl_string,pos,vl_delimsize)==pl_delimiter) { pl_counter:=pl_counter+1}
81 //=========================================================================
83 //=========================================================================
84 //pl_diffLimit: upper limit of acceptable diff lines. 4 means one acceptable difference
85 function f_compareFiles(in charstring pl_file1, in charstring pl_file2, in integer pl_diffLimit) runs on Shell_CT {
86 var integer vl_expectedResult:=0
87 if(pl_diffLimit>0) { vl_expectedResult:=256; }
88 var boolean vl_success:=false;
89 f_shell_command("diff -w " & pl_file1 & " " & pl_file2,"",vl_expectedResult,vl_success);
91 if(v_ASP_PResult.code==0)
95 else if(v_ASP_PResult.code==256) {
96 var integer vl_counter:=0;
97 f_countDelimiters(v_ASP_PResult.stdout,"\n",vl_counter);
98 log("Counted lines: ",vl_counter, " diffLimit: ", pl_diffLimit)
99 if(vl_counter>pl_diffLimit) {
102 } else { //e.g 512: No such file or directory
103 log("Wrong result code: ",v_ASP_PResult.code, " Expected result code: ", vl_expectedResult)
109 //********* SHELL Functions ***********************
111 //=========================================================================
113 //=========================================================================
114 function f_shell_init() runs on Shell_CT {
115 if(v_initialized) { return; }
116 map(self:PIPE_PCO, system:PIPE_PCO);
120 //=========================================================================
122 //=========================================================================
123 function f_shell_cleanup() runs on Shell_CT {
124 if(not v_initialized) { return; }
125 unmap(self:PIPE_PCO, system:PIPE_PCO);
126 v_initialized:=false;
128 //=========================================================================
129 // f_setverdictfromBool
130 //=========================================================================
131 function f_setverdictfromBool(in boolean pl_result, in boolean pl_expected_result:=true) {
132 if(pl_result==pl_expected_result) {
139 //=========================================================================
140 // f_shell_validateXml
141 // Compares pl_xmlFileContent (e.g encoding result) against pl_xsdFileName
142 //=========================================================================
143 function f_shell_validateXml(in octetstring pl_xmlFileContent, in charstring pl_xsdFileName, in integer pl_expected_result, inout boolean pl_success)
146 f_shell_command( "xmllint --noout --schema " & pl_xsdFileName & " - ",oct2char(pl_xmlFileContent), pl_expected_result, pl_success);
150 //=========================================================================
152 //=========================================================================
153 function f_shell_command(in charstring pl_command, in charstring pl_stdin, in integer pl_expected_result, inout boolean pl_success)
158 var integer vl_expectedCode:=-1;
159 if(pl_expected_result==c_shell_successWithoutWarningAndError or
160 pl_expected_result==c_shell_successWithWarning or
161 pl_expected_result==c_shell_successWithoutError) {
164 vl_expectedCode:= pl_expected_result;
167 log("Running: ", pl_command);
168 PIPE_PCO.send(t_PExecute(pl_command,pl_stdin));
170 timer t:=tsp_shellCmdTimeout;
176 [] PIPE_PCO.receive(t_PResult(?, ?, ?)) -> value v_ASP_PResult {
177 log("PResult msg received: ", v_ASP_PResult);
179 if(v_ASP_PResult.code==vl_expectedCode ) {
180 var charstring vl_pattern:="";
181 select(pl_expected_result) {
182 case(c_shell_successWithWarning) {
183 vl_pattern:="*(Warning|WARNING|warning)*";
184 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
185 log("That is an expected Warning!")
188 log("No Warning in the stderr string but expected");
192 case(c_shell_successWithoutWarningAndError) {
193 vl_pattern:="*(Error|ERROR|error)*";
194 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
195 log("That is an unexpected Error!")
198 log("No Error in the stderr string");
201 vl_pattern:="*(Warning|WARNING)*";
202 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
203 log("That is an unexpected Warning!")
206 log("No Warning in the stderr string");
210 case(c_shell_successWithoutError) {
211 vl_pattern:="*(Error|ERROR|error)*";
212 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
213 log("That is an unexpected Error!")
216 log("No Error in the stderr string");
220 case(c_shell_error) {
221 log("Command returned with ERROR as expected");
224 case(c_shell_error_noSuchFileOrDirectory) {
225 log("Command returned with No such file or directory as expected");
234 log("The result code(", v_ASP_PResult.code, ") is not the expected(", vl_expectedCode, ")");
246 //=========================================================================
247 // Name: f_shellCommandWithVerdict
248 // Description: sets verdict for pass, if the command execution returns with the expected value
249 //=========================================================================
250 function f_shellCommandWithVerdict(in charstring pl_command, in charstring pl_stdin, in integer pl_expected_result) runs on Shell_CT {
251 var boolean vl_success:=false;
252 f_shell_command(pl_command, pl_stdin, pl_expected_result, vl_success);
253 f_setverdictfromBool(vl_success)