Example #1
0
func TestPipe(t *testing.T) {
	assert := assert.New(t)

	// given
	file, error := openFuzzSetFile("fuzz_01.txt")
	assert.Nil(error, "Fuzz set file must be available, error: %v", error)

	builder := configuration.NewBuilder()
	configuration := builder.FuzzSetFile(file).Build()
	sut := newFileReader(configuration)

	var out = make(chan string, 3) // number of lines in fuzz file
	var done = make(chan bool, 1)

	// when
	sut.pipe(out, done)

	// then
	assert.True(<-done, "Reading file should be finished.")
	assert.Equal("001", <-out, "Invalid line read from file.")
	assert.Equal("002", <-out, "Invalid line read from file.")
	assert.Equal("003", <-out, "Invalid line read from file.")
	assert.Len(out, 0, "Out buffer should be empty now.")
	close(out)

	assert.Equal(^(uintptr(0)), file.Fd(), "File descriptor should be closed now.")
}
Example #2
0
func TestStartSimpleFuzzNoServerRunning(t *testing.T) {
	assert := assert.New(t)

	// given
	var workersNumber uint64 = 2
	methods := []string{"GET", "POST", "PUT"}
	address := "http://localhost:10604"
	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()

	// then
	assert.Len(sut.input, 0, "Input channel should be empty now")
}
Example #3
0
func TestFinish(t *testing.T) {
	assert := assert.New(t)

	// given
	aFile, error := ioutil.TempFile("", "TestNewFileWriterOutputFileDefined")
	if nil != error {
		log.Fatalf("Can't create a temporary output file: %v", error)
	}
	path := aFile.Name()

	builder := configuration.NewBuilder()
	configuration := builder.
		OutputFile(path).
		Build()
	sut := NewFileWriter(configuration)

	// when
	fi, error := sut.outputFile.Stat()
	if nil != error {
		log.Fatalf("Error occured while checking size of file \"%v\": %v", path, error)
	}

	sut.Finish()
	defer clean(path)

	// then
	assert.NotNil(sut.outputFile, "Output file has been defined")
	assert.True(fi.Size() == 0, "File should be empty")
	assert.Equal(^(uintptr(0)), sut.outputFile.Fd(), "File descriptor should be closed now.")
}
Example #4
0
func TestNewURLCheckerClient(t *testing.T) {
	assert := assert.New(t)

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

	expectedURLResponseTimeout := 99 * time.Second
	builder := configuration.NewBuilder()
	configuration := builder.
		URLResponseTimeout(expectedURLResponseTimeout).
		WorkerWaitPeriod(0).
		HTTPErrorCode(http.StatusNotFound).
		BaseURL(url).
		Build()

	// when
	sut := NewURLChecker(configuration)

	// then
	assert.NotNil(sut, "URL checker instance should be created")
	assert.NotNil(sut.client, "HTTP client should be set")
	assert.Equal(expectedURLResponseTimeout, sut.client.Timeout, "URL response timeout should be same as given")
}
Example #5
0
func TestNewFileWriterOutputFileDefined(t *testing.T) {
	assert := assert.New(t)

	// given
	aFile, error := ioutil.TempFile("", "TestNewFileWriterOutputFileDefined")
	if nil != error {
		log.Fatalf("Can't create a temporary output file: %v", error)
	}
	path := aFile.Name()

	builder := configuration.NewBuilder()
	configuration := builder.
		OutputFile(path).
		Build()

	// when
	sut := NewFileWriter(configuration)
	defer cleanAndClose(aFile)

	fi, error := sut.outputFile.Stat()
	if nil != error {
		log.Fatalf("Error occured while checking size of file \"%v\": %v", path, error)
	}

	// then
	assert.NotNil(sut.outputFile, "Output file has been defined")
	assert.True(fi.Size() == 0, "File should be empty")
}
Example #6
0
func TestOnFoundEntry(t *testing.T) {
	assert := assert.New(t)

	// given
	aFile, error := ioutil.TempFile("", "TestNewFileWriterOutputFileDefined")
	if nil != error {
		log.Fatalf("Can't create a temporary output file: %v", error)
	}
	path := aFile.Name()

	builder := configuration.NewBuilder()
	configuration := builder.
		OutputFile(path).
		Build()
	firstFoundEntry := messages.NewFoundEntry("1", "2", 3)
	sut := NewFileWriter(configuration)

	// when
	sut.OnFoundEntry(firstFoundEntry)
	defer cleanAndClose(aFile)

	fi, error := sut.outputFile.Stat()
	if nil != error {
		log.Fatalf("Error occured while checking size of file \"%v\": %v", path, error)
	}

	// then
	assert.True(fi.Size() > 0, "File should have contents")
}
Example #7
0
func TestOnEntryNoURLsFound(t *testing.T) {
	assert := assert.New(t)

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

	firstEntry := messages.NewEntry("/aaa", "GET")
	secondEntry := messages.NewEntry("/bbb", "POST")

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

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

	// then
	assert.Len(foundEntries, 0, "No entries should be considered as found")
}
Example #8
0
func TestNewURLCheckerProperties(t *testing.T) {
	assert := assert.New(t)

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

	expectedWorkerWaitPeriod := 0 * time.Second
	expectedHTTPErrorCode := http.StatusNotFound

	builder := configuration.NewBuilder()
	configuration := builder.
		URLResponseTimeout(3 * time.Second).
		WorkerWaitPeriod(expectedWorkerWaitPeriod).
		HTTPErrorCode(uint64(expectedHTTPErrorCode)).
		BaseURL(url).
		Build()

	// when
	sut := NewURLChecker(configuration)

	// then
	assert.NotNil(sut, "URL checker instance should be created")
	assert.Equal(*url, sut.baseURL, "Base URL should be same as given")
	assert.Equal(expectedHTTPErrorCode, sut.httpErrorCode, "HTTP error code should be same as given")
	assert.Equal(expectedHTTPErrorCode, sut.httpErrorCode, "HTTP error code should be same as given")
	assert.Equal(expectedWorkerWaitPeriod, sut.waitPeriod, "Worker wait period should be same as given")
}
Example #9
0
func TestOnEntryHTTPHeaders(t *testing.T) {
	assert := assert.New(t)

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

	firstHeaderName := "first"
	firstHeaderValue := "8903434"
	secondHeaderName := "second"
	secondHeaderValue := "34234324"

	headers := map[string]string{firstHeaderName: firstHeaderValue, secondHeaderName: secondHeaderValue}
	expectedURLResponseTimeout := 99 * time.Second
	builder := configuration.NewBuilder()
	configuration := builder.
		Headers(headers).
		URLResponseTimeout(expectedURLResponseTimeout).
		WorkerWaitPeriod(0).
		HTTPErrorCode(http.StatusNotFound).
		BaseURL(url).
		Build()

	// when
	sut := NewURLChecker(configuration)

	// then
	assert.NotNil(sut, "URL checker instance should be created")
	assert.Len(sut.headers, 2, "The number of HTTP headers is different than expected")
	assert.Equal([]string{firstHeaderValue}, sut.headers[firstHeaderName], "HTTP header is different")
	assert.Equal([]string{secondHeaderValue}, sut.headers[secondHeaderName], "HTTP header is different")
}
Example #10
0
func TestNewFuzzMinimalConfiguration(t *testing.T) {
	assert := assert.New(t)

	// given
	var workersNumber uint64 = 2
	methods := []string{"GET", "POST"}
	address := "http://localhost:10603"
	url, error := url.Parse(address)
	if nil != error {
		log.Fatalf("Error occured while parsing an URL: %v, error: %v", address, error)
	}

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

	// when
	sut := NewFuzz(configuration)

	// then
	assert.NotNil(sut, "Instance should be created")
	assert.NotNil(sut.input, "Input channel should be defined")
	assert.NotNil(sut.graph, "Flow graph should be created")
	assert.NotNil(sut.configuration, "Configuration should be set")
}
func TestPipeIncludingFileReader(t *testing.T) {
	assert := assert.New(t)

	// given
	file, error := openFuzzSetFile("fuzz_01.txt")
	assert.Nil(error, "Fuzz set file must be available, error: %v", error)

	builder := configuration.NewBuilder()
	configuration := builder.FuzzSetFile(file).Build()
	sut := NewAbortableFileReader(configuration)

	var out = make(chan string, 3) // number of lines in fuzz file

	// when
	isPipingDone := sut.Pipe(out)

	// then
	assert.Equal("001", <-out, "Invalid line read from file.")
	assert.Equal("002", <-out, "Invalid line read from file.")
	assert.Equal("003", <-out, "Invalid line read from file.")
	assert.Len(out, 0, "Out buffer should be empty now.")
	assert.True(isPipingDone, "Piping data was finished successfully")

	assert.Equal(^(uintptr(0)), file.Fd(), "File descriptor should be closed now.")
}
Example #12
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")
}
func TestNewInstanceHasFileReader(t *testing.T) {
	assert := assert.New(t)

	// given
	builder := configuration.NewBuilder()
	configuration := builder.FuzzSetFile(new(os.File)).Build()

	// when
	sut := NewAbortableFileReader(configuration)

	// then
	assert.NotNil(sut.fileReader, "File reader should not be empty.")
}
Example #14
0
func TestNewInstanceHasInputFile(t *testing.T) {
	assert := assert.New(t)

	// given
	file := new(os.File)
	builder := configuration.NewBuilder()
	configuration := builder.FuzzSetFile(file).Build()

	// when
	sut := newFileReader(configuration)

	// then
	assert.Equal(file, sut.inputFile, "Expected file is different than the specified in configuration.")
}
Example #15
0
func TestNewFileWriterOutputFileUndefined(t *testing.T) {
	assert := assert.New(t)

	// given
	builder := configuration.NewBuilder()
	configuration := builder.Build()

	// when
	sut := NewFileWriter(configuration)

	// then
	assert.NotNil(sut, "File writer is not nil")
	assert.Nil(sut.outputFile, "Output file has not been given")
}
func TestNewEntryProducer(t *testing.T) {
	assert := assert.New(t)

	// given
	methods := []string{"method_a", "method_b"}
	builder := configuration.NewBuilder()
	configuration := builder.Methods(methods).Build()

	// when
	sut := NewEntryProducer(configuration)

	// then
	assert.Equal(sut.methods, methods, "HTTP Methods should be the same as defined by used")
}
Example #17
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")
}
Example #18
0
func TestCloseFile(t *testing.T) {
	assert := assert.New(t)

	// given
	file, error := openFuzzSetFile("fuzz_01.txt")
	assert.Nil(error, "Fuzz set file must be available, error: %v", error)

	builder := configuration.NewBuilder()
	configuration := builder.FuzzSetFile(file).Build()
	sut := newFileReader(configuration)

	// when
	sut.closeFile()

	// then
	assert.Equal(^(uintptr(0)), file.Fd(), "File descriptor should be closed now.")
}
func TestOnRelativeURLOneMethod(t *testing.T) {
	assert := assert.New(t)

	// given
	anURL := "an_url"
	entries := make(chan messages.Entry, 3)

	methods := []string{"method_a"}
	builder := configuration.NewBuilder()
	configuration := builder.Methods(methods).Build()
	sut := NewEntryProducer(configuration)
	sut.Entry = entries

	// when
	sut.OnRelativeURL(anURL)

	// then
	assert.Equal(<-entries, messages.NewEntry(anURL, "method_a"), "Entries are not equal")
	assert.Len(entries, 0, "Entries channel should be empty now")
}
Example #20
0
func TestCreateRequest(t *testing.T) {
	assert := assert.New(t)

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

	firstHeaderName := "first"
	firstHeaderValue := "8903434"
	secondHeaderName := "second"
	secondHeaderValue := "34234324"

	httpMethod := "DELETE"

	headers := map[string]string{firstHeaderName: firstHeaderValue, secondHeaderName: secondHeaderValue}
	expectedURLResponseTimeout := 99 * time.Second
	builder := configuration.NewBuilder()
	configuration := builder.
		Headers(headers).
		URLResponseTimeout(expectedURLResponseTimeout).
		WorkerWaitPeriod(0).
		HTTPErrorCode(http.StatusNotFound).
		BaseURL(url).
		Build()

	sut := NewURLChecker(configuration)

	// when
	result := sut.createRequest(httpMethod, address)

	// then
	assert.NotNil(result, "HTTP result should not be nil")
	assert.Equal(httpMethod, result.Method, "HTTP method should be same as given")
	assert.Equal([]string{firstHeaderValue}, result.Header[firstHeaderName], "HTTP header is different")
	assert.Equal([]string{secondHeaderValue}, result.Header[secondHeaderName], "HTTP header is different")
}