2020-11-30 09:37:17 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDownloadHubIdx(t *testing.T) {
|
|
|
|
back := RawFileURLTemplate
|
|
|
|
//bad url template
|
2021-03-24 17:16:17 +00:00
|
|
|
fmt.Println("Test 'bad URL'")
|
2020-11-30 09:37:17 +00:00
|
|
|
RawFileURLTemplate = "x"
|
2021-03-24 17:16:17 +00:00
|
|
|
ret, err := DownloadHubIdx(&csconfig.Hub{})
|
2020-11-30 09:37:17 +00:00
|
|
|
if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "failed to build request for hub index: parse ") {
|
|
|
|
log.Errorf("unexpected error %s", err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
fmt.Printf("->%+v", ret)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
//bad domain
|
2021-03-24 17:16:17 +00:00
|
|
|
fmt.Println("Test 'bad domain'")
|
2021-09-02 13:17:37 +00:00
|
|
|
RawFileURLTemplate = "https://baddomain/%s/%s"
|
2021-03-24 17:16:17 +00:00
|
|
|
ret, err = DownloadHubIdx(&csconfig.Hub{})
|
2020-11-30 09:37:17 +00:00
|
|
|
if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "failed http request for hub index: Get") {
|
|
|
|
log.Errorf("unexpected error %s", err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
fmt.Printf("->%+v", ret)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
//bad target path
|
2021-03-24 17:16:17 +00:00
|
|
|
fmt.Println("Test 'bad target path'")
|
2020-11-30 09:37:17 +00:00
|
|
|
RawFileURLTemplate = back
|
2021-03-24 17:16:17 +00:00
|
|
|
ret, err = DownloadHubIdx(&csconfig.Hub{HubIndexFile: "/does/not/exist/index.json"})
|
2020-11-30 09:37:17 +00:00
|
|
|
if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "while opening hub index file: open /does/not/exist/index.json:") {
|
|
|
|
log.Errorf("unexpected error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
RawFileURLTemplate = back
|
|
|
|
fmt.Printf("->%+v", ret)
|
|
|
|
}
|