@@ -3,6 +3,7 @@ import { unlinkSync, writeFileSync } from 'fs';
33import { Vulnerability , TrivyOption } from '../src/interface' ;
44
55const downloader = new Downloader ( ) ;
6+ const trivy = new Trivy ( ) ;
67
78function removeTrivyCmd ( path : string ) {
89 path = path . replace ( / \/ t r i v y $ / , '' ) ;
@@ -109,7 +110,7 @@ describe('Trivy command', () => {
109110 } ) ;
110111} ) ;
111112
112- describe ( 'Scan ' , ( ) => {
113+ describe ( 'Trivy scan ' , ( ) => {
113114 let trivyPath : string ;
114115 const image : string = 'alpine:3.10' ;
115116
@@ -123,49 +124,49 @@ describe('Scan', () => {
123124 removeTrivyCmd ( trivyPath ) ;
124125 } ) ;
125126
126- test ( 'with valid options ' , ( ) => {
127- const options : TrivyOption = {
127+ test ( 'with valid option ' , ( ) => {
128+ const option : TrivyOption = {
128129 severity : 'HIGH,CRITICAL' ,
129130 vulnType : 'os,library' ,
130131 ignoreUnfixed : true ,
131132 format : 'json' ,
132133 } ;
133- const result : Vulnerability [ ] | string = Trivy . scan (
134+ const result : Vulnerability [ ] | string = trivy . scan (
134135 trivyPath ,
135136 image ,
136- options
137+ option
137138 ) ;
138139 expect ( result . length ) . toBeGreaterThanOrEqual ( 1 ) ;
139140 expect ( result ) . toBeInstanceOf ( Object ) ;
140141 } ) ;
141142
142143 test ( 'without ignoreUnfixed' , ( ) => {
143- const options : TrivyOption = {
144+ const option : TrivyOption = {
144145 severity : 'HIGH,CRITICAL' ,
145146 vulnType : 'os,library' ,
146147 ignoreUnfixed : false ,
147148 format : 'json' ,
148149 } ;
149- const result : Vulnerability [ ] | string = Trivy . scan (
150+ const result : Vulnerability [ ] | string = trivy . scan (
150151 trivyPath ,
151152 image ,
152- options
153+ option
153154 ) ;
154155 expect ( result . length ) . toBeGreaterThanOrEqual ( 1 ) ;
155156 expect ( result ) . toBeInstanceOf ( Object ) ;
156157 } ) ;
157158
158159 test ( 'with table format' , ( ) => {
159- const options : TrivyOption = {
160+ const option : TrivyOption = {
160161 severity : 'HIGH,CRITICAL' ,
161162 vulnType : 'os,library' ,
162163 ignoreUnfixed : false ,
163164 format : 'table' ,
164165 } ;
165- const result : Vulnerability [ ] | string = Trivy . scan (
166+ const result : Vulnerability [ ] | string = trivy . scan (
166167 trivyPath ,
167168 image ,
168- options
169+ option
169170 ) ;
170171 expect ( result . length ) . toBeGreaterThanOrEqual ( 1 ) ;
171172 expect ( result ) . toMatch ( / a l p i n e : 3 \. 1 0 / ) ;
@@ -179,8 +180,8 @@ describe('Scan', () => {
179180 format : 'json' ,
180181 } ;
181182 expect ( ( ) => {
182- Trivy . scan ( trivyPath , image , invalidOption ) ;
183- } ) . toThrowError ( 'severity option error: INVALID is unknown severity' ) ;
183+ trivy . scan ( trivyPath , image , invalidOption ) ;
184+ } ) . toThrowError ( 'Trivy option error: INVALID is unknown severity' ) ;
184185 } ) ;
185186
186187 test ( 'with invalid vulnType' , ( ) => {
@@ -191,8 +192,8 @@ describe('Scan', () => {
191192 format : 'json' ,
192193 } ;
193194 expect ( ( ) => {
194- Trivy . scan ( trivyPath , image , invalidOption ) ;
195- } ) . toThrowError ( 'vuln-type option error: INVALID is unknown vuln-type' ) ;
195+ trivy . scan ( trivyPath , image , invalidOption ) ;
196+ } ) . toThrowError ( 'Trivy option error: INVALID is unknown vuln-type' ) ;
196197 } ) ;
197198} ) ;
198199
@@ -204,7 +205,7 @@ describe('Parse', () => {
204205 Vulnerabilities : null ,
205206 } ,
206207 ] ;
207- const result = Trivy . parse ( vulnerabilities ) ;
208+ const result = trivy . parse ( vulnerabilities ) ;
208209 expect ( result ) . toBe ( '' ) ;
209210 } ) ;
210211
@@ -247,9 +248,77 @@ describe('Parse', () => {
247248 ] ,
248249 } ,
249250 ] ;
250- const result = Trivy . parse ( vulnerabilities ) ;
251+ const result = trivy . parse ( vulnerabilities ) ;
251252 expect ( result ) . toMatch (
252253 / \| T i t l e \| S e v e r i t y \| C V E \| P a c k a g e N a m e \| I n s t a l l e d V e r s i o n \| F i x e d V e r s i o n \| R e f e r e n c e s \| /
253254 ) ;
254255 } ) ;
255256} ) ;
257+
258+ describe ( 'Validate trivy option' , ( ) => {
259+ test ( 'with a valid severity' , ( ) => {
260+ const options : string [ ] = [ 'HIGH' ] ;
261+ const result = trivy [ 'validateSeverity' ] ( options ) ;
262+ expect ( result ) . toBeTruthy ( ) ;
263+ } ) ;
264+
265+ test ( 'with two valid severities' , ( ) => {
266+ const options : string [ ] = [ 'HIGH' , 'CRITICAL' ] ;
267+ const result = trivy [ 'validateSeverity' ] ( options ) ;
268+ expect ( result ) . toBeTruthy ( ) ;
269+ } ) ;
270+
271+ test ( 'with an invalid severity' , ( ) => {
272+ const options : string [ ] = [ 'INVALID' ] ;
273+ expect ( ( ) => {
274+ trivy [ 'validateSeverity' ] ( options ) ;
275+ } ) . toThrowError ( 'Trivy option error: INVALID is unknown severity' ) ;
276+ } ) ;
277+
278+ test ( 'with two invalid severities' , ( ) => {
279+ const options : string [ ] = [ 'INVALID' , 'ERROR' ] ;
280+ expect ( ( ) => {
281+ trivy [ 'validateSeverity' ] ( options ) ;
282+ } ) . toThrowError ( 'Trivy option error: INVALID,ERROR is unknown severity' ) ;
283+ } ) ;
284+
285+ test ( 'with an invalid and a valid severities' , ( ) => {
286+ const options : string [ ] = [ 'INVALID' , 'HIGH' ] ;
287+ expect ( ( ) => {
288+ trivy [ 'validateSeverity' ] ( options ) ;
289+ } ) . toThrowError ( 'Trivy option error: INVALID,HIGH is unknown severity' ) ;
290+ } ) ;
291+
292+ test ( 'with a valid vuln-type' , ( ) => {
293+ const options : string [ ] = [ 'os' ] ;
294+ const result = trivy [ 'validateVulnType' ] ( options ) ;
295+ expect ( result ) . toBeTruthy ( ) ;
296+ } ) ;
297+
298+ test ( 'with two valid vuln-types' , ( ) => {
299+ const options : string [ ] = [ 'os' , 'library' ] ;
300+ const result = trivy [ 'validateVulnType' ] ( options ) ;
301+ expect ( result ) . toBeTruthy ( ) ;
302+ } ) ;
303+
304+ test ( 'with an invalid vuln-type' , ( ) => {
305+ const options : string [ ] = [ 'INVALID' ] ;
306+ expect ( ( ) => {
307+ trivy [ 'validateVulnType' ] ( options ) ;
308+ } ) . toThrowError ( 'Trivy option error: INVALID is unknown vuln-type' ) ;
309+ } ) ;
310+
311+ test ( 'with two invalid vuln-types' , ( ) => {
312+ const options : string [ ] = [ 'INVALID' , 'ERROR' ] ;
313+ expect ( ( ) => {
314+ trivy [ 'validateVulnType' ] ( options ) ;
315+ } ) . toThrowError ( 'Trivy option error: INVALID,ERROR is unknown vuln-type' ) ;
316+ } ) ;
317+
318+ test ( 'with a valid and an invalid vuln-types' , ( ) => {
319+ const options : string [ ] = [ 'INVALID' , 'os' ] ;
320+ expect ( ( ) => {
321+ trivy [ 'validateVulnType' ] ( options ) ;
322+ } ) . toThrowError ( 'Trivy option error: INVALID,os is unknown vuln-type' ) ;
323+ } ) ;
324+ } ) ;
0 commit comments