@@ -112,17 +112,21 @@ func TestScanDirectSingle(t *testing.T) {
112112 myConfig := setupConfig (t )
113113 myConfig .App .Trace = true
114114 myConfig .Scanning .ScanDebug = true
115+ myConfig .Scanning .MatchConfigAllowed = false
116+ myConfig .Scanning .RankingAllowed = false
115117 apiService := NewAPIService (myConfig )
116118
117119 tests := []struct {
118- name string
119- fieldName string
120- file string
121- binary string
122- telemetry bool
123- scanType string
124- assets string
125- want int
120+ name string
121+ fieldName string
122+ file string
123+ binary string
124+ telemetry bool
125+ scanType string
126+ assets string
127+ scanSettingsB64 string
128+ settingsAllowed bool
129+ want int
126130 }{
127131 {
128132 name : "Scanning - wrong name" ,
@@ -186,6 +190,77 @@ func TestScanDirectSingle(t *testing.T) {
186190 assets : "pkg:github/org/repo" ,
187191 want : http .StatusOK ,
188192 },
193+ {
194+ name : "Scanning - Settings - invalid base64" ,
195+ binary : "../../test-support/scanoss.sh" ,
196+ fieldName : "filename" ,
197+ file : "./tests/fingers.wfp" ,
198+ settingsAllowed : true ,
199+ scanSettingsB64 : "invalid-base64!!!" ,
200+ want : http .StatusBadRequest ,
201+ },
202+ {
203+ name : "Scanning - Settings - invalid json" ,
204+ binary : "../../test-support/scanoss.sh" ,
205+ fieldName : "filename" ,
206+ file : "./tests/fingers.wfp" ,
207+ // Base64 decoded JSON:
208+ // {
209+ // "field": "something,
210+ // "array": [
211+ // }
212+ scanSettingsB64 : "ewoiZmllbGQiOiAic29tZXRoaW5nLAogImFycmF5IjogWwp9" ,
213+ settingsAllowed : true ,
214+ want : http .StatusBadRequest ,
215+ },
216+ {
217+ name : "Scanning - Settings - not allowed" ,
218+ binary : "../../test-support/scanoss.sh" ,
219+ fieldName : "filename" ,
220+ file : "./tests/fingers.wfp" ,
221+ // Base64 decoded JSON:
222+ // {
223+ // "min_snippet_hits": 5,
224+ // "min_snippet_lines": 10
225+ // }
226+ scanSettingsB64 : "eyJtaW5fc25pcHBldF9oaXRzIjo1LCJtaW5fc25pcHBldF9saW5lcyI6MTB9" ,
227+ settingsAllowed : false ,
228+ want : http .StatusBadRequest ,
229+ },
230+ {
231+ name : "Scanning - Settings - success 1" ,
232+ binary : "../../test-support/scanoss.sh" ,
233+ fieldName : "filename" ,
234+ file : "./tests/fingers.wfp" ,
235+ // Base64 decoded JSON:
236+ // {
237+ // "ranking_enabled": true,
238+ // "ranking_threshold": 85,
239+ // "min_snippet_hits": 3,
240+ // "min_snippet_lines": 8,
241+ // "honour_file_exts": false
242+ // }
243+ scanSettingsB64 : "eyJyYW5raW5nX2VuYWJsZWQiOnRydWUsInJhbmtpbmdfdGhyZXNob2xkIjo4NSwibWluX3NuaXBwZXRfaGl0cyI6MywibWluX3NuaXBwZXRfbGluZXMiOjgsImhvbm91cl9maWxlX2V4dHMiOmZhbHNlfQ==" ,
244+ settingsAllowed : true ,
245+ want : http .StatusOK ,
246+ },
247+ {
248+ name : "Scanning - Settings - success 2" ,
249+ binary : "../../test-support/scanoss.sh" ,
250+ fieldName : "filename" ,
251+ file : "./tests/fingers.wfp" ,
252+ // Base64 decoded JSON:
253+ // {
254+ // "ranking_enabled": true,
255+ // "ranking_threshold": -1,
256+ // "min_snippet_hits": 3,
257+ // "min_snippet_lines": 8,
258+ // "honour_file_exts": true
259+ // }
260+ scanSettingsB64 : "ewogICJyYW5raW5nX2VuYWJsZWQiOiB0cnVlLAogICJyYW5raW5nX3RocmVzaG9sZCI6IC0xLAogICJtaW5fc25pcHBldF9oaXRzIjogMywKICAibWluX3NuaXBwZXRfbGluZXMiOiA4LAogICJob25vdXJfZmlsZV9leHRzIjogdHJ1ZQp9" ,
261+ settingsAllowed : true ,
262+ want : http .StatusOK ,
263+ },
189264 }
190265 for _ , test := range tests {
191266 t .Run (test .name , func (t * testing.T ) {
@@ -196,6 +271,8 @@ func TestScanDirectSingle(t *testing.T) {
196271 myConfig .App .Trace = true
197272 }
198273 }
274+ myConfig .Scanning .MatchConfigAllowed = test .settingsAllowed
275+ myConfig .Scanning .RankingEnabled = test .settingsAllowed
199276 myConfig .Scanning .ScanBinary = test .binary
200277 myConfig .Telemetry .Enabled = test .telemetry
201278 filePath := test .file
@@ -225,9 +302,11 @@ func TestScanDirectSingle(t *testing.T) {
225302 }
226303 }
227304 _ = mw .Close () // close the writer before making the request
228-
229305 req := httptest .NewRequest (http .MethodPost , "http://localhost/scan/direct" , postBody )
230306 w := httptest .NewRecorder ()
307+ if len (test .scanSettingsB64 ) > 0 {
308+ req .Header .Set ("Scanoss-Settings" , test .scanSettingsB64 )
309+ }
231310 req .Header .Add ("Content-Type" , mw .FormDataContentType ())
232311 apiService .ScanDirect (w , req )
233312 resp := w .Result ()
@@ -449,3 +528,76 @@ func TestScanDirectSingleHPSM(t *testing.T) {
449528 })
450529 }
451530}
531+
532+ func TestScanDirectSingleSlow (t * testing.T ) {
533+ err := zlog .NewSugaredDevLogger ()
534+ if err != nil {
535+ t .Fatalf ("an error '%s' was not expected when opening a sugared logger" , err )
536+ }
537+ defer zlog .SyncZap ()
538+ myConfig := setupConfig (t )
539+ myConfig .App .Trace = true
540+ myConfig .Scanning .ScanDebug = true
541+ myConfig .Scanning .ScanTimeout = 5
542+ apiService := NewAPIService (myConfig )
543+
544+ tests := []struct {
545+ name string
546+ fieldName string
547+ file string
548+ binary string
549+ scanType string
550+ assets string
551+ want int
552+ }{
553+ {
554+ name : "Scanning - success 1" ,
555+ binary : "../../test-support/scanoss.sh" ,
556+ fieldName : "file" ,
557+ file : "./tests/fingers.wfp" ,
558+ want : http .StatusOK ,
559+ },
560+ {
561+ name : "Scanning - Slow fail" ,
562+ binary : "../../test-support/scanoss-slow.sh" ,
563+ fieldName : "filename" ,
564+ file : "./tests/fingers-hpsm.wfp" ,
565+ want : http .StatusGatewayTimeout ,
566+ },
567+ }
568+ for _ , test := range tests {
569+ t .Run (test .name , func (t * testing.T ) {
570+ myConfig .Scanning .ScanBinary = test .binary
571+ filePath := test .file
572+ fieldName := test .fieldName
573+ postBody := new (bytes.Buffer )
574+ mw := multipart .NewWriter (postBody )
575+ file , err := os .Open (filePath )
576+ if err != nil {
577+ t .Fatal (err )
578+ }
579+ writer , err := mw .CreateFormFile (fieldName , filePath )
580+ if err != nil {
581+ t .Fatal (err )
582+ }
583+ if _ , err = io .Copy (writer , file ); err != nil {
584+ t .Fatal (err )
585+ }
586+ _ = mw .Close () // close the writer before making the request
587+
588+ req := httptest .NewRequest (http .MethodPost , "http://localhost/scan/direct" , postBody )
589+ w := httptest .NewRecorder ()
590+ req .Header .Add ("Content-Type" , mw .FormDataContentType ())
591+ apiService .ScanDirect (w , req )
592+ resp := w .Result ()
593+ body , err := io .ReadAll (resp .Body )
594+ if err != nil {
595+ t .Fatalf ("an error was not expected when reading from request: %v" , err )
596+ }
597+ assert .Equal (t , test .want , resp .StatusCode )
598+ fmt .Println ("Status: " , resp .StatusCode )
599+ fmt .Println ("Type: " , resp .Header .Get ("Content-Type" ))
600+ fmt .Println ("Body: " , string (body ))
601+ })
602+ }
603+ }
0 commit comments