forked from mvidner/cnetworkmanager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnm-mock
executable file
·184 lines (153 loc) · 6.04 KB
/
nm-mock
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
# -*- coding: utf-8 -*-
# TODO: adapt nm lib to connect to session bus too, and try this mock
import dbus
import dbus.service
import _dbus_bindings
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import networkmanager
import networkmanager.device
from networkmanager.base import Bus
class DBusMock(dbus.service.Object):
def __init__(self, bus, opath, bus_name=None):
print "INIT:", self.__class__, opath
if bus_name != None:
NEE = dbus.exceptions.NameExistsException
try:
bus_name = dbus.service.BusName(bus_name, bus,
replace_existing=True,
do_not_queue=True)
except NEE:
raise NEE("%s (pid %d)" % (bus_name, self.service_pid(bus_name)))
super(DBusMock, self).__init__(bus, opath, bus_name)
self.properties = {}
@staticmethod
def service_pid(name):
bus = Bus()
DBS = 'org.freedesktop.DBus'
DBI = DBS
dbo = bus.get_object(DBS, '/')
dbi = dbus.Interface(dbo, DBI)
owner = dbi.GetNameOwner(name)
pid = dbi.GetConnectionUnixProcessID(owner)
return pid
@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties",
in_signature="ss", out_signature="v")
def Get(self, interface, prop_name):
# real NM ignores the interface too
value = self.properties.get(prop_name, 42)
print "Get", self.__dbus_object_path__, prop_name, "->", value
return value
class NetworkManagerMock(DBusMock):
def __init__(self, bus):
opath = "/org/freedesktop/NetworkManager"
bus_name = "org.freedesktop.NetworkManager"
super(NetworkManagerMock, self).__init__(bus, opath, bus_name)
self.properties.update({
"State": networkmanager.NetworkManager.State.CONNECTED,
"ActiveConnections": ["/AC1"],
})
@dbus.service.method(dbus_interface="org.freedesktop.NetworkManager",
in_signature="", out_signature="ao")
def GetDevices(self):
return ["/D1", "/D5"]
class NetworkManagerSettingsMock(DBusMock):
def __init__(self, bus):
opath = "/org/freedesktop/NetworkManagerSettings"
bus_name = "org.freedesktop.NetworkManagerUserSettings"
super(NetworkManagerSettingsMock, self).__init__(bus, opath, bus_name)
@dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerSettings",
in_signature="", out_signature="ao")
def ListConnections(self):
return ["/C1", "/C2"]
class ConnectionMock(DBusMock):
def __init__(self, bus, opath):
super(ConnectionMock, self).__init__(bus, opath)
@dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection",
in_signature="", out_signature="a{sa{sv}}")
def GetSettings(self):
return {
"connection": {
# testing non-ASCII output: str vs unicode
"id": "moje síť", # "my net"
"type": "mocktype"
},
"section1": {
"key1": "value1",
"key2": "value2",
},
"section2": {
"key3": "value4",
"key4": "value4",
},
}
class DeviceMock(DBusMock):
def __init__(self, bus, opath):
super(DeviceMock, self).__init__(bus, opath)
self.properties.update({
"Udi": "/hal/udi",
"Driver": "mock driver",
"HwAddress": "11:22:33:44:55:66",
"Ip4Config": "/IC1",
})
class WiredDeviceMock(DeviceMock):
def __init__(self, bus, opath):
super(WiredDeviceMock, self).__init__(bus, opath)
self.properties.update({
"DeviceType": networkmanager.Device.DeviceType.ETHERNET,
"Interface": "emock0",
})
class WirelessDeviceMock(DeviceMock):
def __init__(self, bus, opath):
super(WirelessDeviceMock, self).__init__(bus, opath)
self.properties.update({
"DeviceType": networkmanager.Device.DeviceType.WIRELESS,
"ActiveAccessPoint": "/AP1",
"Interface": "wmock0",
})
@dbus.service.method(dbus_interface="org.freedesktop.NetworkManager.Device.Wireless",
in_signature="", out_signature="ao")
def GetAccessPoints(self):
return ["/AP1", "/AP2"]
class ActiveConnectionMock(DBusMock):
def __init__(self, bus, opath):
super(ActiveConnectionMock, self).__init__(bus, opath)
self.properties.update({
"ServiceName": "org.freedesktop.NetworkManagerUserSettings",
"Connection": "/C1",
"SpecificObject": "/AP1",
"Devices": ["/D5"],
})
class AccessPointMock(DBusMock):
def __init__(self, bus, opath):
super(AccessPointMock, self).__init__(bus, opath)
self.properties.update({
"Ssid": dbus.ByteArray("mock-ssid"),
"HwAddress": "11:22:33:44:55:66",
})
class Ip4ConfigMock(DBusMock):
def __init__(self, bus, opath):
super(Ip4ConfigMock, self).__init__(bus, opath)
self.properties.update({
"Addresses": [(42, 43, 44)],
"Nameservers": [42, 43, 44],
"Domains": ["mockdom1", "mockdom2"],
"Routes": dbus.Array([], signature="au"), # format? fix nmcli
})
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
networkmanager.base.NM_BUS = dbus.SessionBus()
bus = Bus()
nmm = NetworkManagerMock(bus)
nms = NetworkManagerSettingsMock(bus)
c1 = ConnectionMock(bus, "/C1")
c2 = ConnectionMock(bus, "/C2")
ac = ActiveConnectionMock(bus, "/AC1")
d1 = WiredDeviceMock(bus, "/D1")
d5 = WirelessDeviceMock(bus, "/D5")
ap1 = AccessPointMock(bus, "/AP1")
ap2 = AccessPointMock(bus, "/AP2")
ic1 = Ip4ConfigMock(bus, "/IC1")
loop = gobject.MainLoop()
loop.run()