mirror of
https://github.com/SamTherapy/dnscrypt.git
synced 2024-12-03 17:39:02 +00:00
fix: pad function calculating minQuestionSize (#25)
fix problem with calculating closest divisible by 64 closes #24
This commit is contained in:
parent
75da7c00a0
commit
f28a1a337a
2 changed files with 32 additions and 1 deletions
2
util.go
2
util.go
|
@ -42,7 +42,7 @@ import (
|
|||
// <56-bytes-query> 0x80 (0x00 * 199)
|
||||
func pad(packet []byte) []byte {
|
||||
// get closest divisible by 64 to <packet-len> + 1 byte for 0x80
|
||||
minQuestionSize := (len(packet)+1+63)/64 + 64
|
||||
minQuestionSize := len(packet) + 1 + (64 - (len(packet)+1)%64)
|
||||
|
||||
// padded size can't be less than minUDPQuestionSize
|
||||
minQuestionSize = max(minUDPQuestionSize, minQuestionSize)
|
||||
|
|
31
util_test.go
Normal file
31
util_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package dnscrypt
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPadUnpad(t *testing.T) {
|
||||
longBuf := make([]byte, 272)
|
||||
_, err := rand.Read(longBuf)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
packet []byte
|
||||
expPaddedLen int
|
||||
}{
|
||||
{[]byte("Example Test DNS packet"), 256},
|
||||
{longBuf, 320},
|
||||
}
|
||||
for i, test := range tests {
|
||||
padded := pad(test.packet)
|
||||
assert.Equal(t, test.expPaddedLen, len(padded), "test %d", i)
|
||||
|
||||
unpadded, err := unpad(padded)
|
||||
assert.Nil(t, err, "test %d", i)
|
||||
assert.Equal(t, test.packet, unpadded, "test %d", i)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue