Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
137
pkg/utils/certificate_parser_test.go
Normal file
137
pkg/utils/certificate_parser_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/utils"
|
||||
)
|
||||
|
||||
func TestParseVINFromRequestBothFormats(t *testing.T) {
|
||||
testVIN := "1F15K3R45N1234567"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
headerName string
|
||||
headerValue string
|
||||
expectedVIN string
|
||||
expectedError bool
|
||||
description string
|
||||
}{
|
||||
{
|
||||
name: "SDK 1.2 format - Ssl-Client-Subject-Dn",
|
||||
headerName: "Ssl-Client-Subject-Dn",
|
||||
headerValue: "CN=" + testVIN,
|
||||
expectedVIN: testVIN,
|
||||
expectedError: false,
|
||||
description: "Should parse VIN from SDK 1.2 Ssl-Client-Subject-Dn header",
|
||||
},
|
||||
{
|
||||
name: "SDK 1.4 format - Ssl header",
|
||||
headerName: "Ssl",
|
||||
headerValue: "CN=" + testVIN,
|
||||
expectedVIN: testVIN,
|
||||
expectedError: false,
|
||||
description: "Should parse VIN from SDK 1.4 Ssl header",
|
||||
},
|
||||
{
|
||||
name: "Both headers present - should prefer SDK 1.2",
|
||||
headerName: "both",
|
||||
headerValue: "CN=" + testVIN,
|
||||
expectedVIN: testVIN,
|
||||
expectedError: false,
|
||||
description: "When both headers present, should use SDK 1.2 format first",
|
||||
},
|
||||
{
|
||||
name: "No VIN headers present",
|
||||
headerName: "none",
|
||||
headerValue: "",
|
||||
expectedVIN: "",
|
||||
expectedError: true,
|
||||
description: "Should return error when no VIN headers are present",
|
||||
},
|
||||
{
|
||||
name: "Invalid header format - no CN prefix",
|
||||
headerName: "Ssl-Client-Subject-Dn",
|
||||
headerValue: testVIN, // Missing CN= prefix
|
||||
expectedVIN: "",
|
||||
expectedError: true,
|
||||
description: "Should return error when header doesn't have CN= prefix",
|
||||
},
|
||||
{
|
||||
name: "Empty CN value",
|
||||
headerName: "Ssl",
|
||||
headerValue: "CN=",
|
||||
expectedVIN: "",
|
||||
expectedError: true,
|
||||
description: "Should return error when CN value is empty",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Create HTTP request
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
|
||||
// Set headers based on test case
|
||||
switch tt.headerName {
|
||||
case "Ssl-Client-Subject-Dn":
|
||||
req.Header.Set("Ssl-Client-Subject-Dn", tt.headerValue)
|
||||
case "Ssl":
|
||||
req.Header.Set("Ssl", tt.headerValue)
|
||||
case "both":
|
||||
req.Header.Set("Ssl-Client-Subject-Dn", tt.headerValue)
|
||||
req.Header.Set("Ssl", "CN=DIFFERENT_VIN") // Should not be used
|
||||
case "none":
|
||||
// Don't set any headers
|
||||
}
|
||||
|
||||
// Call function under test
|
||||
vin, err := utils.ParseVINFromRequest(req)
|
||||
|
||||
// Verify results
|
||||
if tt.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error but got none", tt.description)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("%s: unexpected error: %v", tt.description, err)
|
||||
}
|
||||
if vin != tt.expectedVIN {
|
||||
t.Errorf("%s: expected VIN %s, got %s", tt.description, tt.expectedVIN, vin)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test backward compatibility - existing SDK 1.2 test should still pass
|
||||
func TestParseVINFromRequestLegacy(t *testing.T) {
|
||||
const subjectDNHeader = "Ssl-Client-Subject-Dn"
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.Header.Set(subjectDNHeader, "CN=1F15K3R45N1234567")
|
||||
|
||||
vin, err := utils.ParseVINFromRequest(req)
|
||||
if err != nil {
|
||||
t.Errorf("TestParseVINFromRequestLegacy: unexpected error: %v", err)
|
||||
}
|
||||
if vin != "1F15K3R45N1234567" {
|
||||
t.Errorf("TestParseVINFromRequestLegacy: expected VIN 1F15K3R45N1234567, got %s", vin)
|
||||
}
|
||||
}
|
||||
|
||||
// Test SDK 1.4 specific format
|
||||
func TestParseVINFromRequestSDK14(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.Header.Set("Ssl", "CN=1F15K3R45N1234567")
|
||||
|
||||
vin, err := utils.ParseVINFromRequest(req)
|
||||
if err != nil {
|
||||
t.Errorf("TestParseVINFromRequestSDK14: unexpected error: %v", err)
|
||||
}
|
||||
if vin != "1F15K3R45N1234567" {
|
||||
t.Errorf("TestParseVINFromRequestSDK14: expected VIN 1F15K3R45N1234567, got %s", vin)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user