FedP2P/mse/mse_test.go

280 lines
6.6 KiB
Go
Raw Permalink Normal View History

package mse
import (
"bytes"
"crypto/rand"
2017-09-12 22:58:54 +08:00
"crypto/rc4"
"io"
"io/ioutil"
"net"
"sync"
"testing"
2017-09-13 14:19:14 +08:00
_ "github.com/anacrolix/envpprof"
"github.com/stretchr/testify/assert"
2016-09-16 10:13:06 +08:00
"github.com/stretchr/testify/require"
)
2017-11-04 14:07:42 +08:00
func sliceIter(skeys [][]byte) SecretKeyIter {
return func(callback func([]byte) bool) {
for _, sk := range skeys {
if !callback(sk) {
break
}
}
}
}
func TestReadUntil(t *testing.T) {
test := func(data, until string, leftover int, expectedErr error) {
r := bytes.NewReader([]byte(data))
err := readUntil(r, []byte(until))
if err != expectedErr {
t.Fatal(err)
}
if r.Len() != leftover {
t.Fatal(r.Len())
}
}
test("feakjfeafeafegbaabc00", "abc", 2, nil)
test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
}
func TestSuffixMatchLen(t *testing.T) {
test := func(a, b string, expected int) {
actual := suffixMatchLen([]byte(a), []byte(b))
if actual != expected {
t.Fatalf("expected %d, got %d for %q and %q", expected, actual, a, b)
}
}
test("hello", "world", 0)
test("hello", "lo", 2)
test("hello", "llo", 3)
test("hello", "hell", 0)
test("hello", "helloooo!", 5)
test("hello", "lol!", 2)
test("hello", "mondo", 0)
test("mongo", "webscale", 0)
test("sup", "person", 1)
}
2018-02-16 07:36:29 +08:00
func handshakeTest(t testing.TB, ia []byte, aData, bData string, cryptoProvides CryptoMethod, cryptoSelect CryptoSelector) {
a, b := net.Pipe()
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
a, cm, err := InitiateHandshake(a, []byte("yep"), ia, cryptoProvides)
require.NoError(t, err)
assert.Equal(t, cryptoSelect(cryptoProvides), cm)
go a.Write([]byte(aData))
var msg [20]byte
n, _ := a.Read(msg[:])
if n != len(bData) {
t.FailNow()
}
// t.Log(string(msg[:n]))
}()
go func() {
defer wg.Done()
2021-01-04 07:49:28 +08:00
res := ReceiveHandshakeEx(b, sliceIter([][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")}), cryptoSelect)
require.NoError(t, res.error)
assert.EqualValues(t, "yep", res.SecretKey)
b := res.ReadWriter
assert.Equal(t, cryptoSelect(cryptoProvides), res.CryptoMethod)
go b.Write([]byte(bData))
2021-01-04 07:49:28 +08:00
// Need to be exact here, as there are several reads, and net.Pipe is most synchronous.
msg := make([]byte, len(ia)+len(aData))
n, _ := io.ReadFull(b, msg)
if n != len(msg) {
t.FailNow()
}
// t.Log(string(msg[:n]))
}()
wg.Wait()
a.Close()
b.Close()
}
2018-02-16 07:36:29 +08:00
func allHandshakeTests(t testing.TB, provides CryptoMethod, selector CryptoSelector) {
2017-09-13 14:19:14 +08:00
handshakeTest(t, []byte("jump the gun, "), "hello world", "yo dawg", provides, selector)
handshakeTest(t, nil, "hello world", "yo dawg", provides, selector)
handshakeTest(t, []byte{}, "hello world", "yo dawg", provides, selector)
}
2017-09-13 14:19:14 +08:00
func TestHandshakeDefault(t *testing.T) {
allHandshakeTests(t, AllSupportedCrypto, DefaultCryptoSelector)
t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
}
2017-09-13 14:19:14 +08:00
func TestHandshakeSelectPlaintext(t *testing.T) {
2018-02-16 07:36:29 +08:00
allHandshakeTests(t, AllSupportedCrypto, func(CryptoMethod) CryptoMethod { return CryptoMethodPlaintext })
2017-09-13 14:19:14 +08:00
}
func BenchmarkHandshakeDefault(b *testing.B) {
for i := 0; i < b.N; i += 1 {
2017-09-13 14:19:14 +08:00
allHandshakeTests(b, AllSupportedCrypto, DefaultCryptoSelector)
}
}
type trackReader struct {
r io.Reader
n int64
}
func (tr *trackReader) Read(b []byte) (n int, err error) {
n, err = tr.r.Read(b)
tr.n += int64(n)
return
}
func TestReceiveRandomData(t *testing.T) {
tr := trackReader{rand.Reader, 0}
_, _, err := ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil, DefaultCryptoSelector)
2016-09-16 10:13:06 +08:00
// No skey matches
require.Error(t, err)
// Establishing S, and then reading the maximum padding for giving up on
// synchronizing.
require.EqualValues(t, 96+532, tr.n)
}
2017-09-12 22:58:54 +08:00
2017-09-13 14:19:14 +08:00
func fillRand(t testing.TB, bs ...[]byte) {
for _, b := range bs {
_, err := rand.Read(b)
require.NoError(t, err)
}
}
2021-12-18 05:58:56 +08:00
func readAndWrite(rw io.ReadWriter, r, w []byte) error {
2017-09-13 14:19:14 +08:00
var wg sync.WaitGroup
wg.Add(1)
var wErr error
go func() {
defer wg.Done()
_, wErr = rw.Write(w)
}()
_, err := io.ReadFull(rw, r)
if err != nil {
return err
}
wg.Wait()
return wErr
}
2018-02-16 07:36:29 +08:00
func benchmarkStream(t *testing.B, crypto CryptoMethod) {
2017-09-13 14:19:14 +08:00
ia := make([]byte, 0x1000)
a := make([]byte, 1<<20)
b := make([]byte, 1<<20)
fillRand(t, ia, a, b)
t.StopTimer()
t.SetBytes(int64(len(ia) + len(a) + len(b)))
t.ResetTimer()
for i := 0; i < t.N; i += 1 {
2017-09-13 14:19:14 +08:00
ac, bc := net.Pipe()
ar := make([]byte, len(b))
br := make([]byte, len(ia)+len(a))
t.StartTimer()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer ac.Close()
defer wg.Done()
rw, _, err := InitiateHandshake(ac, []byte("cats"), ia, crypto)
2017-09-13 14:19:14 +08:00
require.NoError(t, err)
require.NoError(t, readAndWrite(rw, ar, a))
}()
func() {
defer bc.Close()
rw, _, err := ReceiveHandshake(bc, sliceIter([][]byte{[]byte("cats")}), func(CryptoMethod) CryptoMethod { return crypto })
2017-09-13 14:19:14 +08:00
require.NoError(t, err)
require.NoError(t, readAndWrite(rw, br, b))
}()
2019-10-01 16:11:44 +08:00
wg.Wait()
2017-09-13 14:19:14 +08:00
t.StopTimer()
if !bytes.Equal(ar, b) {
t.Fatalf("A read the wrong bytes")
}
if !bytes.Equal(br[:len(ia)], ia) {
t.Fatalf("B read the wrong IA")
}
if !bytes.Equal(br[len(ia):], a) {
t.Fatalf("B read the wrong A")
}
// require.Equal(t, b, ar)
// require.Equal(t, ia, br[:len(ia)])
// require.Equal(t, a, br[len(ia):])
}
}
func BenchmarkStreamRC4(t *testing.B) {
benchmarkStream(t, CryptoMethodRC4)
2017-09-13 14:19:14 +08:00
}
func BenchmarkStreamPlaintext(t *testing.B) {
benchmarkStream(t, CryptoMethodPlaintext)
2017-09-13 14:19:14 +08:00
}
func BenchmarkPipeRC4(t *testing.B) {
2017-09-12 22:58:54 +08:00
key := make([]byte, 20)
n, _ := rand.Read(key)
require.Equal(t, len(key), n)
var buf bytes.Buffer
c, err := rc4.NewCipher(key)
require.NoError(t, err)
r := cipherReader{
c: c,
r: &buf,
}
c, err = rc4.NewCipher(key)
require.NoError(t, err)
w := cipherWriter{
c: c,
w: &buf,
}
a := make([]byte, 0x1000)
n, _ = io.ReadFull(rand.Reader, a)
require.Equal(t, len(a), n)
b := make([]byte, len(a))
t.SetBytes(int64(len(a)))
t.ResetTimer()
for i := 0; i < t.N; i += 1 {
2017-09-12 22:58:54 +08:00
n, _ = w.Write(a)
if n != len(a) {
t.FailNow()
}
n, _ = r.Read(b)
if n != len(b) {
t.FailNow()
}
if !bytes.Equal(a, b) {
t.FailNow()
}
}
}
func BenchmarkSkeysReceive(b *testing.B) {
var skeys [][]byte
for i := 0; i < 100000; i += 1 {
skeys = append(skeys, make([]byte, 20))
}
fillRand(b, skeys...)
initSkey := skeys[len(skeys)/2]
2021-11-08 11:47:01 +08:00
// c := qt.New(b)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += 1 {
initiator, receiver := net.Pipe()
go func() {
_, _, err := InitiateHandshake(initiator, initSkey, nil, AllSupportedCrypto)
if err != nil {
panic(err)
}
}()
res := ReceiveHandshakeEx(receiver, sliceIter(skeys), DefaultCryptoSelector)
if res.error != nil {
panic(res.error)
}
}
}