|
| 1 | +// Copyright (c) Mondoo, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +//go:build windows |
| 5 | + |
| 6 | +package registry |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "golang.org/x/sys/windows/registry" |
| 14 | +) |
| 15 | + |
| 16 | +func TestParseRegistryKeyPath(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + path string |
| 20 | + wantKey registry.Key |
| 21 | + wantPath string |
| 22 | + wantErr bool |
| 23 | + errContains string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "HKEY_LOCAL_MACHINE full prefix", |
| 27 | + path: `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft`, |
| 28 | + wantKey: registry.LOCAL_MACHINE, |
| 29 | + wantPath: `SOFTWARE\Microsoft`, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "HKLM short prefix", |
| 33 | + path: `HKLM\SOFTWARE\Microsoft`, |
| 34 | + wantKey: registry.LOCAL_MACHINE, |
| 35 | + wantPath: `SOFTWARE\Microsoft`, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "HKEY_CURRENT_USER full prefix", |
| 39 | + path: `HKEY_CURRENT_USER\Software\Classes`, |
| 40 | + wantKey: registry.CURRENT_USER, |
| 41 | + wantPath: `Software\Classes`, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "HKCU short prefix", |
| 45 | + path: `HKCU\Software\Classes`, |
| 46 | + wantKey: registry.CURRENT_USER, |
| 47 | + wantPath: `Software\Classes`, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "HKEY_USERS prefix", |
| 51 | + path: `HKEY_USERS\.DEFAULT`, |
| 52 | + wantKey: registry.USERS, |
| 53 | + wantPath: `.DEFAULT`, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "invalid hive returns error", |
| 57 | + path: `HKEY_INVALID\Some\Path`, |
| 58 | + wantErr: true, |
| 59 | + errContains: "invalid registry key hive", |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + key, path, err := parseRegistryKeyPath(tt.path) |
| 66 | + if tt.wantErr { |
| 67 | + require.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), tt.errContains) |
| 69 | + return |
| 70 | + } |
| 71 | + require.NoError(t, err) |
| 72 | + assert.Equal(t, tt.wantKey, key) |
| 73 | + assert.Equal(t, tt.wantPath, path) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestGetNativeRegistryKeyItems_Integration(t *testing.T) { |
| 79 | + // This key exists on every Windows installation |
| 80 | + items, err := GetNativeRegistryKeyItems(`HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion`) |
| 81 | + require.NoError(t, err) |
| 82 | + require.NotEmpty(t, items, "CurrentVersion should have registry values") |
| 83 | + |
| 84 | + // Check that well-known values exist |
| 85 | + found := make(map[string]bool) |
| 86 | + for _, item := range items { |
| 87 | + found[item.Key] = true |
| 88 | + } |
| 89 | + assert.True(t, found["CurrentBuild"], "expected CurrentBuild value") |
| 90 | + assert.True(t, found["ProductName"], "expected ProductName value") |
| 91 | +} |
| 92 | + |
| 93 | +func TestGetNativeRegistryKeyChildren_Integration(t *testing.T) { |
| 94 | + // This key exists on every Windows installation and has children |
| 95 | + children, err := GetNativeRegistryKeyChildren(`HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft`) |
| 96 | + require.NoError(t, err) |
| 97 | + require.NotEmpty(t, children, "HKLM\\SOFTWARE\\Microsoft should have subkeys") |
| 98 | + |
| 99 | + // Check that at least "Windows NT" subkey exists |
| 100 | + found := false |
| 101 | + for _, child := range children { |
| 102 | + if child.Name == "Windows NT" { |
| 103 | + found = true |
| 104 | + break |
| 105 | + } |
| 106 | + } |
| 107 | + assert.True(t, found, "expected 'Windows NT' subkey under HKLM\\SOFTWARE\\Microsoft") |
| 108 | +} |
| 109 | + |
| 110 | +func TestGetNativeRegistryKeyItems_NotFound(t *testing.T) { |
| 111 | + _, err := GetNativeRegistryKeyItems(`HKEY_LOCAL_MACHINE\SOFTWARE\NonExistentKey12345`) |
| 112 | + require.Error(t, err) |
| 113 | +} |
0 commit comments