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
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Interactive test script for the Chromoting host native messaging component.
import json
import readline
import struct
import subprocess
import sys
def PrintMenuAndGetBuilder(messages):
print
for i in range(0, len(messages)):
print '%d: %s' % (i + 1, messages[i][0])
print 'Q: Quit'
while True:
choice = raw_input('Enter choice: ')
if choice.lower() == 'q':
return None
try:
choice = int(choice)
except ValueError:
continue
if choice >= 1 and choice <= len(messages):
return messages[choice - 1][1]
def ReadMessage(in_stream):
reply_length_bytes = in_stream.read(4)
if len(reply_length_bytes) < 4:
print 'Invalid message length'
return None
reply_length = struct.unpack('i', reply_length_bytes)[0]
reply = in_stream.read(reply_length).decode('utf-8')
if len(reply) != reply_length:
print 'Invalid reply length'
return None
return json.loads(reply)
# Message builder methods.
def BuildHello():
return {'type': 'hello'}
def BuildClearAllPairedClients():
return {'type': 'clearPairedClients'}
def BuildDeletePairedClient():
client_id = raw_input('Enter client id: ')
return {'type': 'deletePairedClient',
'clientId': client_id}
def BuildGetHostName():
return {'type': 'getHostName'}
def BuildGetPinHash():
host_id = raw_input('Enter host id: ')
pin = raw_input('Enter PIN: ')
return {'type': 'getPinHash',
'hostId': host_id,
'pin': pin}
def BuildGenerateKeyPair():
return {'type': 'generateKeyPair'}
def BuildUpdateDaemonConfig():
config_json = raw_input('Enter config JSON: ')
return {'type': 'updateDaemonConfig',
'config': config_json}
def BuildGetDaemonConfig():
return {'type': 'getDaemonConfig'}
def BuildGetPairedClients():
return {'type': 'getPairedClients'}
def BuildGetUsageStatsConsent():
return {'type': 'getUsageStatsConsent'}
def BuildStartDaemon():
while True:
consent = raw_input('Report usage stats [y/n]? ')
if consent.lower() == 'y':
consent = True
elif consent.lower() == 'n':
consent = False
else:
continue
break
config_json = raw_input('Enter config JSON: ')
return {'type': 'startDaemon',
'consent': consent,
'config': config_json}
def BuildStopDaemon():
return {'type': 'stopDaemon'}
def BuildGetDaemonState():
return {'type': 'getDaemonState'}
def BuildIt2MePermissionCheck():
return {'type': 'it2mePermissionCheck'}
def main():
if len(sys.argv) != 2:
print 'Usage: ' + sys.argv[0] + ' <path to native messaging host>'
sys.exit(1)
native_messaging_host = sys.argv[1]
child = subprocess.Popen(native_messaging_host, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, close_fds=True)
message_id = 0
while True:
messages = [
('Hello', BuildHello),
('Clear all paired clients', BuildClearAllPairedClients),
('Delete paired client', BuildDeletePairedClient),
('Get host name', BuildGetHostName),
('Get PIN hash', BuildGetPinHash),
('Generate key pair', BuildGenerateKeyPair),
('Update daemon config', BuildUpdateDaemonConfig),
('Get daemon config', BuildGetDaemonConfig),
('Get paired clients', BuildGetPairedClients),
('Get usage stats consent', BuildGetUsageStatsConsent),
('Start daemon', BuildStartDaemon),
('Stop daemon', BuildStopDaemon),
('Get daemon state', BuildGetDaemonState),
('It2Me permission check', BuildIt2MePermissionCheck),
]
builder = PrintMenuAndGetBuilder(messages)
if not builder:
break
message_dict = builder()
message_dict['id'] = message_id
message = json.dumps(message_dict)
message_id += 1
print 'Message: ' + message
child.stdin.write(struct.pack('I', len(message)))
child.stdin.write(message)
child.stdin.flush()
while True:
reply_dict = ReadMessage(child.stdout)
if reply_dict is None:
break
reply_pretty = json.dumps(reply_dict)
if reply_dict['type'] == '_debug_log':
print 'Log: ' + reply_pretty
continue
print 'Reply: ' + reply_pretty
break
if __name__ == '__main__':
main()
|