<commit_before>package fortuna

import (
	"fmt"
	"io"
	"os"
	"testing"
)

func TestNilRNG(t *testing.T) {
	var rng *Fortuna
	if rng.Initialised() {
		fmt.Fprintf(os.Stderr, "fortuna: PRNG should not have reported it was initialised\n")
		t.FailNow()
	}
}

func TestNotSeeded(t *testing.T) {
	var p []byte
	rng := New(nil)
	if _, err := rng.Read(p); err == nil {
		fmt.Fprintf(os.Stderr, "fortuna: PRNG should report it is not seeded")
		t.FailNow()
	}
}

func TestPRNGEmptyRead(t *testing.T) {
	var p []byte
	rng := New(nil)
	rng.reseed()

	n, err := rng.Read(p)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	} else if n != 0 {
		fmt.Fprintf(os.Stderr, "fortuna: no data should have been read\n")
		t.FailNow()
	}
}

func TestInvalidEvents(t *testing.T) {
	var p = make([]byte, 1)
	rng := New(nil)
	err := rng.AddRandomEvent(0, 33, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	err = rng.AddRandomEvent(0, -1, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	p = nil
	err = rng.AddRandomEvent(0, 0, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	p = make([]byte, MaxEventSize+1)
	err = rng.AddRandomEvent(0, 0, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}
}

func TestSeed(t *testing.T) {
	rng := New(nil)
	sw := NewSourceWriter(rng, 0)

	f, err := os.Open("/dev/zero")
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	}

	io.CopyN(sw, f, 4096)
	seed, err := rng.Seed()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	} else if len(seed) != SeedFileLength {
		fmt.Fprintf(os.Stderr, "fortuna: bad seed file length\n")
		t.FailNow()
	}
<commit_msg>Add more seed tests.<commit_after>package fortuna

import (
	"fmt"
	"io"
	"os"
	"testing"
)

func TestNilRNG(t *testing.T) {
	var rng *Fortuna
	if rng.Initialised() {
		fmt.Fprintf(os.Stderr, "fortuna: PRNG should not have reported it was initialised\n")
		t.FailNow()
	}
}

func TestNotSeeded(t *testing.T) {
	var p []byte
	rng := New(nil)
	if _, err := rng.Read(p); err == nil {
		fmt.Fprintf(os.Stderr, "fortuna: PRNG should report it is not seeded")
		t.FailNow()
	}
}

func TestPRNGEmptyRead(t *testing.T) {
	var p []byte
	rng := New(nil)
	rng.reseed()

	n, err := rng.Read(p)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	} else if n != 0 {
		fmt.Fprintf(os.Stderr, "fortuna: no data should have been read\n")
		t.FailNow()
	}
}

func TestInvalidEvents(t *testing.T) {
	var p = make([]byte, 1)
	rng := New(nil)
	err := rng.AddRandomEvent(0, 33, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	err = rng.AddRandomEvent(0, -1, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	p = nil
	err = rng.AddRandomEvent(0, 0, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}

	p = make([]byte, MaxEventSize+1)
	err = rng.AddRandomEvent(0, 0, p)
	if err != ErrInvalidEvent {
		fmt.Fprintf(os.Stderr, "fortuna: random event should be invalid\n")
		t.FailNow()
	}
}

var seed []byte

func TestSeed(t *testing.T) {
	rng := New(nil)
	sw := NewSourceWriter(rng, 0)

	_, err := rng.Seed()
	if err != ErrNotSeeded {
		fmt.Fprintf(os.Stderr, "fortuna: PRNG seed() should fail for unseeded PRNG")
		t.FailNow()
	}

	f, err := os.Open("/dev/zero")
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	}

	io.CopyN(sw, f, 4096)
	seed, err = rng.Seed()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	} else if len(seed) != SeedFileLength {
		fmt.Fprintf(os.Stderr, "fortuna: bad seed file length\n")
		t.FailNow()
	}
}

func TestReadSeed(t *testing.T) {
	rng := New(nil)
	err := rng.ReadSeed(seed)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		t.FailNow()
	}

	seed = nil
	err = rng.ReadSeed(seed)
	if err == nil {
		fmt.Fprintf(os.Stderr, "fortuna: ReadSeed should fail\n")
		t.FailNow()
	}

	seed = make([]byte, SeedFileLength-1)
	if err = rng.ReadSeed(seed); err == nil {
		fmt.Fprintf(os.Stderr, "fortuna: ReadSeed should fail\n")
		t.FailNow()
	}
{'fraction_non_alphanumeric': 0.17106747912435116, 'fraction_numerical': 0.007898894154818325, 'mean_word_length': 3.5270684371807968, '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': '2444206', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1920, 'n_words': 456}