-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
64 lines (49 loc) · 1.99 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import random
from SafeChain.Controller import Controller
from SafeChain.Channel import Channel
from SafeChain.PrivacyPolicy import PrivacyPolicy
import SafeChain.Variable as Variable
def get_channel_definition(channel):
with open(f'channels/{channel}.json') as f:
definition = json.load(f)
return definition
def set_rand_state(channel):
possible_values_of_variables = channel.getPossibleValuesOfVariables()
state = {variable_name: random.choice(tuple(possible_values))
for variable_name, possible_values
in possible_values_of_variables.items()}
channel.setState(state)
def main():
channels = ['Android Location', 'Philips Hue']
# create channel database
database = {channel: get_channel_definition(channel) for channel in channels}
# initialize controller
controller = Controller(database)
# create channel objects
location = Channel('Android Location', database['Android Location'],
'androidloc')
location.setState({'location': 50, 'location_previous': 0})
hue = Channel('Philips Hue', database['Philips Hue'], 'hue')
set_rand_state(hue)
controller.addChannel(location)
controller.addChannel(hue)
# create rule
rule_name = 'RULE'
trigger_channel = 'Android Location'
trigger_name = 'You enter an area'
action_channel = 'Philips Hue'
action_name = 'Turn on lights'
trigger_input = ('androidloc', 100)
action_input = controller.getFeasibleInputsForAction(action_channel,
action_name)
controller.addRule(rule_name, trigger_channel, trigger_name, trigger_input,
action_channel, action_name, action_input)
# set policies
controller.addVulnerableChannelVariable('hue', 'status')
policy = PrivacyPolicy(set([('androidloc', 'location')]))
# check
filename, result, *time = controller.check(policy)
print(result)
if __name__ == '__main__':
main()