github.com/anchore/syft@v1.38.2/internal/spdxlicense/license_url_test.go (about)

     1  package spdxlicense
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestLicenseByURL(t *testing.T) {
     8  	tests := []struct {
     9  		name      string
    10  		url       string
    11  		wantID    string
    12  		wantFound bool
    13  	}{
    14  		{
    15  			name:      "MIT license URL (https)",
    16  			url:       "https://opensource.org/license/mit/",
    17  			wantID:    "MIT",
    18  			wantFound: true,
    19  		},
    20  		{
    21  			name:      "MIT license URL (http)",
    22  			url:       "http://opensource.org/licenses/MIT",
    23  			wantID:    "MIT",
    24  			wantFound: true,
    25  		},
    26  		{
    27  			name:      "Apache 2.0 license URL",
    28  			url:       "https://www.apache.org/licenses/LICENSE-2.0",
    29  			wantID:    "Apache-2.0",
    30  			wantFound: true,
    31  		},
    32  		{
    33  			name:      "GPL 3.0 or later URL",
    34  			url:       "https://www.gnu.org/licenses/gpl-3.0-standalone.html",
    35  			wantID:    "GPL-3.0-or-later",
    36  			wantFound: true,
    37  		},
    38  		{
    39  			name:      "BSD 3-Clause URL",
    40  			url:       "https://opensource.org/licenses/BSD-3-Clause",
    41  			wantID:    "BSD-3-Clause",
    42  			wantFound: true,
    43  		},
    44  		{
    45  			name:      "URL with trailing whitespace",
    46  			url:       "  http://opensource.org/licenses/MIT  ",
    47  			wantID:    "MIT",
    48  			wantFound: true,
    49  		},
    50  		{
    51  			name:      "Unknown URL",
    52  			url:       "https://example.com/unknown-license",
    53  			wantID:    "",
    54  			wantFound: false,
    55  		},
    56  		{
    57  			name:      "Empty URL",
    58  			url:       "",
    59  			wantID:    "",
    60  			wantFound: false,
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			info, found := LicenseByURL(tt.url)
    67  			if found != tt.wantFound {
    68  				t.Errorf("LicenseByURL() found = %v, want %v", found, tt.wantFound)
    69  			}
    70  			if found {
    71  				if info.ID != tt.wantID {
    72  					t.Errorf("LicenseByURL() ID = %v, want %v", info.ID, tt.wantID)
    73  				}
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestLicenseByURL_DeprecatedLicenses(t *testing.T) {
    80  	// Test that deprecated license URLs map to their replacement licenses
    81  	// For example, GPL-2.0+ should map to GPL-2.0-or-later
    82  
    83  	// This test needs actual URLs from deprecated licenses
    84  	// We can verify by checking if a deprecated license URL maps to a non-deprecated ID
    85  	url := "https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html"
    86  	info, found := LicenseByURL(url)
    87  
    88  	if found {
    89  		// Check that we got a valid non-deprecated license ID
    90  		if info.ID == "" {
    91  			t.Error("Got empty license ID for deprecated license URL")
    92  		}
    93  		// The ID should be the replacement (GPL-2.0-only or GPL-2.0-or-later)
    94  		// depending on the URL
    95  		t.Logf("Deprecated license URL mapped to: ID=%s", info.ID)
    96  	}
    97  }