mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 18:17:52 -05:00
support aggressive mode in auth reader
This commit is contained in:
parent
417284ed99
commit
201d6e6471
@ -158,8 +158,14 @@ func (v *AuthenticationReader) Read(b []byte) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nBytes := v.CopyChunk(b)
|
totalBytes := v.CopyChunk(b)
|
||||||
return nBytes, nil
|
for v.aggressive {
|
||||||
|
if err := v.NextChunk(); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
totalBytes += v.CopyChunk(b[totalBytes:])
|
||||||
|
}
|
||||||
|
return totalBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthenticationWriter struct {
|
type AuthenticationWriter struct {
|
||||||
|
@ -57,7 +57,60 @@ func TestAuthenticationReaderWriter(t *testing.T) {
|
|||||||
nBytes, err = reader.Read(actualPayload)
|
nBytes, err = reader.Read(actualPayload)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Int(nBytes).Equals(len(payload))
|
assert.Int(nBytes).Equals(len(payload))
|
||||||
//assert.Bytes(actualPayload[:nBytes]).Equals(payload)
|
assert.Bytes(actualPayload[:nBytes]).Equals(payload)
|
||||||
|
|
||||||
|
_, err = reader.Read(actualPayload)
|
||||||
|
assert.Error(err).Equals(io.EOF)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthenticationReaderWriterAggressive(t *testing.T) {
|
||||||
|
assert := assert.On(t)
|
||||||
|
|
||||||
|
key := make([]byte, 16)
|
||||||
|
rand.Read(key)
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
aead, err := cipher.NewGCM(block)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
payload := make([]byte, 7*1024)
|
||||||
|
rand.Read(payload)
|
||||||
|
|
||||||
|
cache := buf.NewLocal(16 * 1024)
|
||||||
|
iv := make([]byte, 12)
|
||||||
|
rand.Read(iv)
|
||||||
|
|
||||||
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
||||||
|
AEAD: aead,
|
||||||
|
NonceGenerator: &StaticBytesGenerator{
|
||||||
|
Content: iv,
|
||||||
|
},
|
||||||
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
||||||
|
}, cache)
|
||||||
|
|
||||||
|
nBytes, err := writer.Write(payload)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
assert.Int(nBytes).Equals(len(payload))
|
||||||
|
assert.Int(cache.Len()).GreaterThan(0)
|
||||||
|
_, err = writer.Write(payload)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
assert.Int(nBytes).Equals(len(payload))
|
||||||
|
_, err = writer.Write([]byte{})
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
||||||
|
AEAD: aead,
|
||||||
|
NonceGenerator: &StaticBytesGenerator{
|
||||||
|
Content: iv,
|
||||||
|
},
|
||||||
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
||||||
|
}, cache, true)
|
||||||
|
|
||||||
|
actualPayload := make([]byte, 16*1024)
|
||||||
|
nBytes, err = reader.Read(actualPayload)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
assert.Int(nBytes).Equals(len(payload) * 2)
|
||||||
|
|
||||||
_, err = reader.Read(actualPayload)
|
_, err = reader.Read(actualPayload)
|
||||||
assert.Error(err).Equals(io.EOF)
|
assert.Error(err).Equals(io.EOF)
|
||||||
|
Loading…
Reference in New Issue
Block a user