1
0
Fork 0

fix: pad function calculating minQuestionSize (#25)

fix problem with calculating closest divisible by 64
closes #24
This commit is contained in:
Mohammad Zolfaghari 2024-03-15 15:45:20 +03:30 committed by Sam Therapy
parent 75da7c00a0
commit f28a1a337a
Signed by: sam
GPG Key ID: 4D8B07C18F31ACBD
2 changed files with 32 additions and 1 deletions

View File

@ -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
View 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)
}
}