package main import ( "bytes" "context" "errors" "strings" "testing" ) // RULE: a media pointer no download can use is answered "unfetchable: ", never a bare error, linked or not. // Old history carries pointers with no direct path (and one with no key either); the reader lands // their messages saying what they were, where a bare error held the whole outbox page behind them. func TestAPointerNoDownloadCanUseIsUnfetchable(t *testing.T) { whole := func() *MediaPointer { return &MediaPointer{ Type: "video", Mimetype: "video/mp4", DirectPath: "/v/t62.7161-24/synthetic", MediaKey: []byte{1, 2, 3}, FileEncSHA256: bytes.Repeat([]byte{4}, 32), FileSHA256: bytes.Repeat([]byte{5}, 32), } } if err := whole().fetchable(); err != nil { t.Fatalf("a whole pointer is fetchable: %v", err) } cases := map[string]func(*MediaPointer){ "no direct path": func(m *MediaPointer) { m.DirectPath = "" }, "a path not from the root": func(m *MediaPointer) { m.DirectPath = "v/t62/synthetic" }, "no media key": func(m *MediaPointer) { m.MediaKey = nil }, "no key and no hash": func(m *MediaPointer) { m.DirectPath, m.MediaKey, m.FileEncSHA256 = "", nil, nil }, "no ciphertext hash": func(m *MediaPointer) { m.FileEncSHA256 = nil }, "no file hash": func(m *MediaPointer) { m.FileSHA256 = nil }, "an unknown type": func(m *MediaPointer) { m.Type = "hologram" }, } h := NewHub(nil, nil, nil) for name, spoil := range cases { m := whole() spoil(m) // No account is linked: the pointer is judged before the session is asked for. _, err := h.DownloadMedia(context.Background(), "15550009999@s.whatsapp.net", m, t.TempDir()+"/x") var unfetchable Unfetchable if !errors.As(err, &unfetchable) || !strings.HasPrefix(err.Error(), "unfetchable: ") { t.Errorf("%s: got %v, want an unfetchable answer", name, err) } } if _, err := h.DownloadMedia(context.Background(), "15550009999@s.whatsapp.net", nil, t.TempDir()+"/x"); !strings.HasPrefix(err.Error(), "unfetchable: ") { t.Errorf("no pointer: got %v", err) } // A whole pointer on a number not linked is that, not unfetchable: the reader tries again. if _, err := h.DownloadMedia(context.Background(), "15550009999@s.whatsapp.net", whole(), t.TempDir()+"/x"); err == nil || strings.HasPrefix(err.Error(), "unfetchable: ") { t.Errorf("an unlinked number: got %v", err) } }