Skip to content

Commit

Permalink
patch: update mock181's get, check for non-readable parameters
Browse files Browse the repository at this point in the history
During mock181's get:
- check for any non-readable params
- if any param is not readable, do not return any parameters that succeeded
- add case when request param is not found
- update wildcard case
  • Loading branch information
denopink committed Jul 16, 2024
1 parent 7e28f22 commit 943336d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
45 changes: 35 additions & 10 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,58 @@ func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) {
result := Tr181Payload{
Command: tr181.Command,
Names: tr181.Names,
StatusCode: 520,
StatusCode: http.StatusOK,
}

var (
failedNames []string
readableParams []Parameter
)
for _, name := range tr181.Names {
var found bool
for _, mockParameter := range h.parameters {
if !strings.HasPrefix(mockParameter.Name, name) {
continue
}

switch mockParameter.Access {
case "r", "rw", "wr":
result.Parameters = append(result.Parameters, Parameter{
// Check whether mockParameter is readable.
if strings.Contains(mockParameter.Access, "r") {
found = true
readableParams = append(readableParams, Parameter{
Name: mockParameter.Name,
Value: mockParameter.Value,
DataType: mockParameter.DataType,
Attributes: mockParameter.Attributes,
Message: "Success",
Count: 1,
})
result.StatusCode = http.StatusOK
default:
result.Parameters = append(result.Parameters, Parameter{
Message: fmt.Sprintf("Invalid parameter name: %s", mockParameter.Name),
})
result.StatusCode = 520
continue
}

// If requested parameter is a wild card and mockParameter is not readable,
// then continue and don't count it as a failure.
if name[len(name)-1] == '.' {
continue

Check warning on line 196 in internal/wrphandlers/mocktr181/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/wrphandlers/mocktr181/handler.go#L195-L196

Added lines #L195 - L196 were not covered by tests
}

// mockParameter is not readable.
failedNames = append(failedNames, mockParameter.Name)

Check warning on line 200 in internal/wrphandlers/mocktr181/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/wrphandlers/mocktr181/handler.go#L200

Added line #L200 was not covered by tests
}

if !found {
// Requested parameter was not found.
failedNames = append(failedNames, name)
}
}

result.Parameters = readableParams
// Check if any parameters failed.
if len(failedNames) != 0 {
// If any names failed, then do not return any parameters that succeeded.
result.Parameters = []Parameter{{
Message: fmt.Sprintf("Invalid parameter names: %s", failedNames),
}}
result.StatusCode = 520
}

payload, err := json.Marshal(result)
Expand Down
2 changes: 1 addition & 1 deletion internal/wrphandlers/mocktr181/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestHandler_HandleWrp(t *testing.T) {
var result Tr181Payload
err := json.Unmarshal(msg.Payload, &result)
a.NoError(err)
a.Equal(0, len(result.Parameters))
a.Equal(1, len(result.Parameters))
a.True(h.Enabled())
return nil
},
Expand Down

0 comments on commit 943336d

Please sign in to comment.