<commit_before>package main

import (
	"flag"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"testing"
	"time"
)

func init() {
	// Discard default log output
	log.SetOutput(ioutil.Discard)
}

func TestRateValidation(t *testing.T) {
	rate, duration, targetsf, ordering, output, header := defaultArguments()
	rate = 0

	err := attack(rate, duration, targetsf, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errRatePrefix)) {
		t.Errorf("Rate 0 shouldn't be valid: %s", err)
	}
}

func TestDurationValidation(t *testing.T) {
	rate, duration, targetsf, ordering, output, header := defaultArguments()
	duration = 0

	err := attack(rate, duration, targetsf, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errDurationPrefix)) {
		t.Errorf("Duration 0 shouldn't be valid: %s", err)
	}
}

func TestTargetsValidation(t *testing.T) {
	rate, duration, goodFile, ordering, output, header := defaultArguments()

	// Good case
	err := attack(rate, duration, goodFile, ordering, output, header)
	if err != nil {
		t.Errorf("Targets file `%s` should be valid: %s", goodFile, err)
	}

	// Bad case
	badFile := "randomInexistingFile12345.txt"
	err = attack(rate, duration, badFile, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errTargetsFilePrefix)) {
		t.Errorf("Targets file `%s` shouldn't be valid: %s", badFile, err)
	}
}

func TestOrderingValidation(t *testing.T) {
	rate, duration, targetsf, _, output, header := defaultArguments()

	// Good cases
	for _, ordering := range []string{"random", "sequential"} {
		err := attack(rate, duration, targetsf, ordering, output, header)
		if err != nil {
			t.Errorf("Ordering `%s` should be valid: %s", ordering, err)
		}
	}

	// Bad case
	badOrdering := "lolcat"
	err := attack(rate, duration, targetsf, badOrdering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errOrderingPrefix)) {
		t.Errorf("Ordering `%s` shouldn't be valid: %s", badOrdering, err)
	}
}

func TestHeadersParsing(t *testing.T) {
	fs := flag.NewFlagSet("test", flag.ContinueOnError)
	fs.SetOutput(ioutil.Discard)
	hdrs := headers{Header: make(http.Header)}
	fs.Var(hdrs, "H", "Header")
	// Good case
	good := []string{"-H", "Host: lolcathost"}
	if err := fs.Parse(good); err != nil {
		t.Errorf("%v should be a valid header", good[1])
	}
	// Bad cases
	bad := [][]string{[]string{"-H", "Host:"}, []string{"-H", "Host"}}
	for _, args := range bad {
		if err := fs.Parse(args); err == nil {
			t.Errorf("%v should not be a valid header", args[1])
		}
	}
}

func defaultArguments() (uint64, time.Duration, string, string, string, http.Header) {
	return uint64(1000),
		5 * time.Millisecond,
		".targets.txt",
		"random",
		"/dev/null",
		http.Header{}
}<commit_msg>Improving cross-platform compatibility a bit<commit_after>package main

import (
	"flag"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"testing"
	"time"
	"os"
)

func init() {
	// Discard default log output
	log.SetOutput(ioutil.Discard)
}

func TestRateValidation(t *testing.T) {
	rate, duration, targetsf, ordering, output, header := defaultArguments()
	rate = 0

	err := attack(rate, duration, targetsf, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errRatePrefix)) {
		t.Errorf("Rate 0 shouldn't be valid: %s", err)
	}
}

func TestDurationValidation(t *testing.T) {
	rate, duration, targetsf, ordering, output, header := defaultArguments()
	duration = 0

	err := attack(rate, duration, targetsf, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errDurationPrefix)) {
		t.Errorf("Duration 0 shouldn't be valid: %s", err)
	}
}

func TestTargetsValidation(t *testing.T) {
	rate, duration, goodFile, ordering, output, header := defaultArguments()

	// Good case
	err := attack(rate, duration, goodFile, ordering, output, header)
	if err != nil {
		t.Errorf("Targets file `%s` should be valid: %s", goodFile, err)
	}

	// Bad case
	badFile := "randomInexistingFile12345.txt"
	err = attack(rate, duration, badFile, ordering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errTargetsFilePrefix)) {
		t.Errorf("Targets file `%s` shouldn't be valid: %s", badFile, err)
	}
}

func TestOrderingValidation(t *testing.T) {
	rate, duration, targetsf, _, output, header := defaultArguments()

	// Good cases
	for _, ordering := range []string{"random", "sequential"} {
		err := attack(rate, duration, targetsf, ordering, output, header)
		if err != nil {
			t.Errorf("Ordering `%s` should be valid: %s", ordering, err)
		}
	}

	// Bad case
	badOrdering := "lolcat"
	err := attack(rate, duration, targetsf, badOrdering, output, header)
	if err == nil || (err != nil && !strings.HasPrefix(err.Error(), errOrderingPrefix)) {
		t.Errorf("Ordering `%s` shouldn't be valid: %s", badOrdering, err)
	}
}

func TestHeadersParsing(t *testing.T) {
	fs := flag.NewFlagSet("test", flag.ContinueOnError)
	fs.SetOutput(ioutil.Discard)
	hdrs := headers{Header: make(http.Header)}
	fs.Var(hdrs, "H", "Header")
	// Good case
	good := []string{"-H", "Host: lolcathost"}
	if err := fs.Parse(good); err != nil {
		t.Errorf("%v should be a valid header", good[1])
	}
	// Bad cases
	bad := [][]string{[]string{"-H", "Host:"}, []string{"-H", "Host"}}
	for _, args := range bad {
		if err := fs.Parse(args); err == nil {
			t.Errorf("%v should not be a valid header", args[1])
		}
	}
}

func defaultArguments() (uint64, time.Duration, string, string, string, http.Header) {
	return uint64(1000),
		5 * time.Millisecond,
		".targets.txt",
		"random",
		os.DevNull,
		http.Header{}
}
{'fraction_non_alphanumeric': 0.1735769501054111, 'fraction_numerical': 0.007027406886858749, 'mean_word_length': 4.41674595623216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1402203', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1975, 'n_words': 549}