11package handlers
22
33import (
4+ "bytes"
45 "context"
56 "errors"
7+ "fmt"
68 "testing"
79
810 "maunium.net/go/mautrix/bridgev2"
@@ -11,6 +13,98 @@ import (
1113 "github.com/highesttt/matrix-line-messenger/pkg/line"
1214)
1315
16+ func TestDecryptDownloadedMediaUsesBodyKey (t * testing.T ) {
17+ ciphertext := []byte ("ciphertext" )
18+ plaintext := []byte ("plaintext" )
19+ var keys []string
20+ h := & Handler {DecryptMedia : func (data []byte , key string ) ([]byte , error ) {
21+ if ! bytes .Equal (data , ciphertext ) {
22+ t .Fatalf ("decrypt input = %q, want ciphertext" , data )
23+ }
24+ keys = append (keys , key )
25+ return plaintext , nil
26+ }}
27+
28+ got , err := h .decryptDownloadedMedia (ciphertext , `{"keyMaterial":"body-key"}` , map [string ]string {"ENC_KM" : "metadata-key" }, "image" )
29+ if err != nil {
30+ t .Fatal (err )
31+ }
32+ if ! bytes .Equal (got , plaintext ) {
33+ t .Fatalf ("decrypted data = %q, want %q" , got , plaintext )
34+ }
35+ if len (keys ) != 1 || keys [0 ] != "body-key" {
36+ t .Fatalf ("keys = %v, want body key only" , keys )
37+ }
38+ }
39+
40+ func TestDecryptDownloadedMediaFallsBackToENCKM (t * testing.T ) {
41+ ciphertext := []byte ("ciphertext" )
42+ var keys []string
43+ h := & Handler {DecryptMedia : func (data []byte , key string ) ([]byte , error ) {
44+ if ! bytes .Equal (data , ciphertext ) {
45+ t .Fatalf ("decrypt input = %q, want original ciphertext" , data )
46+ }
47+ keys = append (keys , key )
48+ if key == "body-key" {
49+ return nil , errors .New ("body key failed" )
50+ }
51+ return []byte ("metadata plaintext" ), nil
52+ }}
53+
54+ got , err := h .decryptDownloadedMedia (ciphertext , `{"keyMaterial":"body-key"}` , map [string ]string {"ENC_KM" : "metadata-key" }, "file" )
55+ if err != nil {
56+ t .Fatal (err )
57+ }
58+ if string (got ) != "metadata plaintext" {
59+ t .Fatalf ("decrypted data = %q" , got )
60+ }
61+ if fmt .Sprint (keys ) != "[body-key metadata-key]" {
62+ t .Fatalf ("keys = %v, want body then metadata" , keys )
63+ }
64+ }
65+
66+ func TestDecryptDownloadedMediaPassesThroughPlainMedia (t * testing.T ) {
67+ data := []byte ("plain media" )
68+ got , err := new (Handler ).decryptDownloadedMedia (data , "" , nil , "audio" )
69+ if err != nil {
70+ t .Fatal (err )
71+ }
72+ if ! bytes .Equal (got , data ) {
73+ t .Fatalf ("data = %q, want unchanged media" , got )
74+ }
75+ }
76+
77+ func TestDecryptDownloadedMediaRejectsEmptyDeclaredKey (t * testing.T ) {
78+ got , err := new (Handler ).decryptDownloadedMedia ([]byte ("ciphertext" ), `{"keyMaterial":""}` , nil , "image" )
79+ if got != nil {
80+ t .Fatalf ("data = %q, want no ciphertext returned" , got )
81+ }
82+ if ! errors .Is (err , bridgev2 .ErrIgnoringRemoteEvent ) {
83+ t .Fatalf ("err = %v, want ErrIgnoringRemoteEvent" , err )
84+ }
85+ }
86+
87+ func TestDecryptDownloadedMediaFailsClosed (t * testing.T ) {
88+ ciphertext := []byte ("ciphertext" )
89+ decryptErr := errors .New ("invalid media key" )
90+ var calls int
91+ h := & Handler {DecryptMedia : func ([]byte , string ) ([]byte , error ) {
92+ calls ++
93+ return nil , decryptErr
94+ }}
95+
96+ got , err := h .decryptDownloadedMedia (ciphertext , `{"keyMaterial":"body-key"}` , map [string ]string {"ENC_KM" : "metadata-key" }, "video" )
97+ if got != nil {
98+ t .Fatalf ("data = %q, want no ciphertext returned" , got )
99+ }
100+ if calls != 2 {
101+ t .Fatalf ("decrypt calls = %d, want both declared keys attempted" , calls )
102+ }
103+ if ! errors .Is (err , decryptErr ) || ! errors .Is (err , bridgev2 .ErrIgnoringRemoteEvent ) {
104+ t .Fatalf ("err = %v, want decrypt error and ErrIgnoringRemoteEvent" , err )
105+ }
106+ }
107+
14108func TestTryRecoverClientPassesOriginatingClient (t * testing.T ) {
15109 errAuth := errors .New ("SSE error: 401" )
16110 var recoverCalled bool
0 commit comments