dtchat: exercise WhatsApp incoming files with a real fake file (T9.1)
DTCHAT_WHATSAPP_FAKE_FILE makes the fake adapter offer that file (name, size, SHA-256), and on file-accept read it in 4 KiB blocks, emit file-data and a file-end carrying the hash; the real accept path (DownloadAny) now also sends the SHA-256 in file-end, and the static fake keeps its hello-world hash. tests/whatsapp_fake.py uses a 9000-byte deterministic file, checks the offer name/size/hash, reassembles every chunk byte-for-byte and verifies the file-end hash (31 checks, green stand-alone). Harness entry already exists; in-tree build and harness follow.
This commit is contained in:
1 parent
f0144bc092
commit
056bd30a8f
2 files changed
+59
-17
No files matched your search
@@ -147,11 +147,27 @@ func fileData(id string, data []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func fileEnd(id string, n int) {
|
||||
emit(map[string]any{"type": "file-end", "id": id, "size": n})
|
||||
func fileEnd(id string, n int, sha string) {
|
||||
m := map[string]any{"type": "file-end", "id": id, "size": n}
|
||||
if sha != "" {
|
||||
m["sha256"] = sha
|
||||
}
|
||||
emit(m)
|
||||
}
|
||||
|
||||
func fakeFiles() {
|
||||
if path := os.Getenv("DTCHAT_WHATSAPP_FAKE_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err == nil {
|
||||
sum := sha256.Sum256(data)
|
||||
emit(map[string]any{"type": "file-offer",
|
||||
"conv": "alice", "id": "f1",
|
||||
"name": filepath.Base(path),
|
||||
"size": len(data),
|
||||
"sha256": hex.EncodeToString(sum[:])})
|
||||
return
|
||||
}
|
||||
}
|
||||
sum := sha256.Sum256([]byte("hello world"))
|
||||
emit(map[string]any{"type": "file-offer", "conv": "alice",
|
||||
"id": "f1", "name": "note.txt", "size": 11,
|
||||
@@ -167,10 +183,21 @@ func fakeAnswer() {
|
||||
}
|
||||
|
||||
func fakeFileAccept(id string) {
|
||||
if id == "f1" {
|
||||
fileData(id, []byte("hello world"))
|
||||
fileEnd(id, 11)
|
||||
if id != "f1" {
|
||||
return
|
||||
}
|
||||
if path := os.Getenv("DTCHAT_WHATSAPP_FAKE_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err == nil {
|
||||
sum := sha256.Sum256(data)
|
||||
fileData(id, data)
|
||||
fileEnd(id, len(data), hex.EncodeToString(sum[:]))
|
||||
return
|
||||
}
|
||||
}
|
||||
fileData(id, []byte("hello world"))
|
||||
fileEnd(id, 11,
|
||||
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
|
||||
}
|
||||
|
||||
func messageText(m *waE2E.Message) string {
|
||||
@@ -370,8 +397,9 @@ func acceptRealFile(id string) {
|
||||
"text": err.Error()})
|
||||
return
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
fileData(id, data)
|
||||
fileEnd(id, len(data))
|
||||
fileEnd(id, len(data), hex.EncodeToString(sum[:]))
|
||||
}
|
||||
|
||||
func command(line []byte) {
|
||||
|
||||
+25
-11
@@ -8,7 +8,9 @@ the IPC path (hello, configure, connect, QR request, online, send,
|
||||
disconnect) is tested without an account.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -26,11 +28,16 @@ def check(cond, what):
|
||||
failures.append(what)
|
||||
|
||||
|
||||
PAYLOAD = bytes((i * 53 + 7) & 0xff for i in range(9000))
|
||||
FILE = "/tmp/dtchat-whatsapp-file.bin"
|
||||
with open(FILE, "wb") as f:
|
||||
f.write(PAYLOAD)
|
||||
proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
env={"PATH": "/usr/bin:/bin",
|
||||
"DTCHAT_WHATSAPP_FAKE": "1"})
|
||||
"DTCHAT_WHATSAPP_FAKE": "1",
|
||||
"DTCHAT_WHATSAPP_FAKE_FILE": FILE})
|
||||
q = queue.Queue()
|
||||
|
||||
|
||||
@@ -120,26 +127,32 @@ check(roster.get("bob", {}).get("name") == "Bob",
|
||||
"roster has bob")
|
||||
check(roster.get("alice", {}).get("online") is True,
|
||||
"alice is online")
|
||||
check(offer is not None and offer.get("name") == "note.txt",
|
||||
check(offer is not None and
|
||||
offer.get("name") == "dtchat-whatsapp-file.bin",
|
||||
"a file offer arrives")
|
||||
if offer is not None:
|
||||
check(offer.get("size") == 11, "the offer has the size")
|
||||
check(len(offer.get("sha256", "")) == 64, "the offer has the SHA-256")
|
||||
check(offer.get("size") == len(PAYLOAD), "the offer has the size")
|
||||
check(offer.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
||||
"the offer has the SHA-256")
|
||||
send({"type": "file-accept", "id": offer.get("id")})
|
||||
data = end = None
|
||||
chunks = []
|
||||
end = None
|
||||
deadline = time.time() + 5
|
||||
while time.time() < deadline and (data is None or end is None):
|
||||
while time.time() < deadline:
|
||||
e = ev(1.0)
|
||||
if e is None:
|
||||
continue
|
||||
if e.get("type") == "file-data":
|
||||
data = e
|
||||
chunks.append(base64.b64decode(e.get("data", "")))
|
||||
elif e.get("type") == "file-end":
|
||||
end = e
|
||||
check(data is not None and
|
||||
base64.b64decode(data.get("data", "")) == b"hello world",
|
||||
"accepted file data")
|
||||
check(end is not None and end.get("size") == 11, "file end")
|
||||
break
|
||||
received = b"".join(chunks)
|
||||
check(received == PAYLOAD, "accepted file data arrives unchanged")
|
||||
check(end is not None and end.get("size") == len(PAYLOAD), "file end size")
|
||||
check(end is not None and
|
||||
end.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
||||
"file end SHA-256")
|
||||
|
||||
send({"type": "typing", "conv": "alice", "on": True})
|
||||
typing = None
|
||||
@@ -195,6 +208,7 @@ except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
check(False, "exit 0")
|
||||
check(proc.stderr.read().strip() == "", "no stderr output")
|
||||
os.unlink(FILE)
|
||||
|
||||
if failures:
|
||||
print("test_whatsapp_fake: FAIL (%d)" % len(failures))
|
||||
|
||||
Reference in new issue
Block a user