-
Notifications
You must be signed in to change notification settings - Fork 33
🟢 Add Windows-native test coverage for OS provider #6690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c7a472
🟢 Add Windows-native test coverage for OS provider
czunker 679f284
🟢 Fix TestFileResource for Windows: use t.TempDir() instead of /tmp
czunker 4c67eec
🟢 Fix TestFindFiles for Windows: skip Chmod and permission tests
czunker 32988e9
🧹 Extract mockLocalConnection to shared testhelpers_windows_test.go
czunker 3b6d01b
🟢 Split permission tests into build-tag-guarded unix-only file
czunker d70cf5a
🟢 Fix TestFileResource on Windows: close file before TempDir cleanup
czunker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
providers/os/detector/windows/build_version_windows_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Mondoo, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //go:build windows | ||
|
|
||
| package windows | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetWindowsOSBuild_Integration(t *testing.T) { | ||
| conn := &mockLocalConnection{} | ||
| ver, err := GetWindowsOSBuild(conn) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, ver) | ||
|
|
||
| assert.NotEmpty(t, ver.CurrentBuild, "CurrentBuild should not be empty") | ||
| assert.NotEmpty(t, ver.ProductName, "ProductName should not be empty") | ||
| assert.NotEmpty(t, ver.Architecture, "Architecture should not be empty") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) Mondoo, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //go:build windows | ||
|
|
||
| package windows | ||
|
|
||
| import ( | ||
| "github.com/spf13/afero" | ||
| "go.mondoo.com/mql/v13/providers-sdk/v1/inventory" | ||
| "go.mondoo.com/mql/v13/providers/os/connection/shared" | ||
| ) | ||
|
|
||
| // mockLocalConnection implements shared.Connection and returns Type_Local. | ||
| // It is used by multiple test files in this package. | ||
| type mockLocalConnection struct{} | ||
|
|
||
| func (m *mockLocalConnection) ID() uint32 { return 0 } | ||
| func (m *mockLocalConnection) ParentID() uint32 { return 0 } | ||
| func (m *mockLocalConnection) RunCommand(command string) (*shared.Command, error) { return nil, nil } | ||
| func (m *mockLocalConnection) FileInfo(path string) (shared.FileInfoDetails, error) { | ||
| return shared.FileInfoDetails{}, nil | ||
| } | ||
| func (m *mockLocalConnection) FileSystem() afero.Fs { return afero.NewOsFs() } | ||
| func (m *mockLocalConnection) Name() string { return "mock-local" } | ||
| func (m *mockLocalConnection) Type() shared.ConnectionType { return shared.Type_Local } | ||
| func (m *mockLocalConnection) Asset() *inventory.Asset { return &inventory.Asset{} } | ||
| func (m *mockLocalConnection) UpdateAsset(asset *inventory.Asset) {} | ||
| func (m *mockLocalConnection) Capabilities() shared.Capabilities { | ||
| return shared.Capability_None | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) Mondoo, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //go:build windows | ||
|
|
||
| package windows | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestToString(t *testing.T) { | ||
| t.Run("nil returns empty string", func(t *testing.T) { | ||
| assert.Equal(t, "", toString(nil)) | ||
| }) | ||
|
|
||
| t.Run("non-nil returns value", func(t *testing.T) { | ||
| s := "hello" | ||
| assert.Equal(t, "hello", toString(&s)) | ||
| }) | ||
|
|
||
| t.Run("empty string returns empty string", func(t *testing.T) { | ||
| s := "" | ||
| assert.Equal(t, "", toString(&s)) | ||
| }) | ||
| } | ||
|
|
||
| func TestIntToString(t *testing.T) { | ||
| t.Run("nil returns empty string", func(t *testing.T) { | ||
| assert.Equal(t, "", intToString(nil)) | ||
| }) | ||
|
|
||
| t.Run("non-nil returns string representation", func(t *testing.T) { | ||
| i := 42 | ||
| assert.Equal(t, "42", intToString(&i)) | ||
| }) | ||
|
|
||
| t.Run("zero returns 0", func(t *testing.T) { | ||
| i := 0 | ||
| assert.Equal(t, "0", intToString(&i)) | ||
| }) | ||
| } | ||
|
|
||
| func TestGetWmiInformation_Integration(t *testing.T) { | ||
| conn := &mockLocalConnection{} | ||
| info, err := GetWmiInformation(conn) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, info) | ||
|
|
||
| assert.NotEmpty(t, info.Version, "Version should not be empty") | ||
| assert.NotEmpty(t, info.BuildNumber, "BuildNumber should not be empty") | ||
| assert.NotEmpty(t, info.Caption, "Caption should not be empty") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) Mondoo, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package fsutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/afero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFindFilesPermissionFilter(t *testing.T) { | ||
| fs := afero.NewMemMapFs() | ||
| mkDir(t, fs, "root/a") | ||
| mkDir(t, fs, "root/b") | ||
| mkDir(t, fs, "root/c") | ||
| mkDir(t, fs, "root/c/d") | ||
|
|
||
| mkFile(t, fs, "root/file0") | ||
| mkFile(t, fs, "root/a/file1") | ||
| mkFile(t, fs, "root/a/file2") | ||
| mkFile(t, fs, "root/b/file1") | ||
| mkFile(t, fs, "root/c/file4") | ||
| mkFile(t, fs, "root/c/d/file5") | ||
|
|
||
| require.NoError(t, fs.Chmod("root/c/file4", 0o002)) | ||
|
|
||
| perm := uint32(0o002) | ||
| permFiles, err := FindFiles(afero.NewIOFS(fs), "root", nil, "f", &perm, nil) | ||
| require.NoError(t, err) | ||
| assert.ElementsMatch(t, permFiles, []string{"root/c/file4"}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) Mondoo, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //go:build windows | ||
|
|
||
| package fsutil | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestHandleFsError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| wantSkip bool | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "nil error", | ||
| err: nil, | ||
| wantSkip: false, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "permission denied is skipped", | ||
| err: os.ErrPermission, | ||
| wantSkip: true, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "not exist is skipped", | ||
| err: os.ErrNotExist, | ||
| wantSkip: true, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "invalid is skipped", | ||
| err: os.ErrInvalid, | ||
| wantSkip: true, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "other error is propagated", | ||
| err: errors.New("some other error"), | ||
| wantSkip: true, | ||
| wantErr: errors.New("some other error"), | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| skip, err := handleFsError(tt.err) | ||
| assert.Equal(t, tt.wantSkip, skip) | ||
| if tt.wantErr != nil { | ||
| require.Error(t, err) | ||
| assert.Equal(t, tt.wantErr.Error(), err.Error()) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.