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