示例#1
0
func TestValidHttpsHostBaseUrl(t *testing.T) {
	assert := assert.New(t)

	// given
	hostPort := "localhost:10602"
	scheme := "https"
	server := localserver.NewLocalServer(hostPort, scheme)
	validURL, _ := url.Parse(scheme + "://" + hostPort)

	builder := NewBuilder()
	configuration := builder.
		BaseURL(validURL).
		URLResponseTimeout(1 * time.Second).
		Build()
	sut := newValidator(configuration)
	sut.errorTagMapper = newMockedErrorTagMapper()

	server.StartTLS("../resources/certs/server_ca.pem", "../resources/certs/server_ca.key")

	// when
	error := sut.validateOnline()

	server.Stop()

	// then
	assert.Nil(error, "There should not be error returned.")
}
示例#2
0
func TestProcessDataFromFile(t *testing.T) {
	// given
	ip := net.IPv4(127, 0, 0, 1)
	port := 45679
	timeout := 2 * time.Second
	options := newMockedTelnetClientOptions(ip.String(), uint64(port), timeout)
	sut := NewTelnetClient(options)

	fi, err := os.Open("../resources/input-data/localhost_1.bin")
	if err != nil {
		panic(err)
	}
	defer fi.Close()
	buffer := bufio.NewReader(fi)

	localServer := localserver.NewLocalServer(fmt.Sprintf("%v:%d", ip.String(), port), "http")
	http.HandleFunc("/second", func(response http.ResponseWriter, request *http.Request) {
		response.Write([]byte("b_response\n"))
	})
	localServer.StartHTTP()

	var response = new(bytes.Buffer)

	// when
	sut.ProcessData(buffer, response)

	// then
	localServer.Stop()
	http.DefaultServeMux = http.NewServeMux()

	assert := assert.New(t)
	assert.Regexp("HTTP/1.1 200 OK\r\nDate: Mon, .+ GMT\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nb_response\n", response.String(), "Expected different response.")
}
示例#3
0
func TestProcessDataFromBufferedString(t *testing.T) {
	// given
	ip := net.IPv4(127, 0, 0, 1)
	port := 45678
	timeout := 2 * time.Second
	options := newMockedTelnetClientOptions(ip.String(), uint64(port), timeout)
	sut := NewTelnetClient(options)

	request := "GET /first HTTP/1.1\nHost: localhost\n\n"
	buffer := bytes.NewBuffer([]byte(request))
	localServer := localserver.NewLocalServer(fmt.Sprintf("%v:%d", ip.String(), port), "http")
	http.HandleFunc("/first", func(response http.ResponseWriter, request *http.Request) {
		response.Write([]byte("a_response\n"))
	})
	localServer.StartHTTP()

	var response = new(bytes.Buffer)

	// when
	sut.ProcessData(buffer, response)

	// then
	localServer.Stop()
	http.DefaultServeMux = http.NewServeMux()

	assert := assert.New(t)
	assert.Regexp("HTTP/1.1 200 OK\r\nDate: Mon, .+ GMT\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n\r\na_response\n", response.String(), "Expected different response.")
}
示例#4
0
func TestStartSimpleFuzzWithServerRunning(t *testing.T) {
	assert := assert.New(t)

	// given
	hostPort := "localhost:10612"
	scheme := "http"
	address := scheme + "://" + hostPort

	server := localserver.NewLocalServer(hostPort, scheme)
	firstHandler := newVisitHandler("/smietnik", "GET")
	secondHandler := newVisitHandler("/spotkania", "POST")
	thirdHandler := newVisitHandler("/aukcje", "PUT")

	http.HandleFunc(firstHandler.endpoint, firstHandler.handle)
	http.HandleFunc(secondHandler.endpoint, secondHandler.handle)
	http.HandleFunc(thirdHandler.endpoint, thirdHandler.handle)

	server.Start()

	var workersNumber uint64 = 4
	methods := []string{"GET", "POST", "PUT"}

	url, error := url.Parse(address)
	if nil != error {
		log.Fatalf("Error occured while parsing an URL: %v, error: %v", address, error)
	}

	inputFile, error := os.OpenFile("../resources/input-data/fuzz_03.txt", os.O_RDONLY, 0666)
	if nil != error {
		log.Fatal("TestStartFuzz: ", error)
	}

	builder := configuration.NewBuilder()
	configuration := builder.
		WorkersNumber(workersNumber).
		WorkerWaitPeriod(0).
		Methods(methods).
		URLResponseTimeout(3 * time.Second).
		FuzzSetFile(inputFile).
		HTTPErrorCode(404).
		BaseURL(url).
		Build()
	sut := NewFuzz(configuration)

	// when
	sut.Start()

	server.Stop()
	http.DefaultServeMux = http.NewServeMux()

	// then
	assert.Len(sut.input, 0, "Input channel should be empty now")
	assert.True(firstHandler.visitted, "Handler "+firstHandler.endpoint+" should be found")
	assert.True(secondHandler.visitted, "Handler "+secondHandler.endpoint+" should be found")
	assert.True(thirdHandler.visitted, "Handler "+thirdHandler.endpoint+" should be found")
}
示例#5
0
func TestOnEntryURLsFound(t *testing.T) {
	assert := assert.New(t)

	// given
	scheme := "http"
	hostPort := "127.0.0.1:10608"
	server := localserver.NewLocalServer(hostPort, scheme)

	firstRegisteredPattern := "/aaa"
	secondRegisteredPattern := "/bbb"
	http.HandleFunc(firstRegisteredPattern, noOperationHandler)
	http.HandleFunc(secondRegisteredPattern, noOperationHandler)
	server.Start()

	address := scheme + "://" + hostPort

	url, error := url.Parse(address)
	if nil != error {
		log.Fatalf("Error occured while parsing an URL: %v, error: %v", address, error)
	}

	firstEntry := messages.NewEntry(firstRegisteredPattern, "GET")
	secondEntry := messages.NewEntry(secondRegisteredPattern, "POST")
	thirdEntry := messages.NewEntry("/ccc", "POST")

	builder := configuration.NewBuilder()
	configuration := builder.
		URLResponseTimeout(3 * time.Second).
		WorkerWaitPeriod(0).
		HTTPErrorCode(http.StatusNotFound).
		BaseURL(url).
		Build()
	sut := NewURLChecker(configuration)
	foundEntries := make(chan messages.FoundEntry, 4)
	assignChannel(sut, foundEntries)

	// when
	sut.OnEntry(firstEntry)
	sut.OnEntry(secondEntry)
	sut.OnEntry(thirdEntry)

	server.Stop()
	http.DefaultServeMux = http.NewServeMux()

	// then
	assert.Len(foundEntries, 2, "Two entries should be considered as found")
	assert.Equal(messages.NewFoundEntry(address+firstEntry.RelativeURL(), firstEntry.HTTPMethod(), http.StatusOK), <-foundEntries, "First entry should be found")
	assert.Equal(messages.NewFoundEntry(address+secondEntry.RelativeURL(), secondEntry.HTTPMethod(), http.StatusOK), <-foundEntries, "Second entry should be found")
}
示例#6
0
func TestReadConfiguration(t *testing.T) {
	assert := assert.New(t)

	// given
	scheme := "http"
	hostPort := "127.0.0.1:10600"
	server := localserver.NewLocalServer(hostPort, scheme)
	server.Start()

	setCommandLineArgs("resources/input-data/fuzz_01.txt", scheme+"://"+hostPort)
	sut := newURLFuzzer()

	// when
	configuration := sut.readConfiguration()
	server.Stop()

	// then
	assert.NotNil(configuration, "Simple configuration should be read from command line.")
}