Skip to content

Commit

Permalink
test(fileid): add decode-encode-decode fuzzer, some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Nov 26, 2021
1 parent c6aaca2 commit 17bb425
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 5 deletions.
1 change: 0 additions & 1 deletion fileid/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ func EncodeFileID(id FileID) (string, error) {
func base64Encode(s []byte) string {
return base64.RawURLEncoding.EncodeToString(s)
}

47 changes: 47 additions & 0 deletions fileid/file_id_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build go1.18
// +build go1.18

package fileid

import (
"testing"

"github.com/k0kubun/pp/v3"
"github.com/stretchr/testify/require"

"github.com/gotd/td/bin"
)

func FuzzDecodeEncodeDecode(f *testing.F) {
for name, input := range testData {
data, err := base64Decode(input)
if err != nil {
f.Fatal(name, err)
}
data = rleDecode(data)
f.Add(data)
}

f.Fuzz(func(t *testing.T, data []byte) {
a := require.New(t)
input := bin.Buffer{Buf: data}

var fileID FileID
if err := fileID.decodeLatestFileID(&input); err != nil {
t.Skip(err)
}
if data[len(data)-1] < 32 {
t.Skip("Legacy file_id encoding is not supported")
}
if fileID.PhotoSizeSource.Type >= PhotoSizeSourceStickerSetThumbnail {
t.Log(pp.Sprint(input))
}
input.Reset()

a.NoError(fileID.encodeLatestFileID(&input))

var decoded FileID
a.NoError(decoded.decodeLatestFileID(&input))
a.Equal(fileID, decoded)
})
}
151 changes: 151 additions & 0 deletions fileid/file_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/gotd/td/constant"
"github.com/gotd/td/tg"
)

Expand Down Expand Up @@ -122,6 +123,137 @@ func TestFileID_AsInputFileLocation(t *testing.T) {
},
true,
},
{
"Secure",
FileID{
Type: Secure,
ID: 10,
AccessHash: 10,
},
&tg.InputSecureFileLocation{
ID: 10,
AccessHash: 10,
},
true,
},
{
"Encrypted",
FileID{
Type: Encrypted,
ID: 10,
AccessHash: 10,
},
&tg.InputEncryptedFileLocation{
ID: 10,
AccessHash: 10,
},
true,
},
{
"PhotoSizeSourceFullLegacy",
FileID{
Type: Photo,
ID: 10,
AccessHash: 11,
FileReference: []byte{12},
PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceFullLegacy,
VolumeID: 13,
LocalID: 14,
Secret: 15,
},
},
&tg.InputPhotoLegacyFileLocation{
ID: 10,
AccessHash: 11,
FileReference: []byte{12},
VolumeID: 13,
LocalID: 14,
Secret: 15,
},
true,
},
{
name: "PhotoSizeSourceDialogPhotoBigLegacy",
fileID: FileID{
Type: ProfilePhoto,
PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceDialogPhotoBigLegacy,
VolumeID: 13,
LocalID: 14,
DialogID: constant.MaxUserID - 1,
DialogAccessHash: 15,
},
},
want: &tg.InputPeerPhotoFileLocationLegacy{
Big: true,
Peer: &tg.InputPeerUser{
UserID: constant.MaxUserID - 1,
AccessHash: 15,
},
VolumeID: 13,
LocalID: 14,
},
wantOk: true,
},
{
name: "PhotoSizeSourceStickerSetThumbnailLegacy",
fileID: FileID{
Type: Thumbnail,
PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceStickerSetThumbnailLegacy,
VolumeID: 10,
LocalID: 11,
StickerSetID: 12,
StickerSetAccessHash: 13,
},
},
want: &tg.InputStickerSetThumbLegacy{
Stickerset: &tg.InputStickerSetID{
ID: 12,
AccessHash: 13,
},
VolumeID: 10,
LocalID: 11,
},
wantOk: true,
},
{
name: "PhotoSizeSourceStickerSetThumbnailLegacy",
fileID: FileID{
Type: Thumbnail,
PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceStickerSetThumbnailVersion,
StickerSetID: 12,
StickerSetAccessHash: 13,
StickerVersion: 1,
},
},
want: &tg.InputStickerSetThumb{
Stickerset: &tg.InputStickerSetID{
ID: 12,
AccessHash: 13,
},
ThumbVersion: 1,
},
wantOk: true,
},
{
"PhotoSizeSourceLegacy",
FileID{Type: Photo, PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceLegacy,
}},
nil,
false,
},
{
"PhotoSizeSourceStickerSetThumbnail",
FileID{Type: Thumbnail, PhotoSizeSource: PhotoSizeSource{
Type: PhotoSizeSourceStickerSetThumbnail,
}},
nil,
false,
},
{
"Temp",
FileID{Type: Temp},
Expand All @@ -138,3 +270,22 @@ func TestFileID_AsInputFileLocation(t *testing.T) {
})
}
}

