def test_only_methods(self): trace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_null()], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ci1 = CCallin(1, 1, "", "void m2()", [TestGrounding._get_null()], None) ci2 = CCallin(1, 1, "", "void m2()", [TestGrounding._get_null()], None) ci3 = CCallin(1, 1, "", "void m2()", [TestGrounding._get_null()], None) cb.add_msg(ci1) cb.add_msg(ci2) cb.add_msg(ci3) trace.add_msg(cb) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] void m2() |- [CB] [EXIT] void m1()") ground_specs = gs.ground_spec(specs[0]) self.assertTrue(1 == len(ground_specs)) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC ! ([CB] [ENTRY] void m3()) |- [CB] [ENTRY] void m1()") ground_specs = gs.ground_spec(specs[0]) self.assertTrue(1 == len(ground_specs)) self.assertTrue( new_star(new_true()) == get_regexp_node(ground_specs[0].ast)) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] void m3() |- [CB] [ENTRY] void m1()") ground_specs = gs.ground_spec(specs[0]) self.assertTrue(0 == len(ground_specs))
def test_iss161(self): trace = CTrace() cb1 = CCallback(1, 1, "", "void doA()", [TestGrounding._get_int(2)], None, [TestGrounding._get_fmwkov("", "void doA()", False)]) trace.add_msg(cb1) cb2 = CCallback(1, 1, "", "void doB()", [TestGrounding._get_int(1)], None, [TestGrounding._get_fmwkov("", "void doB()", False)]) trace.add_msg(cb2) cb3 = CCallback(1, 1, "", "void doA()", [TestGrounding._get_int(3)], None, [TestGrounding._get_fmwkov("", "void doA()", False)]) trace.add_msg(cb3) gs = GroundSpecs(trace) #specs = Spec.get_specs_from_string("SPEC [CB] [ENTRY] [l] void doA(); (! [CB] [ENTRY] [l] void doA()) |- [CB] [ENTRY] [l2] void doB()") specs = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void doA(); [CB] [ENTRY] [l] void doA() |- [CB] [ENTRY] [l2] void doB()" ) real_ground_spec = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void doA(); (! [CB] [ENTRY] [l] void doA()) |- [CB] [ENTRY] [l] void doB()" ) ground_specs = gs.ground_spec(specs[0]) for g in ground_specs: g.print_spec(sys.stdout) print ""
def test_iss131_2(self): trace = CTrace() cb1 = CCallback(1, 1, "", "void doA()", [TestGrounding._get_int(2)], None, [TestGrounding._get_fmwkov("", "void doA()", False)]) trace.add_msg(cb1) cb2 = CCallback(1, 1, "", "void doB()", [TestGrounding._get_int(1)], None, [TestGrounding._get_fmwkov("", "void doB()", False)]) trace.add_msg(cb2) cb3 = CCallback(1, 1, "", "void doC()", [TestGrounding._get_int(1)], None, [TestGrounding._get_fmwkov("", "void doC()", False)]) trace.add_msg(cb3) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC ([CB] [ENTRY] [l] void doA() | [CB] [ENTRY] [l] void doB()) |- [CB] [ENTRY] [f] void doC()" ) real_ground_spec = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [2] void doA() |- [CB] [ENTRY] [1] void doC();" + "SPEC [CB] [ENTRY] [1] void doB() |- [CB] [ENTRY] [1] void doC()") ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec))
def test_regexp_and(self): trace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_null()], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ci1 = CCallin(1, 1, "", "void doA()", [TestGrounding._get_int(1)], None) ci2 = CCallin(1, 1, "", "void doB()", [TestGrounding._get_int(1)], None) ci3 = CCallin(1, 1, "", "void doC()", [TestGrounding._get_int(2)], None) cb.add_msg(ci1) cb.add_msg(ci2) cb.add_msg(ci3) trace.add_msg(cb) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC ([CI] [ENTRY] [l] void doA() & [CI] [ENTRY] [l] void doB()) |- [CI] [ENTRY] [f] void doC()" ) real_ground_spec = Spec.get_specs_from_string( "SPEC ([CI] [ENTRY] [1] void doA() & [CI] [ENTRY] [1] void doB()) |- [CI] [ENTRY] [2] void doC()" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec))
def test_instantiate_regexp(self): spec_string = "REGEXP pollo(x,y) = [[CB] [ENTRY] [x] type methodName(y : boolean)]; SPEC pollo(1,2) |- pollo(x,y)" res = "SPEC [CB] [ENTRY] [1] type methodName(2 : boolean) |- [CB] [ENTRY] [x] type methodName(y : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) spec_string = "REGEXP pollo(x,y) = [[CB] [ENTRY] [x] type methodName(z : boolean)]; SPEC pollo(1,2) |- pollo(x,y)" res = "SPEC [CB] [ENTRY] [1] type methodName(z : boolean) |- [CB] [ENTRY] [x] type methodName(z : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) spec_string = """REGEXP pollo1(x,y) = [[CB] [ENTRY] [x] type methodName1(y : boolean)]; REGEXP pollo2(x,y) = [[CB] [ENTRY] [x] type methodName2(y : boolean)]; SPEC pollo1(1,2) |- pollo2(x,y)""" res = "SPEC [CB] [ENTRY] [1] type methodName1(2 : boolean) |- [CB] [ENTRY] [x] type methodName2(y : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) with self.assertRaises(Exception): # pollo with 1 arguments is not declared spec_string = "REGEXP pollo(x) = [[CB] [ENTRY] [x] type methodName(z : boolean)]; SPEC pollo(1,2) |- pollo(x)" specs = Spec.get_specs_from_string(spec_string) spec_list = Spec.get_specs_from_string(""" REGEXP pollo(x) = [[CI] [ENTRY] [x] void method_name()]; SPEC pollo(l) |- TRUE ALIASES method_name = [subs1]""") res = "SPEC [CI] [ENTRY] [l] void subs1() |- TRUE" self.assertTrue(len(spec_list) == 1) self.assertTrue(str(spec_list[0]) == res)
def test_aliasing(self): spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name() |- TRUE ALIASES method_name = [subs1]" ) res = "SPEC [CI] [ENTRY] [l] void subs1() |- TRUE" self.assertTrue(len(spec_list) == 1) self.assertTrue(str(spec_list[0]) == res) spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name2(); " + " [CI] [ENTRY] [l] void method_name(); " + " [CI] [ENTRY] [l] void method_name() " + " |- [CI] [ENTRY] [l] void method_name() " + "ALIASES method_name = [subs1]") res = [ "SPEC (([CI] [ENTRY] [l] void method_name2(); " + "[CI] [ENTRY] [l] void subs1()); [CI] [ENTRY] [l] void subs1()) |- " + "[CI] [ENTRY] [l] void subs1()" ] self.assertTrue(len(spec_list) == len(res)) for i in range(len(res)): self.assertTrue(str(spec_list[i]) in res) spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name() |- TRUE " + "ALIASES method_name = [subs1, subs2]") res = [ "SPEC [CI] [ENTRY] [l] void subs1() |- TRUE", "SPEC [CI] [ENTRY] [l] void subs2() |- TRUE" ] self.assertTrue(len(spec_list) == len(res)) for i in range(len(res)): self.assertTrue(str(spec_list[i]) in res) spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name() |- " + "[CI] [ENTRY] [l] void method_name2() " + "ALIASES method_name = [subs1, subs2], method_name2 = [subs3, subs4]" ) res = [ "SPEC [CI] [ENTRY] [l] void subs1() |- [CI] [ENTRY] [l] void subs3()", "SPEC [CI] [ENTRY] [l] void subs1() |- [CI] [ENTRY] [l] void subs4()", "SPEC [CI] [ENTRY] [l] void subs2() |- [CI] [ENTRY] [l] void subs3()", "SPEC [CI] [ENTRY] [l] void subs2() |- [CI] [ENTRY] [l] void subs4()" ] self.assertTrue(len(spec_list) == len(res)) for i in range(len(res)): self.assertTrue(str(spec_list[i]) in res)
def check_sg(test): (r, t, expected_specs_str) = test trace = TestGrounding.get_trace(t) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string(r) ground_specs = gs.ground_spec(specs[0]) expected_ground_specs = Spec.get_specs_from_string(expected_specs_str) if expected_ground_specs is None: expected_ground_specs = [] assert (TestGrounding._eq_specs(ground_specs, expected_ground_specs))
def test_get_ground_spec_ts(self): def _encode_error(accepting, final): f_error = FALSE() for f in accepting: f_error = Or(f, f_error) f_error = And(f_error, final) return f_error spec_list = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void m1() |- [CI] [ENTRY] [l] void m2()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ground_s = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [1] void m1() |- [CI] [ENTRY] [1] void m2()")[0] ts_enc = TSEncoder(ctrace, []) ts_var = ts_enc._encode_vars() accepting = [] gs_ts = ts_enc._get_ground_spec_ts(ground_s, 0, accepting) gs_ts.product(ts_var) error = _encode_error(accepting, TRUE()) self.assertTrue( self._accept_word(ts_enc, gs_ts, ["[CB]_[ENTRY]_void m1()(1)"], error)) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CI]_[ENTRY]_void m2()(1)"], error)) # check the disable error = _encode_error( accepting, TSEncoder._get_state_var("[CI]_[ENTRY]_void m2()(1)")) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CB]_[ENTRY]_void m1()(1)"], error)) self.assertFalse( self._accept_word(ts_enc, gs_ts, ["[CI]_[ENTRY]_void m2()(1)"], error))
def test_simplify_exit_callin(self): spec_list = Spec.get_specs_from_string( "SPEC [CB] [EXIT] [l] void m1() |- [CI] [ENTRY] [l] void m3()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m3()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb2 = CCallback(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m2()", False)]) ctrace.add_msg(cb2) ts_enc = TSEncoder(ctrace, spec_list, True) ts_enc.trace.print_trace(sys.stdout) self.assertTrue(3 == ts_enc.trace_length) ts = ts_enc.get_ts_encoding() trace_enc = ts_enc.get_trace_encoding() print trace_enc self.assertTrue(len(trace_enc) == 3) bmc = BMC(ts_enc.helper, ts, FALSE()) (step, cex, _) = bmc.simulate(trace_enc) self.assertTrue(cex is not None)
def test_simplify_2(self): spec_list = Spec.get_specs_from_string( "SPEC FALSE[*] |- [CB] [ENTRY] [l] void m3(); SPEC FALSE[*] |- [CI] [ENTRY] [l] void m4()" ) assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb = CCallback(1, 1, "", "void m5()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m5()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m4()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb = CCallback(1, 1, "", "void m3()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m3()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m4()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ts_enc = TSEncoder(ctrace, spec_list, True) self.assertTrue(4 == ts_enc.trace.get_total_msg())
def test_multiple_single_cb(self): spec_list = Spec.get_specs_from_string( "SPEC FALSE[*] |- [CB] [ENTRY] [l] void m3(); SPEC FALSE[*] |- [CI] [ENTRY] [l] void m4()" ) assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb = CCallback(1, 1, "", "void m5()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m5()", False)]) ctrace.add_msg(cb) cb = CCallback(1, 1, "", "void m3()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m3()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m4()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ts_enc = TSEncoder(ctrace, spec_list) ts = ts_enc.get_ts_encoding() error = ts_enc.error_prop bmc = BMC(ts_enc.helper, ts, error) cex = bmc.find_bug(2, True) self.assertTrue(cex is None)
def test_exception(self): """ Test the removal of exception from top-level callbacks """ spec_list = Spec.get_specs_from_string( "SPEC FALSE[*] |- [CI] [ENTRY] void m2()") ctrace = CTrace() cb1 = CCallback(1, 1, "", "void m1()", [TestGrounding._get_null()], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) cb1.exception = CTraceException("void m1()", "", "NullPointerException", "NullPointerException message") ci1 = CCallin(1, 1, "", "void m2()", [TestGrounding._get_null()], None) cb1.add_msg(ci1) ctrace.add_msg(cb1) cb2 = CCallback(1, 1, "", "void m1()", [TestGrounding._get_null()], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ci2 = CCallin(1, 1, "", "void m3()", [TestGrounding._get_null()], None) cb2.add_msg(ci2) ctrace.add_msg(cb2) ts_enc = TSEncoder(ctrace, spec_list) assert (1 == len(ts_enc.trace.children)) ts = ts_enc.get_ts_encoding() bmc = BMC(ts_enc.helper, ts, ts_enc.error_prop) # if the first callback is removed, m2 cannot be called anymore self.assertTrue(bmc.find_bug(2) is None) self.assertTrue(bmc.find_bug(2, True) is None)
def test_cex_printer_exit(self): spec_list = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void m1() |- [CB] [EXIT] [l] void m1()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj(1, "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj(1, "string")], None) cb.add_msg(ci) ts_enc = TSEncoder(ctrace, spec_list) ts = ts_enc.get_ts_encoding() error = ts_enc.error_prop bmc = BMC(ts_enc.helper, ts, error) cex = bmc.find_bug(4) cex = bmc.find_bug(4, True) self.assertFalse(cex is None) stringio = StringIO() printer = CexPrinter(ts_enc.mapback, cex, stringio) printer.print_cex() io_string = stringio.getvalue() self.assertTrue( "SPEC [CB] [ENTRY] [1] void m1() |- [CB] [EXIT] [1] void m1()" in io_string) self.assertTrue("Reached an error state in step 4" in io_string)
def _test_parse(self, spec, same_out=True): res = spec_parser.parse(spec) self.assertTrue(res is not None) # test the printing of the spec ast stringio = StringIO() pretty_print(res, stringio) # print "---" # print spec # pretty_print(res, sys.stdout) # print "\n---" self.assertTrue((not same_out) or stringio.getvalue() == spec) Spec.get_specs_from_string(stringio.getvalue())
def __init__(self, opts): self.opts = opts self.stats = Stats() self.stats.enable() # Parse the trace try: self.stats.start_timer(Stats.PARSING_TIME) self.trace = CTraceSerializer.read_trace_file_name(self.opts.tracefile, self.opts.traceformat == "json", self.opts.allow_exception) self.stats.stop_timer(Stats.PARSING_TIME) self.stats.write_times(sys.stdout, Stats.PARSING_TIME) except MalformedTraceException as e: raise except TraceEndsInErrorException as e: raise except Exception as e: raise Exception("An error happened reading the trace in %s (%s)" % (self.opts.tracefile, e.message)) # Parse the specs self.spec_list = Spec.get_specs_from_files(self.opts.spec_file_list) if self.spec_list is None: raise Exception("Error parsing the specification file!")
def test_spec_creation(self): spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name() |- TRUE; " + "SPEC [CI] [EXIT] [l] void method_name() |- TRUE;" + "SPEC [CI] [ENTRY] [b] void android.widget.Button.setOnClickListener(l : type) |+ [CB] [ENTRY] [l] void onClick(b : type);" + "SPEC TRUE[*]; [CI] [EXIT] [b] void android.widget.Button.setOnClickListener(l : type) |+ [CB] [ENTRY] [l] void onClick(b : type)" ) self.assertTrue(len(spec_list) == 4)
def test_array_types(self): test_path = os.path.dirname(cbverifier.test.examples.__file__) t1 = os.path.join(test_path, "trace_array.json") trace = CTraceSerializer.read_trace_file_name(t1, True) self.assertTrue(trace is not None) spec_file_path = os.path.join(test_path, "spec_array.spec") specs = Spec.get_specs_from_files([spec_file_path]) self.assertTrue(specs is not None) self.assertTrue(len(specs) == 1) real_ground_spec = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [1] java.lang.String android.app.Activity.getString(2131427336 : int,4fe67f6 : java.lang.Object[],efe45f6 : java.lang.Test[][]) |- [CI] [ENTRY] [1] java.lang.String android.app.Activity.getString(2131427336 : int, 4fe67f6 : java.lang.Object[],efe45f6 : java.lang.Test[][])" ) self.assertTrue(real_ground_spec is not None) gs = GroundSpecs(trace) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(ground_specs is not None) self.assertTrue(1 == len(ground_specs)) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec))
def test_090_multiple_cbs(self): spec_list = Spec.get_specs_from_string("SPEC FALSE[*] |- [CI] [ENTRY] [l] void m4();" \ "SPEC FALSE[*] |- [CB] [ENTRY] [l] void m3();" \ "SPEC [CB] [ENTRY] [l1] void m1() |+ [CB] [ENTRY] [l2] void m3()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(2, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ci = CCallin(3, 1, "", "void m6()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb = CCallback(4, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(5, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) cb = CCallback(6, 1, "", "void m3()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m3()", False)]) ctrace.add_msg(cb) ci = CCallin(7, 1, "", "void m4()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ts_enc = TSEncoder(ctrace, spec_list) ts = ts_enc.get_ts_encoding() error = ts_enc.error_prop bmc = BMC(ts_enc.helper, ts, error) cex = bmc.find_bug(6) self.assertTrue(cex is not None) stringio = StringIO() printer = CexPrinter(ts_enc.mapback, cex, stringio) printer.print_cex() io_string = stringio.getvalue() self.assertTrue("[4] [CB] [ENTRY] void m1()" in io_string) self.assertTrue("[5] [CI] [ENTRY] void m2()" in io_string) self.assertTrue("[6] [CB] [ENTRY] void m3()" in io_string) self.assertTrue("[7] [CI] [ENTRY] void m4()" in io_string)
def _get_sample_trace(self): spec_list = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void m1() |- [CI] [ENTRY] [l] void m2()") assert spec_list is not None ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ctrace.add_msg(cb) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_obj("1", "string")], None) cb.add_msg(ci) ts_enc = TSEncoder(ctrace, spec_list) return ts_enc
def test_instantiate_regexp_nested(self): spec_string = """REGEXP pollo(x) = [[CB] [ENTRY] [x] type methodName()]; REGEXP pollo2(z) = [pollo(z)]; SPEC pollo2(1) |- pollo(1) """ res = "SPEC [CB] [ENTRY] [1] type methodName() |- [CB] [ENTRY] [1] type methodName()" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) spec_string = """REGEXP pollo(x,y) = [[CB] [ENTRY] [x] type methodName(y : boolean)]; REGEXP pollo2(x,y) = [pollo(y,x)]; SPEC pollo2(1,2) |- pollo(1,2) """ res = "SPEC [CB] [ENTRY] [2] type methodName(1 : boolean) |- [CB] [ENTRY] [1] type methodName(2 : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) spec_string = """REGEXP pollo(x) = [[CB] [ENTRY] [x] type methodName(y : boolean)]; REGEXP pollo2(x,y) = [pollo(x)]; SPEC pollo2(1,2) |- pollo2(1,2) """ res = "SPEC [CB] [ENTRY] [1] type methodName(y : boolean) |- [CB] [ENTRY] [1] type methodName(y : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res) spec_string = """REGEXP pollo(x) = [[CB] [ENTRY] [x] type methodName(y : boolean)]; REGEXP pollo2(x,y) = [pollo(x)]; SPEC pollo2(1,2) |- pollo2(1,2) """ res = "SPEC [CB] [ENTRY] [1] type methodName(y : boolean) |- [CB] [ENTRY] [1] type methodName(y : boolean)" specs = Spec.get_specs_from_string(spec_string) self.assertTrue(len(specs) == 1) self.assertTrue(str(specs[0]) == res)
def test_int(self): trace = CTrace() cb = CCallback( 1, 1, "", "void m1(%s)" % TraceConverter.JAVA_INT, [TestGrounding._get_null(), TestGrounding._get_int(2)], None, [ TestGrounding._get_fmwkov( "", "void m1(%s)" % TraceConverter.JAVA_INT, False) ]) trace.add_msg(cb) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC TRUE |- [CB] [ENTRY] void m1(2 : %s)" % TraceConverter.JAVA_INT) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(1 == len(ground_specs))
def test_constants(self): trace = CTrace() cb = CCallback(1, 1, "edu.colorado.test", "void inheritedMethodMethod(int)", [TestGrounding._get_null(), TestGrounding._get_int(2)], None, [ TestGrounding._get_fmwkov( "android", "void inheritedMethod(int)", False) ]) trace.add_msg(cb) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC ! ([CB] [ENTRY] void android.inheritedMethodA(3 : int)) |- [CB] [ENTRY] void android.inheritedMethod(2 : int)" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(len(ground_specs) == 1)
def test_get_key_from_call(self): spec_list = Spec.get_specs_from_string( "SPEC TRUE |- [CI] [ENTRY] [1] void m1(); " + "SPEC TRUE |- [CI] [ENTRY] [1] void m1(2 : int,1 : int,2 : int);" + "SPEC TRUE |- 3 = [CI] [EXIT] [1] void m1(2 : int,1 : int,2 : int)" ) assert spec_list is not None ci1 = CCallin(1, 1, "", "void m1()", [TestGrounding._get_obj("1", "string")], None) ci2 = CCallin(1, 1, "", "void m1(int,int,int)", [ TestGrounding._get_obj("1", "string"), TestGrounding._get_int(2), TestGrounding._get_int(1), TestGrounding._get_int(2), ], None) ci3 = CCallin(1, 1, "", "void m1(int,int,int)", [ TestGrounding._get_obj("1", "string"), TestGrounding._get_int(2), TestGrounding._get_int(1), TestGrounding._get_int(2), ], TestGrounding._get_int(3)) calls_nodes = [] for s in spec_list: msg = get_spec_rhs(s.ast) assert get_node_type(msg) == CALL_ENTRY or get_node_type( msg) == CALL_EXIT calls_nodes.append(msg) assert (len(calls_nodes) == len(spec_list)) res = TSEncoder.get_key_from_call(calls_nodes[0]) res2 = TSEncoder.get_key_from_msg(ci1, TSEncoder.ENTRY) self.assertTrue("[CI]_[ENTRY]_void m1()(1)" == res) self.assertTrue(res == res2) res = TSEncoder.get_key_from_call(calls_nodes[1]) res2 = TSEncoder.get_key_from_msg(ci2, TSEncoder.ENTRY) self.assertTrue("[CI]_[ENTRY]_void m1(int,int,int)(1,2,1,2)" == res) self.assertTrue(res == res2) res = TSEncoder.get_key_from_call(calls_nodes[2]) res2 = TSEncoder.get_key_from_msg(ci3, TSEncoder.EXIT) self.assertTrue("3=[CI]_[EXIT]_void m1(int,int,int)(1,2,1,2)" == res) self.assertTrue(res == res2)
def cycle_lines(in_file, out_file): with open(in_file, 'r') as f: for line in f: if line is not None and len(line) > 2 and line[0] != '/': line2 = line.strip() if line2[-1] == ";": trimedLine = line2[:-1] else: trimedLine = line2 parsedspec = Spec.get_specs_from_string(trimedLine) assert (len(parsedspec) == 1) spec = parsedspec[0].ast aliasList = get_aliases_for_spec(spec) assert (len(aliasList) > 0) for alias in aliasList: spec = add_alias(spec, alias[0], alias[1]) pretty_print(spec) print ";" else: print line
def test_call_set(self): spec_list = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void method_name() |- TRUE; " + "SPEC [CI] [ENTRY] [l] void method_name() |- TRUE;" + "SPEC [CI] [ENTRY] [b] void android.widget.Button.setOnClickListener(l : type) |+ [CB] [ENTRY] [l] void onClick(b : type);" + "SPEC TRUE[*]; [CI] [EXIT] [b] void android.widget.Button.setOnClickListener(l : type) |+ [CB] [ENTRY] [l] void onClick(b : type);" + "SPEC [CI] [EXIT] [b] void android.widget.Button.setOnClickListener(l : type); " + "[CB] [ENTRY] [b] void android.widget.Button.setOnClickListener(l : type); " + "[CI] [EXIT] [b] void android.widget.Button.setOnClickListener(l : type) |+ [CB] [ENTRY] [l] void onClick(b : type)" ) self.assertTrue(len(spec_list) == 5) self.assertTrue(1 == len(spec_list[0].get_spec_calls())) self.assertTrue(1 == len(spec_list[1].get_spec_calls())) self.assertTrue(2 == len(spec_list[2].get_spec_calls())) self.assertTrue(2 == len(spec_list[3].get_spec_calls())) self.assertTrue(3 == len(spec_list[4].get_spec_calls()))
def test_init_state(self): """ Test if specification can force an initial value - m2 is disabled in the initial state - the trace try to call m1 and then m2, causing an exception """ spec_list = Spec.get_specs_from_string( "SPEC FALSE[*] |- [CI] [ENTRY] void m2()") ctrace = CTrace() cb = CCallback(1, 1, "", "void m1()", [TestGrounding._get_null()], None, [TestGrounding._get_fmwkov("", "void m1()", False)]) ci = CCallin(1, 1, "", "void m2()", [TestGrounding._get_null()], None) cb.add_msg(ci) ctrace.add_msg(cb) ts_enc = TSEncoder(ctrace, spec_list) ts = ts_enc.get_ts_encoding() bmc = BMC(ts_enc.helper, ts, ts_enc.error_prop) self.assertTrue(bmc.find_bug(2) is not None) self.assertTrue(bmc.find_bug(2, True) is not None)
def test_regexptoauto_exit(self): auto_env = AutoEnv.get_global_auto_env() cenc = CounterEnc(auto_env.pysmt_env) alphabet = set( ["[CB]_[ENTRY]_void m1()(1)", "[CB]_[EXIT]_void m1()(1)"]) r2a = RegExpToAuto(cenc, alphabet, TSMapback(auto_env.pysmt_env, None, None), auto_env) env = r2a.auto_env l_m1_entry = r2a.get_msg_eq("[CB]_[ENTRY]_void m1()(1)") l_m1_exit = r2a.get_msg_eq("[CB]_[EXIT]_void m1()(1)") spec_list = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [1] void m1() |- TRUE; " + "SPEC [CB] [EXIT] [1] void m1() |- TRUE; " + "SPEC [CB] [ENTRY] [1] void m1() & [CB] [EXIT] [1] void m1() |- TRUE" ) assert spec_list is not None # Test l.m1() regexp = get_regexp_node(spec_list[0].ast) auto = r2a.get_from_regexp(regexp) res = Automaton.get_singleton(env.new_label(l_m1_entry)) self.assertTrue(auto.is_equivalent(res)) # Test l.m1_exit() regexp = get_regexp_node(spec_list[1].ast) auto = r2a.get_from_regexp(regexp) res = Automaton.get_singleton(env.new_label(l_m1_exit)) self.assertTrue(auto.is_equivalent(res)) # Test l.m1() and l.m2() regexp = get_regexp_node(spec_list[2].ast) auto = r2a.get_from_regexp(regexp) res = Automaton.get_singleton(env.new_label(And(l_m1_entry, l_m1_exit))) self.assertTrue(auto.is_equivalent(res))
def test_boolean(self): trace = CTrace() cb = CCallback( 1, 1, "", "void m1(%s)" % TraceConverter.JAVA_BOOLEAN, [TestGrounding._get_null(), TestGrounding._get_true()], None, [ TestGrounding._get_fmwkov( "", "void m1(%s)" % TraceConverter.JAVA_BOOLEAN, False) ]) trace.add_msg(cb) cb = CCallback( 1, 1, "", "void m2(%s)" % TraceConverter.JAVA_BOOLEAN, [TestGrounding._get_null(), TestGrounding._get_false()], None, [ TestGrounding._get_fmwkov( "", "void m2(%s)" % TraceConverter.JAVA_BOOLEAN, False) ]) trace.add_msg(cb) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC TRUE |- [CB] [ENTRY] void m2(FALSE : %s)" % (TraceConverter.JAVA_BOOLEAN)) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(1 == len(ground_specs))
def _process_subs(self, data, substitution): (spec, ast_set, ground_specs) = data (new_spec_ast, reasons) = GroundSpecs._substitute(spec, substitution) # WORKAROUND: # # The grounding produce a disjunction in the rhs of the SPEC if # there are multiple messages there. # # However, The downstream toolchain expects to have only one message # in the rhs. # # If this is the case, we duplicate the spec for each disjunct # regexp = get_regexp_node(new_spec_ast) rhs = get_spec_rhs(new_spec_ast) is_lhs_false = FALSE == get_node_type(regexp) if len(reasons) > 0 and not is_lhs_false: reasons = set() # remove unuseful reasons new_reasons = set() for reason in reasons: if not (len(reason) == len(substitution) and len(substitution) > 0): new_reasons.add(reason) reasons = new_reasons # skip the specification if # - the rhs is false (or) # - the regexp is false if (is_lhs_false): return reasons # loop on the disojunctions in the rhs atoms_rhs = [] rhs_stack = [rhs] while (len(rhs_stack) != 0): current = rhs_stack.pop() # Skip false is_current_false = FALSE == get_node_type(current) if is_current_false: continue # requirement on the RHS assert (OR_OP == get_node_type(current) or CALL_ENTRY == get_node_type(current) or CALL_EXIT == get_node_type(current)) if (get_node_type(current) != OR_OP): atoms_rhs.append(current) else: rhs_stack.append(current[1]) rhs_stack.append(current[2]) for atom_rhs in atoms_rhs: assert get_node_type(new_spec_ast) == SPEC_SYMB assert (ENABLE_OP == get_node_type(new_spec_ast[1]) or DISABLE_OP == get_node_type(new_spec_ast[1])) op_node = create_node(get_node_type(new_spec_ast[1]), [regexp, atom_rhs]) split_spec_ast = create_node(SPEC_SYMB, [op_node, new_nil()]) # skip duplicates if split_spec_ast not in ast_set: ast_set.add(split_spec_ast) new_spec = Spec(split_spec_ast) ground_specs.append(new_spec) self.ground_to_spec[new_spec] = spec return reasons
def test_ground_bindings(self): trace = CTrace() cb1 = CCallback( 1, 1, "", "void doSomethingCb()", [TestGrounding._get_obj("1", "string")], None, [TestGrounding._get_fmwkov("", "void doSomethingCb()", False)]) trace.add_msg(cb1) ci1 = CCallin(1, 1, "", "void doSomethingCi(string)", [ TestGrounding._get_obj("1", "string"), TestGrounding._get_obj("2", "string") ], None) cb1.add_msg(ci1) ci2 = CCallin(1, 1, "", "void otherCi(string)", [ TestGrounding._get_obj("4", "string"), TestGrounding._get_obj("1", "string") ], None) cb1.add_msg(ci2) ci3 = CCallin(1, 1, "", "void doSomethingCi(string)", [ TestGrounding._get_obj("1", "string"), TestGrounding._get_obj("4", "string") ], None) cb1.add_msg(ci3) cb2 = CCallback( 1, 1, "", "void doSomethingCb2()", [TestGrounding._get_obj("1", "string")], TestGrounding._get_obj("1", "string"), [TestGrounding._get_fmwkov("", "void doSomethingCb2()", False)]) trace.add_msg(cb2) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void doSomethingCi(z : string) |- [CI] [ENTRY] [z] void otherCi(f : string)" ) real_ground_spec = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [1] void doSomethingCi(4 : string) |- [CI] [ENTRY] [4] void otherCi(1 : string)" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec)) # WARNING: the results is sensitive to the order of the atoms in the OR gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l] void doSomethingCi(# : string) |- [CI] [ENTRY] [#] void otherCi(# : string)" ) real_ground_spec_1 = Spec.get_specs_from_string( "SPEC ([CI] [ENTRY] [1] void doSomethingCi(4 : string) | " + "[CI] [ENTRY] [1] void doSomethingCi(2 : string)) " + "|- [CI] [ENTRY] [4] void otherCi(1 : string)") real_ground_spec_2 = Spec.get_specs_from_string( "SPEC ([CI] [ENTRY] [1] void doSomethingCi(2 : string) | " + "[CI] [ENTRY] [1] void doSomethingCi(4 : string)) " + "|- [CI] [ENTRY] [4] void otherCi(1 : string)") ground_specs = gs.ground_spec(specs[0]) self.assertTrue( TestGrounding._eq_specs(ground_specs, real_ground_spec_1) or TestGrounding._eq_specs(ground_specs, real_ground_spec_2)) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [l] void doSomethingCb() |- [CI] [ENTRY] [#] void otherCi(l : string)" ) real_ground_spec = Spec.get_specs_from_string( "SPEC [CB] [ENTRY] [1] void doSomethingCb() |- [CI] [ENTRY] [4] void otherCi(1 : string)" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec)) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC TRUE |- [CI] [ENTRY] [l1] void doSomethingCi(l1 : string)") ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, [])) # doSomethingCi will be instantiated to FALSE gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CI] [ENTRY] [l1] void doSomethingCi(l1 : string) |- [CI] [ENTRY] [z] void otherCi(l : string)" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, [])) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC [CB] [EXIT] [l] void doSomethingCb() |- [CI] [EXIT] [#] void otherCi(l : string)" ) real_ground_spec = Spec.get_specs_from_string( "SPEC [CB] [EXIT] [1] void doSomethingCb() |- [CI] [EXIT] [4] void otherCi(1 : string)" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec)) gs = GroundSpecs(trace) specs = Spec.get_specs_from_string( "SPEC m = [CB] [EXIT] [l] void doSomethingCb2() |- m = [CB] [EXIT] [l] void doSomethingCb2()" ) real_ground_spec = Spec.get_specs_from_string( "SPEC 1 = [CB] [EXIT] [1] void doSomethingCb2() |- 1 = [CB] [EXIT] [1] void doSomethingCb2()" ) ground_specs = gs.ground_spec(specs[0]) self.assertTrue(TestGrounding._eq_specs(ground_specs, real_ground_spec))