github.com/google/osv-scalibr@v0.4.1/extractor/standalone/windows/ospackages/ospackages_windows_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build windows
    16  
    17  package ospackages
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/osv-scalibr/common/windows/registry"
    24  	"github.com/google/osv-scalibr/extractor"
    25  	"github.com/google/osv-scalibr/inventory"
    26  	"github.com/google/osv-scalibr/purl"
    27  	"github.com/google/osv-scalibr/testing/mockregistry"
    28  )
    29  
    30  func TestExtract(t *testing.T) {
    31  	hkuRootWithTwoUsers := &mockregistry.MockKey{
    32  		KName: "",
    33  		KSubkeys: []registry.Key{
    34  			&mockregistry.MockKey{KName: "User00"},
    35  			&mockregistry.MockKey{KName: "User01"},
    36  		},
    37  	}
    38  
    39  	tests := []struct {
    40  		name    string
    41  		reg     *mockregistry.MockRegistry
    42  		want    []*extractor.Package
    43  		wantErr bool
    44  	}{
    45  		{
    46  			name: "reports_system_software_and_wow64",
    47  			reg: &mockregistry.MockRegistry{
    48  				Keys: map[string]registry.Key{
    49  					// User-related installs.
    50  					"":                                     hkuRootWithTwoUsers,
    51  					"User00\\" + regUninstallRelativeUsers: &mockregistry.MockKey{},
    52  					// System-related installs.
    53  					regUninstallRootDefault: &mockregistry.MockKey{
    54  						KName: "Uninstall",
    55  						KSubkeys: []registry.Key{
    56  							&mockregistry.MockKey{
    57  								KName: "SomeSoftware",
    58  								KValues: []registry.Value{
    59  									&mockregistry.MockValue{VName: "DisplayName"},
    60  									&mockregistry.MockValue{VName: "DisplayVersion"},
    61  								},
    62  							},
    63  						},
    64  					},
    65  					regUninstallRootDefault + "\\SomeSoftware": &mockregistry.MockKey{
    66  						KName: "SomeSoftware",
    67  						KValues: []registry.Value{
    68  							&mockregistry.MockValue{
    69  								VName:       "DisplayName",
    70  								VDataString: "Some software install",
    71  							},
    72  							&mockregistry.MockValue{
    73  								VName:       "DisplayVersion",
    74  								VDataString: "1.0.0",
    75  							},
    76  						},
    77  					},
    78  					regUninstallRootWow64: &mockregistry.MockKey{
    79  						KName: "Uninstall",
    80  						KSubkeys: []registry.Key{
    81  							&mockregistry.MockKey{
    82  								KName: "SomeSoftwareWow64",
    83  								KValues: []registry.Value{
    84  									&mockregistry.MockValue{VName: "DisplayName"},
    85  									&mockregistry.MockValue{VName: "DisplayVersion"},
    86  								},
    87  							},
    88  						},
    89  					},
    90  					regUninstallRootWow64 + "\\SomeSoftwareWow64": &mockregistry.MockKey{
    91  						KName: "SomeSoftwareWow64",
    92  						KValues: []registry.Value{
    93  							&mockregistry.MockValue{
    94  								VName:       "DisplayName",
    95  								VDataString: "Some software install wow64",
    96  							},
    97  							&mockregistry.MockValue{
    98  								VName:       "DisplayVersion",
    99  								VDataString: "1.4.0",
   100  							},
   101  						},
   102  					},
   103  				},
   104  			},
   105  			want: []*extractor.Package{
   106  				{Name: "Some software install", Version: "1.0.0", PURLType: "windows"},
   107  				{Name: "Some software install wow64", Version: "1.4.0", PURLType: "windows"},
   108  			},
   109  		},
   110  		{
   111  			name: "reports_user_software",
   112  			reg: &mockregistry.MockRegistry{
   113  				Keys: map[string]registry.Key{
   114  					// User-related installs.
   115  					"": hkuRootWithTwoUsers,
   116  					"User00\\" + regUninstallRelativeUsers: &mockregistry.MockKey{
   117  						KName: "Uninstall",
   118  						KSubkeys: []registry.Key{
   119  							&mockregistry.MockKey{
   120  								KName: "SomeSoftwareUser00",
   121  								KValues: []registry.Value{
   122  									&mockregistry.MockValue{VName: "DisplayName"},
   123  									&mockregistry.MockValue{VName: "DisplayVersion"},
   124  								},
   125  							},
   126  						},
   127  					},
   128  					"User00\\" + regUninstallRelativeUsers + "\\SomeSoftwareUser00": &mockregistry.MockKey{
   129  						KName: "SomeSoftwareForUser00",
   130  						KValues: []registry.Value{
   131  							&mockregistry.MockValue{
   132  								VName:       "DisplayName",
   133  								VDataString: "Some software install for user00",
   134  							},
   135  							&mockregistry.MockValue{
   136  								VName:       "DisplayVersion",
   137  								VDataString: "1.00.1",
   138  							},
   139  						},
   140  					},
   141  					"User01\\" + regUninstallRelativeUsers: &mockregistry.MockKey{
   142  						KName: "Uninstall",
   143  						KSubkeys: []registry.Key{
   144  							&mockregistry.MockKey{
   145  								KName: "SomeSoftwareUser01",
   146  								KValues: []registry.Value{
   147  									&mockregistry.MockValue{VName: "DisplayName"},
   148  									&mockregistry.MockValue{VName: "DisplayVersion"},
   149  								},
   150  							},
   151  						},
   152  					},
   153  					"User01\\" + regUninstallRelativeUsers + "\\SomeSoftwareUser01": &mockregistry.MockKey{
   154  						KName: "SomeSoftwareForUser01",
   155  						KValues: []registry.Value{
   156  							&mockregistry.MockValue{
   157  								VName:       "DisplayName",
   158  								VDataString: "GooGet - Some software install for user01",
   159  							},
   160  							&mockregistry.MockValue{
   161  								VName:       "DisplayVersion",
   162  								VDataString: "1.01.1",
   163  							},
   164  						},
   165  					},
   166  					// System-related installs.
   167  					regUninstallRootDefault: &mockregistry.MockKey{
   168  						KName:    "Uninstall",
   169  						KSubkeys: []registry.Key{},
   170  					},
   171  					regUninstallRootWow64: &mockregistry.MockKey{
   172  						KName:    "Uninstall",
   173  						KSubkeys: []registry.Key{},
   174  					},
   175  				},
   176  			},
   177  			want: []*extractor.Package{
   178  				{Name: "Some software install for user00", Version: "1.00.1", PURLType: "windows"},
   179  				{Name: "GooGet - Some software install for user01", Version: "1.01.1", PURLType: purl.TypeGooget},
   180  			},
   181  		},
   182  		{
   183  			name:    "empty_registry_returns_error",
   184  			reg:     &mockregistry.MockRegistry{},
   185  			want:    []*extractor.Package{},
   186  			wantErr: true,
   187  		},
   188  	}
   189  
   190  	for _, tc := range tests {
   191  		t.Run(tc.name, func(t *testing.T) {
   192  			cfg := Configuration{mockregistry.NewOpener(tc.reg)}
   193  			e := New(cfg)
   194  			got, err := e.Extract(t.Context(), nil)
   195  			if tc.wantErr != (err != nil) {
   196  				t.Fatalf("Extract() returned an unexpected error: %v", err)
   197  			}
   198  
   199  			if tc.wantErr == true {
   200  				return
   201  			}
   202  
   203  			if diff := cmp.Diff(inventory.Inventory{Packages: tc.want}, got); diff != "" {
   204  				t.Errorf("Extract() returned an unexpected diff (-want +got): %v", diff)
   205  			}
   206  		})
   207  	}
   208  }