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