From e946cffadb189afa548b6162478778adcd6b462d Mon Sep 17 00:00:00 2001 From: Bhoopesh Date: Fri, 11 Oct 2024 12:49:31 +0530 Subject: [PATCH 1/2] feat: tests for daemon and run Signed-off-by: Bhoopesh --- sztp-agent/go.mod | 1 + sztp-agent/go.sum | 2 + sztp-agent/pkg/secureagent/daemon_test.go | 388 ++++++++++++++++++++++ sztp-agent/pkg/secureagent/run_test.go | 166 +++++---- 4 files changed, 497 insertions(+), 60 deletions(-) diff --git a/sztp-agent/go.mod b/sztp-agent/go.mod index 8bdcc74e..806a9e0f 100644 --- a/sztp-agent/go.mod +++ b/sztp-agent/go.mod @@ -16,6 +16,7 @@ require ( github.com/ghodss/yaml v1.0.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jarcoal/httpmock v1.3.1 github.com/jaypipes/pcidb v1.0.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect diff --git a/sztp-agent/go.sum b/sztp-agent/go.sum index 570323d0..0d239d2e 100644 --- a/sztp-agent/go.sum +++ b/sztp-agent/go.sum @@ -19,6 +19,8 @@ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= +github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho= github.com/jaypipes/ghw v0.12.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= diff --git a/sztp-agent/pkg/secureagent/daemon_test.go b/sztp-agent/pkg/secureagent/daemon_test.go index c9341c04..9b7af4ca 100644 --- a/sztp-agent/pkg/secureagent/daemon_test.go +++ b/sztp-agent/pkg/secureagent/daemon_test.go @@ -12,6 +12,8 @@ import ( "net/http/httptest" "os" "testing" + + "github.com/jarcoal/httpmock" ) const DHCPTestContent = `lease { @@ -426,3 +428,389 @@ func TestAgent_doReqBootstrap(t *testing.T) { }) } } + +//nolint:funlen +func TestAgent_performBootstrapSequence(t *testing.T) { + type fields struct { + InputBootstrapURL string + SerialNumber string + DevicePassword string + DevicePrivateKey string + DeviceEndEntityCert string + BootstrapTrustAnchorCert string + ContentTypeReq string + InputJSONContent string + DhcpLeaseFile string + ProgressJSON ProgressJSON + BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo + BootstrapServerRedirectInfo BootstrapServerRedirectInfo + } + + expectedOnboarding := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDYwYLKoZIhvcNAQkQASugggNSBIIDTnsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvdGVzdC5pbWciLAogICAgICAgICJmdHBzOi8vd2ViOjk5MC90ZXN0LmltZyIKICAgICAgXSwKICAgICAgImltYWdlLXZlcmlmaWNhdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAiaGFzaC1hbGdvcml0aG0iOiAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86c2hhLTI1NiIsCiAgICAgICAgICAiaGFzaC12YWx1ZSI6ICJlMzpiMDpjNDo0Mjo5ODpmYzoxYzoxNDo5YTpmYjpmNDpjODo5OTo2ZjpiOToyNDoyNzphZTo0MTplNDo2NDo5Yjo5Mzo0YzphNDo5NTo5OToxYjo3ODo1MjpiODo1NSIKICAgICAgICB9CiAgICAgIF0KICAgIH0sCiAgICAicHJlLWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQjBhR2x5WkMxd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0IwYUdseVpDMXdiM04wTFdOdmJtWnBaM1Z5WVhScGIyNHRjMk55YVhCMExpNHVJZ289IgogIH0KfQ==", + }, + } + expectedRedirect := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDfQYLKoZIhvcNAQkQASugggNsBIIDaHsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvc2Vjb25kLWJvb3QtaW1hZ2UuaW1nIiwKICAgICAgICAiZnRwczovL3dlYjo5OTAvc2Vjb25kLWJvb3QtaW1hZ2UuaW1nIgogICAgICBdLAogICAgICAiaW1hZ2UtdmVyaWZpY2F0aW9uIjogWwogICAgICAgIHsKICAgICAgICAgICJoYXNoLWFsZ29yaXRobSI6ICJpZXRmLXN6dHAtY29udmV5ZWQtaW5mbzpzaGEtMjU2IiwKICAgICAgICAgICJoYXNoLXZhbHVlIjogIjdiOmNhOmU2OmFjOjIzOjA2OmQ4Ojc5OjA2OjhjOmFjOjAzOjgwOmUyOjE2OjQ0OjdlOjQwOjZhOjY1OmZhOmQ0OjY5OjYxOjZlOjA1OmNlOmY1Ojg3OmRjOjJiOjk3IgogICAgICAgIH0KICAgICAgXQogICAgfSwKICAgICJwcmUtY29uZmlndXJhdGlvbi1zY3JpcHQiOiAiSXlFdlltbHVMMkpoYzJnS1pXTm9ieUFpYVc1emFXUmxJSFJvWlNCelpXTnZibVF0Y0hKbExXTnZibVpwWjNWeVlYUnBiMjR0YzJOeWFYQjBMaTR1SWdvPSIsCiAgICAiY29uZmlndXJhdGlvbi1oYW5kbGluZyI6ICJtZXJnZSIsCiAgICAiY29uZmlndXJhdGlvbiI6ICJQSFJ2Y0NCNGJXeHVjejBpYUhSMGNITTZMMlY0WVcxd2JHVXVZMjl0TDJOdmJtWnBaeUkrQ2lBZ1BHRnVlUzE0Yld3dFkyOXVkR1Z1ZEMxdmEyRjVMejRLUEM5MGIzQStDZz09IiwKICAgICJwb3N0LWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQnpaV052Ym1RdGNHOXpkQzFqYjI1bWFXZDFjbUYwYVc5dUxYTmpjbWx3ZEM0dUxpSUsiCiAgfQp9", + }, + } + expectedFailedBase64 := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "{wrongBASE64}", + }, + } + expectedFailedImage := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDfwYLKoZIhvcNAQkQASugggNuBIIDansKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwOi8vd2ViOjgwODIvdmFyL2xpYi9taXNjL215LWJvb3QtaW1hZ2UuaW1nIiwKICAgICAgICAiZnRwOi8vd2ViOjMwODIvdmFyL2xpYi9taXNjL215LWJvb3QtaW1hZ2UuaW1nIgogICAgICBdLAogICAgICAiaW1hZ2UtdmVyaWZpY2F0aW9uIjogWwogICAgICAgIHsKICAgICAgICAgICJoYXNoLWFsZ29yaXRobSI6ICJpZXRmLXN6dHAtY29udmV5ZWQtaW5mbzpzaGEtMjU2IiwKICAgICAgICAgICJoYXNoLXZhbHVlIjogIjdiOmNhOmU2OmFjOjIzOjA2OmQ4Ojc5OjA2OjhjOmFjOjAzOjgwOmUyOjE2OjQ0OjdlOjQwOjZhOjY1OmZhOmQ0OjY5OjYxOjZlOjA1OmNlOmY1Ojg3OmRjOjJiOjk3IgogICAgICAgIH0KICAgICAgXQogICAgfSwKICAgICJwcmUtY29uZmlndXJhdGlvbi1zY3JpcHQiOiAiSXlFdlltbHVMMkpoYzJnS1pXTm9ieUFpYVc1emFXUmxJSFJvWlNCd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0J3YjNOMExXTnZibVpwWjNWeVlYUnBiMjR0YzJOeWFYQjBMaTR1SWdvPSIKICB9Cn0=", + }, + } + expectedFailedPreScript := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDbwYLKoZIhvcNAQkQASugggNeBIIDWnsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvdGVzdC5pbWciLAogICAgICAgICJmdHBzOi8vd2ViOjk5MC90ZXN0LmltZyIKICAgICAgXSwKICAgICAgImltYWdlLXZlcmlmaWNhdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAiaGFzaC1hbGdvcml0aG0iOiAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86c2hhLTI1NiIsCiAgICAgICAgICAiaGFzaC12YWx1ZSI6ICJlMzpiMDpjNDo0Mjo5ODpmYzoxYzoxNDo5YTpmYjpmNDpjODo5OTo2ZjpiOToyNDoyNzphZTo0MTplNDo2NDo5Yjo5Mzo0YzphNDo5NTo5OToxYjo3ODo1MjpiODo1NSIKICAgICAgICB9CiAgICAgIF0KICAgIH0sCiAgICAicHJlLWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQjBhR2x5WkMxd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNtVnljbTl5IiwKICAgICJjb25maWd1cmF0aW9uLWhhbmRsaW5nIjogIm1lcmdlIiwKICAgICJjb25maWd1cmF0aW9uIjogIlBIUnZjQ0I0Yld4dWN6MGlhSFIwY0hNNkwyVjRZVzF3YkdVdVkyOXRMMk52Ym1acFp5SStDaUFnUEdGdWVTMTRiV3d0WTI5dWRHVnVkQzF2YTJGNUx6NEtQQzkwYjNBK0NnPT0iLAogICAgInBvc3QtY29uZmlndXJhdGlvbi1zY3JpcHQiOiAiSXlFdlltbHVMMkpoYzJnS1pXTm9ieUFpYVc1emFXUmxJSFJvWlNCMGFHbHlaQzF3YjNOMExXTnZibVpwWjNWeVlYUnBiMjR0YzJOeWFYQjBMaTR1SWdwbGNuSnZjZz09IgogIH0KfQ==", + }, + } + expectedFailedPostScript := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDawYLKoZIhvcNAQkQASugggNaBIIDVnsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvdGVzdC5pbWciLAogICAgICAgICJmdHBzOi8vd2ViOjk5MC90ZXN0LmltZyIKICAgICAgXSwKICAgICAgImltYWdlLXZlcmlmaWNhdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAiaGFzaC1hbGdvcml0aG0iOiAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86c2hhLTI1NiIsCiAgICAgICAgICAiaGFzaC12YWx1ZSI6ICJlMzpiMDpjNDo0Mjo5ODpmYzoxYzoxNDo5YTpmYjpmNDpjODo5OTo2ZjpiOToyNDoyNzphZTo0MTplNDo2NDo5Yjo5Mzo0YzphNDo5NTo5OToxYjo3ODo1MjpiODo1NSIKICAgICAgICB9CiAgICAgIF0KICAgIH0sCiAgICAicHJlLWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQjBhR2x5WkMxd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0IwYUdseVpDMXdiM04wTFdOdmJtWnBaM1Z5WVhScGIyNHRjMk55YVhCMExpNHVJZ3BsY25KdmNnbz0iCiAgfQp9", + }, + } + expectedError := BootstrapServerErrorOutput{ + IetfRestconfErrors: struct { + Error []struct { + ErrorType string `json:"error-type"` + ErrorTag string `json:"error-tag"` + ErrorMessage string `json:"error-message"` + } `json:"error"` + }{ + Error: []struct { + ErrorType string `json:"error-type"` + ErrorTag string `json:"error-tag"` + ErrorMessage string `json:"error-message"` + }{ + { + ErrorType: "protocol", + ErrorTag: "access-denied", + ErrorMessage: "failed", + }, + }, + }, + } + + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "Test KO test where discover bootstrapURL fails", + fields: fields{ + InputBootstrapURL: "", + SerialNumber: "my-serial-number", + DevicePassword: "my-password", + DevicePrivateKey: "privateKey", + DeviceEndEntityCert: "endEntityCert", + BootstrapTrustAnchorCert: "trustAnchorCert", + ContentTypeReq: "application/json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "/file/does/not/exist", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: true, + }, + { + name: "Test KO where doRequestBootstrapServerOnboardingInfo fails", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOBASE64", + DevicePassword: "KO", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: "", + InputJSONContent: "", + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: true, + }, + { + name: "Test KO where doHandleBootstrapRedirect fails", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "REDIRECT", + DevicePassword: "PASS", + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{ + IetfSztpConveyedInfoRedirectInformation: struct { + BootstrapServer []struct { + Address string `json:"address"` + Port int `json:"port"` + TrustAnchor string `json:"trust-anchor"` + } `json:"bootstrap-server"` + }{ + BootstrapServer: []struct { + Address string `json:"address"` + Port int `json:"port"` + TrustAnchor string `json:"trust-anchor"` + }{{ + Address: "", + Port: 0, + }}, + }, + }, + }, + wantErr: true, + }, + { + name: "Test KO where downloadAndValidateImage fails", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOIMAGE", + DevicePassword: "KO", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: "", + InputJSONContent: "", + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: true, + }, + { + name: "Test KO where launchScriptsConfiguration PRE fails", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOPRESCRIPT", + DevicePassword: "KO", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: true, + }, + { + name: "Test KO where launchScriptsConfiguration POST fails", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOPOSTSCRIPT", + DevicePassword: "KO", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: true, + }, + { + name: "Test OK", + fields: fields{ + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + wantErr: false, + }, + } + + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder("POST", "https://bootstrap-server.com", func(req *http.Request) (*http.Response, error) { + user, pass, _ := req.BasicAuth() + + if (user + ":" + pass) == "USER:PASS" { + output, _ := json.Marshal(expectedOnboarding) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "REDIRECT:PASS" { + output, _ := json.Marshal(expectedRedirect) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "KOBASE64:KO" { + output, _ := json.Marshal(expectedFailedBase64) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "KOIMAGE:KO" { + output, _ := json.Marshal(expectedFailedImage) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "KOPRESCRIPT:KO" { + output, _ := json.Marshal(expectedFailedPreScript) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "KOPOSTSCRIPT:KO" { + output, _ := json.Marshal(expectedFailedPostScript) + return httpmock.NewStringResponse(200, string(output)), nil + } + + if (user + ":" + pass) == "KO:KO" { + output, _ := json.Marshal(expectedError) + return httpmock.NewStringResponse(401, string(output)), nil + } + + return httpmock.NewStringResponse(401, string("output")), nil + }) + + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + return httpmock.NewBytesResponse(200, []byte{}), nil + }) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Agent{ + InputBootstrapURL: tt.fields.InputBootstrapURL, + SerialNumber: tt.fields.SerialNumber, + DevicePassword: tt.fields.DevicePassword, + DevicePrivateKey: tt.fields.DevicePrivateKey, + DeviceEndEntityCert: tt.fields.DeviceEndEntityCert, + BootstrapTrustAnchorCert: tt.fields.BootstrapTrustAnchorCert, + ContentTypeReq: tt.fields.ContentTypeReq, + InputJSONContent: tt.fields.InputJSONContent, + DhcpLeaseFile: tt.fields.DhcpLeaseFile, + ProgressJSON: tt.fields.ProgressJSON, + BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, + BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, + HttpClient: &http.Client{}, + } + if err := a.performBootstrapSequence(); (err != nil) != tt.wantErr { + t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) + } + + }) + } +} + +func TestAgent_DaemonCommand(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + type fields struct { + InputBootstrapURL string + SerialNumber string + DevicePassword string + DevicePrivateKey string + DeviceEndEntityCert string + BootstrapTrustAnchorCert string + ContentTypeReq string + InputJSONContent string + DhcpLeaseFile string + ProgressJSON ProgressJSON + BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo + BootstrapServerRedirectInfo BootstrapServerRedirectInfo + } + + expectedOnboarding := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` + }{ + ConveyedInformation: "MIIDYwYLKoZIhvcNAQkQASugggNSBIIDTnsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvdGVzdC5pbWciLAogICAgICAgICJmdHBzOi8vd2ViOjk5MC90ZXN0LmltZyIKICAgICAgXSwKICAgICAgImltYWdlLXZlcmlmaWNhdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAiaGFzaC1hbGdvcml0aG0iOiAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86c2hhLTI1NiIsCiAgICAgICAgICAiaGFzaC12YWx1ZSI6ICJlMzpiMDpjNDo0Mjo5ODpmYzoxYzoxNDo5YTpmYjpmNDpjODo5OTo2ZjpiOToyNDoyNzphZTo0MTplNDo2NDo5Yjo5Mzo0YzphNDo5NTo5OToxYjo3ODo1MjpiODo1NSIKICAgICAgICB9CiAgICAgIF0KICAgIH0sCiAgICAicHJlLWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQjBhR2x5WkMxd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0IwYUdseVpDMXdiM04wTFdOdmJtWnBaM1Z5WVhScGIyNHRjMk55YVhCMExpNHVJZ289IgogIH0KfQ==", + }, + } + + httpmock.RegisterResponder("POST", "https://daemon-command.com", func(req *http.Request) (*http.Response, error) { + user, pass, _ := req.BasicAuth() + + if (user + ":" + pass) == "USER:PASS" { + output, _ := json.Marshal(expectedOnboarding) + return httpmock.NewStringResponse(200, string(output)), nil + } + return httpmock.NewStringResponse(401, ""), nil + }) + + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + return httpmock.NewBytesResponse(200, []byte{}), nil + }) + + tests := []struct { + name string + fields fields + wantErr bool + }{ + + { + name: "TestAgent_RunCommand", + fields: fields{ + InputBootstrapURL: "https://daemon-command.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Agent{ + InputBootstrapURL: tt.fields.InputBootstrapURL, + SerialNumber: tt.fields.SerialNumber, + DevicePassword: tt.fields.DevicePassword, + DevicePrivateKey: tt.fields.DevicePrivateKey, + DeviceEndEntityCert: tt.fields.DeviceEndEntityCert, + BootstrapTrustAnchorCert: tt.fields.BootstrapTrustAnchorCert, + ContentTypeReq: tt.fields.ContentTypeReq, + InputJSONContent: tt.fields.InputJSONContent, + DhcpLeaseFile: tt.fields.DhcpLeaseFile, + ProgressJSON: tt.fields.ProgressJSON, + BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, + BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, + HttpClient: &http.Client{}, + } + if err := a.RunCommandDaemon(); (err != nil) != tt.wantErr { + t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/sztp-agent/pkg/secureagent/run_test.go b/sztp-agent/pkg/secureagent/run_test.go index 242474ef..0e3d0208 100644 --- a/sztp-agent/pkg/secureagent/run_test.go +++ b/sztp-agent/pkg/secureagent/run_test.go @@ -4,70 +4,116 @@ // Package secureagent implements the secure agent package secureagent -import "testing" +import ( + "encoding/json" + "net/http" + "testing" -func TestAgent_RunCommand(_ *testing.T) { - /* - type fields struct { - BootstrapURL string - SerialNumber string - DevicePassword string - DevicePrivateKey string - DeviceEndEntityCert string - BootstrapTrustAnchorCert string - ContentTypeReq string - InputJSONContent string - DhcpLeaseFile string - ProgressJSON ProgressJSON - BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo - BootstrapServerRedirectInfo BootstrapServerRedirectInfo - } + "github.com/jarcoal/httpmock" +) + +const DHCPTestContent1 = `lease { + interface "eth0"; + fixed-address 10.127.127.100; + filename "grubx64.efi"; + option subnet-mask 255.255.255.0; + option sztp-redirect-urls "http:// + option dhcp-lease-time 600; + option tftp-server-name "w.x.y.z"; + option bootfile-name "test.cfg"; + option dhcp-message-type 5; + option dhcp-server-identifier 10.127.127.2; + renew 1 2022/08/15 19:16:40; + rebind 1 2022/08/15 19:20:50; + expire 1 2022/08/15 19:22:05; +}` + +func TestAgent_RunCommand(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + type fields struct { + InputBootstrapURL string + SerialNumber string + DevicePassword string + DevicePrivateKey string + DeviceEndEntityCert string + BootstrapTrustAnchorCert string + ContentTypeReq string + InputJSONContent string + DhcpLeaseFile string + ProgressJSON ProgressJSON + BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo + BootstrapServerRedirectInfo BootstrapServerRedirectInfo + } - tests := []struct { - name string - fields fields - wantErr bool + expectedOnboarding := BootstrapServerPostOutput{ + IetfSztpBootstrapServerOutput: struct { + ConveyedInformation string `json:"conveyed-information"` }{ + ConveyedInformation: "MIIDYwYLKoZIhvcNAQkQASugggNSBIIDTnsKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwczovL3dlYjo0NDMvdGVzdC5pbWciLAogICAgICAgICJmdHBzOi8vd2ViOjk5MC90ZXN0LmltZyIKICAgICAgXSwKICAgICAgImltYWdlLXZlcmlmaWNhdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAiaGFzaC1hbGdvcml0aG0iOiAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86c2hhLTI1NiIsCiAgICAgICAgICAiaGFzaC12YWx1ZSI6ICJlMzpiMDpjNDo0Mjo5ODpmYzoxYzoxNDo5YTpmYjpmNDpjODo5OTo2ZjpiOToyNDoyNzphZTo0MTplNDo2NDo5Yjo5Mzo0YzphNDo5NTo5OToxYjo3ODo1MjpiODo1NSIKICAgICAgICB9CiAgICAgIF0KICAgIH0sCiAgICAicHJlLWNvbmZpZ3VyYXRpb24tc2NyaXB0IjogIkl5RXZZbWx1TDJKaGMyZ0taV05vYnlBaWFXNXphV1JsSUhSb1pTQjBhR2x5WkMxd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0IwYUdseVpDMXdiM04wTFdOdmJtWnBaM1Z5WVhScGIyNHRjMk55YVhCMExpNHVJZ289IgogIH0KfQ==", + }, + } - { - name: "TestAgent_RunCommand", - fields: fields{ - BootstrapURL: "https://localhost:8443", - SerialNumber: "1234567890", - DevicePassword: "password", - DevicePrivateKey: "privateKey", - DeviceEndEntityCert: "endEntityCert", - BootstrapTrustAnchorCert: "trustAnchorCert", - ContentTypeReq: "application/json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "DHCPLEASEFILE", - ProgressJSON: ProgressJSON{}, - BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, - BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, - }, - }, - } + httpmock.RegisterResponder("POST", "https://run-command.com", func(req *http.Request) (*http.Response, error) { + user, pass, _ := req.BasicAuth() - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - a := &Agent{ - BootstrapURL: tt.fields.BootstrapURL, - SerialNumber: tt.fields.SerialNumber, - DevicePassword: tt.fields.DevicePassword, - DevicePrivateKey: tt.fields.DevicePrivateKey, - DeviceEndEntityCert: tt.fields.DeviceEndEntityCert, - BootstrapTrustAnchorCert: tt.fields.BootstrapTrustAnchorCert, - ContentTypeReq: tt.fields.ContentTypeReq, - InputJSONContent: tt.fields.InputJSONContent, - DhcpLeaseFile: tt.fields.DhcpLeaseFile, - ProgressJSON: tt.fields.ProgressJSON, - BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, - BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, - } - if err := a.RunCommand(); (err != nil) != tt.wantErr { - t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) - } - }) + if (user + ":" + pass) == "USER:PASS" { + output, _ := json.Marshal(expectedOnboarding) + return httpmock.NewStringResponse(200, string(output)), nil } - */ + return httpmock.NewStringResponse(401, ""), nil + }) + + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + return httpmock.NewBytesResponse(200, []byte{}), nil + }) + + tests := []struct { + name string + fields fields + wantErr bool + }{ + + { + name: "TestAgent_RunCommand", + fields: fields{ + InputBootstrapURL: "https://run-command.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, + BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Agent{ + InputBootstrapURL: tt.fields.InputBootstrapURL, + SerialNumber: tt.fields.SerialNumber, + DevicePassword: tt.fields.DevicePassword, + DevicePrivateKey: tt.fields.DevicePrivateKey, + DeviceEndEntityCert: tt.fields.DeviceEndEntityCert, + BootstrapTrustAnchorCert: tt.fields.BootstrapTrustAnchorCert, + ContentTypeReq: tt.fields.ContentTypeReq, + InputJSONContent: tt.fields.InputJSONContent, + DhcpLeaseFile: tt.fields.DhcpLeaseFile, + ProgressJSON: tt.fields.ProgressJSON, + BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, + BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, + HttpClient: &http.Client{}, + } + if err := a.RunCommand(); (err != nil) != tt.wantErr { + t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } } From 8a38e221032310f18107d05771e50f58f36012e5 Mon Sep 17 00:00:00 2001 From: Bhoopesh Date: Mon, 4 Nov 2024 20:54:07 +0530 Subject: [PATCH 2/2] fix: tests for daemon and run command Signed-off-by: Bhoopesh --- sztp-agent/pkg/secureagent/daemon_test.go | 147 +++++++++++----------- sztp-agent/pkg/secureagent/run_test.go | 28 ++--- 2 files changed, 87 insertions(+), 88 deletions(-) diff --git a/sztp-agent/pkg/secureagent/daemon_test.go b/sztp-agent/pkg/secureagent/daemon_test.go index 9b7af4ca..5ec17f81 100644 --- a/sztp-agent/pkg/secureagent/daemon_test.go +++ b/sztp-agent/pkg/secureagent/daemon_test.go @@ -293,13 +293,13 @@ func TestAgent_doReqBootstrap(t *testing.T) { log.Println(user, pass) switch { - case (user + ":" + pass) == "USER:PASS": + case (user + ":" + pass) == "USER:PASS": // nolint:goconst w.WriteHeader(200) output, _ = json.Marshal(expectedOnboarding) case (user + ":" + pass) == "REDIRECT:PASS": w.WriteHeader(200) output, _ = json.Marshal(expectedRedirect) - case (user + ":" + pass) == "KOBASE64:KO": + case (user + ":" + pass) == "KOBASE64:KO": // nolint:goconst w.WriteHeader(200) output, _ = json.Marshal(expectedFailedBase64) case (user + ":" + pass) == "KO:KO": @@ -536,16 +536,16 @@ func TestAgent_performBootstrapSequence(t *testing.T) { { name: "Test KO where doRequestBootstrapServerOnboardingInfo fails", fields: fields{ - InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "KOBASE64", - DevicePassword: "KO", - DevicePrivateKey: "", - DeviceEndEntityCert: "", - BootstrapTrustAnchorCert: "", - ContentTypeReq: "", - InputJSONContent: "", - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOBASE64", + DevicePassword: "KO", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: "", + InputJSONContent: "", + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -555,8 +555,8 @@ func TestAgent_performBootstrapSequence(t *testing.T) { name: "Test KO where doHandleBootstrapRedirect fails", fields: fields{ InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "REDIRECT", - DevicePassword: "PASS", + SerialNumber: "REDIRECT", + DevicePassword: "PASS", BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{ IetfSztpConveyedInfoRedirectInformation: struct { BootstrapServer []struct { @@ -581,34 +581,34 @@ func TestAgent_performBootstrapSequence(t *testing.T) { { name: "Test KO where downloadAndValidateImage fails", fields: fields{ - InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "KOIMAGE", - DevicePassword: "KO", - DevicePrivateKey: "", - DeviceEndEntityCert: "", - BootstrapTrustAnchorCert: "", - ContentTypeReq: "", - InputJSONContent: "", - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOIMAGE", + DevicePassword: "KO", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: "", + InputJSONContent: "", + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, - BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, + BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, wantErr: true, }, { name: "Test KO where launchScriptsConfiguration PRE fails", fields: fields{ - InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "KOPRESCRIPT", - DevicePassword: "KO", - DevicePrivateKey: "/certs/second_private_key.pem", - DeviceEndEntityCert: "/certs/second_my_cert.pem", - BootstrapTrustAnchorCert: "/certs/opi.pem", - ContentTypeReq: "application/yang-data+json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOPRESCRIPT", + DevicePassword: "KO", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -617,16 +617,16 @@ func TestAgent_performBootstrapSequence(t *testing.T) { { name: "Test KO where launchScriptsConfiguration POST fails", fields: fields{ - InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "KOPOSTSCRIPT", - DevicePassword: "KO", - DevicePrivateKey: "/certs/second_private_key.pem", - DeviceEndEntityCert: "/certs/second_my_cert.pem", - BootstrapTrustAnchorCert: "/certs/opi.pem", - ContentTypeReq: "application/yang-data+json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "KOPOSTSCRIPT", + DevicePassword: "KO", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -635,16 +635,16 @@ func TestAgent_performBootstrapSequence(t *testing.T) { { name: "Test OK", fields: fields{ - InputBootstrapURL: "https://bootstrap-server.com", - SerialNumber: "USER", - DevicePassword: "PASS", - DevicePrivateKey: "/certs/second_private_key.pem", - DeviceEndEntityCert: "/certs/second_my_cert.pem", - BootstrapTrustAnchorCert: "/certs/opi.pem", - ContentTypeReq: "application/yang-data+json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://bootstrap-server.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -656,7 +656,7 @@ func TestAgent_performBootstrapSequence(t *testing.T) { defer httpmock.DeactivateAndReset() httpmock.RegisterResponder("POST", "https://bootstrap-server.com", func(req *http.Request) (*http.Response, error) { - user, pass, _ := req.BasicAuth() + user, pass, _ := req.BasicAuth() if (user + ":" + pass) == "USER:PASS" { output, _ := json.Marshal(expectedOnboarding) @@ -694,9 +694,9 @@ func TestAgent_performBootstrapSequence(t *testing.T) { } return httpmock.NewStringResponse(401, string("output")), nil - }) + }) - httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(_ *http.Request) (*http.Response, error) { return httpmock.NewBytesResponse(200, []byte{}), nil }) @@ -715,12 +715,11 @@ func TestAgent_performBootstrapSequence(t *testing.T) { ProgressJSON: tt.fields.ProgressJSON, BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, - HttpClient: &http.Client{}, + HttpClient: &http.Client{}, } if err := a.performBootstrapSequence(); (err != nil) != tt.wantErr { t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) } - }) } } @@ -728,7 +727,7 @@ func TestAgent_performBootstrapSequence(t *testing.T) { func TestAgent_DaemonCommand(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - + type fields struct { InputBootstrapURL string SerialNumber string @@ -753,7 +752,7 @@ func TestAgent_DaemonCommand(t *testing.T) { } httpmock.RegisterResponder("POST", "https://daemon-command.com", func(req *http.Request) (*http.Response, error) { - user, pass, _ := req.BasicAuth() + user, pass, _ := req.BasicAuth() if (user + ":" + pass) == "USER:PASS" { output, _ := json.Marshal(expectedOnboarding) @@ -762,7 +761,7 @@ func TestAgent_DaemonCommand(t *testing.T) { return httpmock.NewStringResponse(401, ""), nil }) - httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(_ *http.Request) (*http.Response, error) { return httpmock.NewBytesResponse(200, []byte{}), nil }) @@ -775,16 +774,16 @@ func TestAgent_DaemonCommand(t *testing.T) { { name: "TestAgent_RunCommand", fields: fields{ - InputBootstrapURL: "https://daemon-command.com", - SerialNumber: "USER", - DevicePassword: "PASS", - DevicePrivateKey: "/certs/second_private_key.pem", - DeviceEndEntityCert: "/certs/second_my_cert.pem", - BootstrapTrustAnchorCert: "/certs/opi.pem", - ContentTypeReq: "application/yang-data+json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://daemon-command.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -806,7 +805,7 @@ func TestAgent_DaemonCommand(t *testing.T) { ProgressJSON: tt.fields.ProgressJSON, BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, - HttpClient: &http.Client{}, + HttpClient: &http.Client{}, } if err := a.RunCommandDaemon(); (err != nil) != tt.wantErr { t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) diff --git a/sztp-agent/pkg/secureagent/run_test.go b/sztp-agent/pkg/secureagent/run_test.go index 0e3d0208..b3189651 100644 --- a/sztp-agent/pkg/secureagent/run_test.go +++ b/sztp-agent/pkg/secureagent/run_test.go @@ -31,7 +31,7 @@ const DHCPTestContent1 = `lease { func TestAgent_RunCommand(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - + type fields struct { InputBootstrapURL string SerialNumber string @@ -56,7 +56,7 @@ func TestAgent_RunCommand(t *testing.T) { } httpmock.RegisterResponder("POST", "https://run-command.com", func(req *http.Request) (*http.Response, error) { - user, pass, _ := req.BasicAuth() + user, pass, _ := req.BasicAuth() if (user + ":" + pass) == "USER:PASS" { output, _ := json.Marshal(expectedOnboarding) @@ -65,7 +65,7 @@ func TestAgent_RunCommand(t *testing.T) { return httpmock.NewStringResponse(401, ""), nil }) - httpmock.RegisterResponder("GET", "https://web:443/test.img", func(req *http.Request) (*http.Response, error) { + httpmock.RegisterResponder("GET", "https://web:443/test.img", func(_ *http.Request) (*http.Response, error) { return httpmock.NewBytesResponse(200, []byte{}), nil }) @@ -78,16 +78,16 @@ func TestAgent_RunCommand(t *testing.T) { { name: "TestAgent_RunCommand", fields: fields{ - InputBootstrapURL: "https://run-command.com", - SerialNumber: "USER", - DevicePassword: "PASS", - DevicePrivateKey: "/certs/second_private_key.pem", - DeviceEndEntityCert: "/certs/second_my_cert.pem", - BootstrapTrustAnchorCert: "/certs/opi.pem", - ContentTypeReq: "application/yang-data+json", - InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: "", - ProgressJSON: ProgressJSON{}, + InputBootstrapURL: "https://run-command.com", + SerialNumber: "USER", + DevicePassword: "PASS", + DevicePrivateKey: "/certs/second_private_key.pem", + DeviceEndEntityCert: "/certs/second_my_cert.pem", + BootstrapTrustAnchorCert: "/certs/opi.pem", + ContentTypeReq: "application/yang-data+json", + InputJSONContent: generateInputJSONContent(), + DhcpLeaseFile: "", + ProgressJSON: ProgressJSON{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, }, @@ -109,7 +109,7 @@ func TestAgent_RunCommand(t *testing.T) { ProgressJSON: tt.fields.ProgressJSON, BootstrapServerOnboardingInfo: tt.fields.BootstrapServerOnboardingInfo, BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo, - HttpClient: &http.Client{}, + HttpClient: &http.Client{}, } if err := a.RunCommand(); (err != nil) != tt.wantErr { t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr)