From 943336d878de1267e16dcf892906e6ead8996822 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Mon, 15 Jul 2024 19:48:37 -0400 Subject: [PATCH 1/2] patch: update mock181's get, check for non-readable parameters 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 --- internal/wrphandlers/mocktr181/handler.go | 45 ++++++++++++++----- .../wrphandlers/mocktr181/handler_test.go | 2 +- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index 1ee5198..a4e717a 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -162,18 +162,24 @@ 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, @@ -181,14 +187,33 @@ func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { 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 + } + + // mockParameter is not readable. + failedNames = append(failedNames, mockParameter.Name) } + + 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) diff --git a/internal/wrphandlers/mocktr181/handler_test.go b/internal/wrphandlers/mocktr181/handler_test.go index 717b31a..620842a 100644 --- a/internal/wrphandlers/mocktr181/handler_test.go +++ b/internal/wrphandlers/mocktr181/handler_test.go @@ -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 }, From 9e5f3a032f4183c4b6513227ca073923bb74eac6 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Tue, 16 Jul 2024 08:46:02 -0400 Subject: [PATCH 2/2] chore: docs --- internal/wrphandlers/mocktr181/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index a4e717a..910b2fc 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -190,7 +190,7 @@ func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { continue } - // If requested parameter is a wild card and mockParameter is not readable, + // If the requested parameter is a wild card and is not readable, // then continue and don't count it as a failure. if name[len(name)-1] == '.' { continue