|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGetKernelVersion(t *testing.T) { |
| 9 | + version := GetKernelVersion() |
| 10 | + if version == "" { |
| 11 | + t.Error("Expected non-empty kernel version") |
| 12 | + } |
| 13 | + if version == "unknown" { |
| 14 | + t.Log("Warning: Could not determine kernel version") |
| 15 | + } |
| 16 | + t.Logf("Kernel version: %s", version) |
| 17 | +} |
| 18 | + |
| 19 | +func TestGetDistroName(t *testing.T) { |
| 20 | + distro := GetDistroName() |
| 21 | + if distro == "" { |
| 22 | + t.Error("Expected non-empty distro name") |
| 23 | + } |
| 24 | + t.Logf("Distro name: %s", distro) |
| 25 | +} |
| 26 | + |
| 27 | +func TestGetArchitecture(t *testing.T) { |
| 28 | + arch := GetArchitecture() |
| 29 | + if arch == "" { |
| 30 | + t.Error("Expected non-empty architecture") |
| 31 | + } |
| 32 | + // Common architectures |
| 33 | + validArchs := []string{"amd64", "386", "arm64", "arm", "ppc64le", "s390x"} |
| 34 | + isValid := false |
| 35 | + for _, valid := range validArchs { |
| 36 | + if arch == valid { |
| 37 | + isValid = true |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + if !isValid { |
| 42 | + t.Logf("Warning: Unusual architecture: %s", arch) |
| 43 | + } |
| 44 | + t.Logf("Architecture: %s", arch) |
| 45 | +} |
| 46 | + |
| 47 | +func TestGetUserAgent(t *testing.T) { |
| 48 | + productVersion := "5.14.0" |
| 49 | + userAgent := GetUserAgent(productVersion) |
| 50 | + |
| 51 | + if userAgent == "" { |
| 52 | + t.Fatal("Expected non-empty user agent") |
| 53 | + } |
| 54 | + |
| 55 | + // Verify format: Malwarebytes Privacy/VERSION (github.com/Malwarebytes/mbvpn-linux; Build: VERSION; DISTRO KERNEL ARCH) |
| 56 | + if !strings.HasPrefix(userAgent, "Malwarebytes Privacy/"+productVersion) { |
| 57 | + t.Errorf("Expected user agent to start with 'Malwarebytes Privacy/%s', got: %s", productVersion, userAgent) |
| 58 | + } |
| 59 | + |
| 60 | + if !strings.Contains(userAgent, "github.com/Malwarebytes/mbvpn-linux") { |
| 61 | + t.Error("Expected user agent to contain 'github.com/Malwarebytes/mbvpn-linux'") |
| 62 | + } |
| 63 | + |
| 64 | + if !strings.Contains(userAgent, "Build: "+productVersion) { |
| 65 | + t.Errorf("Expected user agent to contain 'Build: %s'", productVersion) |
| 66 | + } |
| 67 | + |
| 68 | + t.Logf("User agent: %s", userAgent) |
| 69 | + |
| 70 | + // Test caching - should return same value |
| 71 | + userAgent2 := GetUserAgent(productVersion) |
| 72 | + if userAgent != userAgent2 { |
| 73 | + t.Error("Expected cached user agent to match first call") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestGetUserAgentFormat(t *testing.T) { |
| 78 | + productVersion := "5.14.0" |
| 79 | + userAgent := GetUserAgent(productVersion) |
| 80 | + |
| 81 | + // Verify structure |
| 82 | + parts := strings.Split(userAgent, " (") |
| 83 | + if len(parts) != 2 { |
| 84 | + t.Fatalf("Expected user agent to have format 'PREFIX (DETAILS)', got: %s", userAgent) |
| 85 | + } |
| 86 | + |
| 87 | + prefix := parts[0] |
| 88 | + if prefix != "Malwarebytes Privacy/"+productVersion { |
| 89 | + t.Errorf("Expected prefix 'Malwarebytes Privacy/%s', got: %s", productVersion, prefix) |
| 90 | + } |
| 91 | + |
| 92 | + details := strings.TrimSuffix(parts[1], ")") |
| 93 | + detailParts := strings.Split(details, "; ") |
| 94 | + if len(detailParts) != 3 { |
| 95 | + t.Errorf("Expected 3 detail parts separated by '; ', got %d parts: %v", len(detailParts), detailParts) |
| 96 | + } |
| 97 | + |
| 98 | + // Check first detail part |
| 99 | + if detailParts[0] != "github.com/Malwarebytes/mbvpn-linux" { |
| 100 | + t.Errorf("Expected first detail 'github.com/Malwarebytes/mbvpn-linux', got: %s", detailParts[0]) |
| 101 | + } |
| 102 | + |
| 103 | + // Check second detail part |
| 104 | + if !strings.HasPrefix(detailParts[1], "Build: ") { |
| 105 | + t.Errorf("Expected second detail to start with 'Build: ', got: %s", detailParts[1]) |
| 106 | + } |
| 107 | + |
| 108 | + // Check third detail part has distro, kernel, and arch |
| 109 | + systemInfo := detailParts[2] |
| 110 | + systemParts := strings.Fields(systemInfo) |
| 111 | + if len(systemParts) < 2 { |
| 112 | + t.Errorf("Expected system info to have at least distro and kernel, got: %s", systemInfo) |
| 113 | + } |
| 114 | +} |
0 commit comments