github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/transform/artifact_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package transform provides helper functions to transform URLs to airgap equivalents
     5  package transform
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestNoTransformTarget(t *testing.T) {
    14  	// Properly removes the NoTransform target
    15  	newURL, err := NoTransformTarget("https://gitlab.com", NoTransform+"/some-path/without-query")
    16  	require.NoError(t, err)
    17  	require.Equal(t, "https://gitlab.com/some-path/without-query", newURL.String())
    18  
    19  	// Passes through without the NoTransform target
    20  	newURL, err = NoTransformTarget("https://gitlab.com", "/some-path/without-query")
    21  	require.NoError(t, err)
    22  	require.Equal(t, "https://gitlab.com/some-path/without-query", newURL.String())
    23  
    24  	// Returns an error when given a bad base url
    25  	_, err = NoTransformTarget("https*://gitlab.com", "/some-path/without-query")
    26  	require.Error(t, err)
    27  }
    28  
    29  func TestNpmTransformURL(t *testing.T) {
    30  	protocolPaths := []string{
    31  		"/npm",
    32  		"/npm/-rev/undefined",
    33  		"/npm/-/8.19.2/npm-8.19.2.tgz",
    34  		"/npm/-/8.19.2/npm-8.19.2.tgz/-rev/undefined",
    35  		"/lodash",
    36  		"/lodash/-rev/1",
    37  		"/lodash/-/4.17.21/lodash-4.17.21.tgz",
    38  		"/lodash/-/4.17.21/lodash-4.17.21.tgz/-rev/1",
    39  		"/@types/node",
    40  		"/@types%2fnode",
    41  		"/@types%2Fnode",
    42  		"/@types/node/-rev/100",
    43  		"/@types%2fnode/-rev/100",
    44  		"/@types%2Fnode/-rev/100",
    45  		"/@types/node/-/18.11.2/types-node-18.11.2.tgz",
    46  		"/@types%2fnode/-/18.11.2/types-node-18.11.2.tgz",
    47  		"/@types%2Fnode/-/18.11.2/types-node-18.11.2.tgz",
    48  		"/@types/node/-/18.11.2/types-node-18.11.2.tgz/-rev/100",
    49  		"/-/package/lodash/dist-tags",
    50  		"/-/package/lodash/dist-tags/latest",
    51  		"/-/package/@types/node/dist-tags",
    52  		"/-/package/@types/node/dist-tags/release",
    53  		"/-/npm/v1/security/advisories/bulk",
    54  		"/-/npm/v1/security/audits/quick",
    55  		"/-/user/org.couchdb.user:username",
    56  		"/-/v1/login",
    57  		"/-/v1/search",
    58  	}
    59  
    60  	protocolHosts := []string{
    61  		"https://git.privatemirror.com/api/packages/jackal-mirror-user/npm",
    62  		"https://registry.npmjs.org",
    63  	}
    64  
    65  	for _, host := range protocolHosts {
    66  		for _, path := range protocolPaths {
    67  			newURL, err := NpmTransformURL("https://gitlab.com/project", host+path)
    68  			require.NoError(t, err)
    69  			// For each host/path swap them and add `npm` for compatibility with Gitea/Gitlab
    70  			require.Equal(t, "https://gitlab.com/project/npm"+path, newURL.String())
    71  		}
    72  	}
    73  
    74  	// Returns an error when given a bad base url
    75  	_, err := NpmTransformURL("https*://gitlab.com/project", "https://registry.npmjs.org/npm")
    76  	require.Error(t, err)
    77  }
    78  
    79  func TestPipTransformURL(t *testing.T) {
    80  	protocolPaths := []string{
    81  		"",
    82  		"/",
    83  		"/simple",
    84  		"/simple/",
    85  		"/simple/numpy",
    86  		"/simple/numpy/",
    87  		"/files/numpy/1.23.4/numpy-1.23.4-pp38-pypy38_pp73-win_amd64.whl#sha256-4d52914c88b4930dafb6c48ba5115a96cbab40f45740239d9f4159c4ba779962",
    88  	}
    89  
    90  	protocolHosts := []string{
    91  		"https://git.privatemirror.com/api/packages/jackal-mirror-user/pip",
    92  		"https://pypi.org",
    93  	}
    94  
    95  	for _, host := range protocolHosts {
    96  		for _, path := range protocolPaths {
    97  			newURL, err := PipTransformURL("https://gitlab.com/project", host+path)
    98  			require.NoError(t, err)
    99  			// For each host/path swap them and add `pypi` for compatibility with Gitea/Gitlab
   100  			require.Equal(t, "https://gitlab.com/project/pypi"+path, newURL.String())
   101  		}
   102  	}
   103  
   104  	// Returns an error when given a bad base url
   105  	_, err := PipTransformURL("https*://gitlab.com/project", "https://pypi.org/simple")
   106  	require.Error(t, err)
   107  }
   108  
   109  func TestGenTransformURL(t *testing.T) {
   110  	urls := []string{
   111  		"https://git.example.com/api/packages/jackal-git-user/generic",
   112  		"https://git.example.com/api/packages/jackal-git-user/generic/packageVersion/packageName",
   113  		"https://git.example.com/api/packages/jackal-git-user/generic/package+name/packageVersion/filename",
   114  		"https://git.example.com/api/packages/jackal-git-user/generic/some%20generic%20package/0.0.1/superGeneric.tar.gz",
   115  		"https://git.example.com/archive.zip",
   116  		"https://git.example.com:443/archive.zip",
   117  		"https://git.example.com/api/packages/username/generic/some-package-name/file.1.4.4.tar.gz",
   118  		"https://git.example.com/facebook/zstd/releases/download/v1.4.4/zstd-1.4.4.tar.gz",
   119  		"https://some.host.com/mirror/some-package-release/github.com/baselbuild/bazel-toolchains/archive/someshasum9318490392.tar.gz",
   120  		"https://some.host.com/mirror/some-package-release/some-org/some-library/archive/refs/tags/file.zip",
   121  		"https://i.am.a.weird.php.thing:8080/~/and/rfc/3986/loves/me?array[]=1&array[]=2&pie=(1)",
   122  		"http://why.microsoft/did/you/do/this/Foo.aspx?id=1,2,3,4",
   123  		"http://this.is.legal.too/according/to/spec/?world=!$;,*'",
   124  		"http://i.end.in.nothing.com",
   125  		"http://i.end.in.slash.com/",
   126  	}
   127  
   128  	expectedURLs := []string{
   129  		// We want most of these to exist in the form of /project/generic/packageName/version/filename
   130  		"https://gitlab.com/project/generic/apipackages-3151594639/jackal-git-user/generic",
   131  		"https://gitlab.com/project/generic/apipackages-2561175711/packageVersion/packageName",
   132  		"https://gitlab.com/project/generic/apipackages-2265319408/packageVersion/filename",
   133  		"https://gitlab.com/project/generic/apipackages-4040139506/0.0.1/superGeneric.tar.gz",
   134  		"https://gitlab.com/project/generic/archive.zip-2052577494/archive.zip/archive.zip",
   135  		"https://gitlab.com/project/generic/archive.zip-2052577494/archive.zip/archive.zip",
   136  		"https://gitlab.com/project/generic/apipackages-764706626/some-package-name/file.1.4.4.tar.gz",
   137  		"https://gitlab.com/project/generic/facebookzstd-1475713874/v1.4.4/zstd-1.4.4.tar.gz",
   138  		"https://gitlab.com/project/generic/mirrorsome-package-release-1448769245/archive/someshasum9318490392.tar.gz",
   139  		"https://gitlab.com/project/generic/mirrorsome-package-release-2849990407/tags/file.zip",
   140  		"https://gitlab.com/project/generic/~and-1138178232/loves/me",
   141  		"https://gitlab.com/project/generic/didyou-2239567090/this/Foo.aspx",
   142  		"https://gitlab.com/project/generic/accordingto-29577836/spec/this.is.legal.too",
   143  		"https://gitlab.com/project/generic/i.end.in.nothing.com-2766891503/i.end.in.nothing.com/i.end.in.nothing.com",
   144  		"https://gitlab.com/project/generic/i.end.in.slash.com-1566625415/i.end.in.slash.com/i.end.in.slash.com",
   145  	}
   146  
   147  	for idx, url := range urls {
   148  		newURL, err := GenTransformURL("https://gitlab.com/project", url)
   149  		require.NoError(t, err)
   150  		// For each host/path swap them and add `pypi` for compatibility with Gitea/Gitlab
   151  		require.Equal(t, expectedURLs[idx], newURL.String())
   152  	}
   153  
   154  	// Returns an error when given a bad base url
   155  	_, err := GenTransformURL("https*://gitlab.com/project", "http://i.end.in.nothing.com")
   156  	require.Error(t, err)
   157  }