|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright (C) 2022-2023 Red Hat. |
| 3 | + |
| 4 | +// Package secureagent implements the secure agent |
| 5 | +package secureagent |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestProgressTypeString(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + input ProgressType |
| 19 | + expected string |
| 20 | + }{ |
| 21 | + {ProgressTypeBootstrapInitiated, "bootstrap-initiated"}, |
| 22 | + {ProgressTypeParsingInitiated, "parsing-initiated"}, |
| 23 | + {ProgressTypeParsingWarning, "parsing-warning"}, |
| 24 | + {ProgressTypeParsingError, "parsing-error"}, |
| 25 | + {ProgressTypeParsingComplete, "parsing-complete"}, |
| 26 | + {ProgressTypeBootImageInitiated, "boot-image-initiated"}, |
| 27 | + {ProgressTypeBootImageWarning, "boot-image-warning"}, |
| 28 | + {ProgressTypeBootImageError, "boot-image-error"}, |
| 29 | + {ProgressTypeBootImageMismatch, "boot-image-mismatch"}, |
| 30 | + {ProgressTypeBootImageInstalledRebooting, "boot-image-installed-rebooting"}, |
| 31 | + {ProgressTypeBootImageComplete, "boot-image-complete"}, |
| 32 | + {ProgressTypePreScriptInitiated, "pre-script-initiated"}, |
| 33 | + {ProgressTypePreScriptWarning, "pre-script-warning"}, |
| 34 | + {ProgressTypePreScriptError, "pre-script-error"}, |
| 35 | + {ProgressTypePreScriptComplete, "pre-script-complete"}, |
| 36 | + {ProgressTypeConfigInitiated, "config-initiated"}, |
| 37 | + {ProgressTypeConfigWarning, "config-warning"}, |
| 38 | + {ProgressTypeConfigError, "config-error"}, |
| 39 | + {ProgressTypeConfigComplete, "config-complete"}, |
| 40 | + {ProgressTypePostScriptInitiated, "post-script-initiated"}, |
| 41 | + {ProgressTypePostScriptWarning, "post-script-warning"}, |
| 42 | + {ProgressTypePostScriptError, "post-script-error"}, |
| 43 | + {ProgressTypePostScriptComplete, "post-script-complete"}, |
| 44 | + {ProgressTypeBootstrapWarning, "bootstrap-warning"}, |
| 45 | + {ProgressTypeBootstrapError, "bootstrap-error"}, |
| 46 | + {ProgressTypeBootstrapComplete, "bootstrap-complete"}, |
| 47 | + {ProgressTypeInformational, "informational"}, |
| 48 | + {ProgressType(999), "unknown"}, // Test for an unknown value |
| 49 | + } |
| 50 | + |
| 51 | + for _, test := range tests { |
| 52 | + result := test.input.String() |
| 53 | + if result != test.expected { |
| 54 | + t.Errorf("For %v expected %v, but got %v", test.input, test.expected, result) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +//nolint:funlen |
| 60 | +func TestAgent_doReportProgress(t *testing.T) { |
| 61 | + var output []byte |
| 62 | + expected := BootstrapServerPostOutput{ |
| 63 | + IetfSztpBootstrapServerOutput: struct { |
| 64 | + ConveyedInformation string `json:"conveyed-information"` |
| 65 | + }{ |
| 66 | + ConveyedInformation: "MIIDfwYLKoZIhvcNAQkQASugggNuBIIDansKICAiaWV0Zi1zenRwLWNvbnZleWVkLWluZm86b25ib2FyZGluZy1pbmZvcm1hdGlvbiI6IHsKICAgICJib290LWltYWdlIjogewogICAgICAiZG93bmxvYWQtdXJpIjogWwogICAgICAgICJodHRwOi8vd2ViOjgwODIvdmFyL2xpYi9taXNjL215LWJvb3QtaW1hZ2UuaW1nIiwKICAgICAgICAiZnRwOi8vd2ViOjMwODIvdmFyL2xpYi9taXNjL215LWJvb3QtaW1hZ2UuaW1nIgogICAgICBdLAogICAgICAiaW1hZ2UtdmVyaWZpY2F0aW9uIjogWwogICAgICAgIHsKICAgICAgICAgICJoYXNoLWFsZ29yaXRobSI6ICJpZXRmLXN6dHAtY29udmV5ZWQtaW5mbzpzaGEtMjU2IiwKICAgICAgICAgICJoYXNoLXZhbHVlIjogIjdiOmNhOmU2OmFjOjIzOjA2OmQ4Ojc5OjA2OjhjOmFjOjAzOjgwOmUyOjE2OjQ0OjdlOjQwOjZhOjY1OmZhOmQ0OjY5OjYxOjZlOjA1OmNlOmY1Ojg3OmRjOjJiOjk3IgogICAgICAgIH0KICAgICAgXQogICAgfSwKICAgICJwcmUtY29uZmlndXJhdGlvbi1zY3JpcHQiOiAiSXlFdlltbHVMMkpoYzJnS1pXTm9ieUFpYVc1emFXUmxJSFJvWlNCd2NtVXRZMjl1Wm1sbmRYSmhkR2x2YmkxelkzSnBjSFF1TGk0aUNnPT0iLAogICAgImNvbmZpZ3VyYXRpb24taGFuZGxpbmciOiAibWVyZ2UiLAogICAgImNvbmZpZ3VyYXRpb24iOiAiUEhSdmNDQjRiV3h1Y3owaWFIUjBjSE02TDJWNFlXMXdiR1V1WTI5dEwyTnZibVpwWnlJK0NpQWdQR0Z1ZVMxNGJXd3RZMjl1ZEdWdWRDMXZhMkY1THo0S1BDOTBiM0ErQ2c9PSIsCiAgICAicG9zdC1jb25maWd1cmF0aW9uLXNjcmlwdCI6ICJJeUV2WW1sdUwySmhjMmdLWldOb2J5QWlhVzV6YVdSbElIUm9aU0J3YjNOMExXTnZibVpwWjNWeVlYUnBiMjR0YzJOeWFYQjBMaTR1SWdvPSIKICB9Cn0=", |
| 67 | + }, |
| 68 | + } |
| 69 | + expectedFailedBase64 := BootstrapServerPostOutput{ |
| 70 | + IetfSztpBootstrapServerOutput: struct { |
| 71 | + ConveyedInformation string `json:"conveyed-information"` |
| 72 | + }{ |
| 73 | + ConveyedInformation: "{wrongBASE64}", |
| 74 | + }, |
| 75 | + } |
| 76 | + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + user, pass, _ := r.BasicAuth() |
| 78 | + log.Println(user, pass) |
| 79 | + |
| 80 | + switch { |
| 81 | + case (user + ":" + pass) == "USER:PASS": |
| 82 | + w.WriteHeader(200) |
| 83 | + output, _ = json.Marshal(expected) |
| 84 | + case (user + ":" + pass) == "KOBASE64:KO": |
| 85 | + w.WriteHeader(200) |
| 86 | + output, _ = json.Marshal(expectedFailedBase64) |
| 87 | + default: |
| 88 | + w.WriteHeader(400) |
| 89 | + output, _ = json.Marshal(expected) |
| 90 | + } |
| 91 | + |
| 92 | + _, err := fmt.Fprint(w, string(output)) |
| 93 | + if err != nil { |
| 94 | + return |
| 95 | + } |
| 96 | + })) |
| 97 | + defer svr.Close() |
| 98 | + type fields struct { |
| 99 | + BootstrapURL string |
| 100 | + SerialNumber string |
| 101 | + DevicePassword string |
| 102 | + DevicePrivateKey string |
| 103 | + DeviceEndEntityCert string |
| 104 | + BootstrapTrustAnchorCert string |
| 105 | + ContentTypeReq string |
| 106 | + InputJSONContent string |
| 107 | + DhcpLeaseFile string |
| 108 | + ProgressJSON ProgressJSON |
| 109 | + } |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + fields fields |
| 113 | + wantErr bool |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "OK", |
| 117 | + fields: fields{ |
| 118 | + BootstrapURL: svr.URL, |
| 119 | + SerialNumber: "USER", |
| 120 | + DevicePassword: "PASS", |
| 121 | + DevicePrivateKey: "PRIVATEKEY", |
| 122 | + DeviceEndEntityCert: "ENDENTITYCERT", |
| 123 | + BootstrapTrustAnchorCert: "TRUSTANCHORCERT", |
| 124 | + ContentTypeReq: "application/vnd.ietf.sztp.bootstrap-server+json", |
| 125 | + InputJSONContent: "INPUTJSON", |
| 126 | + DhcpLeaseFile: "DHCPLEASEFILE", |
| 127 | + ProgressJSON: ProgressJSON{}, |
| 128 | + }, |
| 129 | + wantErr: false, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "KO", |
| 133 | + fields: fields{ |
| 134 | + BootstrapURL: svr.URL, |
| 135 | + SerialNumber: "USER", |
| 136 | + DevicePassword: "PASSWORDWRONG", |
| 137 | + DevicePrivateKey: "PRIVATEKEY", |
| 138 | + DeviceEndEntityCert: "ENDENTITYCERT", |
| 139 | + BootstrapTrustAnchorCert: "TRUSTANCHORCERT", |
| 140 | + ContentTypeReq: "application/vnd.ietf.sztp.bootstrap-server+json", |
| 141 | + InputJSONContent: "INPUTJSON", |
| 142 | + DhcpLeaseFile: "DHCPLEASEFILE", |
| 143 | + ProgressJSON: ProgressJSON{}, |
| 144 | + }, |
| 145 | + wantErr: true, |
| 146 | + }, |
| 147 | + } |
| 148 | + for _, tt := range tests { |
| 149 | + t.Run(tt.name, func(t *testing.T) { |
| 150 | + a := &Agent{ |
| 151 | + BootstrapURL: tt.fields.BootstrapURL, |
| 152 | + SerialNumber: tt.fields.SerialNumber, |
| 153 | + DevicePassword: tt.fields.DevicePassword, |
| 154 | + DevicePrivateKey: tt.fields.DevicePrivateKey, |
| 155 | + DeviceEndEntityCert: tt.fields.DeviceEndEntityCert, |
| 156 | + BootstrapTrustAnchorCert: tt.fields.BootstrapTrustAnchorCert, |
| 157 | + ContentTypeReq: tt.fields.ContentTypeReq, |
| 158 | + InputJSONContent: tt.fields.InputJSONContent, |
| 159 | + DhcpLeaseFile: tt.fields.DhcpLeaseFile, |
| 160 | + ProgressJSON: tt.fields.ProgressJSON, |
| 161 | + } |
| 162 | + if err := a.doReportProgress(ProgressTypeBootstrapInitiated, "Bootstrap Initiated"); (err != nil) != tt.wantErr { |
| 163 | + t.Errorf("doReportProgress() error = %v, wantErr %v", err, tt.wantErr) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments