e334b2bf6f562ab0642bc2f13d2c50a4914b73c0
[deliverable/titan.core.git] / regression_test / ttcn2json / Shell.ttcn
1 /******************************************************************************
2 * Copyright (c) 2000-2014 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 ******************************************************************************/
8
9 module Shell
10 {
11
12 import from PIPEasp_Types all;
13 import from PIPEasp_PortType all;
14 import from PIPEasp_Templates all;
15
16
17 modulepar float tsp_shellCmdTimeout :=1.0;
18
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;
36 }
37
38 type component Shell_CT extends PIPE_CT {
39 var boolean v_initialized:=false;
40 }
41
42 type component mtc_CT {}
43
44 //=========================================================================
45 // Constants
46 //=========================================================================
47
48 const integer c_shell_successWithoutWarningAndError:=0;
49 const integer c_shell_success := 0;
50 const integer c_shell_successWithWarning:=1; //temp until licence is solved
51 const integer c_shell_error:=256;
52 const integer c_shell_error_noSuchFileOrDirectory:=512;
53
54 //Expected and accepted diffs:
55 // Line "Copyright Ericsson AB 2013" - 4 diffs
56 // Line "XSD to TTCN-3 Translator version:" - 4 diffs
57 // Line "File" - 4 diffs
58 // Line "Updated: " - 4 diffs
59 // Line "module www_" - 4 diffs
60 // Line "ETSI ES 201 873-9 V4.1.2" - 2 diffs ???? << Fix it !
61 // Line "variant \"namespace as" - 4 diffs
62 // Script counts the strings "\n" thus N different lines mean N-1 numOfDiff
63 //Possible values: 19,21,23,25 but 21 and 25 should be eliminated!
64
65 const integer c_numOfDiff_headerAndModuleName := 19;
66 const integer c_numOfDiff_headerModNameAndNamespace := 23;
67 const integer c_numOfDiff_headerModNameAndImport := 23;
68
69 function f_countDelimiters(in charstring pl_string, in charstring pl_delimiter, inout integer pl_counter) {
70 pl_counter:=0;
71 var integer pos:=0;
72 var integer vl_size:=lengthof(pl_string);
73 var integer vl_delimsize:=lengthof(pl_delimiter);
74 while(pos<vl_size) {
75 if( substr(pl_string,pos,vl_delimsize)==pl_delimiter) { pl_counter:=pl_counter+1}
76 pos:=pos+1;
77 }
78 }//f_
79
80 //=========================================================================
81 // f_compareFiles
82 //=========================================================================
83 //pl_diffLimit: upper limit of acceptable diff lines. 4 means one acceptable difference
84 function f_compareFiles(in charstring pl_file1, in charstring pl_file2, in integer pl_diffLimit) runs on Shell_CT {
85 var integer vl_expectedResult:=0
86 if(pl_diffLimit>0) { vl_expectedResult:=256; }
87 var boolean vl_success:=false;
88 f_shell_command("diff -w " & pl_file1 & " " & pl_file2,"",vl_expectedResult,vl_success);
89
90 if(v_ASP_PResult.code==0)
91 {
92 setverdict(pass);
93 }
94 else if(v_ASP_PResult.code==256) {
95 var integer vl_counter:=0;
96 f_countDelimiters(v_ASP_PResult.stdout,"\n",vl_counter);
97 log("Counted lines: ",vl_counter, " diffLimit: ", pl_diffLimit)
98 if(vl_counter>pl_diffLimit) {
99 setverdict(fail);
100 }
101 } else { //e.g 512: No such file or directory
102 log("Wrong result code: ",v_ASP_PResult.code, " Expected result code: ", vl_expectedResult)
103 setverdict(fail);
104 }
105 }//f_
106
107
108 //********* SHELL Functions ***********************
109
110 //=========================================================================
111 // f_shell_init
112 //=========================================================================
113 function f_shell_init() runs on Shell_CT {
114 if(v_initialized) { return; }
115 map(self:PIPE_PCO, system:PIPE_PCO);
116 v_initialized:=true;
117 }
118
119 //=========================================================================
120 // f_shell_cleanup
121 //=========================================================================
122 function f_shell_cleanup() runs on Shell_CT {
123 if(not v_initialized) { return; }
124 unmap(self:PIPE_PCO, system:PIPE_PCO);
125 v_initialized:=false;
126 }
127 //=========================================================================
128 // f_setverdictfromBool
129 //=========================================================================
130 function f_setverdictfromBool(in boolean pl_result, in boolean pl_expected_result:=true) {
131 if(pl_result==pl_expected_result) {
132 setverdict(pass);
133 }else{
134 setverdict(fail);
135 }
136 return;
137 }
138 //=========================================================================
139 // f_shell_validateXml
140 // Compares pl_xmlFileContent (e.g encoding result) against pl_xsdFileName
141 //=========================================================================
142 function f_shell_validateXml(in octetstring pl_xmlFileContent, in charstring pl_xsdFileName, in integer pl_expected_result, inout boolean pl_success)
143 runs on Shell_CT
144 {
145 f_shell_command( "xmllint --noout --schema " & pl_xsdFileName & " - ",oct2char(pl_xmlFileContent), pl_expected_result, pl_success);
146 }
147
148
149 //=========================================================================
150 // f_shell_command
151 //=========================================================================
152 function f_shell_command(in charstring pl_command, in charstring pl_stdin, in integer pl_expected_result, inout boolean pl_success)
153 runs on Shell_CT
154 {
155 f_shell_init();
156
157 var integer vl_expectedCode:=-1;
158 if(pl_expected_result==c_shell_successWithoutWarningAndError or
159 pl_expected_result==c_shell_successWithWarning) {
160 vl_expectedCode:=0
161 } else {
162 vl_expectedCode:= pl_expected_result;
163 }
164
165 log("Running: ", pl_command);
166 PIPE_PCO.send(t_PExecute(pl_command,pl_stdin));
167
168 timer t:=tsp_shellCmdTimeout;
169 t.start;
170 pl_success:=false;
171
172 alt {
173
174 [] PIPE_PCO.receive(t_PResult(?, ?, ?)) -> value v_ASP_PResult {
175 log("PResult msg received: ", v_ASP_PResult);
176
177 if(v_ASP_PResult.code==vl_expectedCode ) {
178 var charstring vl_pattern:="";
179 select(pl_expected_result) {
180 case(c_shell_successWithWarning) {
181 vl_pattern:="*(Warning|WARNING|warning)*";
182 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
183 log("That is an expected Warning!")
184 pl_success:=true;
185 } else {
186 log("No Warning in the stderr string but expected");
187 pl_success:=false;
188 }
189 }
190 case(c_shell_successWithoutWarningAndError) {
191 vl_pattern:="*(Error|ERROR|error)*";
192 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
193 log("That is an unexpected Error!")
194 pl_success:=false;
195 } else {
196 log("No Error in the stderr string");
197 pl_success:=true;
198 }
199 vl_pattern:="*(Warning|WARNING)*";
200 if(regexp(v_ASP_PResult.stderr,vl_pattern,0)!=""){
201 log("That is an unexpected Warning!")
202 pl_success:=false;
203 } else {
204 log("No Warning in the stderr string");
205 pl_success:=true;
206 }
207 }//case
208 case(c_shell_error) {
209 log("Command returned with ERROR as expected");
210 pl_success:=true;
211 }
212 case(c_shell_error_noSuchFileOrDirectory) {
213 log("Command returned with No such file or directory as expected");
214 pl_success:=true;
215 }
216 case else {
217 log("Other case");
218 pl_success:=false;
219 }
220 }//select
221 } else {
222 log("The result code(", v_ASP_PResult.code, ") is not the expected(", vl_expectedCode, ")");
223 pl_success:=false;
224 }//if
225 }
226 [] t.timeout {
227 pl_success:=false;
228 }
229 }//alt
230
231 f_shell_cleanup();
232 return;
233 }//f_shell_command
234 //=========================================================================
235 // Name: f_shellCommandWithVerdict
236 // Description: sets verdict for pass, if the command execution returns with the expected value
237 //=========================================================================
238 function f_shellCommandWithVerdict(in charstring pl_command, in charstring pl_stdin, in integer pl_expected_result) runs on Shell_CT {
239 var boolean vl_success:=false;
240 f_shell_command(pl_command, pl_stdin, pl_expected_result, vl_success);
241 f_setverdictfromBool(vl_success)
242 }
243
244 } // end of module
This page took 0.04504 seconds and 4 git commands to generate.