github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/repo/apk/apk_test.go (about) 1 package apk 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/devseccon/trivy/pkg/fanal/analyzer" 11 "github.com/devseccon/trivy/pkg/fanal/types" 12 ) 13 14 func Test_apkRepoAnalyzer_Analyze(t *testing.T) { 15 tests := []struct { 16 name string 17 input analyzer.AnalysisInput 18 want *analyzer.AnalysisResult 19 wantErr string 20 }{ 21 { 22 name: "alpine", 23 input: analyzer.AnalysisInput{ 24 FilePath: "/etc/apk/repositories", 25 Content: strings.NewReader("http://nl.alpinelinux.org/alpine/v3.7/main"), 26 }, 27 want: &analyzer.AnalysisResult{ 28 Repository: &types.Repository{ 29 Family: types.Alpine, 30 Release: "3.7", 31 }, 32 }, 33 }, 34 { 35 name: "adelie", 36 input: analyzer.AnalysisInput{ 37 FilePath: "/etc/apk/repositories", 38 Content: strings.NewReader("https://distfiles.adelielinux.org/adelie/1.0-beta4/system/"), 39 }, 40 want: nil, 41 }, 42 { 43 name: "repository has 'http' schema", 44 input: analyzer.AnalysisInput{ 45 FilePath: "/etc/apk/repositories", 46 Content: strings.NewReader("http://nl.alpinelinux.org/alpine/v3.7/main"), 47 }, 48 want: &analyzer.AnalysisResult{ 49 Repository: &types.Repository{ 50 Family: types.Alpine, 51 Release: "3.7", 52 }, 53 }, 54 }, 55 { 56 name: "repository has 'https' schema", 57 input: analyzer.AnalysisInput{ 58 FilePath: "/etc/apk/repositories", 59 Content: strings.NewReader("https://dl-cdn.alpinelinux.org/alpine/v3.15/main"), 60 }, 61 want: &analyzer.AnalysisResult{ 62 Repository: &types.Repository{ 63 Family: types.Alpine, 64 Release: "3.15", 65 }, 66 }, 67 }, 68 { 69 name: "repository has 'ftp' schema", 70 input: analyzer.AnalysisInput{ 71 FilePath: "/etc/apk/repositories", 72 Content: strings.NewReader("ftp://dl-3.alpinelinux.org/alpine/v2.6/main"), 73 }, 74 want: &analyzer.AnalysisResult{ 75 Repository: &types.Repository{ 76 Family: types.Alpine, 77 Release: "2.6", 78 }, 79 }, 80 }, 81 { 82 name: "edge version", 83 input: analyzer.AnalysisInput{ 84 FilePath: "/etc/apk/repositories", 85 Content: strings.NewReader("https://dl-cdn.alpinelinux.org/alpine/edge/main"), 86 }, 87 want: &analyzer.AnalysisResult{ 88 Repository: &types.Repository{ 89 Family: types.Alpine, 90 Release: "edge", 91 }, 92 }, 93 }, 94 { 95 name: "happy path. 'etc/apk/repositories' contains some line with v* versions", 96 input: analyzer.AnalysisInput{ 97 FilePath: "/etc/apk/repositories", 98 Content: strings.NewReader(`https://dl-cdn.alpinelinux.org/alpine/v3.1/main 99 100 https://dl-cdn.alpinelinux.org/alpine/v3.10/main 101 `), 102 }, 103 want: &analyzer.AnalysisResult{ 104 Repository: &types.Repository{ 105 Family: types.Alpine, 106 Release: "3.10", 107 }, 108 }, 109 }, 110 { 111 name: "multiple v* versions", 112 input: analyzer.AnalysisInput{ 113 FilePath: "/etc/apk/repositories", 114 Content: strings.NewReader(`https://dl-cdn.alpinelinux.org/alpine/v3.10/main 115 https://dl-cdn.alpinelinux.org/alpine/v3.1/main 116 `), 117 }, 118 want: &analyzer.AnalysisResult{ 119 Repository: &types.Repository{ 120 Family: types.Alpine, 121 Release: "3.10", 122 }, 123 }, 124 }, 125 { 126 name: "multiple v* and edge versions", 127 input: analyzer.AnalysisInput{ 128 FilePath: "/etc/apk/repositories", 129 Content: strings.NewReader(`https://dl-cdn.alpinelinux.org/alpine/edge/main 130 https://dl-cdn.alpinelinux.org/alpine/v3.10/main 131 `), 132 }, 133 want: &analyzer.AnalysisResult{ 134 Repository: &types.Repository{ 135 Family: types.Alpine, 136 Release: "edge", 137 }, 138 }, 139 }, 140 { 141 name: "sad path", 142 input: analyzer.AnalysisInput{ 143 FilePath: "/etc/apk/repositories", 144 Content: strings.NewReader("https://dl-cdn.alpinelinux.org/alpine//edge/main"), 145 }, 146 }, 147 } 148 149 for _, test := range tests { 150 t.Run(test.name, func(t *testing.T) { 151 a := apkRepoAnalyzer{} 152 got, err := a.Analyze(context.Background(), test.input) 153 154 if test.wantErr != "" { 155 assert.Error(t, err) 156 assert.Equal(t, test.wantErr, err.Error()) 157 return 158 } 159 assert.NoError(t, err) 160 assert.Equal(t, test.want, got) 161 }) 162 } 163 }