def test_reg_noshake(self): """ Test registration without prior handshake """ # no handshake invalid ret = self.msg( self.ws, { "messageType": "register", "channelID": "reg_noshake_chan_1", "uaid": get_uaid("reg_noshake_uaid_1") }) self.compare_dict(ret, { "messageType": "register", "status": 401, "error": "Invalid Command" }) # valid ret = self.msg( self.ws, { "messageType": "hello", "channelIDs": ["reg_noshake_chan_1"], "uaid": get_uaid("reg_noshake_uaid_1") }) if allowDupes: ret = self.msg( self.ws, { "messageType": "register", "channelID": get_uaid("reg_noshake_chan_1") }) self.compare_dict(ret, {"messageType": "register", "status": 200}) self.validate_endpoint(ret['pushEndpoint']) #clean-up self.msg(self.ws, {"messageType": "purge"})
def test_reg_duplicate(self): """ Test registration with duplicate channel name """ uaid = get_uaid("reg_noshake_uaid_1") self.msg(self.ws, {"messageType": "hello", "channelIDs": [get_uaid("reg_noshake_chan_1")], "uaid": uaid}) if allowDupes: ret = self.msg(self.ws, {"messageType": "register", "channelID": "dupe_handshake"}) self.compare_dict(ret, {"messageType": "register", "status": 200}) # duplicate handshake ret = self.msg(self.ws, {"messageType": "register", "channelID": "dupe_handshake"}) self.compare_dict(ret, {"messageType": "register", "status": 200}) # passing in list to channelID ret = self.msg(self.ws, {"messageType": "register", "channelIDs": ["chan_list"]}) self.compare_dict(ret, {"messageType": "register", "status": 401, "error": "Invalid Command"}) self.msg(self.ws, {"messageType": "unregister", "channelID": "dupe_handshake"})
def test_empty_args(self): uaid = get_uaid("empty_uaid") ret = self.msg(self.ws, { "messageType": "", "channelIDs": ["CASE_UAID"], "uaid": uaid }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) # Test that an empty UAID after "hello" returns the same UAID tmp_uaid = get_uaid("empty_uaid") self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, { "messageType": "hello", "channelIDs": [], "uaid": tmp_uaid }) self.compare_dict(ret, {"status": 200, "messageType": "hello"}) ret = self.msg(self.ws, { "messageType": "hello", "channelIDs": [], "uaid": "" }) self.compare_dict(ret, { 'status': 200, "uaid": tmp_uaid, "messageType": "hello" }) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() self.ws = websocket.create_connection(self.url) #register (clearing the channel first in case it's already present) self.msg(self.ws, { "messageType": "hello", "channelIDs": [], "uaid": uaid }) ret = self.msg(self.ws, { "messageType": "register", "channelID": "EMPTY_ARG", "uaid": "" }) self.compare_dict(ret, {'status': 200, 'messageType': 'register'}) self.validate_endpoint(ret['pushEndpoint']) ret = self.msg(self.ws, { "messageType": "register", "channelID": "", "uaid": uaid }) self.compare_dict(ret, { "status": 401, "messageType": "register", "error": "Invalid Command" }) self.msg(self.ws, {"messageType": "purge"})
def test_reg_plural(self): """ Test registration with a lot of channels and uaids """ # XXX bug uaid can get overloaded with channels, # adding epoch to unique-ify it. if allowDupes: self.msg(self.ws, {"messageType": "hello", "channelIDs": ["reg_plural_chan"], "uaid": get_uaid("reg_plural")}) ret = self.msg(self.ws, {"messageType": "register", "channelID": "reg_plural_chan", "uaid": get_uaid("reg_plural")}) self.compare_dict(ret, {"messageType": "register", "status": 200}) # valid with same channelID ret = self.msg(self.ws, {"messageType": "register", "channelID": "reg_plural_chan"}) self.compare_dict(ret, {"messageType": "register", "status": 200}) # loop through different channelID values for dt in self.data_types: ret = self.msg(self.ws, {"messageType": "register", "channelID": "%s" % dt, "uaid": get_uaid("diff_uaid")}) if 'error' in ret: # lots of errors here, lots of gross logic to # validate them here continue self.compare_dict(ret, {"messageType": "register", "status": 200})
def test_case_ack(self): """ Test ack without handshake """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {"messageType": "acK", "channelIDs": [chan_id], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'})
def test_key_whitespace(self): """ Test leading and trailing whitespace in key name """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {" messageType ": "hello", "channelIDs": [chan_id], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'})
def test_uppercase_type(self): """ Test uppercase handshake message type """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {"messageType": "HELLO", "channelIDs": [chan_id], "uaid": uaid}) self.compare_dict(ret, {'status': 200, "messageType": "hello"})
def test_missing_type(self): """ Test missing message type """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {"messageType": "", "channelIDs": [chan_id], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'})
def test_ping(self): # Ping responses can contain any data. # The reference server returns the minimal data set "{}" ws2 = websocket.create_connection(self.url) # happy ret = self.msg(self.ws, {}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # happy ret = self.msg(self.ws, {'messageType': 'ping'}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # extra args ret = self.msg( self.ws, { 'messageType': 'ping', 'channelIDs': ['ping_chan'], 'uaid': get_uaid('ping_uaid'), 'nada': '' }) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # do a register between pings self.msg( ws2, { "messageType": "hello", "channelIDs": ["ping_chan_1"], "uaid": get_uaid("ping_uaid") }) ret = self.msg(ws2, { "messageType": "register", "channelID": "ping_chan_1a-ws2" }) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.msg(ws2, {"messageType": "purge"}) ws2.close() # send and ack too # XXX ack can hang socket # ret = self.msg(self.ws, {"messageType": "ack", # "updates": [{ "channelID": get_uaid("ping_chan_1"), # "version": 123 }]}) # self.compare_dict(ret, {"status": 200, "messageType": "ack"}) # empty braces is a valid ping ret = self.msg(self.ws, {}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) for ping in range(100): ret = self.msg(self.ws, {'messageType': 'ping'}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) #cleanup self.msg(self.ws, {"messageType": "purge"})
def test_case_ping(self): """ Test uppercase ping message type """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {"messageType": "PING", "channelIDs": [chan_id], "uaid": uaid}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200})
def test_ping(self): # Ping responses can contain any data. # The reference server returns the minimal data set "{}" ws2 = websocket.create_connection(self.url) # happy ret = self.msg(self.ws, {}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # happy ret = self.msg(self.ws, {'messageType': 'ping'}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # extra args ret = self.msg(self.ws, {'messageType': 'ping', 'channelIDs': ['ping_chan'], 'uaid': get_uaid('ping_uaid'), 'nada': ''}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) # do a register between pings self.msg(ws2, {"messageType": "hello", "channelIDs": ["ping_chan_1"], "uaid": get_uaid("ping_uaid")}) ret = self.msg(ws2, {"messageType": "register", "channelID": "ping_chan_1a-ws2"}) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.msg(ws2, {"messageType": "purge"}) ws2.close() # send and ack too # XXX ack can hang socket # ret = self.msg(self.ws, {"messageType": "ack", # "updates": [{ "channelID": get_uaid("ping_chan_1"), # "version": 123 }]}) # self.compare_dict(ret, {"status": 200, "messageType": "ack"}) # empty braces is a valid ping ret = self.msg(self.ws, {}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) for ping in range(100): ret = self.msg(self.ws, {'messageType': 'ping'}) if ret != {}: self.compare_dict(ret, {"messageType": "ping", "status": 200}) #cleanup self.msg(self.ws, {"messageType": "purge"})
def test_invalid_reg(self): """ Test invalid registration request """ uaid = get_uaid() chan_id = get_uaid() self.msg(self.ws, {"messageType": "hello", "channelIDs": [chan_id], "uaid": uaid}) ret = self.msg(self.ws, {"messageType": "registeR", "channelIDs": [chan_id], "uaiD": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'})
def test_too_many_chans(self): """ Test string limits for keys """ uaid = get_uaid() chan_id = get_uaid() self.msg(self.ws, {"messageType": "hello", "channelIDs": [chan_id], "uaid": uaid}) # hello 100 channels ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": self.big_uuid, "uaid": uaid}) self.compare_dict(ret, {"status": 503, "messageType": "hello"})
def test_type_case(self): """ Test message type field name case sensitivity """ uaid = get_uaid() chan_id = get_uaid() ret = self.msg(self.ws, {"messagetype": "hello", "channelIDs": [chan_id], "uaid": uaid}) if self.case_sensitive_keys: self.compare_dict(ret, {"status": 401, "error": "Invalid Command"}) else: self.compare_dict(ret, {"messageType": "hello", "status": 200})
def test_reg_all_chans(self): uaid = get_uaid() chan_id = get_uaid() self.msg(self.ws, {"messageType": "hello", "channelIDs": [chan_id], "uaid": uaid}) # register 100 channels for chan in self.big_uuid: ret = self.msg(self.ws, {"messageType": "register", "channelID": chan, "uaid": uaid}) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.validate_endpoint(ret['pushEndpoint'])
def test_hello_bad_types(self): """ Test handshake messageType with lots of data types """ for dt in self.data_types: tmp_uaid = get_uaid("uaid") verify_json = { "messageType": ("%s" % dt).lower(), "status": 401, "uaid": tmp_uaid } ret = self.msg( self.ws, { "messageType": ('%s' % dt).lower(), "channelIDs": [], "uaid": tmp_uaid }) if dt == 'HeLLO': verify_json["status"] = 200 else: verify_json["error"] = "Invalid Command" self.compare_dict(ret, verify_json) # sending non self.strings to make sure it doesn't break server self.ws.send('{"messageType": 123}') self.compare_dict(json.loads(self.ws.recv()), {"status": 401}) try: self.ws.send('{"messageType": null}') except Exception, e: print 'Exception', e
def test_ack(self): """ Test ack """ # no hello ret = self.msg(self.ws, {"messageType": "ack", "updates": [{"channelID": "ack_chan_1", "version": 23}]}) self.compare_dict(ret, {"error": "Invalid Command", "status": 401, "messageType": "ack"}) self.assertEqual(ret["updates"][0]["channelID"], "ack_chan_1") self.assertEqual(ret["updates"][0]["version"], 23) # happy path self.msg(self.ws, {"messageType": "hello", "channelIDs": ["ack_chan_1"], "uaid": get_uaid("ack_uaid")}) reg = self.msg(self.ws, {"messageType": "register", "channelID": "ack_chan_1"}) assert (reg["pushEndpoint"] is not None) # send an http PUT request to the endpoint send_http_put(reg['pushEndpoint']) # this blocks the socket on read # print 'RECV', self.ws.recv() # hanging socket against AWS ret = self.msg(self.ws, {"messageType": "ack", "updates": [{"channelID": "ack_chan_1", "version": 23}]}) self.compare_dict(ret, {"messageType": "notification"}) self.assertEqual(ret["updates"][0]["channelID"], "ack_chan_1")
def test_chan_limits(self): """ Test string limits for keys """ uaid = get_uaid("chan_limit_uaid") self.msg(self.ws, {"messageType": "hello", "channelIDs": ["chan_limits"], "uaid": uaid}) ret = self.msg(self.ws, {"messageType": "register", "channelID": "%s" % self.chan_150[:101]}) self.compare_dict(ret, {"status": 401, "messageType": "register", "error": "Invalid Command"}) ret = self.msg(self.ws, {"messageType": "register", "channelID": "%s" % self.chan_150[:100]}) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.validate_endpoint(ret['pushEndpoint']) # hello 100 channels ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": self.big_uuid, "uaid": uaid}) self.compare_dict(ret, {"status": 503, "messageType": "hello"}) # register 100 channels for chan in self.big_uuid: ret = self.msg(self.ws, {"messageType": "register", "channelID": chan, "uaid": uaid}) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.validate_endpoint(ret['pushEndpoint'])
def test_empty_args(self): uaid = get_uaid("empty_uaid") ret = self.msg(self.ws, {"messageType": "", "channelIDs": ["CASE_UAID"], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) # Test that an empty UAID after "hello" returns the same UAID tmp_uaid = get_uaid("empty_uaid") self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": tmp_uaid}) self.compare_dict(ret, {"status": 200, "messageType": "hello"}) ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": ""}) self.compare_dict(ret, {'status': 200, "uaid": tmp_uaid, "messageType": "hello"}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() self.ws = websocket.create_connection(self.url) #register (clearing the channel first in case it's already present) self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": uaid}) ret = self.msg(self.ws, {"messageType": "register", "channelID": "EMPTY_ARG", "uaid": ""}) self.compare_dict(ret, {'status': 200, 'messageType': 'register'}) self.validate_endpoint(ret['pushEndpoint']) ret = self.msg(self.ws, {"messageType": "register", "channelID": "", "uaid": uaid}) self.compare_dict(ret, {"status": 401, "messageType": "register", "error": "Invalid Command"}) self.msg(self.ws, {"messageType": "purge"})
def opened(self): super(WsClient, self).opened() self.client_type = get_prob(self.client_types) logger.debug(self.client_type) self.sleep = get_rand(self.max_sleep) self.chan = str_gen(8) self.uaid = get_uaid() self.version = int(str_gen(8)) self.start_time = time.time() self.hello()
def test_large_chan_id(self): uaid = get_uaid() self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": uaid}) chan_id = str_gen(17) ret = self.msg(self.ws, {"messageType": "register", "channelID": chan_id}) self.compare_dict(ret, {"status": 401, "messageType": "register", "error": "Invalid Command"})
def test_reg_plural(self): """ Test registration with a lot of channels and uaids """ # XXX bug uaid can get overloaded with channels, # adding epoch to unique-ify it. if allowDupes: self.msg( self.ws, { "messageType": "hello", "channelIDs": ["reg_plural_chan"], "uaid": get_uaid("reg_plural") }) ret = self.msg( self.ws, { "messageType": "register", "channelID": "reg_plural_chan", "uaid": get_uaid("reg_plural") }) self.compare_dict(ret, {"messageType": "register", "status": 200}) # valid with same channelID ret = self.msg(self.ws, { "messageType": "register", "channelID": "reg_plural_chan" }) self.compare_dict(ret, {"messageType": "register", "status": 200}) # loop through different channelID values for dt in self.data_types: ret = self.msg( self.ws, { "messageType": "register", "channelID": "%s" % dt, "uaid": get_uaid("diff_uaid") }) if 'error' in ret: # lots of errors here, lots of gross logic to # validate them here continue self.compare_dict(ret, {"messageType": "register", "status": 200})
def test_empty_chan_id(self): """ Test empty channel ID following registration """ uaid = get_uaid() chan_id = get_uaid() self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": uaid}) ret = self.msg(self.ws, {"messageType": "register", "channelID": chan_id, # uaid is an extra field that should be ignored. "uaid": ""}) self.compare_dict(ret, {'status': 200, 'messageType': 'register'}) self.validate_endpoint(ret['pushEndpoint']) ret = self.msg(self.ws, {"messageType": "register", "channelID": "", "uaid": uaid}) self.compare_dict(ret, {"status": 401, "messageType": "register", "error": "Invalid Command"})
def test_chans_case(self): """ Test channel IDs field name case sensitivity """ uaid = get_uaid() ret = self.msg(self.ws, {"messageType": "hello", "ChannelIds": ["CASE_UAID"], "uaid": uaid}) if self.case_sensitive_keys: self.compare_dict(ret, {"status": 401, "error": "Invalid Command"}) else: self.compare_dict(ret, {"messageType": "hello", "status": 200})
def test_reg_noshake(self): """ Test registration without prior handshake """ # no handshake invalid ret = self.msg(self.ws, {"messageType": "register", "channelID": "reg_noshake_chan_1", "uaid": get_uaid("reg_noshake_uaid_1")}) self.compare_dict(ret, {"messageType": "register", "status": 401, "error": "Invalid Command"}) # valid ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": ["reg_noshake_chan_1"], "uaid": get_uaid("reg_noshake_uaid_1")}) if allowDupes: ret = self.msg(self.ws, {"messageType": "register", "channelID": get_uaid("reg_noshake_chan_1")}) self.compare_dict(ret, {"messageType": "register", "status": 200}) self.validate_endpoint(ret['pushEndpoint']) #clean-up self.msg(self.ws, {"messageType": "purge"})
def test_reg_duplicate(self): """ Test registration with duplicate channel name """ uaid = get_uaid("reg_noshake_uaid_1") self.msg( self.ws, { "messageType": "hello", "channelIDs": [get_uaid("reg_noshake_chan_1")], "uaid": uaid }) if allowDupes: ret = self.msg(self.ws, { "messageType": "register", "channelID": "dupe_handshake" }) self.compare_dict(ret, {"messageType": "register", "status": 200}) # duplicate handshake ret = self.msg(self.ws, { "messageType": "register", "channelID": "dupe_handshake" }) self.compare_dict(ret, {"messageType": "register", "status": 200}) # passing in list to channelID ret = self.msg(self.ws, { "messageType": "register", "channelIDs": ["chan_list"] }) self.compare_dict(ret, { "messageType": "register", "status": 401, "error": "Invalid Command" }) self.msg(self.ws, { "messageType": "unregister", "channelID": "dupe_handshake" })
def test_ack(self): """ Test ack """ # no hello ret = self.msg( self.ws, { "messageType": "ack", "updates": [{ "channelID": "ack_chan_1", "version": 23 }] }) self.compare_dict(ret, { "error": "Invalid Command", "status": 401, "messageType": "ack" }) self.assertEqual(ret["updates"][0]["channelID"], "ack_chan_1") self.assertEqual(ret["updates"][0]["version"], 23) # happy path self.msg( self.ws, { "messageType": "hello", "channelIDs": ["ack_chan_1"], "uaid": get_uaid("ack_uaid") }) reg = self.msg(self.ws, { "messageType": "register", "channelID": "ack_chan_1" }) assert (reg["pushEndpoint"] is not None) # send an http PUT request to the endpoint send_http_put(reg['pushEndpoint']) # this blocks the socket on read # print 'RECV', self.ws.recv() # hanging socket against AWS ret = self.msg( self.ws, { "messageType": "ack", "updates": [{ "channelID": "ack_chan_1", "version": 23 }] }) self.compare_dict(ret, {"messageType": "notification"}) self.assertEqual(ret["updates"][0]["channelID"], "ack_chan_1")
def test_empty_uaid(self): """ Test empty UAID following handshake """ tmp_uaid = get_uaid() ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": tmp_uaid}) self.compare_dict(ret, {"status": 200, "messageType": "hello"}) # Omitting the UAID after the handshake should return the # original UAID. ret = self.msg(self.ws, {"messageType": "hello", "channelIDs": [], "uaid": ""}) self.compare_dict(ret, {'status': 200, "uaid": tmp_uaid, "messageType": "hello"})
def test_chan_limits(self): """ Test string limits for keys """ uaid = get_uaid("chan_limit_uaid") self.msg(self.ws, { "messageType": "hello", "channelIDs": ["chan_limits"], "uaid": uaid }) ret = self.msg(self.ws, { "messageType": "register", "channelID": "%s" % self.chan_150[:101] }) self.compare_dict(ret, { "status": 401, "messageType": "register", "error": "Invalid Command" }) ret = self.msg(self.ws, { "messageType": "register", "channelID": "%s" % self.chan_150[:100] }) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.validate_endpoint(ret['pushEndpoint']) # hello 100 channels ret = self.msg(self.ws, { "messageType": "hello", "channelIDs": self.big_uuid, "uaid": uaid }) self.compare_dict(ret, {"status": 503, "messageType": "hello"}) # register 100 channels for chan in self.big_uuid: ret = self.msg(self.ws, { "messageType": "register", "channelID": chan, "uaid": uaid }) self.compare_dict(ret, {"status": 200, "messageType": "register"}) self.validate_endpoint(ret['pushEndpoint'])
def test_hello_bad_types(self): """ Test handshake messageType with lots of data types """ for dt in self.data_types: tmp_uaid = get_uaid("uaid") verify_json = {"messageType": ("%s" % dt).lower(), "status": 401, "uaid": tmp_uaid} ret = self.msg(self.ws, {"messageType": ('%s' % dt).lower(), "channelIDs": [], "uaid": tmp_uaid}) if dt == 'HeLLO': verify_json["status"] = 200 else: verify_json["error"] = "Invalid Command" self.compare_dict(ret, verify_json) # sending non self.strings to make sure it doesn't break server self.ws.send('{"messageType": 123}') self.compare_dict(json.loads(self.ws.recv()), {"status": 401}) try: self.ws.send('{"messageType": null}') except Exception, e: print 'Exception', e
def test_two_chan(self): self.uaid = get_uaid() self.chan1_id = get_uaid() self.chan1 = "" self.chan2_id = get_uaid() self.chan2 = "" def on_close(ws): self.log('on_close:') def _assert_equal(a, b): """ Running socket swallows asserts, force an exit on mismatch """ if a != b: self.log('ERROR: value mismatch:', '%s != %s' % (a, b)) exit('assert equal mismatch: %s != %s' % (a, b)) def _check_updates(updates_list, chan1_val="", chan2_val=""): for chan in updates_list: if chan1_val != "" and chan["channelID"] == self.chan1_id: _assert_equal(chan["version"], chan1_val) if chan2_val != "" and chan["channelID"] == self.chan2_id: _assert_equal(chan["version"], chan2_val) return def on_message(ws, message): ret = json.loads(message) if ws.state == 'purge': ws.close() return if len(ret) == 0: # skip blank records return self.log('on_msg:', ret) self.log('state', ws.state) if ws.state == 'hello': reg_chan(ws, 'register1', self.chan1_id) elif ws.state == 'register1': # register chan1 self.chan1 = ret.get("pushEndpoint") self.log('self.chan1', self.chan1) reg_chan(ws, 'register2', self.chan2_id) elif ws.state == 'register2': # register chan2 self.chan2 = ret.get("pushEndpoint") self.log('self.chan2', self.chan2) # http put to chan1 send_update(self.chan1, 'version=12345789', 'update1') elif ws.state == 'update1': #verify chan1 self.compare_dict(ret, {"messageType": "notification"}, True) _assert_equal(ret["updates"][0]["channelID"], self.chan1_id) _check_updates(ret["updates"], 12345789) # http put to chan2 send_update(self.chan2, 'version=987654321', 'update2') elif ws.state == 'update2': #verify chan1 and chan2 self.compare_dict(ret, {"messageType": "notification"}, True) _check_updates(ret["updates"], 12345789, 987654321) send_ack(ws, ret["updates"]) # http put to chan1 send_update(self.chan1, 'version=1', 'update3') elif ws.state == 'update3': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) _check_updates(ret["updates"], 1) send_ack(ws, ret["updates"]) # http put to chan2 send_update(self.chan2, 'version=0', 'update4', 'text/plain') elif ws.state == 'update4': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) for chan in ret["updates"]: if chan["channelID"] == self.chan2_id: # check if 0 version returns epoch _assert_equal(len(str(chan["version"])), 10) send_ack(ws, ret["updates"]) # http put to chan2 with invalid content-type, results in epoch send_update(self.chan1, 'version=999', 'update5', 'application/json') elif ws.state == 'update5': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) for chan in ret["updates"]: if chan["channelID"] == self.chan1_id: # check if 0 version returns epoch _assert_equal(len(str(chan["version"])), 10) send_ack(ws, ret["updates"]) ws.state="purge" self.msg(ws, {"messageType": "purge"}, cb=False) def on_open(ws): self.log('open:', ws) setup_chan(ws) def on_error(ws, error): self.log('on error') ws.close() raise AssertionError(error) def setup_chan(ws): ws.state = 'hello' self.msg(ws, {"messageType": "hello", "channelIDs": [], "uaid": self.uaid}, cb=False) self.msg(ws, {"messageType": "purge"}, cb=False) def reg_chan(ws, state, chan_str): ws.state = state self.msg(ws, {"messageType": "register", "channelID": chan_str, "uaid": self.uaid}, cb=False) def send_ack(ws, updates_list): # there should be no response from ack self.log('Sending ACK') self.msg(ws, {"messageType": "ack", "updates": updates_list, "uaid": self.uaid}, cb=False) def send_update(update_url, str_data, state='update1', ct='application/x-www-form-urlencoded'): if update_url == '': raise AssertionError("No update_url found") ws.state = state resp = send_http_put(update_url, str_data, ct, True) _assert_equal(resp, 200) ws = websocket.WebSocketApp(self.url, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close) ws.run_forever()
def test_key_case(self): """ Test key case sensitivity """ self.ws = websocket.create_connection(self.url) ret = self.msg( self.ws, { "messagetype": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID") }) self.compare_dict(ret, {"status": 401, "error": "Invalid Command"}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # leading trailing spaces self.ws = websocket.create_connection(self.url) ret = self.msg( self.ws, { " messageType ": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID") }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # Cap channelIds self.ws = websocket.create_connection(self.url) ret = self.msg( self.ws, { "messageType": "hello", "ChannelIds": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID") }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # all cap hello self.ws = websocket.create_connection(self.url) ret = self.msg( self.ws, { "messageType": "HELLO", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID") }) self.compare_dict(ret, {'status': 200, "messageType": "hello"}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # bad register case self.ws = websocket.create_connection(self.url) uaid = get_uaid("CASE_UAID") self.msg( self.ws, { "messageType": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID") }) ret = self.msg(self.ws, { "messageType": "registeR", "channelIDs": ["CASE_UAID"], "uaiD": uaid }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) # test ack self.msg(self.ws, { "messageType": "acK", "channelIDs": ["CASE_UAID"], "uaid": uaid }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) # test ping self.msg(self.ws, { "messageType": "PING", "channelIDs": ["CASE_UAID"], "uaid": uaid }) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"})
def test_key_case(self): """ Test key case sensitivity """ self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, {"messagetype": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID")}) self.compare_dict(ret, {"status": 401, "error": "Invalid Command"}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # leading trailing spaces self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, {" messageType ": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID")}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # Cap channelIds self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, {"messageType": "hello", "ChannelIds": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID")}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # all cap hello self.ws = websocket.create_connection(self.url) ret = self.msg(self.ws, {"messageType": "HELLO", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID")}) self.compare_dict(ret, {'status': 200, "messageType": "hello"}) self.msg(self.ws, {"messageType": "purge"}) self.ws.close() # bad register case self.ws = websocket.create_connection(self.url) uaid = get_uaid("CASE_UAID") self.msg(self.ws, {"messageType": "hello", "channelIDs": ["CASE_UAID"], "uaid": get_uaid("CASE_UAID")}) ret = self.msg(self.ws, {"messageType": "registeR", "channelIDs": ["CASE_UAID"], "uaiD": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) # test ack self.msg(self.ws, {"messageType": "acK", "channelIDs": ["CASE_UAID"], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) # test ping self.msg(self.ws, {"messageType": "PING", "channelIDs": ["CASE_UAID"], "uaid": uaid}) self.compare_dict(ret, {'status': 401, 'error': 'Invalid Command'}) self.msg(self.ws, {"messageType": "purge"})
def test_two_chan(self): self.chan1 = "" self.chan2 = "" self.uaid = get_uaid('notify') def on_close(ws): self.log('on_close:') def _assert_equal(a, b): """ Running socket swallows asserts, force an exit on mismatch """ if a != b: self.log('ERROR: value mismatch:', '%s != %s' % (a, b)) exit('assert equal mismatch: %s != %s' % (a, b)) def _check_updates(updates_list, chan1_val="", chan2_val=""): for chan in updates_list: if chan1_val != "" and chan["channelID"] == "tn.chan1": _assert_equal(chan["version"], chan1_val) if chan2_val != "" and chan["channelID"] == "tn.chan2": _assert_equal(chan["version"], chan2_val) return def on_message(ws, message): ret = json.loads(message) if ws.state == 'purge': ws.close() return if len(ret) == 0: # skip blank records return self.log('on_msg:', ret) self.log('state', ws.state) if ws.state == 'hello': reg_chan(ws, 'register1', 'tn_chan1') elif ws.state == 'register1': # register chan1 self.chan1 = ret.get("pushEndpoint") self.log('self.chan1', self.chan1) reg_chan(ws, 'register2', 'tn_chan2') elif ws.state == 'register2': # register chan2 self.chan2 = ret.get("pushEndpoint") self.log('self.chan2', self.chan2) # http put to chan1 send_update(self.chan1, 'version=12345789', 'update1') elif ws.state == 'update1': #verify chan1 self.compare_dict(ret, {"messageType": "notification"}, True) _assert_equal(ret["updates"][0]["channelID"], "tn_chan1") _check_updates(ret["updates"], 12345789) # http put to chan2 send_update(self.chan2, 'version=987654321', 'update2') elif ws.state == 'update2': #verify chan1 and chan2 self.compare_dict(ret, {"messageType": "notification"}, True) _check_updates(ret["updates"], 12345789, 987654321) send_ack(ws, ret["updates"]) # http put to chan1 send_update(self.chan1, 'version=1', 'update3') elif ws.state == 'update3': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) _check_updates(ret["updates"], 1) send_ack(ws, ret["updates"]) # http put to chan2 send_update(self.chan2, 'version=0', 'update4', 'text/plain') elif ws.state == 'update4': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) for chan in ret["updates"]: if chan["channelID"] == "tn.chan2": # check if 0 version returns epoch _assert_equal(len(str(chan["version"])), 10) send_ack(ws, ret["updates"]) # http put to chan2 with invalid content-type, results in epoch send_update(self.chan1, 'version=999', 'update5', 'application/json') elif ws.state == 'update5': # update the same channel self.compare_dict(ret, {"messageType": "notification"}, True) for chan in ret["updates"]: if chan["channelID"] == "tn.chan1": # check if 0 version returns epoch _assert_equal(len(str(chan["version"])), 10) send_ack(ws, ret["updates"]) ws.state = "purge" self.msg(ws, {"messageType": "purge"}, cb=False) def on_open(ws): self.log('open:', ws) setup_chan(ws) def on_error(ws): self.log('on error') ws.close() raise AssertionError(ws) def setup_chan(ws): ws.state = 'hello' self.msg(ws, { "messageType": "hello", "channelIDs": [], "uaid": self.uaid }, cb=False) self.msg(ws, {"messageType": "purge"}, cb=False) def reg_chan(ws, state, chan_str): ws.state = state self.msg(ws, { "messageType": "register", "channelID": chan_str, "uaid": self.uaid }, cb=False) def send_ack(ws, updates_list): # there should be no response from ack self.log('Sending ACK') self.msg(ws, { "messageType": "ack", "updates": updates_list, "uaid": self.uaid }, cb=False) def send_update(update_url, str_data, state='update1', ct='application/x-www-form-urlencoded'): if update_url == '': raise AssertionError("No update_url found") ws.state = state resp = send_http_put(update_url, str_data, ct, True) _assert_equal(resp, 200) ws = websocket.WebSocketApp(self.url, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close) ws.run_forever()