Skip to content

Commit

Permalink
Check for empty unique ID
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo <lorenzo.donini90@gmail.com>
  • Loading branch information
lorenzodonini committed Apr 2, 2024
1 parent e5ac6a5 commit 9b48003
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ocppj/ocppj.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ func (endpoint *Endpoint) ParseMessage(arr []interface{}, pendingRequestState Cl
if !ok {
return nil, ocpp.NewError(FormatErrorType(endpoint), fmt.Sprintf("Invalid element %v at 1, expected unique ID (string)", arr[1]), uniqueId)
}
if uniqueId == "" {
return nil, ocpp.NewError(FormatErrorType(endpoint), "Invalid unique ID, cannot be empty", uniqueId)
}
// Parse message
if typeId == CALL {
if len(arr) != 4 {
Expand Down
16 changes: 16 additions & 0 deletions ocppj/ocppj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,22 @@ func (suite *OcppJTestSuite) TestParseMessageInvalidMessageId() {
assert.Equal(t, fmt.Sprintf("Invalid element %v at 1, expected unique ID (string)", invalidMessageId), protoErr.Description)
}

func (suite *OcppJTestSuite) TestParseMessageEmptyMessageID() {
t := suite.T()
mockMessage := make([]interface{}, 3)
// Test invalid message length
mockMessage[0] = float64(ocppj.CALL_RESULT) // Message Type ID
mockMessage[1] = "" // Empty ID
message, err := suite.chargePoint.ParseMessage(mockMessage, suite.chargePoint.RequestState)
require.Nil(t, message)
require.Error(t, err)
protoErr := err.(*ocpp.Error)
require.NotNil(t, protoErr)
suite.Equal("", protoErr.MessageId)
suite.Equal(ocppj.FormatErrorType(suite.chargePoint), protoErr.Code)
suite.Errorf(protoErr, "Invalid unique ID, cannot be empty")
}

func (suite *OcppJTestSuite) TestParseMessageUnknownTypeId() {
t := suite.T()
mockMessage := make([]interface{}, 3)
Expand Down

0 comments on commit 9b48003

Please sign in to comment.