github.com/google/osv-scalibr@v0.4.1/extractor/standalone/windows/dismpatch/dismpatch_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  package dismpatch
    16  
    17  import (
    18  	_ "embed"
    19  	"errors"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/google/osv-scalibr/extractor"
    25  	"github.com/google/osv-scalibr/extractor/standalone/windows/common/metadata"
    26  	"github.com/google/osv-scalibr/extractor/standalone/windows/dismpatch/dismparser"
    27  	"github.com/google/osv-scalibr/inventory"
    28  )
    29  
    30  func TestPackageFromOutput(t *testing.T) {
    31  	dismTestData, err := os.ReadFile("dismparser/testdata/dism_testdata.txt")
    32  	if err != nil {
    33  		t.Fatalf("Failed to read testdata: %v", err)
    34  	}
    35  
    36  	tests := []struct {
    37  		desc    string
    38  		flavor  string
    39  		output  string
    40  		want    inventory.Inventory
    41  		wantErr error
    42  	}{
    43  		{
    44  			desc:   "Valid test data returns package",
    45  			flavor: "server",
    46  			output: string(dismTestData),
    47  			want: inventory.Inventory{Packages: []*extractor.Package{
    48  				{
    49  					Name:     "windows_server_2019",
    50  					Version:  "10.0.17763.3406",
    51  					PURLType: "windows",
    52  					Metadata: &metadata.OSVersion{
    53  						Product:     "windows_server_2019",
    54  						FullVersion: "10.0.17763.3406",
    55  					},
    56  				},
    57  				{
    58  					Name:     "Microsoft-Windows-FodMetadata-Package~31bf3856ad364e35~amd64~~10.0.17763.1",
    59  					Version:  "10.0.17763.1",
    60  					PURLType: "windows",
    61  				},
    62  				{
    63  					Name:     "Package_for_KB4470788~31bf3856ad364e35~amd64~~17763.164.1.1",
    64  					Version:  "17763.164.1.1",
    65  					PURLType: "windows",
    66  				},
    67  				{
    68  					Name:     "Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.3406.1.5",
    69  					Version:  "17763.3406.1.5",
    70  					PURLType: "windows",
    71  				},
    72  				{
    73  					Name:     "Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.379.1.11",
    74  					Version:  "17763.379.1.11",
    75  					PURLType: "windows",
    76  				},
    77  				{
    78  					Name:     "Package_for_ServicingStack_3232~31bf3856ad364e35~amd64~~17763.3232.1.1",
    79  					Version:  "17763.3232.1.1",
    80  					PURLType: "windows",
    81  				},
    82  				{
    83  					Name:     "Microsoft-Windows-WordPad-FoD-Package~31bf3856ad364e35~wow64~en-US~10.0.19041.1",
    84  					Version:  "10.0.19041.1",
    85  					PURLType: "windows",
    86  				},
    87  			}},
    88  			wantErr: nil,
    89  		},
    90  		{
    91  			desc:    "Empty output returns parsing error",
    92  			flavor:  "server",
    93  			output:  "",
    94  			want:    inventory.Inventory{},
    95  			wantErr: dismparser.ErrParsingError,
    96  		},
    97  	}
    98  
    99  	for _, tc := range tests {
   100  		t.Run(tc.desc, func(t *testing.T) {
   101  			got, gotErr := inventoryFromOutput(tc.flavor, tc.output)
   102  			if !errors.Is(gotErr, tc.wantErr) {
   103  				t.Fatalf("packageFromOutput(%q, %q) returned an unexpected error: %v", tc.flavor, tc.output, gotErr)
   104  			}
   105  
   106  			if tc.wantErr != nil {
   107  				return
   108  			}
   109  
   110  			if diff := cmp.Diff(tc.want, got); diff != "" {
   111  				t.Errorf("packageFromOutput(%q, %q) returned an unexpected diff (-want +got): %v", tc.flavor, tc.output, diff)
   112  			}
   113  		})
   114  	}
   115  }