github.com/google/osv-scalibr@v0.4.1/extractor/standalone/windows/regosversion/regosversion_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 regosversion
    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/extractor/standalone/windows/common/metadata"
    26  	"github.com/google/osv-scalibr/inventory"
    27  	"github.com/google/osv-scalibr/testing/mockregistry"
    28  )
    29  
    30  func TestExtract(t *testing.T) {
    31  	tests := []struct {
    32  		name    string
    33  		reg     *mockregistry.MockRegistry
    34  		want    []*extractor.Package
    35  		wantErr bool
    36  	}{
    37  		{
    38  			name: "newerKnownWindows_returnsFullVersion",
    39  			reg: &mockregistry.MockRegistry{
    40  				Keys: map[string]registry.Key{
    41  					regVersionPath: &mockregistry.MockKey{
    42  						KName: "CurrentVersion",
    43  						KValues: []registry.Value{
    44  							&mockregistry.MockValue{
    45  								VName:       "CurrentMajorVersionNumber",
    46  								VDataString: "10",
    47  							},
    48  							&mockregistry.MockValue{
    49  								VName:       "CurrentMinorVersionNumber",
    50  								VDataString: "0",
    51  							},
    52  							&mockregistry.MockValue{
    53  								VName:       "CurrentBuildNumber",
    54  								VDataString: "22000",
    55  							},
    56  							&mockregistry.MockValue{
    57  								VName:       "UBR",
    58  								VDataString: "1234",
    59  							},
    60  							&mockregistry.MockValue{
    61  								VName:       "InstallationType",
    62  								VDataString: "client",
    63  							},
    64  						},
    65  					},
    66  				},
    67  			},
    68  			want: []*extractor.Package{
    69  				{
    70  					Name:     "windows_11:21H2",
    71  					Version:  "10.0.22000.1234",
    72  					PURLType: "windows",
    73  					Metadata: &metadata.OSVersion{
    74  						Product:     "windows_11:21H2",
    75  						FullVersion: "10.0.22000.1234",
    76  					},
    77  				},
    78  			},
    79  		},
    80  		{
    81  			name: "newerUnknownWindows_returnsFullVersion",
    82  			reg: &mockregistry.MockRegistry{
    83  				Keys: map[string]registry.Key{
    84  					regVersionPath: &mockregistry.MockKey{
    85  						KName: "CurrentVersion",
    86  						KValues: []registry.Value{
    87  							&mockregistry.MockValue{
    88  								VName:       "CurrentMajorVersionNumber",
    89  								VDataString: "10",
    90  							},
    91  							&mockregistry.MockValue{
    92  								VName:       "CurrentMinorVersionNumber",
    93  								VDataString: "0",
    94  							},
    95  							&mockregistry.MockValue{
    96  								VName:       "CurrentBuildNumber",
    97  								VDataString: "12345",
    98  							},
    99  							&mockregistry.MockValue{
   100  								VName:       "UBR",
   101  								VDataString: "1234",
   102  							},
   103  							&mockregistry.MockValue{
   104  								VName:       "InstallationType",
   105  								VDataString: "client",
   106  							},
   107  						},
   108  					},
   109  				},
   110  			},
   111  			want: []*extractor.Package{
   112  				{
   113  					Name:     "unknownWindows",
   114  					Version:  "10.0.12345.1234",
   115  					PURLType: "windows",
   116  					Metadata: &metadata.OSVersion{
   117  						Product:     "unknownWindows",
   118  						FullVersion: "10.0.12345.1234",
   119  					},
   120  				},
   121  			},
   122  		},
   123  		{
   124  			name: "olderKnownWindows_returnsFullVersion",
   125  			reg: &mockregistry.MockRegistry{
   126  				Keys: map[string]registry.Key{
   127  					regVersionPath: &mockregistry.MockKey{
   128  						KName: "CurrentVersion",
   129  						KValues: []registry.Value{
   130  							&mockregistry.MockValue{
   131  								VName:       "CurrentVersion",
   132  								VDataString: "5.1",
   133  							},
   134  							&mockregistry.MockValue{
   135  								VName:       "CurrentBuildNumber",
   136  								VDataString: "2600",
   137  							},
   138  							&mockregistry.MockValue{
   139  								VName:       "BuildLabEx",
   140  								VDataString: "5678.1234",
   141  							},
   142  							&mockregistry.MockValue{
   143  								VName:       "InstallationType",
   144  								VDataString: "client",
   145  							},
   146  						},
   147  					},
   148  				},
   149  			},
   150  			want: []*extractor.Package{
   151  				{
   152  					Name:     "windows_xp",
   153  					Version:  "5.1.2600.1234",
   154  					PURLType: "windows",
   155  					Metadata: &metadata.OSVersion{
   156  						Product:     "windows_xp",
   157  						FullVersion: "5.1.2600.1234",
   158  					},
   159  				},
   160  			},
   161  		},
   162  		{
   163  			name: "olderUnknownWindows_returnsFullVersion",
   164  			reg: &mockregistry.MockRegistry{
   165  				Keys: map[string]registry.Key{
   166  					regVersionPath: &mockregistry.MockKey{
   167  						KName: "CurrentVersion",
   168  						KValues: []registry.Value{
   169  							&mockregistry.MockValue{
   170  								VName:       "CurrentVersion",
   171  								VDataString: "5.1",
   172  							},
   173  							&mockregistry.MockValue{
   174  								VName:       "CurrentBuildNumber",
   175  								VDataString: "1234",
   176  							},
   177  							&mockregistry.MockValue{
   178  								VName:       "BuildLabEx",
   179  								VDataString: "5678.1234",
   180  							},
   181  							&mockregistry.MockValue{
   182  								VName:       "InstallationType",
   183  								VDataString: "client",
   184  							},
   185  						},
   186  					},
   187  				},
   188  			},
   189  			want: []*extractor.Package{
   190  				{
   191  					Name:     "unknownWindows",
   192  					Version:  "5.1.1234.1234",
   193  					PURLType: "windows",
   194  					Metadata: &metadata.OSVersion{
   195  						Product:     "unknownWindows",
   196  						FullVersion: "5.1.1234.1234",
   197  					},
   198  				},
   199  			},
   200  		},
   201  		{
   202  			name:    "emptyRegistry_returnsError",
   203  			reg:     &mockregistry.MockRegistry{},
   204  			wantErr: true,
   205  		},
   206  	}
   207  
   208  	for _, tc := range tests {
   209  		t.Run(tc.name, func(t *testing.T) {
   210  			cfg := Configuration{mockregistry.NewOpener(tc.reg)}
   211  			e := New(cfg)
   212  			got, err := e.Extract(t.Context(), nil)
   213  			if tc.wantErr != (err != nil) {
   214  				t.Fatalf("Extract() returned an unexpected error: %v", err)
   215  			}
   216  
   217  			if tc.wantErr == true {
   218  				return
   219  			}
   220  
   221  			if diff := cmp.Diff(inventory.Inventory{Packages: tc.want}, got); diff != "" {
   222  				t.Errorf("Extract() returned an unexpected diff (-want +got): %v", diff)
   223  			}
   224  		})
   225  	}
   226  }