Skip to content

Commit

Permalink
fix: pad function calculating minQuestionSize (#25)
Browse files Browse the repository at this point in the history
fix problem with calculating closest divisible by 64
closes #24
  • Loading branch information
zoli authored Mar 15, 2024
1 parent d84a81e commit 0554874
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 0554874

Please sign in to comment.