github.com/wolfi-dev/wolfictl@v0.16.11/pkg/configs/advisory/v2/detection_test.go (about)

     1  package v2
     2  
     3  import "testing"
     4  
     5  func TestDetection_Validate(t *testing.T) {
     6  	tests := []struct {
     7  		name      string
     8  		detection Detection
     9  		wantErr   bool
    10  	}{
    11  		{
    12  			name: "manual",
    13  			detection: Detection{
    14  				Type: DetectionTypeManual,
    15  			},
    16  			wantErr: false,
    17  		},
    18  		{
    19  			name: "nvdapi",
    20  			detection: Detection{
    21  				Type: DetectionTypeNVDAPI,
    22  				Data: DetectionNVDAPI{
    23  					CPESearched: "cpe:2.3:a:*:tinyxml:*:*:*:*:*:*:*:*",
    24  					CPEFound:    "cpe:2.3:a:tinyxml_project:tinyxml:*:*:*:*:*:*:*:*",
    25  				},
    26  			},
    27  			wantErr: false,
    28  		},
    29  		{
    30  			name: "nvdapi missing CPE data",
    31  			detection: Detection{
    32  				Type: DetectionTypeNVDAPI,
    33  				Data: DetectionNVDAPI{
    34  					CPESearched: "",
    35  					CPEFound:    "cpe:2.3:a:tinyxml_project:tinyxml:*:*:*:*:*:*:*:*",
    36  				},
    37  			},
    38  			wantErr: true,
    39  		},
    40  		{
    41  			name: "invalid type",
    42  			detection: Detection{
    43  				Type: "foo",
    44  			},
    45  			wantErr: true,
    46  		},
    47  		{
    48  			name: "no type",
    49  			detection: Detection{
    50  				Type: "",
    51  			},
    52  			wantErr: true,
    53  		},
    54  		{
    55  			name: "mismatched type and data",
    56  			detection: Detection{
    57  				Type: DetectionTypeManual,
    58  				Data: DetectionNVDAPI{
    59  					CPESearched: "cpe:2.3:a:*:tinyxml:*:*:*:*:*:*:*:*",
    60  					CPEFound:    "cpe:2.3:a:tinyxml_project:tinyxml:*:*:*:*:*:*:*:*",
    61  				},
    62  			},
    63  			wantErr: true,
    64  		},
    65  		{
    66  			name: "scan/v1",
    67  			detection: Detection{
    68  				Type: DetectionTypeScanV1,
    69  				Data: DetectionScanV1{
    70  					SubpackageName:    "crane-docs",
    71  					ComponentID:       "fe8053a3adedc5d0",
    72  					ComponentName:     "github.com/docker/distribution",
    73  					ComponentVersion:  "v2.8.1+incompatible",
    74  					ComponentType:     "go-module",
    75  					ComponentLocation: "/usr/bin/crane",
    76  					Scanner:           "grype",
    77  				},
    78  			},
    79  			wantErr: false,
    80  		},
    81  		{
    82  			name: "scan/v1 missing required fields",
    83  			detection: Detection{
    84  				Type: DetectionTypeScanV1,
    85  				Data: DetectionScanV1{
    86  					SubpackageName:    "",
    87  					ComponentID:       "fe8053a3adedc5d0",
    88  					ComponentName:     "github.com/docker/distribution",
    89  					ComponentVersion:  "",
    90  					ComponentType:     "go-module",
    91  					ComponentLocation: "",
    92  					Scanner:           "",
    93  				},
    94  			},
    95  			wantErr: true,
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			err := tt.detection.Validate()
   102  			if tt.wantErr && err == nil {
   103  				t.Errorf("expected error, got nil")
   104  			}
   105  			if !tt.wantErr && err != nil {
   106  				t.Errorf("unexpected error: %v", err)
   107  			}
   108  		})
   109  	}
   110  }