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
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# 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
choice = int(choice)
if choice >= 1 and choice <= len(messages):
return messages[choice - 1][1]
# 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 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)
]
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()
reply_length_bytes = child.stdout.read(4)
if len(reply_length_bytes) < 4:
print 'Invalid message length'
break
reply_length = struct.unpack('i', reply_length_bytes)[0]
reply = child.stdout.read(reply_length).decode('utf-8')
print 'Reply: ' + reply
if len(reply) != reply_length:
print 'Invalid reply length'
break
if __name__ == '__main__':
main()
|