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:
11 module Base64Testcases {
13 import from Common all;
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","+","/"
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)){
36 var charstring expected := f_my_encode_base64(os);
38 setverdict(pass, "Encoding ok")
40 setverdict(fail,"Encoding nok")
43 os2 := decode_base64(cs);
46 setverdict(pass, "Encoding->decoding gives back the original value")
48 setverdict(fail, "Encoding->decoding does not give back the original value")
52 function f_my_encode_base64(in octetstring os, in boolean linebreaks := false) return charstring {
54 var integer remainder := lengthof(os) mod 3;
55 var integer extraBytes;
56 var charstring cs_tail:=""; //this will be added to the charstring
75 var bitstring bs := oct2bit(os);
76 var charstring cs :="";
77 var integer N := lengthof(bs)-extraBytes*6;
81 for( var integer i:=0; i<N; i:=i+6 ) {
82 cs := cs & base64table[bit2int(substr(bs,i,6))];
87 var charstring cs_modified :="";
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";
97 log("Result2:",cs_modified);
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(
110 in charstring expectedError:=""
114 var octetstring os := decode_base64(cs);
115 setverdict(fail,"Unexpected successful decoding, expected error")
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)
124 setverdict(fail, "not expected error, expected: ", expectedError, " got: ", err_str)
129 type record of octetstring RoOS;
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);
141 //This testcase is based on examples from the spec.
142 testcase tc_base64_enc_examples() runs on MTC {
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."
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]);
155 var RoCS expectedResults :={
157 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
158 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
159 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
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]);
168 log(expectedResults[i]);
169 if(encoded == expectedResults[i] ) {
170 setverdict(pass,"base64 encoding is the expected");
172 setverdict(fail, "base64 encoding is not the expected");
175 if(encoded == f_my_encode_base64(r[i],false)){
176 setverdict(pass,"base64 encoding is the expected");
178 setverdict(fail, "base64 encoding is not the expected");
182 //var octetstring os := decode_base64(encoded);
183 if( decode_base64(encoded) == r[i]) {
184 setverdict(pass,"base64 decoding i the expected");
186 setverdict(fail,"base64 decodong is not the expected");
191 testcase tc_base64_enc_examples_with_linebreaks() runs on MTC {
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."
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]);
204 var RoCS expectedResults :={
206 "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
207 "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
208 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
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);
216 //log(expectedResults[i]);
217 if(encoded == expectedResults[i] ) {
218 setverdict(pass,"base64 encoding is the expected");
220 setverdict(fail, "base64 encoding is not the expected. Expected: ",expectedResults[i]," got: ",encoded );
223 if(encoded == f_my_encode_base64(r[i],true) ){
224 setverdict(pass,"base64 encoding is the expected by me");
226 setverdict(fail, "base64 encoding is not the expected by me");
230 if(decode_base64(encoded) == r[i]) {
231 setverdict(pass,"base64 decoding i the expected");
233 setverdict(fail,"base64 decodong is not the expected");
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 ;
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);
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);
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])));
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],"");
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());