Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add tests for bootstrap redirect #450

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/asn1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
Expand Down Expand Up @@ -114,6 +115,12 @@ func (a *Agent) doHandleBootstrapRedirect() error {
addr := a.BootstrapServerRedirectInfo.IetfSztpConveyedInfoRedirectInformation.BootstrapServer[0].Address
port := a.BootstrapServerRedirectInfo.IetfSztpConveyedInfoRedirectInformation.BootstrapServer[0].Port

if addr == "" {
return errors.New("invalid redirect address")
}
if port <= 0 {
return errors.New("invalid port")
}
// Change URL to point to new redirect IP and PORT
u, err := url.Parse(a.GetBootstrapURL())
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions sztp-agent/pkg/secureagent/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,83 @@ func deleteTempTestFile(file string) {
}
}

func TestAgent_doHandleBootstrapRedirect(t *testing.T) {
type fields struct {
InputBootstrapURL string
BootstrapServerRedirectInfo BootstrapServerRedirectInfo
}
tests := []struct {
name string
fields fields
wantErr bool
expectedBootstrapURL string
}{
{
name: "Fail test with invalid address",
fields: fields{
InputBootstrapURL: "",
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,
expectedBootstrapURL: "",
},
{
name: "Fail test with invalid port",
fields: fields{
InputBootstrapURL: "",
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: "8.8.8.8",
Port: -1000,
}},
},
},
},
wantErr: true,
expectedBootstrapURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Agent{
BootstrapURL: tt.fields.InputBootstrapURL,
BootstrapServerRedirectInfo: tt.fields.BootstrapServerRedirectInfo,
}
if err := a.doHandleBootstrapRedirect(); (err != nil) != tt.wantErr {
t.Errorf("doHandleBootstrapRedirect() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

//nolint:funlen
func TestAgent_doReqBootstrap(t *testing.T) {
var output []byte
Expand Down
Loading