Sync with 5.1.0
[deliverable/titan.core.git] / regression_test / ucharstrOper / Base64Testcases.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 // This module tests the following functions:
9 // encode_base64
10 // decode_base64
11 module Base64Testcases {
12
13 import from Common all;
14
15 type record of charstring RoCS;
16 const RoCS base64table := {
17 "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
18 "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
19 "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
20 "w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"
21 }
22
23 function f_base64_encdec_positivetest(octetstring os, boolean use_linebreaks := false) {
24 var charstring cs :=encode_base64(os);
25 var octetstring os2:=''O;
26 if(use_linebreaks==false) {
27 if(cs == encode_base64(os,use_linebreaks)){
28 setverdict(pass);
29 } else {
30 setverdict(fail);
31 }
32
33 }
34
35 log("Encoded:",cs)
36 var charstring expected := f_my_encode_base64(os);
37 if(cs == expected) {
38 setverdict(pass, "Encoding ok")
39 } else {
40 setverdict(fail,"Encoding nok")
41 }
42
43 os2 := decode_base64(cs);
44 log(os2);
45 if(os2 == os){
46 setverdict(pass, "Encoding->decoding gives back the original value")
47 } else {
48 setverdict(fail, "Encoding->decoding does not give back the original value")
49 }
50 }
51
52 function f_my_encode_base64(in octetstring os, in boolean linebreaks := false) return charstring {
53
54 var integer remainder := lengthof(os) mod 3;
55 var integer extraBytes;
56 var charstring cs_tail:=""; //this will be added to the charstring
57
58 select(remainder) {
59 case(0){
60 cs_tail:="";
61 extraBytes:=0;
62 }
63 case(1){
64 cs_tail := "==";
65 extraBytes:=2;
66 os := os & '0000'O;
67 }
68 case(2){
69 cs_tail := "=";
70 extraBytes := 1;
71 os := os & '00'O;
72 }
73 }
74
75 var bitstring bs := oct2bit(os);
76 var charstring cs :="";
77 var integer N := lengthof(bs)-extraBytes*6;
78 var charstring temp;
79 var integer tempi;
80
81 for( var integer i:=0; i<N; i:=i+6 ) {
82 cs := cs & base64table[bit2int(substr(bs,i,6))];
83 }
84 cs := cs & cs_tail;
85
86
87 var charstring cs_modified :="";
88 if(linebreaks==true){
89 //insert line breaks:
90 var integer len := lengthof(cs);
91 for(var integer i:=0; i<len; i:=i+1) {
92 cs_modified := cs_modified & cs[i];
93 if( ((i+1) mod 76) == 0) {
94 cs_modified := cs_modified & "\r\n";
95 }
96 }
97 log("Result2:",cs_modified);
98 return cs_modified;
99 } else {
100 log("Result: ",cs);
101 return cs;
102 }
103
104 }
105
106 //checks if the error is the expected.
107 //If expectedError is the empty string, the error message is not checked
108 function f_base64_decode_negative(
109 in charstring cs,
110 in charstring expectedError:=""
111 ) {
112
113 @try {
114 var octetstring os := decode_base64(cs);
115 setverdict(fail,"Unexpected successful decoding, expected error")
116 }
117 @catch(err_str) {
118 var template charstring expectedErrorTempl := pattern "*{expectedError}";
119 if(expectedError==""){
120 setverdict(pass,"Error, as expected: ",err_str)
121 } else if(match(err_str, expectedErrorTempl)) {
122 setverdict(pass,"expected error, expected: ", expectedError, " got: ", err_str)
123 } else {
124 setverdict(fail, "not expected error, expected: ", expectedError, " got: ", err_str)
125 }
126 }
127 }
128
129 type record of octetstring RoOS;
130
131 testcase tc_base64_encdec() runs on MTC {
132 var RoOS r := {'01'O, '0102'O, '010203'O, '0102030405'O}
133 var integer len:=lengthof(r);
134 for( var integer i:=0;i<len;i:=i+1) {
135 f_base64_encdec_positivetest(r[i],false);
136 f_base64_encdec_positivetest(r[i],true);
137 }
138
139 }
140
141 //This testcase is based on examples from the spec.
142 testcase tc_base64_enc_examples() runs on MTC {
143 var RoCS inputcs:= {
144 "Man",
145 "any carnal pleasure.",
146 "any carnal pleasure",
147 "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
148 }
149 var RoOS r := {char2oct("any carnal pleasure.")}
150 var integer lencs:= lengthof(inputcs);
151 for(var integer i:=0;i<lencs;i:=i+1){
152 r[i]:=char2oct(inputcs[i]);
153 }
154
155 var RoCS expectedResults :={
156 "TWFu",
157 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
158 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
159 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
160 }
161 //var RoOS r := {'01'O, '0102'O, '010203'O, '0102030405'O}
162 var integer len:=lengthof(r);
163 var charstring encoded;
164 for( var integer i:=0;i<len;i:=i+1) {
165 log(f_my_encode_base64(r[i]));
166 encoded := encode_base64(r[i]);
167 log(encoded);
168 log(expectedResults[i]);
169 if(encoded == expectedResults[i] ) {
170 setverdict(pass,"base64 encoding is the expected");
171 } else {
172 setverdict(fail, "base64 encoding is not the expected");
173 }
174
175 if(encoded == f_my_encode_base64(r[i],false)){
176 setverdict(pass,"base64 encoding is the expected");
177 } else {
178 setverdict(fail, "base64 encoding is not the expected");
179 }
180
181 //decoding:
182 //var octetstring os := decode_base64(encoded);
183 if( decode_base64(encoded) == r[i]) {
184 setverdict(pass,"base64 decoding i the expected");
185 } else {
186 setverdict(fail,"base64 decodong is not the expected");
187 }
188 }
189 }
190
191 testcase tc_base64_enc_examples_with_linebreaks() runs on MTC {
192 var RoCS inputcs:= {
193 "Man",
194 "any carnal pleasure.",
195 "any carnal pleasure",
196 "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
197 }
198 var RoOS r := {char2oct("any carnal pleasure.")}
199 var integer lencs:= lengthof(inputcs);
200 for(var integer i:=0;i<lencs;i:=i+1){
201 r[i]:=char2oct(inputcs[i]);
202 }
203
204 var RoCS expectedResults :={
205 "TWFu",
206 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
207 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
208 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
209 }
210 //var RoOS r := {'01'O, '0102'O, '010203'O, '0102030405'O}
211 var integer len:=lengthof(r);
212 var charstring encoded;
213 for( var integer i:=0;i<len;i:=i+1) {
214 encoded := encode_base64(r[i],true);
215 //log(encoded);
216 //log(expectedResults[i]);
217 if(encoded == expectedResults[i] ) {
218 setverdict(pass,"base64 encoding is the expected");
219 } else {
220 setverdict(fail, "base64 encoding is not the expected. Expected: ",expectedResults[i]," got: ",encoded );
221 }
222
223 if(encoded == f_my_encode_base64(r[i],true) ){
224 setverdict(pass,"base64 encoding is the expected by me");
225 } else {
226 setverdict(fail, "base64 encoding is not the expected by me");
227 }
228
229 //decoding:
230 if(decode_base64(encoded) == r[i]) {
231 setverdict(pass,"base64 decoding i the expected");
232 } else {
233 setverdict(fail,"base64 decodong is not the expected");
234 }
235 }
236 }
237
238
239 //This testcase is based on encoding-decoding of octetstrings.
240 //The octetstring is a concatenation of the first N integers converted to one length octetstring
241 testcase tc_base64_generated_strings_1() runs on MTC {
242 var octetstring os:=''O ;
243 var integer N:=256;
244 for( var integer i:=0; i<N; i:=i+1){
245 os := os & int2oct(i,1);
246 log("======================");
247 log("i: ",i," os: ",os);
248 f_base64_encdec_positivetest(os,false);
249 }
250
251 }
252
253 //This testcase is based on encoding-decoding of octetstrings.
254 //The octetstring is a concatenation of the first N integers converted to two length octetstring
255 testcase tc_base64_generated_strings_2() runs on MTC {
256 var octetstring os:=''O ;
257 var integer N:=256*256;
258 for( var integer i:=0; i<N; i:=i+254){
259 os := os & int2oct(i,2);
260 log("======================");
261 log("i: ",i," os: ",os);
262 f_base64_encdec_positivetest(os,false);
263 }
264
265 }
266
267
268 testcase tc_base64_decode_negative_with_checkingErrorMessage() runs on MTC {
269 var RoCS rofcs := { " ","!", "#","$","%","&","'","(",")","*",":", "<",">", "{", "}", "~" };
270 var integer len := lengthof(rofcs);
271 for(var integer i:=0;i<len;i:=i+1) {
272 f_base64_decode_negative(rofcs[i],"Error: Invalid character in Base64 encoded data: 0x"& oct2str(char2oct(rofcs[i])));
273 }
274 }
275
276 testcase tc_base64_decode_negative_without_checkingErrorMessage() runs on MTC {
277 var RoCS rofcs := { " Baba","Baba!", "ba#ba","bla$%bla","bla ","&","'","(",")","*",":", "<",">", "{", "}", "~" };
278 var integer len := lengthof(rofcs);
279 for(var integer i:=0;i<len;i:=i+1) {
280 f_base64_decode_negative(rofcs[i],"");
281 }
282 }
283 control {
284 execute(tc_base64_encdec());
285 execute(tc_base64_enc_examples());
286 execute(tc_base64_enc_examples_with_linebreaks());
287 execute(tc_base64_generated_strings_1());
288 execute(tc_base64_generated_strings_2());
289 execute(tc_base64_decode_negative_with_checkingErrorMessage());
290 execute(tc_base64_decode_negative_with_checkingErrorMessage());
291 }
292
293 }
This page took 0.037905 seconds and 5 git commands to generate.