Refactor session_test.go

This commit is contained in:
Andy Wang 2020-12-21 20:38:28 +00:00
parent de0daac123
commit c9ac93b0b9
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
1 changed files with 174 additions and 167 deletions

View File

@ -12,10 +12,9 @@ import (
"time" "time"
) )
var seshConfigOrdered = SessionConfig{} var seshConfigs = map[string]SessionConfig{
"ordered": {},
var seshConfigUnordered = SessionConfig{ "unordered": {Unordered: true},
Unordered: true,
} }
const testPayloadLen = 1024 const testPayloadLen = 1024
@ -43,40 +42,20 @@ func TestRecvDataFromRemote(t *testing.T) {
return ret return ret
} }
sessionTypes := []struct { encryptionMethods := map[string]Obfuscator{
name string "plain": MakeObfuscatorUnwrap(EncryptionMethodPlain, sessionKey),
config SessionConfig "aes-gcm": MakeObfuscatorUnwrap(EncryptionMethodAESGCM, sessionKey),
}{ "chacha20-poly1305": MakeObfuscatorUnwrap(EncryptionMethodChaha20Poly1305, sessionKey),
{"ordered",
SessionConfig{}},
{"unordered",
SessionConfig{Unordered: true}},
} }
encryptionMethods := []struct { for seshType, seshConfig := range seshConfigs {
name string seshConfig := seshConfig
obfuscator Obfuscator t.Run(seshType, func(t *testing.T) {
}{ for method, obfuscator := range encryptionMethods {
{ obfuscator := obfuscator
"plain", t.Run(method, func(t *testing.T) {
MakeObfuscatorUnwrap(EncryptionMethodPlain, sessionKey), seshConfig.Obfuscator = obfuscator
}, sesh := MakeSession(0, seshConfig)
{
"aes-gcm",
MakeObfuscatorUnwrap(EncryptionMethodAESGCM, sessionKey),
},
{
"chacha20-poly1305",
MakeObfuscatorUnwrap(EncryptionMethodChaha20Poly1305, sessionKey),
},
}
for _, st := range sessionTypes {
t.Run(st.name, func(t *testing.T) {
for _, em := range encryptionMethods {
t.Run(em.name, func(t *testing.T) {
st.config.Obfuscator = em.obfuscator
sesh := MakeSession(0, st.config)
n, err := sesh.Obfs(f, obfsBuf, 0) n, err := sesh.Obfs(f, obfsBuf, 0)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -116,8 +95,10 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) seshConfig := seshConfigs["ordered"]
seshConfig.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfig)
f1 := &Frame{ f1 := &Frame{
1, 1,
@ -245,8 +226,10 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) seshConfig := seshConfigs["ordered"]
seshConfig.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfig)
// receive stream 1 closing first // receive stream 1 closing first
f1CloseStream := &Frame{ f1CloseStream := &Frame{
@ -300,8 +283,12 @@ func TestParallelStreams(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) for seshType, seshConfig := range seshConfigs {
seshConfig := seshConfig
t.Run(seshType, func(t *testing.T) {
seshConfig.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfig)
numStreams := acceptBacklog numStreams := acceptBacklog
seqs := make([]*uint64, numStreams) seqs := make([]*uint64, numStreams)
@ -356,15 +343,17 @@ func TestParallelStreams(t *testing.T) {
if sc != count { if sc != count {
t.Errorf("broken referential integrety: actual %v, reference count: %v", count, sc) t.Errorf("broken referential integrety: actual %v, reference count: %v", count, sc)
} }
})
}
} }
func TestStream_SetReadDeadline(t *testing.T) { func TestStream_SetReadDeadline(t *testing.T) {
var sessionKey [32]byte for seshType, seshConfig := range seshConfigs {
rand.Read(sessionKey[:]) seshConfig := seshConfig
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) t.Run(seshType, func(t *testing.T) {
seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfig)
sesh.AddConnection(connutil.Discard())
testReadDeadline := func(sesh *Session) {
t.Run("read after deadline set", func(t *testing.T) { t.Run("read after deadline set", func(t *testing.T) {
stream, _ := sesh.OpenStream() stream, _ := sesh.OpenStream()
_ = stream.SetReadDeadline(time.Now().Add(-1 * time.Second)) _ = stream.SetReadDeadline(time.Now().Add(-1 * time.Second))
@ -392,27 +381,27 @@ func TestStream_SetReadDeadline(t *testing.T) {
t.Error("Read did not unblock after deadline has passed") t.Error("Read did not unblock after deadline has passed")
} }
}) })
})
} }
sesh := MakeSession(0, seshConfigOrdered)
sesh.AddConnection(connutil.Discard())
testReadDeadline(sesh)
sesh = MakeSession(0, seshConfigUnordered)
sesh.AddConnection(connutil.Discard())
testReadDeadline(sesh)
} }
func TestSession_timeoutAfter(t *testing.T) { func TestSession_timeoutAfter(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator
seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond for seshType, seshConfig := range seshConfigs {
sesh := MakeSession(0, seshConfigOrdered) seshConfig := seshConfig
t.Run(seshType, func(t *testing.T) {
seshConfig.Obfuscator = obfuscator
seshConfig.InactivityTimeout = 100 * time.Millisecond
sesh := MakeSession(0, seshConfig)
assert.Eventually(t, func() bool { assert.Eventually(t, func() bool {
return sesh.IsClosed() return sesh.IsClosed()
}, 5*seshConfigOrdered.InactivityTimeout, seshConfigOrdered.InactivityTimeout, "session should have timed out") }, 5*seshConfig.InactivityTimeout, seshConfig.InactivityTimeout, "session should have timed out")
})
}
} }
func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
@ -429,38 +418,20 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
b.Run("plain", func(b *testing.B) { table := map[string]byte{
obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) "plain": EncryptionMethodPlain,
seshConfigOrdered.Obfuscator = obfuscator "aes-gcm": EncryptionMethodAESGCM,
sesh := MakeSession(0, seshConfigOrdered) "chacha20poly1305": EncryptionMethodChaha20Poly1305,
n, _ := sesh.Obfs(f, obfsBuf, 0)
b.SetBytes(int64(len(f.Payload)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
sesh.recvDataFromRemote(obfsBuf[:n])
} }
})
b.Run("aes-gcm", func(b *testing.B) { for name, ep := range table {
obfuscator, _ := MakeObfuscator(EncryptionMethodAESGCM, sessionKey) seshConfig := seshConfigs["ordered"]
seshConfigOrdered.Obfuscator = obfuscator obfuscator, _ := MakeObfuscator(ep, sessionKey)
sesh := MakeSession(0, seshConfigOrdered) seshConfig.Obfuscator = obfuscator
n, _ := sesh.Obfs(f, obfsBuf, 0) sesh := MakeSession(0, seshConfig)
b.SetBytes(int64(len(f.Payload)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
sesh.recvDataFromRemote(obfsBuf[:n])
}
})
b.Run("chacha20-poly1305", func(b *testing.B) {
obfuscator, _ := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
b.Run(name, func(b *testing.B) {
b.SetBytes(int64(len(f.Payload))) b.SetBytes(int64(len(f.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -468,3 +439,39 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
} }
}) })
} }
}
func BenchmarkMultiStreamWrite(b *testing.B) {
var sessionKey [32]byte
rand.Read(sessionKey[:])
table := map[string]byte{
"plain": EncryptionMethodPlain,
"aes-gcm": EncryptionMethodAESGCM,
"chacha20poly1305": EncryptionMethodChaha20Poly1305,
}
testPayload := make([]byte, testPayloadLen)
for name, ep := range table {
b.Run(name, func(b *testing.B) {
for seshType, seshConfig := range seshConfigs {
seshConfig := seshConfig
b.Run(seshType, func(b *testing.B) {
obfuscator, _ := MakeObfuscator(ep, sessionKey)
seshConfig.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfig)
sesh.AddConnection(connutil.Discard())
b.ResetTimer()
b.SetBytes(testPayloadLen)
b.RunParallel(func(pb *testing.PB) {
stream, _ := sesh.OpenStream()
for pb.Next() {
stream.Write(testPayload)
}
})
})
}
})
}
}