update verigraph
[parser.git] / verigraph / src / it / polito / verigraph / mcnet / netobjs / PolitoWebClient.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9 package it.polito.verigraph.mcnet.netobjs;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import com.microsoft.z3.BoolExpr;
15 import com.microsoft.z3.Context;
16 import com.microsoft.z3.DatatypeExpr;
17 import com.microsoft.z3.Expr;
18 import com.microsoft.z3.FuncDecl;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
21
22 import it.polito.verigraph.mcnet.components.NetContext;
23 import it.polito.verigraph.mcnet.components.Network;
24 import it.polito.verigraph.mcnet.components.NetworkObject;
25
26 /**
27  * WebClient
28  */
29 public class PolitoWebClient extends NetworkObject{
30
31     List<BoolExpr> constraints;
32     Context ctx;
33     DatatypeExpr politoWebClient;
34     Network net;
35     NetContext nctx;
36     FuncDecl isInBlacklist;
37
38     public PolitoWebClient(Context ctx, Object[]... args) {
39         super(ctx, args);
40     }
41
42     @Override
43     protected void init(Context ctx, Object[]... args) {
44         this.ctx = ctx;
45         isEndHost=true;
46         constraints = new ArrayList<BoolExpr>();
47         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
48         politoWebClient = z3Node;
49         net = (Network)args[0][1];
50         nctx = (NetContext)args[0][2];
51         DatatypeExpr ipServer = (DatatypeExpr) args[0][3];
52         webClientRules(ipServer);
53         //net.saneSend(this);
54     }
55
56     @Override
57     public DatatypeExpr getZ3Node() {
58         return politoWebClient;
59     }
60
61     @Override
62     protected void addConstraints(Solver solver) {
63         //System.out.println("[MailClient] Installing rules.");
64         BoolExpr[] constr = new BoolExpr[constraints.size()];
65         solver.add(constraints.toArray(constr));
66     }
67
68     private void webClientRules (DatatypeExpr ipServer){
69         Expr n_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_n_0", nctx.node);
70         Expr p_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_p_0", nctx.packet);
71         IntExpr t_0 = ctx.mkIntConst("PolitoWebClient_"+politoWebClient+"_t_0");
72
73         //Constraint1send(politoWebClient, n_0, p, t_0) -> nodeHasAddr(politoWebClient,p.src)
74         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
75                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
76                         (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("src").apply(p_0))),1,null,null,null,null));
77
78         //Constraint2send(politoWebClient, n_0, p, t_0) -> p.origin == politoWebClient
79         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
80                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
81                         ctx.mkEq(nctx.pf.get("origin").apply(p_0),politoWebClient)),1,null,null,null,null));
82
83         //Constraint3send(politoWebClient, n_0, p, t_0) -> p.orig_body == p.body
84         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
85                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
86                         ctx.mkEq(nctx.pf.get("orig_body").apply(p_0),nctx.pf.get("body").apply(p_0))),1,null,null,null,null));
87
88         //Constraint4recv(n_0, politoWebClient, p, t_0) -> nodeHasAddr(politoWebClient,p.dest)
89         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
90                 ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0,politoWebClient, p_0, t_0),
91                         (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("dest").apply(p_0))),1,null,null,null,null));
92
93
94         //Constraint5This client is only able to produce HTTP requests
95         //send(politoWebClient, n_0, p, t_0) -> p.proto == HTTP_REQ
96         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
97                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
98                         ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST))),1,null,null,null,null));
99
100         //Constraint6send(politoWebClient, n_0, p, t_0) -> p.dest == ipServer
101         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
102                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
103                         ctx.mkEq(nctx.pf.get("dest").apply(p_0), ipServer)),1,null,null,null,null));
104
105     }
106 }