github.com/anchore/syft@v1.38.2/syft/pkg/license_url_enrichment_test.go (about)

     1  package pkg
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  )
     7  
     8  func TestNewLicenseFromFieldsWithContext_URLEnrichment(t *testing.T) {
     9  	tests := []struct {
    10  		name       string
    11  		value      string
    12  		url        string
    13  		wantValue  string
    14  		wantHasURL bool
    15  	}{
    16  		{
    17  			name:       "Empty value with MIT URL should enrich",
    18  			value:      "",
    19  			url:        "http://opensource.org/licenses/MIT",
    20  			wantValue:  "MIT",
    21  			wantHasURL: true,
    22  		},
    23  		{
    24  			name:       "Empty value with Apache URL should enrich",
    25  			value:      "",
    26  			url:        "https://www.apache.org/licenses/LICENSE-2.0",
    27  			wantValue:  "Apache-2.0",
    28  			wantHasURL: true,
    29  		},
    30  		{
    31  			name:       "Non-empty value should not be overridden",
    32  			value:      "Custom-License",
    33  			url:        "http://opensource.org/licenses/MIT",
    34  			wantValue:  "Custom-License",
    35  			wantHasURL: true,
    36  		},
    37  		{
    38  			name:       "Unknown URL should not enrich",
    39  			value:      "",
    40  			url:        "https://example.com/unknown-license",
    41  			wantValue:  "",
    42  			wantHasURL: true,
    43  		},
    44  		{
    45  			name:       "Empty value and empty URL",
    46  			value:      "",
    47  			url:        "",
    48  			wantValue:  "",
    49  			wantHasURL: false,
    50  		},
    51  	}
    52  
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			ctx := context.Background()
    56  			license := NewLicenseFromFieldsWithContext(ctx, tt.value, tt.url, nil)
    57  
    58  			if license.Value != tt.wantValue {
    59  				t.Errorf("NewLicenseFromFieldsWithContext() Value = %v, want %v", license.Value, tt.wantValue)
    60  			}
    61  
    62  			hasURL := len(license.URLs) > 0
    63  			if hasURL != tt.wantHasURL {
    64  				t.Errorf("NewLicenseFromFieldsWithContext() has URL = %v, want %v", hasURL, tt.wantHasURL)
    65  			}
    66  
    67  			if tt.wantHasURL && tt.url != "" && license.URLs[0] != tt.url {
    68  				t.Errorf("NewLicenseFromFieldsWithContext() URL = %v, want %v", license.URLs[0], tt.url)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestLicenseBuilder_URLOnlyEnrichment(t *testing.T) {
    75  	tests := []struct {
    76  		name       string
    77  		urls       []string
    78  		wantValue  string
    79  		wantSPDX   string
    80  		wantHasURL bool
    81  	}{
    82  		{
    83  			name:       "MIT URL only should enrich",
    84  			urls:       []string{"http://opensource.org/licenses/MIT"},
    85  			wantValue:  "MIT",
    86  			wantSPDX:   "MIT",
    87  			wantHasURL: true,
    88  		},
    89  		{
    90  			name:       "Apache URL only should enrich",
    91  			urls:       []string{"https://www.apache.org/licenses/LICENSE-2.0"},
    92  			wantValue:  "Apache-2.0",
    93  			wantSPDX:   "Apache-2.0",
    94  			wantHasURL: true,
    95  		},
    96  		{
    97  			name:       "Multiple URLs should use first",
    98  			urls:       []string{"http://opensource.org/licenses/MIT", "https://example.com/other"},
    99  			wantValue:  "MIT",
   100  			wantSPDX:   "MIT",
   101  			wantHasURL: true,
   102  		},
   103  		{
   104  			name:       "Unknown URL should not enrich",
   105  			urls:       []string{"https://example.com/unknown-license"},
   106  			wantValue:  "",
   107  			wantSPDX:   "",
   108  			wantHasURL: true,
   109  		},
   110  	}
   111  
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			ctx := context.Background()
   115  			builder := newLicenseBuilder().WithURLs(tt.urls...)
   116  			licenses := builder.Build(ctx).ToSlice()
   117  
   118  			if len(licenses) == 0 {
   119  				t.Fatal("Expected at least one license")
   120  			}
   121  
   122  			license := licenses[0]
   123  
   124  			if license.Value != tt.wantValue {
   125  				t.Errorf("License Value = %v, want %v", license.Value, tt.wantValue)
   126  			}
   127  
   128  			if license.SPDXExpression != tt.wantSPDX {
   129  				t.Errorf("License SPDXExpression = %v, want %v", license.SPDXExpression, tt.wantSPDX)
   130  			}
   131  
   132  			hasURL := len(license.URLs) > 0
   133  			if hasURL != tt.wantHasURL {
   134  				t.Errorf("License has URL = %v, want %v", hasURL, tt.wantHasURL)
   135  			}
   136  
   137  			if tt.wantHasURL && len(tt.urls) > 0 {
   138  				if len(license.URLs) != len(tt.urls) {
   139  					t.Errorf("License URL count = %v, want %v", len(license.URLs), len(tt.urls))
   140  				}
   141  				if license.URLs[0] != tt.urls[0] {
   142  					t.Errorf("License first URL = %v, want %v", license.URLs[0], tt.urls[0])
   143  				}
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func TestNewLicenseFromURLsWithContext_URLEnrichment(t *testing.T) {
   150  	tests := []struct {
   151  		name      string
   152  		value     string
   153  		urls      []string
   154  		wantValue string
   155  	}{
   156  		{
   157  			name:      "Empty value with MIT URL should enrich via builder",
   158  			value:     "",
   159  			urls:      []string{"http://opensource.org/licenses/MIT"},
   160  			wantValue: "MIT",
   161  		},
   162  		{
   163  			name:      "Non-empty value should not be changed",
   164  			value:     "Custom-License",
   165  			urls:      []string{"http://opensource.org/licenses/MIT"},
   166  			wantValue: "Custom-License",
   167  		},
   168  	}
   169  
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			ctx := context.Background()
   173  			license := NewLicenseFromURLsWithContext(ctx, tt.value, tt.urls...)
   174  
   175  			if license.Value != tt.wantValue {
   176  				t.Errorf("NewLicenseFromURLsWithContext() Value = %v, want %v", license.Value, tt.wantValue)
   177  			}
   178  		})
   179  	}
   180  }