merged from gramsrv upstream
This commit is contained in:
parent
79c64ee916
commit
21a0856587
651 changed files with 54774 additions and 4590 deletions
|
|
@ -1,9 +1,13 @@
|
|||
package rpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/iamxvbaba/td/tg"
|
||||
"github.com/iamxvbaba/td/tgerr"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
// TestSendEncryptedFileFlow:sendEncryptedFile 铸造 EncryptedFile、随消息投递、返回
|
||||
|
|
@ -71,13 +75,70 @@ func TestUploadEncryptedFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestEncryptedFileLocationKey:inputEncryptedFileLocation → "enc:<id>" 下载 key。
|
||||
func TestEncryptedFileLocationKey(t *testing.T) {
|
||||
key, ok := fileLocationKey(&tg.InputEncryptedFileLocation{ID: 123, AccessHash: 456})
|
||||
if !ok || key != "enc:123" {
|
||||
t.Fatalf("location key = %q ok %v, want enc:123", key, ok)
|
||||
// TestEncryptedFileDownloadRequiresCapability:密聊 blob 只有在 id+access_hash 元数据能力
|
||||
// 校验成功后才会转换为内部 enc:<id> key;错误 hash 不能触达 Files.GetFile。
|
||||
func TestEncryptedFileDownloadRequiresCapability(t *testing.T) {
|
||||
f := newEncryptedFixture(t)
|
||||
chatID, _ := f.acceptChat(t)
|
||||
chat, _, _ := f.store.GetSecretChat(f.ctx, chatID)
|
||||
res, err := f.router.onMessagesUploadEncryptedFile(f.adminCtx(), &tg.MessagesUploadEncryptedFileRequest{
|
||||
Peer: tg.InputEncryptedChat{ChatID: chatID, AccessHash: chat.AdminAccessHash},
|
||||
File: &tg.InputEncryptedFileUploaded{ID: 888, Parts: 1, KeyFingerprint: 9},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("uploadEncryptedFile: %v", err)
|
||||
}
|
||||
if _, ok := fileLocationKey(&tg.InputEncryptedFileLocation{ID: 0}); ok {
|
||||
t.Fatal("id=0 must be rejected")
|
||||
ef := res.(*tg.EncryptedFile)
|
||||
files := f.router.deps.Files.(*fakeFiles)
|
||||
files.getFileFound = true
|
||||
files.getFileChunk = domain.FileChunk{MimeType: "application/octet-stream", Bytes: []byte{1, 2, 3}}
|
||||
|
||||
got, err := f.router.onUploadGetFile(f.adminCtx(), &tg.UploadGetFileRequest{
|
||||
Location: &tg.InputEncryptedFileLocation{ID: ef.ID, AccessHash: ef.AccessHash},
|
||||
Offset: 0,
|
||||
Limit: 1024,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("get encrypted file: %v", err)
|
||||
}
|
||||
file, ok := got.(*tg.UploadFile)
|
||||
if !ok || !bytes.Equal(file.Bytes, []byte{1, 2, 3}) {
|
||||
t.Fatalf("download = %T %+v", got, got)
|
||||
}
|
||||
if files.getFileCalls != 1 || files.getFileRequest.LocationKey != "enc:9001" {
|
||||
t.Fatalf("GetFile calls/key = %d/%q", files.getFileCalls, files.getFileRequest.LocationKey)
|
||||
}
|
||||
|
||||
_, err = f.router.onUploadGetFile(f.adminCtx(), &tg.UploadGetFileRequest{
|
||||
Location: &tg.InputEncryptedFileLocation{ID: ef.ID, AccessHash: ef.AccessHash + 1},
|
||||
Offset: 0,
|
||||
Limit: 1024,
|
||||
})
|
||||
if !tgerr.Is(err, "LOCATION_INVALID") {
|
||||
t.Fatalf("wrong access hash err = %v", err)
|
||||
}
|
||||
if files.getFileCalls != 1 {
|
||||
t.Fatalf("wrong access hash reached blob store: calls=%d", files.getFileCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptedDataLimit(t *testing.T) {
|
||||
f := newEncryptedFixture(t)
|
||||
chatID, _ := f.acceptChat(t)
|
||||
chat, _, _ := f.store.GetSecretChat(f.ctx, chatID)
|
||||
tooLong := make([]byte, domain.MaxSecretMessageDataBytes+1)
|
||||
|
||||
_, err := f.router.onMessagesSendEncrypted(f.adminCtx(), &tg.MessagesSendEncryptedRequest{
|
||||
Peer: tg.InputEncryptedChat{ChatID: chatID, AccessHash: chat.AdminAccessHash}, RandomID: 1, Data: tooLong,
|
||||
})
|
||||
if !tgerr.Is(err, "DATA_TOO_LONG") {
|
||||
t.Fatalf("sendEncrypted oversized err = %v", err)
|
||||
}
|
||||
_, err = f.router.onMessagesSendEncryptedFile(f.adminCtx(), &tg.MessagesSendEncryptedFileRequest{
|
||||
Peer: tg.InputEncryptedChat{ChatID: chatID, AccessHash: chat.AdminAccessHash}, RandomID: 2, Data: tooLong,
|
||||
File: &tg.InputEncryptedFileUploaded{ID: 888, Parts: 1, KeyFingerprint: 9},
|
||||
})
|
||||
if !tgerr.Is(err, "DATA_TOO_LONG") {
|
||||
t.Fatalf("sendEncryptedFile oversized err = %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue