bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/cwhub/download_test.go (about)

     1  package cwhub
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"bitbucket.org/Aishee/synsec/pkg/csconfig"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  func TestDownloadHubIdx(t *testing.T) {
    13  	back := RawFileURLTemplate
    14  	//bad url template
    15  	fmt.Println("Test 'bad URL'")
    16  	RawFileURLTemplate = "x"
    17  	ret, err := DownloadHubIdx(&csconfig.Hub{})
    18  	if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "failed to build request for hub index: parse ") {
    19  		log.Errorf("unexpected error %s", err)
    20  	}
    21  	fmt.Printf("->%+v", ret)
    22  
    23  	//bad domain
    24  	fmt.Println("Test 'bad domain'")
    25  	RawFileURLTemplate = "https://baddomain/breakteam/hub/%s/%s"
    26  	ret, err = DownloadHubIdx(&csconfig.Hub{})
    27  	if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "failed http request for hub index: Get") {
    28  		log.Errorf("unexpected error %s", err)
    29  	}
    30  	fmt.Printf("->%+v", ret)
    31  
    32  	//bad target path
    33  	fmt.Println("Test 'bad target path'")
    34  	RawFileURLTemplate = back
    35  	ret, err = DownloadHubIdx(&csconfig.Hub{HubIndexFile: "/does/not/exist/index.json"})
    36  	if err == nil || !strings.HasPrefix(fmt.Sprintf("%s", err), "while opening hub index file: open /does/not/exist/index.json:") {
    37  		log.Errorf("unexpected error %s", err)
    38  	}
    39  
    40  	RawFileURLTemplate = back
    41  	fmt.Printf("->%+v", ret)
    42  }