diff --git a/compliance/test_fuzzingclient.py b/compliance/test_fuzzingclient.py index 3dfd10c..7ff19fd 100644 --- a/compliance/test_fuzzingclient.py +++ b/compliance/test_fuzzingclient.py @@ -17,31 +17,29 @@ ws = websocket.create_connection(SERVER + "/getCaseCount") count = json.loads(ws.recv()) ws.close() -success = 0 -failed = 0 for case in range(1, count+1): url = SERVER + '/runCase?case={}&agent={}'.format(case, AGENT) status = websocket.STATUS_NORMAL try: ws = websocket.create_connection(url) - opcode, msg = ws.recv_data() - if opcode == websocket.ABNF.OPCODE_TEXT: - msg.decode("utf-8") - if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY): - ws.send(msg, opcode) - success += 1 + while True: + opcode, msg = ws.recv_data() + if opcode == websocket.ABNF.OPCODE_TEXT: + msg.decode("utf-8") + if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY): + ws.send(msg, opcode) except UnicodeDecodeError: # this case is ok. - success += 1 + status = websocket.STATUS_PROTOCOL_ERROR + except websocket.WebSocketProtocolException: status = websocket.STATUS_PROTOCOL_ERROR except Exception as e: - failed += 1 - status = websocket.STATUS_PROTOCOL_ERROR + # status = websocket.STATUS_PROTOCOL_ERROR print(traceback.format_exc()) finally: ws.close(status) -print("Ran {} test cases. success: {}, faield: {}".format(case, success, failed)) +print("Ran {} test cases.".format(case)) url = SERVER + '/updateReports?agent={}'.format(AGENT) ws = websocket.create_connection(url) diff --git a/websocket/_abnf.py b/websocket/_abnf.py index c5ef295..48a230a 100644 --- a/websocket/_abnf.py +++ b/websocket/_abnf.py @@ -105,22 +105,22 @@ class ABNF(object): validate the ABNF frame. """ if self.rsv1 or self.rsv2 or self.rsv3: - raise NotImplementedError("rsv is not implemented, yet") + raise WebSocketProtocolException("rsv is not implemented, yet") if self.opcode == ABNF.OPCODE_PING and not self.fin: - raise WebSocketException("Invalid ping frame.") + raise WebSocketProtocolException("Invalid ping frame.") if self.opcode == ABNF.OPCODE_CLOSE: l = len(self.data) if not l: return if l == 1 or l >= 126: - raise WebSocketException("Invalid close frame.") + raise WebSocketProtocolException("Invalid close frame.") if l > 2 and not validate_utf8(self.data[2:]): - raise WebSocketException("Invalid close frame.") + raise WebSocketProtocolException("Invalid close frame.") code = 256*six.byte2int(self.data[0]) + six.byte2int(self.data[1]) if not self._is_valid_close_status(code): - raise WebSocketException("Invalid close opcode.") + raise WebSocketProtocolException("Invalid close opcode.") def _is_valid_close_status(self, code): return code in VALID_CLOSE_STATUS or (3000 <= code <5000) diff --git a/websocket/_core.py b/websocket/_core.py index 9c2abf2..8dcbff7 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -698,12 +698,12 @@ class WebSocket(object): if not frame: # handle error: # 'NoneType' object has no attribute 'opcode' - raise WebSocketException("Not a valid frame %s" % frame) + raise WebSocketProtocolException("Not a valid frame %s" % frame) elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT): if not self._recving_frames and frame.opcode == ABNF.OPCODE_CONT: - raise WebSocketException("Illegal frame") + raise WebSocketProtocolException("Illegal frame") if self._recving_frames and frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY): - raise WebSocketException("Illegal frame") + raise WebSocketProtocolException("Illegal frame") if self._cont_data: self._cont_data[1] += frame.data @@ -720,7 +720,7 @@ class WebSocket(object): self._cont_data = None frame.data = data[1] if not self.fire_cont_frame and data[0] == ABNF.OPCODE_TEXT and not validate_utf8(frame.data): - raise WebSocketException("cannot decode: " + repr(frame.data)) + raise WebSocketProtocolException("cannot decode: " + repr(frame.data)) return [data[0], frame] elif frame.opcode == ABNF.OPCODE_CLOSE: @@ -730,7 +730,7 @@ class WebSocket(object): if len(frame.data) < 126: self.pong(frame.data) else: - raise WebSocketException("Protocol Error") + raise WebSocketProtocolException("Ping message is too long") if control_frame: return (frame.opcode, frame) elif frame.opcode == ABNF.OPCODE_PONG: diff --git a/websocket/_exceptions.py b/websocket/_exceptions.py index b0be191..85aa0e3 100644 --- a/websocket/_exceptions.py +++ b/websocket/_exceptions.py @@ -30,6 +30,11 @@ class WebSocketException(Exception): """ pass +class WebSocketProtocolException(WebSocketException): + """ + If the webscoket protocol is invalid, this exception will be raised. + """ + pass class WebSocketConnectionClosedException(WebSocketException): """ diff --git a/websocket/tests/test_websocket.py b/websocket/tests/test_websocket.py index ca901d0..4403fca 100644 --- a/websocket/tests/test_websocket.py +++ b/websocket/tests/test_websocket.py @@ -30,6 +30,7 @@ from websocket._utils import validate_utf8 # Skip test to access the internet. TEST_WITH_INTERNET = False +TEST_WITH_INTERNET = True # Skip Secure WebSocket test. TEST_SECURE_WS = False