test: Add session test script
[platform/upstream/connman.git] / test / test-session
1 #!/usr/bin/python
2
3 import sys
4 import gobject
5
6 import dbus
7 import dbus.service
8 import dbus.mainloop.glib
9
10 import glib
11
12 import traceback
13
14 def extract_list(list):
15         val = "["
16         for i in list:
17                 val += " " + str(i)
18         val += " ]"
19         return val
20
21 def extract_values(values):
22         val = "{"
23         for key in values.keys():
24                 val += " " + key + "="
25                 if key in ["PrefixLength"]:
26                         val += "%s" % (int(values[key]))
27                 else:
28                         if key in ["Servers", "Excludes"]:
29                                 val += extract_list(values[key])
30                         else:
31                                 val += str(values[key])
32         val += " }"
33         return val
34
35 class Notification(dbus.service.Object):
36         def __init__(self, bus, app, notify_path):
37                 dbus.service.Object.__init__(self)
38                 self.app = app
39
40         @dbus.service.method("net.connman.Notification",
41                                 in_signature='', out_signature='')
42         def Release(self):
43                 print("Release")
44                 self.app.release()
45
46         @dbus.service.method("net.connman.Notification",
47                                 in_signature='a{sv}', out_signature='')
48         def Update(self, settings):
49                 print "Update called"
50
51                 try:
52                         for key in settings.keys():
53                                 if key in ["IPv4", "IPv6"]:
54                                         val = extract_values(settings[key])
55                                 elif key in  ["AllowedBearers"]:
56                                         val = extract_list(settings[key])
57                                 else:
58                                         val = settings[key]
59                                 print "    %s = %s" % (key, val)
60                 except:
61                         print "Exception:"
62                         traceback.print_exc()
63
64 class SessionApplication(dbus.service.Object):
65         def __init__(self, bus, object_path, mainloop):
66                 dbus.service.Object.__init__(self, bus, object_path)
67
68                 self.manager = None
69                 self.notify_path = object_path + "/notify"
70                 self.notify = None
71                 self.session_path = None
72                 self.mainloop = mainloop
73                 self.session = None
74
75                 try:
76                         bus = dbus.SystemBus()
77                         bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
78                 except dbus.DBusException:
79                         traceback.print_exc()
80                         exit(1)
81
82         def connman_name_owner_changed(self, proxy):
83                 try:
84                         if proxy:
85                                 print "connman appeared on D-Bus ", str(proxy)
86
87                                 bus = dbus.SystemBus()
88                                 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
89                                                               "net.connman.Manager")
90                         else:
91                                 print "connman disappeared on D-Bus"
92                                 self.manager = None
93                                 self.session = None
94                                 if self.notify:
95                                         self.notify.remove_from_connection()
96                                         self.notify = None
97
98                 except dbus.DBusException:
99                         traceback.print_exc()
100                         exit(1)
101
102         def release(self):
103                 if self.session:
104                         self.manager.DestroySession(self.session_path)
105                         self.session = None
106                 if self.notify:
107                         self.notify.remove_from_connection()
108                         self.notify = None
109
110         @dbus.service.method("com.example.TestSession",
111                                 in_signature='', out_signature='')
112         def CreateSession(self):
113                 print "Create session"
114
115                 if self.session:
116                         print "already running a session -> drop reqest"
117                         return
118
119                 try:
120                         bus = dbus.SystemBus()
121
122                         self.notify = Notification(bus, self, self.notify_path)
123                         self.notify.add_to_connection(bus, self.notify_path)
124
125                         #settings = { "AllowedBearers" : [ "ethernet", "*" ] }
126                         settings = { "AllowedBearers" : [ "ethernet" ] }
127                         self.session_path = self.manager.CreateSession(settings, self.notify_path)
128
129                         print "notify path %s" % (self.notify_path)
130                         print "session path %s" % (self.session_path)
131
132                         self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
133                                                       "net.connman.Session")
134
135                 except dbus.DBusException:
136                         traceback.print_exc()
137                         exit(1)
138
139         @dbus.service.method("com.example.TestSession",
140                                 in_signature='', out_signature='')
141         def DestroySession(self):
142                 print "Destroy session"
143
144                 if self.session == None:
145                         print "no session running -> drop request"
146                         return
147
148                 try:
149                         self.release()
150                 except dbus.DBusException:
151                         traceback.print_exc()
152                         exit(1)
153
154         @dbus.service.method("com.example.TestSession",
155                                 in_signature='', out_signature='')
156         def Connect(self):
157                 print "Connect session"
158                 try:
159                         self.session.Connect()
160                 except dbus.DBusException, e:
161                         if e.get_dbus_name() in ['net.connman.Error.InProgress',
162                                                  'net.connman.Error.AlreadyConnected']:
163                                 print e.get_dbus_message()
164                                 return
165                         traceback.print_exc()
166                         exit(1)
167
168         @dbus.service.method("com.example.TestSession",
169                                 in_signature='', out_signature='')
170         def Disconnect(self):
171                 print "Disconnect session"
172                 try:
173                         self.session.Disconnect()
174                 except dbus.DBusException, e:
175                         if e.get_dbus_name() in ['net.connman.Error.AlreadyDisabled',
176                                                  'net.connman.Error.NotConnected']:
177                                 print e.get_dbus_message()
178                                 return
179                         traceback.print_exc()
180                         exit(1)
181
182 def main():
183         if len(sys.argv) < 2:
184                 print "Usage: %s <command>" % (sys.argv[0])
185                 print ""
186                 print "  enable"
187                 print "  disable"
188                 print "  create <app_path>"
189                 print "  destroy <app_path>"
190                 print "  connect <app_path>"
191                 print "  disconnect <app_path>"
192                 print ""
193                 print "  run <app_path>"
194                 sys.exit(1)
195
196         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
197
198         if sys.argv[1] == "enable":
199                 bus = dbus.SystemBus()
200                 manager = dbus.Interface(bus.get_object("net.connman", "/"),
201                                          "net.connman.Manager")
202                 manager.SetProperty("SessionMode", True)
203                 return
204
205         elif sys.argv[1] == "disable":
206                 bus = dbus.SystemBus()
207                 manager = dbus.Interface(bus.get_object("net.connman", "/"),
208                                          "net.connman.Manager")
209                 manager.SetProperty("SessionMode", False)
210                 return
211
212         if (len(sys.argv) < 3):
213                 print "Need test application path"
214                 sys.exit(1)
215
216         app_path = sys.argv[2]
217         bus = dbus.SessionBus()
218
219         if sys.argv[1] == "run":
220                 name = dbus.service.BusName("com.example.SessionApplication", bus)
221                 mainloop = gobject.MainLoop()
222
223                 app = SessionApplication(bus, app_path, mainloop)
224
225                 mainloop.run()
226                 return
227
228         app = dbus.Interface(bus.get_object("com.example.SessionApplication", app_path),
229                              "com.example.TestSession")
230
231         if sys.argv[1] == "create":
232                 app.CreateSession()
233
234         elif sys.argv[1] == "destroy":
235                 app.DestroySession()
236
237         elif sys.argv[1] == "connect":
238                 app.Connect()
239
240         elif sys.argv[1] == "disconnect":
241                 app.Disconnect()
242
243 if __name__ == '__main__':
244         main()