2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # Interactive test script for the Chromoting host native messaging component.
15 def PrintMenuAndGetBuilder(messages
):
17 for i
in range(0, len(messages
)):
18 print '%d: %s' % (i
+ 1, messages
[i
][0])
21 choice
= raw_input('Enter choice: ')
22 if choice
.lower() == 'q':
25 if choice
>= 1 and choice
<= len(messages
):
26 return messages
[choice
- 1][1]
29 # Message builder methods.
31 return {'type': 'hello'}
34 def BuildClearAllPairedClients():
35 return {'type': 'clearPairedClients'}
38 def BuildDeletePairedClient():
39 client_id
= raw_input('Enter client id: ')
40 return {'type': 'deletePairedClient',
41 'clientId': client_id
}
44 def BuildGetHostName():
45 return {'type': 'getHostName'}
48 def BuildGetPinHash():
49 host_id
= raw_input('Enter host id: ')
50 pin
= raw_input('Enter PIN: ')
51 return {'type': 'getPinHash',
56 def BuildGenerateKeyPair():
57 return {'type': 'generateKeyPair'}
60 def BuildUpdateDaemonConfig():
61 config_json
= raw_input('Enter config JSON: ')
62 return {'type': 'updateDaemonConfig',
63 'config': config_json
}
66 def BuildGetDaemonConfig():
67 return {'type': 'getDaemonConfig'}
70 def BuildGetPairedClients():
71 return {'type': 'getPairedClients'}
74 def BuildGetUsageStatsConsent():
75 return {'type': 'getUsageStatsConsent'}
78 def BuildStartDaemon():
80 consent
= raw_input('Report usage stats [y/n]? ')
81 if consent
.lower() == 'y':
83 elif consent
.lower() == 'n':
88 config_json
= raw_input('Enter config JSON: ')
89 return {'type': 'startDaemon',
91 'config': config_json
}
94 def BuildStopDaemon():
95 return {'type': 'stopDaemon'}
98 def BuildGetDaemonState():
99 return {'type': 'getDaemonState'}
103 if len(sys
.argv
) != 2:
104 print 'Usage: ' + sys
.argv
[0] + ' <path to native messaging host>'
107 native_messaging_host
= sys
.argv
[1]
109 child
= subprocess
.Popen(native_messaging_host
, stdin
=subprocess
.PIPE
,
110 stdout
=subprocess
.PIPE
, close_fds
=True)
115 ('Hello', BuildHello
),
116 ('Clear all paired clients', BuildClearAllPairedClients
),
117 ('Delete paired client', BuildDeletePairedClient
),
118 ('Get host name', BuildGetHostName
),
119 ('Get PIN hash', BuildGetPinHash
),
120 ('Generate key pair', BuildGenerateKeyPair
),
121 ('Update daemon config', BuildUpdateDaemonConfig
),
122 ('Get daemon config', BuildGetDaemonConfig
),
123 ('Get paired clients', BuildGetPairedClients
),
124 ('Get usage stats consent', BuildGetUsageStatsConsent
),
125 ('Start daemon', BuildStartDaemon
),
126 ('Stop daemon', BuildStopDaemon
),
127 ('Get daemon state', BuildGetDaemonState
)
129 builder
= PrintMenuAndGetBuilder(messages
)
132 message_dict
= builder()
133 message_dict
['id'] = message_id
134 message
= json
.dumps(message_dict
)
136 print 'Message: ' + message
137 child
.stdin
.write(struct
.pack('I', len(message
)))
138 child
.stdin
.write(message
)
141 reply_length_bytes
= child
.stdout
.read(4)
142 if len(reply_length_bytes
) < 4:
143 print 'Invalid message length'
145 reply_length
= struct
.unpack('i', reply_length_bytes
)[0]
146 reply
= child
.stdout
.read(reply_length
).decode('utf-8')
147 print 'Reply: ' + reply
148 if len(reply
) != reply_length
:
149 print 'Invalid reply length'
152 if __name__
== '__main__':