package hashrng

import (
	_ "crypto/sha256"
	"fmt"
	"testing"
	"time"

	"xdx.jelly/xgcl/grand/drng/entropy"
	"xdx.jelly/xgcl/grand/drng/internal"
	"xdx.jelly/xgcl/sm"
)

func TestSysEntropySource(t *testing.T) {
	e := entropy.GetEntropyPool()
	b, err := e.GetEntropy(128, 128, 128)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("%x\n", b)
}

func TestSm3Df(t *testing.T) {
	input := make([]byte, 10)
	for n := 0; n < 100; n++ {
		output := newDF(sm.SM3)(input, n)
		if len(output) != n {
			t.Fatal()
		}
	}
}

// 0.03GBps, 主要是internal.Outlen太小
func TestDrngSpeed(t *testing.T) {
	raw, _ := NewHashDrng(sm.SM3, Config(internal.SecureLevel2))
	rng, _ := raw.Instantiate(nil, internal.Nonce(), entropy.GetEntropyPool())
	msg := make([]byte, rng.OutLen())

	start := time.Now()
	times := 10000
	buf := make([]byte, 32)
	for i := 0; i < times; i++ {
		if err := rng.Generate(msg, nil); err == internal.ErrNeedReseed {
			entropy.GetEntropyPool().Read(buf)
			rng.Reseed(buf, nil)
		} else if err != nil {
			t.Fatal(err)
		}
	}

	end := time.Now()
	elapsed := end.Sub(start)
	t.Logf("Generate Random speed: %.2f MBps\n", float64(times*len(msg))/float64(1024*1024)/elapsed.Seconds())
}

// func TestSm3DrngRandomness(t *testing.T) {
// 	// 测试Sm3Drng的随机性测试通过率
// 	total := 10
// 	pass := 0
// 	for i := 0; i < total; i++ {
// 		fmt.Println("======================================", i)
// 		if statistics.SampleSelfTests(Reader) {
// 			fmt.Printf("%d-th test: [O]\n", i)
// 			pass += 1
// 		} else {
// 			fmt.Printf("%d-th test: [X]\n", i)
// 		}
// 	}

// 	fmt.Printf("Pass Rate: %d/%d\n", pass, total)
// }
