Commit 7138b138 authored by balaskoa's avatar balaskoa
Browse files

JSON test have been moved into folder EncodeDecode + new tests have been added to JSON


Signed-off-by: default avatarbalaskoa <Jeno.Balasko@ericsson.com>
Change-Id: I2423c17c9f5f1e88c00cd99f2430a8ce4de646b2
parent 5e73c9d3
*.cc *.cc
*.hh *.hh
*.o
*.exe *.exe
*.log *.log
\ No newline at end of file
module JsonBasicASN1EncDecTest1 {
import from JsonBasicASN1Types all;
type component CT {}
//===== Encode ====
//x->bs
external function f_json_enc_abs2bs(in Asn1_bs p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
external function f_json_enc_abool2bs(in Asn1_bool p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
external function f_json_enc_abmpstr2bs(in Asn1_bmpstr p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
external function f_json_enc_anumber2bs(in Asn1_Number p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
//===
external function f_json_enc_ai2bs(in Asn1_i p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
external function f_json_enc_af2bs(in Asn1_f p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
external function f_json_enc_ao2bs(in Asn1_os p) return bitstring
with { extension "prototype(convert) encode(JSON)" };
// x->os
//external function f_json_enc_aos2os(in Asn1_os p) return octetstring
//with { extension "prototype(convert) encode(JSON)" };
//==== Decode ======
external function f_json_dec_bs2abs(in bitstring p ) return Asn1_bs
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2abool(in bitstring p ) return Asn1_bool
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2abmpstr(in bitstring p ) return Asn1_bmpstr
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2anumber(in bitstring p ) return Asn1_Number
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2ai(in bitstring p ) return Asn1_i
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2af(in bitstring p ) return Asn1_f
with { extension "prototype(convert) decode(JSON)" };
external function f_json_dec_bs2ao(in bitstring p ) return Asn1_os
with { extension "prototype(convert) decode(JSON)" };
//==== compare =====
function f_compare_bitstring(in bitstring pl_val, in bitstring pl_expected) {
if ( pl_val == pl_expected ){
setverdict(pass);
} else {
setverdict( fail, "expected: ", pl_expected, " got: ", pl_val)
}
} with { extension "transparent"}
//========================
//===== Testcases ========
//========================
//testcase tc_asn1_bs2bs_bug() runs on CT {
// var Asn1_bs x := '01'B, d;
// var bitstring encoded2 := encvalue(x, "", "JSON");
// var bitstring expected := '0010 0010 00110000 00110001 0010 0010'B;// "01"
// f_compare_bitstring(encoded2,expected);
//}
// " = ascii x22 = 0010 0010
// ' = ascii x27 = 0010 0111
// 0 = ascii x30 = 00110000
// 1 = ascii x31 = 00110001
//
testcase tc_asn1_bs2bs() runs on CT{
var Asn1_bs x := '01'B, d;
var bitstring encoded1 := f_json_enc_abs2bs(x);
log("encoded1:", encoded1);
var bitstring expected := '0010 0010 00110000 00110001 0010 0010'B;// "01"
// var bitstring encoded2 := encvalue(x, "", "JSON"); //semantic error: No coding rule specified for type `bitstring' TODO: this shall be accepted!!!
// log("encoded2:", encoded2);
f_compare_bitstring(encoded1,expected);
// f_compare_bitstring(encoded2,expected);
//dec:
d := f_json_dec_bs2abs(encoded1);
if ( d==x ){
setverdict(pass);
} else {
setverdict( fail, "expected: ", x, " got: ", d)
}
}
//t = ascii x74 = 01110100
//r = ascii x72 = 01110010
//u = ascii x75 = 01110101
//e = ascii x65 = 01100101
testcase tc_asn1_bool2bs() runs on CT {
var Asn1_bool x := true, d;
var bitstring encoded1 := f_json_enc_abool2bs(x);
log("encoded1:", encoded1);
var bitstring expected := '01110100 01110010 01110101 01100101'B; //true
// var bitstring encoded2 := encvalue(x); //semantic error: No coding rule specified for type `bitstring' TODO: this shall be accepted!!!
// log("encoded2:", encoded2);
f_compare_bitstring(encoded1,expected);
//dec:
d := f_json_dec_bs2abool(encoded1);
if ( d==x ){
setverdict(pass);
} else {
setverdict( fail, "expected: ", x, " got: ", d)
}
}
// 0 = ascii x30 = 00110000
// 1 = ascii x31 = 00110001
// 2 = ascii x32 = 00110010
testcase tc_asn1_i2bs() runs on CT {
var Asn1_i x := 12, y;
var bitstring encoded1 := f_json_enc_ai2bs(x);
log("encoded1:", encoded1);
var bitstring expected := '00110001 00110010'B
// var bitstring encoded2 := encvalue(x); //semantic error: No coding rule specified for type `integer' TODO: this shall be accepted!!!
// log("encoded2:", encoded2);
f_compare_bitstring(encoded1,expected);
//decode:
//y :=
}
control {
// execute(tc_asn1_bs2bs_bug());
execute( tc_asn1_i2bs());
// execute( tc_asn1_bs2bs()); //TODO: runtime fails in java
execute( tc_asn1_bool2bs());
}
} with { encode "JSON"}
JsonBasicASN1Types DEFINITIONS
AUTOMATIC TAGS ::=
BEGIN
IMPORTS ;
Asn1-bs ::= BIT STRING
Asn1-bool ::= BOOLEAN
Asn1-bmpstr ::= BMPString
Asn1-Number ::= CHOICE {
decimal INTEGER,
binary BIT STRING,
hexadecimal OCTET STRING
}
Asn1-genstr ::= GeneralString
Asn1-graphstr ::= GraphicString
Asn1-ia5str ::= IA5String
Asn1-i ::= INTEGER
Asn1-f ::= REAL
Asn1-null ::= NULL
Asn1-numstr ::= NumericString
Asn1-oid ::= OBJECT IDENTIFIER
Asn1-os ::= OCTET STRING
Asn1-pstr ::= PrintableString
Asn1-seqofI ::= SEQUENCE OF INTEGER
Asn1-setofI ::= SET OF INTEGER
Asn1-teletexstr ::= TeletexString
Asn1-utf8str ::= UTF8String
Asn1-videotexstr ::= VideotexString
Asn1-visiblestr ::= VisibleString
END
module JsonBasicAttributeTest {
type component CT {}
function f_compare_bitstring(in bitstring pl_val, in bitstring pl_expected) {
if ( pl_val == pl_expected ){
setverdict(pass);
} else {
setverdict( fail, "expected: ", pl_expected, " got: ", pl_val)
}
} with { extension "transparent"}
//=======================================
//============= Types ===================
//=======================================
//======= Record with one field =========
//no other attr than JSON
type record R0_json {
integer i optional
} with { encode "JSON" }
type record R1_json {
integer i optional
} with { encode "JSON"; variant(i) "JSON: omit as null"; }
type record R2_json {
integer i optional
} with { encode "JSON"; variant(i) "JSON: name as Integer"; }
type record R3_json {
integer i optional
} with { encode "JSON"; variant(i) "JSON: name as Integer"; variant(i) "JSON: omit as null";}
type record R4_json {
integer i optional
} with { encode "JSON"; variant "JSON: as value";}
//======= Record with more field =========
//==== Testcases ====
//No attribute
testcase tc_attr0_0() runs on CT {
var R0_json x := { i:=omit }
var bitstring expected := oct2bit(char2oct("{}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
testcase tc_attr0_1() runs on CT {
var R0_json x := { i:= 1 }
var bitstring expected := oct2bit(char2oct("{\"i\":1}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
//Attribute: omit as null
testcase tc_attr1_0() runs on CT {
var R1_json x := { i:=omit }
var bitstring expected := oct2bit(char2oct("{\"i\":null}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
testcase tc_attr1_1() runs on CT {
var R1_json x := { i:= 1 }
var bitstring expected := oct2bit(char2oct("{\"i\":1}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
//Attribute: name as
testcase tc_attr2_0() runs on CT {
var R2_json x := { i:=omit }
var bitstring expected := oct2bit(char2oct("{}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
testcase tc_attr2_1() runs on CT {
var R2_json x := { i:= 1 }
var bitstring expected := oct2bit(char2oct("{\"Integer\":1}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
//Attribute: name as & omit as null
testcase tc_attr3_0() runs on CT {
var R3_json x := { i:=omit }
var bitstring expected := oct2bit(char2oct("{\"Integer\":null}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
testcase tc_attr3_1() runs on CT {
var R3_json x := { i:= 1 }
var bitstring expected := oct2bit(char2oct("{\"Integer\":1}"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
//Attribute: as value & omit as null
testcase tc_attr4_0() runs on CT {
var R4_json x := { i:= omit }
var bitstring expected := oct2bit(char2oct("null"));
var bitstring encoded := encvalue(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
testcase tc_attr4_1() runs on CT {
var R4_json x := { i:= 1 }
var bitstring expected := oct2bit(char2oct("1"));
var bitstring encoded := encvalue(x);
log(x);
log(bit2oct(encoded));
f_compare_bitstring(expected,encoded);
}
control {
execute(tc_attr0_0());
execute(tc_attr0_1());
execute(tc_attr1_0());
execute(tc_attr1_1());
execute(tc_attr2_0());
execute(tc_attr2_1());
execute(tc_attr3_0());
execute(tc_attr3_1());
execute(tc_attr4_0());
execute(tc_attr4_1());
}
}
// The testcases checks if the encode and decode funcions run and checks the result.
// The return value of the encoding functions is octetstring
// Anytype is out of scope
// Status: cpp: compiled and run with 14 pass
// java 14 pass
module JsonBasicEncDecTest_os {
import from JsonBasicTypes all;
import from JsonEncDecFunctions all;
type component CT {}
function f_compare_octetstring(in octetstring pl_val, in octetstring pl_expected) {
if ( pl_val == pl_expected ){
setverdict(pass);
} else {
setverdict( fail, "expected:", pl_expected, " got: ", pl_val)
}
} with { extension "transparent"}
//========================
//===== Testcases ========
//========================
//JSON can represent four primitive types (strings, numbers, booleans, and null)
//and two structured types (objects and arrays).
//==== Numbers====
testcase tc_jsonBasic_encdec_os_integer() runs on CT {
const I_json x := 234;
var I_json y := 234, z;
var octetstring os;
var octetstring expected := char2oct("234");
log("expected:",expected);
os := f_json_enc_i2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_i2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2i(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
// //Whitespace test
// expected := char2oct(" 2 3 4 ");
// log(expected);
//
// //decode test:
// z := f_json_dec_os2i(expected);
// if ( y ==z ) {
// setverdict(pass);
// } else {
// setverdict(fail);
// }
}
testcase tc_jsonBasic_encdec_os_float() runs on CT {
const F_json x := 2.0;
var F_json y := 2.0, z;
var octetstring os;
//var octetstring expected := '00110010'B;
var octetstring expected := char2oct("2.000000");
log("expected:",expected);
os := f_json_enc_f2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_f2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2f(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
testcase tc_jsonBasic_encdec_os_enum() runs on CT {
const E_json x := first;
var E_json y := first, z;
var octetstring os;
//var octetstring expected := '00100010011001100110100101110010011100110111010000100010'B
var octetstring expected := char2oct("\"first\"");
log("expected:",expected);
os := f_json_enc_e2os(x);
log("os:",os);
f_compare_octetstring(os,expected);
os := f_json_enc_e2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2e(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
//=== Strings ====
//"A string is a sequence of zero or more Unicode characters" RFC 7159
testcase tc_jsonBasic_encdec_os_bitstring() runs on CT {
const BS_json x := '1'B;
var BS_json y := '1'B, z;
var octetstring os;
var octetstring expected := char2oct("\"1\"");
log("expected:",expected);
os := f_json_enc_bs2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_bs2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2bs(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
testcase tc_jsonBasic_encdec_os_charstring() runs on CT {
const CS_json x := "1";
var CS_json y := "1", z;
var octetstring os;
var octetstring expected := char2oct("\"1\"");
log("expected:",expected);
os := f_json_enc_cs2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_cs2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2cs(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
testcase tc_jsonBasic_encdec_os_hexstring() runs on CT {
const HS_json x := '1'H;
var HS_json y := '1'H, z;
var octetstring os;
var octetstring expected := char2oct("\"1\"");
log("expected:",expected);
os := f_json_enc_hs2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_hs2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2hs(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
testcase tc_jsonBasic_encdec_os_octetstring() runs on CT {
const OS_json x := 'F1'O;
var OS_json y := 'F1'O, z;
var octetstring os;
var octetstring expected := char2oct("\"F1\"");
log("expected:",expected);
os := f_json_enc_os2os(x);
f_compare_octetstring(os,expected);
os := f_json_enc_os2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2os(expected);
if ( y==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
testcase tc_jsonBasic_encdec_os_ucharstring() runs on CT {
const UCS_json x := char(0,0,0,1);
var UCS_json y := char(0,0,0,1), z;
var octetstring os;
//var octetstring expected := '00100010 00000001 00100010'B; //"x01" ok, too
var octetstring expected := unichar2oct("\"" & char(0, 0, 0, 1) & "\""); //ok
log("expected:",expected);
os := f_json_enc_ucs2os(x);
log(os);
log(oct2char(os)); // "\"" & char(0, 0, 0, 1) & "\""
f_compare_octetstring(os,expected);
os := f_json_enc_ucs2os(y);
f_compare_octetstring(os,expected);
//decode test:
z := f_json_dec_os2ucs(expected);
if ( y ==z ) {
setverdict(pass);
} else {
setverdict(fail);
}
}
//===== Verdict ====
//V (verdict)
testcase tc_jsonBasic_encdec_os_verdict() runs on CT {
const V_json x := pass;
var V_json y := pass;
var octetstring bs;
var octetstring expected := char2oct("\"pass\"");
log("expected:",expected );
bs := f_json_enc_v2os(x);
f_compare_octetstring(bs, expected);