-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stricter checking when decoding with .from_payload()
Earlier, it was possible that a protobuf payload not including a certain message was decoded using .from_payload(). For example when using GetConfigsRequest.from_payload() with a message missing GenericMessage::wirepas::get_configs_req, the library was not reporting an error and setting the req_id to 0 even though it was not initialized in the protobuf message. If that happens now, and InvalidMessageType exception is raised. This exception has the GatewayAPIParsingException as its parent to help with backwards compatibility. Also refactoring the tests and having a new test file for decoding related error testing.
- Loading branch information
1 parent
c66bfe9
commit afa7ad0
Showing
14 changed files
with
77 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# flake8: noqa | ||
|
||
import pytest | ||
|
||
import wirepas_mesh_messaging | ||
from wirepas_mesh_messaging.proto import GenericMessage | ||
from wirepas_mesh_messaging.wirepas_exceptions import ( | ||
GatewayAPIParsingException, | ||
InvalidMessageType, | ||
) | ||
|
||
MESSAGE_CLASSES = [ | ||
wirepas_mesh_messaging.GetConfigsRequest, | ||
wirepas_mesh_messaging.GetConfigsResponse, | ||
wirepas_mesh_messaging.GetGatewayInfoRequest, | ||
wirepas_mesh_messaging.GetGatewayInfoResponse, | ||
wirepas_mesh_messaging.GetScratchpadStatusRequest, | ||
wirepas_mesh_messaging.GetScratchpadStatusResponse, | ||
wirepas_mesh_messaging.ProcessScratchpadRequest, | ||
wirepas_mesh_messaging.ProcessScratchpadResponse, | ||
wirepas_mesh_messaging.ReceivedDataEvent, | ||
wirepas_mesh_messaging.SendDataRequest, | ||
wirepas_mesh_messaging.SendDataResponse, | ||
wirepas_mesh_messaging.SetConfigRequest, | ||
wirepas_mesh_messaging.SetConfigResponse, | ||
wirepas_mesh_messaging.SetScratchpadTargetAndActionRequest, | ||
wirepas_mesh_messaging.SetScratchpadTargetAndActionResponse, | ||
wirepas_mesh_messaging.StatusEvent, | ||
wirepas_mesh_messaging.UploadScratchpadRequest, | ||
wirepas_mesh_messaging.UploadScratchpadResponse, | ||
] | ||
|
||
|
||
def _get_payload_excluding_message_type(message_class): | ||
if message_class == wirepas_mesh_messaging.GetConfigsRequest: | ||
return wirepas_mesh_messaging.GetGatewayInfoRequest().payload | ||
|
||
return wirepas_mesh_messaging.GetConfigsRequest().payload | ||
|
||
|
||
@pytest.mark.parametrize("message_class", MESSAGE_CLASSES) | ||
def test_decoding_errors(message_class): | ||
invalid_protobuf_message = bytes([0]) | ||
|
||
with pytest.raises(GatewayAPIParsingException, match="Cannot decode"): | ||
message_class.from_payload(invalid_protobuf_message) | ||
|
||
|
||
@pytest.mark.parametrize("message_class", MESSAGE_CLASSES) | ||
def test_decoding_wrong_message_type(message_class): | ||
payload = _get_payload_excluding_message_type(message_class) | ||
|
||
with pytest.raises(InvalidMessageType, match=message_class.__name__): | ||
message_class.from_payload(payload) | ||
|
||
|
||
@pytest.mark.parametrize("message_class", MESSAGE_CLASSES) | ||
def test_decoding_missing_message_type(message_class): | ||
message = GenericMessage() | ||
message.wirepas.SetInParent() | ||
payload = message.SerializeToString() | ||
|
||
with pytest.raises(InvalidMessageType, match=message_class.__name__): | ||
message_class.from_payload(payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters