def test_fresh_const_name(self): ctx = SolverContext(z3.Context()) name1 = ctx.fresh_constraint_name() name2 = ctx.fresh_constraint_name() self.assertTrue(isinstance(name1, basestring)) self.assertTrue(isinstance(name2, basestring)) self.assertFalse(name1 == name2)
def test_create_var_prefix(self): # Arrange ctx = SolverContext(z3.Context()) prefix = 'CustPrefix' # Act var = ctx.create_fresh_var(z3.IntSort(), value=10, name_prefix=prefix) # Assert self.assertTrue(isinstance(var, SMTVar)) self.assertTrue(var.name.startswith(prefix))
def test_get_constraints(self): var1 = SMTVar('var1', z3.IntSort()) var2 = SMTVar('var1', z3.IntSort()) const = var1.var + var2.var > 10 name = 'cosnt1' ctx = SolverContext(z3.Context()) ctx.register_constraint(const, name) self.assertIsNotNone(ctx.get_constraint(name)) self.assertIsNotNone(ctx.get_constraints_info(name)) with self.assertRaises(ValueError): ctx.get_constraint('NotRegistered') with self.assertRaises(ValueError): ctx.get_constraints_info('NotRegistered')
def test_fresh_const_name_prefix(self): # Arrange ctx = SolverContext(z3.Context()) prefix = 'CustPrefix' # Act name1 = ctx.fresh_constraint_name(prefix=prefix) name2 = ctx.fresh_constraint_name(prefix=prefix) # Assert self.assertTrue(isinstance(name1, basestring)) self.assertTrue(isinstance(name2, basestring)) self.assertFalse(name1 == name2) self.assertTrue(name1.startswith(prefix)) self.assertTrue(name2.startswith(prefix))
def test_read(self): concrete_anns = self.get_anns() ctx = SolverContext.create_context(concrete_anns) sym_anns = read_announcements(concrete_anns, ctx) self.assertEqual(len(sym_anns), len(concrete_anns)) for sym_ann, con_ann in zip(sym_anns, concrete_anns): for attr in Announcement.attributes: if attr == 'communities': for community in con_ann.communities: con_val = con_ann.communities[community] sym_val = sym_ann.communities[community] if is_empty(con_val): self.assertFalse(sym_val.is_concrete) else: self.assertTrue(sym_val.is_concrete) self.assertEquals(sym_val.get_value(), con_val) continue con_val = getattr(con_ann, attr) sym_val = getattr(sym_ann, attr) if is_empty(con_val): self.assertFalse(sym_val.is_concrete) else: self.assertTrue(sym_val.is_concrete) if attr == 'as_path': con_val = get_as_path_key(con_val) sym_val_concrete = sym_val.get_value() if attr == 'origin': sym_val_concrete = BGP_ATTRS_ORIGIN.__members__[ sym_val_concrete] self.assertEquals(sym_val_concrete, con_val)
def test_create(self): # Arrange concrete_anns = self.get_anns() ctx = SolverContext.create_context(concrete_anns) # Act sym_anns = read_announcements(concrete_anns, ctx) # Assert self.assertEquals(len(sym_anns), 2)
def test_compare_enum_type(self): # Arrange values = ['A', 'B', 'C'] sort_name = 'TestType' ctx = SolverContext(z3.Context()) solver = z3.Solver(ctx=ctx.z3_ctx) vsort = ctx.create_enum_type(sort_name, values) A, B, C = [vsort.get_symbolic_value(x) for x in values] GREATER, LESS, EQUAL, INCOMPLETE = ctx.compare_vars # Act func = ctx.create_enum_compare(sort_name) solver.add(func(A, A) == EQUAL) solver.add(func(A, B) == GREATER) solver.add(func(A, C) == GREATER) solver.add(func(B, B) == EQUAL) solver.add(func(C, C) == EQUAL) # Assert ret = ctx.check(solver) self.assertEquals(ret, z3.sat)
def create_context(reqs, g, announcements, create_as_paths=False): connected = ConnectedSyn([], g, full=True) connected.synthesize() next_hops_map = compute_next_hop_map(g) next_hops = extract_all_next_hops(next_hops_map) peers = [node for node in g.routers_iter() if g.is_bgp_enabled(node)] ctx = SolverContext.create_context(announcements, peer_list=peers, next_hop_list=next_hops, create_as_paths=create_as_paths) return ctx
def test_sub(self): # Arrange concrete_anns = self.get_anns() ctx = SolverContext.create_context(concrete_anns) sym_anns = read_announcements(concrete_anns, ctx) mutator = self # Act new_anns = sym_anns.create_new([sym_anns[0]], mutator) new_anns2 = new_anns.create_new([new_anns[0]], mutator) self.assertEquals(len(sym_anns), 2) self.assertEquals(len(new_anns), 1) self.assertEquals(len(new_anns2), 1) self.assertEquals(sym_anns.mutators, []) self.assertEquals(new_anns.mutators, [mutator]) self.assertEquals(new_anns2.mutators, [mutator, mutator])
def test_register_const(self): # Arrange var1 = SMTVar('var1', z3.IntSort()) var2 = SMTVar('var1', z3.IntSort()) const = var1.var + var2.var > 10 # Act ctx = SolverContext(z3.Context()) name = ctx.register_constraint(const) with self.assertRaises(ValueError): ctx.register_constraint(const, name) constraints = list(ctx.constraints_itr()) # Assert self.assertIsNotNone(ctx.get_constraint(name)) self.assertIsNotNone(ctx.get_constraints_info(name)) self.assertEquals(len(constraints), 1) self.assertEquals(constraints[0][0], name) self.assertEquals(constraints[0][1], const)
def test_register_var(self): name = 'TestVar1' vsort = z3.IntSort() var = SMTVar(name, vsort) ctx = SolverContext(z3.Context()) ctx._register_var(var) self.assertTrue(var.name in ctx._vars) with self.assertRaises(ValueError): ctx._register_var(var)
def create_context(self, reqs, g): connected = ConnectedSyn(reqs, g, full=True) connected.synthesize() next_hops_map = compute_next_hop_map(g) next_hops = extract_all_next_hops(next_hops_map) peers = [node for node in g.routers_iter() if g.is_bgp_enabled(node)] anns = self.get_anns() for ann in anns: g.add_bgp_advertise(node=ann.peer, announcement=ann, loopback='lo0') ctx = SolverContext.create_context(anns, next_hop_list=next_hops, peer_list=peers, create_as_paths=False) return ctx
def test_create_enum_type(self): # Arrange values = ['A', 'B', 'C'] sort_name = 'TestType' ctx = SolverContext(z3.Context()) # Act vsort = ctx.create_enum_type(sort_name, values) # Assert self.assertTrue(isinstance(vsort, EnumType)) self.assertEquals(vsort.name, sort_name) self.assertEquals(vsort.concrete_values, values) with self.assertRaises(ValueError): ctx.create_enum_type(sort_name, values) with self.assertRaises(ValueError): ctx.create_enum_type("name2", values)
def _create_context(self, create_as_paths=False): """ Create the context to hold symbolic variables used in BGP synthesis :param create_as_paths: :return: SolverContext """ next_hops_map = compute_next_hop_map(self.topo) next_hops = extract_all_next_hops(next_hops_map) peers = [ node for node in self.topo.routers_iter() if self.topo.is_bgp_enabled(node) ] print "PEER", peers print "NEXTHOPS", next_hops ctx = SolverContext.create_context(self.announcements, peer_list=peers, next_hop_list=next_hops, create_as_paths=create_as_paths) return ctx
def test_set_model(self): # Arrange values = ['A', 'B', 'C'] sort_name = 'TestType' ctx = SolverContext(z3.Context()) vsort = ctx.create_enum_type(sort_name, values) var1 = ctx.create_fresh_var(vsort) val1 = vsort.get_symbolic_value('A') var2 = ctx.create_fresh_var(z3.IntSort(ctx=ctx.z3_ctx)) var3 = ctx.create_fresh_var(z3.BoolSort(ctx=ctx.z3_ctx)) solver = z3.Solver(ctx=ctx.z3_ctx) solver.add(var1.var == val1) solver.add(var2.var == 10) solver.add(var3.var == True) solver.check() # Act ctx.set_model(solver.model()) # Assert self.assertTrue(var1.is_concrete) self.assertTrue(var2.is_concrete) self.assertTrue(var3.is_concrete) self.assertEquals(var1.get_value(), 'A') self.assertEquals(var2.get_value(), 10) self.assertEquals(var3.get_value(), True)
def test_create_var(self): ctx = SolverContext(z3.Context()) var = ctx.create_fresh_var(z3.IntSort(), value=10) self.assertTrue(isinstance(var, SMTVar)) with self.assertRaises(ValueError): var = ctx.create_fresh_var(z3.IntSort(), name=var.name, value=10)