From e5df702e09c6c479a1e51784456e34a3cfef47c7 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Mon, 15 Jul 2024 12:22:44 -0400 Subject: [PATCH 1/3] patch: update mock181's `set` to check for non-writable params before updating params During mock181's `set`: - check for any non-writable params - if any param is not writable, do not update any param --- internal/wrphandlers/mocktr181/handler.go | 63 ++++++++++++++++------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index 1ee5198..58f8f91 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -205,6 +205,12 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { Names: tr181.Names, StatusCode: http.StatusAccepted, } + + var ( + writableParams []*MockParameter + failedParams []Parameter + ) + // Check for any parameters that are not writable. for _, parameter := range tr181.Parameters { for i := range h.parameters { mockParameter := &h.parameters[i] @@ -212,25 +218,46 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { continue } - switch mockParameter.Access { - case "w", "wr", "rw": - mockParameter.Value = parameter.Value - mockParameter.DataType = parameter.DataType - mockParameter.Attributes = parameter.Attributes - result.Parameters = append(result.Parameters, Parameter{ - Name: mockParameter.Name, - Value: mockParameter.Value, - DataType: mockParameter.DataType, - Attributes: mockParameter.Attributes, - Message: "Success", - }) - default: - result.Parameters = append(result.Parameters, Parameter{ - Name: mockParameter.Name, - Message: "Parameter is not writable", - }) - result.StatusCode = 520 + // Check whether mockParameter is writable. + if strings.Contains(mockParameter.Access, "w") { + // Add mockParameter to the list of parameters to be updated. + writableParams = append(writableParams, mockParameter) + continue + } + + // mockParameter is not writable. + failedParams = append(failedParams, Parameter{ + Name: mockParameter.Name, + Message: "Parameter is not writable", + }) + } + } + + if len(failedParams) != 0 { + // If any parameter failed, then do not apply any changes to the parameters in writableParams. + writableParams = nil + result.Parameters = failedParams + result.StatusCode = 520 + } + + // If all the selected parameters are writable, then update the parameters. Otherwise, do nothing. + for _, parameter := range tr181.Parameters { + // writableParams will be nil if any parameters failed (i.e.: were not writable). + for _, mockParameter := range writableParams { + if mockParameter.Name != parameter.Name { + continue } + + mockParameter.Value = parameter.Value + mockParameter.DataType = parameter.DataType + mockParameter.Attributes = parameter.Attributes + result.Parameters = append(result.Parameters, Parameter{ + Name: mockParameter.Name, + Value: mockParameter.Value, + DataType: mockParameter.DataType, + Attributes: mockParameter.Attributes, + Message: "Success", + }) } } From fffe243fe5ab97b162125f6e8e0025ff2782e412 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Mon, 15 Jul 2024 20:55:53 -0400 Subject: [PATCH 2/3] chore: docs --- internal/wrphandlers/mocktr181/handler.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index 58f8f91..ba19d7c 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -233,6 +233,7 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { } } + // Check if any parameters failed. if len(failedParams) != 0 { // If any parameter failed, then do not apply any changes to the parameters in writableParams. writableParams = nil From 6c924ef88f0ab8cd2e1217c40fa5a9798f9a1d13 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Tue, 16 Jul 2024 08:52:47 -0400 Subject: [PATCH 3/3] patch: update mock181's set, check for non-existing parameters - add case when request param is not found --- internal/wrphandlers/mocktr181/handler.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index ba19d7c..4bb08bb 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -212,6 +212,7 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { ) // Check for any parameters that are not writable. for _, parameter := range tr181.Parameters { + var found bool for i := range h.parameters { mockParameter := &h.parameters[i] if mockParameter.Name != parameter.Name { @@ -220,6 +221,7 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { // Check whether mockParameter is writable. if strings.Contains(mockParameter.Access, "w") { + found = true // Add mockParameter to the list of parameters to be updated. writableParams = append(writableParams, mockParameter) continue @@ -231,6 +233,14 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { Message: "Parameter is not writable", }) } + + if !found { + // Requested parameter was not found. + failedParams = append(failedParams, Parameter{ + Name: parameter.Name, + Message: "Invalid parameter name", + }) + } } // Check if any parameters failed.