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

     1  package v2
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestAdvisory_Validate(t *testing.T) {
     9  	testTime := Timestamp(time.Date(2022, 9, 26, 0, 0, 0, 0, time.UTC))
    10  
    11  	tests := []struct {
    12  		name    string
    13  		adv     Advisory
    14  		wantErr bool
    15  	}{
    16  		{
    17  			name: "valid",
    18  			adv: Advisory{
    19  				ID: "CVE-2020-0001",
    20  				Events: []Event{
    21  					{
    22  						Timestamp: testTime,
    23  						Type:      EventTypeDetection,
    24  						Data: Detection{
    25  							Type: DetectionTypeManual,
    26  						},
    27  					},
    28  				},
    29  			},
    30  			wantErr: false,
    31  		},
    32  		{
    33  			name: "valid with aliases",
    34  			adv: Advisory{
    35  				ID: "CVE-2020-0001",
    36  				Aliases: []string{
    37  					"GHSA-5j9q-4xjw-3j3q",
    38  					"GO-2023-0001",
    39  				},
    40  				Events: []Event{
    41  					{
    42  						Timestamp: testTime,
    43  						Type:      EventTypeDetection,
    44  						Data: Detection{
    45  							Type: DetectionTypeManual,
    46  						},
    47  					},
    48  				},
    49  			},
    50  			wantErr: false,
    51  		},
    52  		{
    53  			name: "invalid ID",
    54  			adv: Advisory{
    55  				ID: "vulnerability",
    56  				Events: []Event{
    57  					{
    58  						Timestamp: testTime,
    59  						Type:      EventTypeDetection,
    60  						Data: Detection{
    61  							Type: DetectionTypeManual,
    62  						},
    63  					},
    64  				},
    65  			},
    66  			wantErr: true,
    67  		},
    68  		{
    69  			name: "invalid alias",
    70  			adv: Advisory{
    71  				ID: "CVE-2020-0001",
    72  				Aliases: []string{
    73  					"DSA-12345678",
    74  				},
    75  				Events: []Event{
    76  					{
    77  						Timestamp: testTime,
    78  						Type:      EventTypeDetection,
    79  						Data: Detection{
    80  							Type: DetectionTypeManual,
    81  						},
    82  					},
    83  				},
    84  			},
    85  			wantErr: true,
    86  		},
    87  		{
    88  			name: "duplicate aliases",
    89  			adv: Advisory{
    90  				ID: "CVE-2020-0001",
    91  				Aliases: []string{
    92  					"GHSA-5j9q-4xjw-3j3q",
    93  					"GHSA-5j9q-4xjw-3j3q",
    94  				},
    95  				Events: []Event{
    96  					{
    97  						Timestamp: testTime,
    98  						Type:      EventTypeDetection,
    99  						Data: Detection{
   100  							Type: DetectionTypeManual,
   101  						},
   102  					},
   103  				},
   104  			},
   105  			wantErr: true,
   106  		},
   107  		{
   108  			name: "alias duplicates advisory ID",
   109  			adv: Advisory{
   110  				ID: "GHSA-5j9q-4xjw-3j3q",
   111  				Aliases: []string{
   112  					"GHSA-5j9q-4xjw-3j3q",
   113  				},
   114  				Events: []Event{
   115  					{
   116  						Timestamp: testTime,
   117  						Type:      EventTypeDetection,
   118  						Data: Detection{
   119  							Type: DetectionTypeManual,
   120  						},
   121  					},
   122  				},
   123  			},
   124  			wantErr: true,
   125  		},
   126  		{
   127  			name: "CVE in alias instead of advisory ID",
   128  			adv: Advisory{
   129  				ID: "GHSA-5j9q-4xjw-3j3q",
   130  				Aliases: []string{
   131  					"CVE-2020-0001",
   132  				},
   133  				Events: []Event{
   134  					{
   135  						Timestamp: testTime,
   136  						Type:      EventTypeDetection,
   137  						Data: Detection{
   138  							Type: DetectionTypeManual,
   139  						},
   140  					},
   141  				},
   142  			},
   143  			wantErr: true,
   144  		},
   145  		{
   146  			name: "no events",
   147  			adv: Advisory{
   148  				ID:     "CVE-2020-0001",
   149  				Events: []Event{},
   150  			},
   151  			wantErr: true,
   152  		},
   153  	}
   154  
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			if err := tt.adv.Validate(); (err != nil) != tt.wantErr {
   158  				t.Errorf("Advisory.Validate() error = %v, wantErr %v", err, tt.wantErr)
   159  			}
   160  		})
   161  	}
   162  }