func TestFileID_AsInputWebFileLocation(t *testing.T) {
a := require.New(t)
fileID := FileID{
AccessHash: 10,
}

loc, ok := fileID.AsInputWebFileLocation()
a.False(ok)
a.Nil(loc)

fileID.URL = "a"
loc, ok = fileID.AsInputWebFileLocation()
a.True(ok)
a.Equal(&tg.InputWebFileLocation{
URL: "a",
AccessHash: 10,
}, loc)
}
8 changes: 4 additions & 4 deletions fileid/photo_size_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,22 @@ func (p *PhotoSizeSource) decode(b *bin.Buffer, subVersion byte) error {
return nil
}

func (p *PhotoSizeSource) writeLocalIDVolumeID(b *bin.Buffer) {
func (p *PhotoSizeSource) writeLocalIDVolumeID(b *bin.Buffer) {
b.PutLong(p.VolumeID)
b.PutInt(p.LocalID)
}

func (p *PhotoSizeSource) writeDialog(b *bin.Buffer) {
func (p *PhotoSizeSource) writeDialog(b *bin.Buffer) {
b.PutLong(p.DialogID)
b.PutLong(p.DialogAccessHash)
}

func (p *PhotoSizeSource) writeStickerSet(b *bin.Buffer) {
func (p *PhotoSizeSource) writeStickerSet(b *bin.Buffer) {
b.PutLong(p.StickerSetID)
b.PutLong(p.StickerSetAccessHash)
}

func (p *PhotoSizeSource) encode(b *bin.Buffer) {
func (p *PhotoSizeSource) encode(b *bin.Buffer) {
b.PutInt(int(p.Type))
switch p.Type {
case PhotoSizeSourceLegacy:
Expand Down
61 changes: 61 additions & 0 deletions fileid/photo_size_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fileid

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/gotd/td/bin"
)

func TestPhotoSizeSourceEncodeDecode(t *testing.T) {
tests := []PhotoSizeSource{
{
Type: PhotoSizeSourceLegacy,
Secret: 10,
},
{
Type: PhotoSizeSourceStickerSetThumbnail,
StickerSetID: 12,
StickerSetAccessHash: 13,
},
{
Type: PhotoSizeSourceFullLegacy,
VolumeID: 13,
LocalID: 14,
Secret: 15,
},
{
Type: PhotoSizeSourceDialogPhotoBigLegacy,
VolumeID: 13,
LocalID: 14,
DialogID: 1099511627774,
DialogAccessHash: 15,
},
{
Type: PhotoSizeSourceStickerSetThumbnailLegacy,
VolumeID: 10,
LocalID: 11,
StickerSetID: 12,
StickerSetAccessHash: 13,
},
{
Type: PhotoSizeSourceStickerSetThumbnailVersion,
StickerSetID: 12,
StickerSetAccessHash: 13,
StickerVersion: 1,
},
}

for _, tt := range tests {
t.Run(tt.Type.String(), func(t *testing.T) {
a := require.New(t)
var b bin.Buffer

tt.encode(&b)
var got PhotoSizeSource
a.NoError(got.decode(&b, latestSubVersion))
a.Equal(tt, got)
})
}
}

0 comments on commit 17bb425

Please sign in to comment.