2015-03-12 09:03:29 +00:00
|
|
|
package mse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-18 07:16:27 +00:00
|
|
|
"crypto/rand"
|
2017-09-12 14:58:54 +00:00
|
|
|
"crypto/rc4"
|
2015-03-12 09:03:29 +00:00
|
|
|
"io"
|
2015-03-18 07:16:27 +00:00
|
|
|
"io/ioutil"
|
2015-03-12 09:03:29 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
2015-03-26 06:18:08 +00:00
|
|
|
"testing"
|
2015-03-12 09:03:29 +00:00
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
|
|
|
|
2015-03-13 03:30:48 +00:00
|
|
|
"github.com/bradfitz/iter"
|
2016-09-16 02:13:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-03-12 09:03:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
func handshakeTest(t testing.TB, ia []byte, aData, bData string, cryptoProvides uint32, cryptoSelect func(uint32) uint32) {
|
2015-03-12 09:03:29 +00:00
|
|
|
a, b := net.Pipe()
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2017-09-13 06:19:14 +00:00
|
|
|
a, err := InitiateHandshake(a, []byte("yep"), ia, cryptoProvides)
|
2015-03-12 09:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-13 03:30:48 +00:00
|
|
|
go a.Write([]byte(aData))
|
|
|
|
|
2015-03-12 09:29:48 +00:00
|
|
|
var msg [20]byte
|
|
|
|
n, _ := a.Read(msg[:])
|
2015-03-13 03:30:48 +00:00
|
|
|
if n != len(bData) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// t.Log(string(msg[:n]))
|
2015-03-12 09:03:29 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2017-09-13 06:19:14 +00:00
|
|
|
b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")}, cryptoSelect)
|
2015-03-12 09:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-13 03:30:48 +00:00
|
|
|
go b.Write([]byte(bData))
|
|
|
|
// 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]))
|
2015-03-12 09:03:29 +00:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
2015-03-13 03:30:48 +00:00
|
|
|
a.Close()
|
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
func allHandshakeTests(t testing.TB, provides uint32, selector CryptoSelector) {
|
|
|
|
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)
|
2015-03-13 03:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
func TestHandshakeDefault(t *testing.T) {
|
|
|
|
allHandshakeTests(t, AllSupportedCrypto, DefaultCryptoSelector)
|
2015-03-13 03:30:48 +00:00
|
|
|
t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
func TestHandshakeSelectPlaintext(t *testing.T) {
|
|
|
|
allHandshakeTests(t, AllSupportedCrypto, func(uint32) uint32 { return cryptoMethodPlaintext })
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHandshakeDefault(b *testing.B) {
|
2015-03-13 03:30:48 +00:00
|
|
|
for range iter.N(b.N) {
|
2017-09-13 06:19:14 +00:00
|
|
|
allHandshakeTests(b, AllSupportedCrypto, DefaultCryptoSelector)
|
2015-03-13 03:30:48 +00:00
|
|
|
}
|
2015-03-12 09:03:29 +00:00
|
|
|
}
|
2015-03-18 07:16:27 +00:00
|
|
|
|
|
|
|
type trackReader struct {
|
|
|
|
r io.Reader
|
|
|
|
n int64
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (tr *trackReader) Read(b []byte) (n int, err error) {
|
|
|
|
n, err = tr.r.Read(b)
|
|
|
|
tr.n += int64(n)
|
2015-03-18 07:16:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReceiveRandomData(t *testing.T) {
|
|
|
|
tr := trackReader{rand.Reader, 0}
|
2017-09-13 06:19:14 +00:00
|
|
|
_, err := ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil, DefaultCryptoSelector)
|
2016-09-16 02:13:06 +00: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)
|
2015-03-18 07:16:27 +00:00
|
|
|
}
|
2017-09-12 14:58:54 +00:00
|
|
|
|
2017-09-13 06:19:14 +00:00
|
|
|
func fillRand(t testing.TB, bs ...[]byte) {
|
|
|
|
for _, b := range bs {
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAndWrite(rw io.ReadWriter, r []byte, w []byte) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkStream(t *testing.B, crypto uint32) {
|
|
|
|
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 range iter.N(t.N) {
|
|
|
|
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)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, readAndWrite(rw, ar, a))
|
|
|
|
}()
|
|
|
|
func() {
|
|
|
|
defer bc.Close()
|
|
|
|
rw, err := ReceiveHandshake(bc, [][]byte{[]byte("cats")}, func(uint32) uint32 { return crypto })
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, readAndWrite(rw, br, b))
|
|
|
|
}()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStreamPlaintext(t *testing.B) {
|
|
|
|
benchmarkStream(t, cryptoMethodPlaintext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPipeRC4(t *testing.B) {
|
2017-09-12 14:58:54 +00: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 range iter.N(t.N) {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|