@@ -3,11 +3,15 @@ package main
33import (
44 "bytes"
55 "context"
6+ "crypto/aes"
7+ "crypto/cipher"
68 "encoding/base64"
9+ "encoding/hex"
710 "encoding/json"
811 "fmt"
912 "io"
1013 "net/http"
14+ "net/http/httptest"
1115 "os"
1216 "strings"
1317 "testing"
@@ -125,6 +129,7 @@ func startTestServer(cfg *testConfig) (*echo.Echo, error) {
125129 ExtAuthURL : "http://localhost:18081" ,
126130 ExtAuthTimeoutS : 5 ,
127131 ExtAuthTimeout : 5 * time .Second ,
132+ MMMSGURL : os .Getenv ("MMMSG_URL" ),
128133 }
129134
130135 // Initialize push token database
@@ -506,6 +511,33 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
506511 t .Skip ("Skipping integration tests; set RUN_INTEGRATION_TESTS=1 to run." )
507512 }
508513
514+ // Start mock MMMSG server
515+ var uploadedData []byte
516+ mmmsgServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
517+ if r .Method == "POST" {
518+ t .Logf ("Mock MMMSG: received POST request" )
519+ w .Header ().Set ("Content-Type" , "application/json" )
520+ json .NewEncoder (w ).Encode (map [string ]string {
521+ "url" : "http://" + r .Host + "/upload/123" ,
522+ })
523+ return
524+ }
525+ if r .Method == "PUT" && strings .HasPrefix (r .URL .Path , "/upload/" ) {
526+ t .Logf ("Mock MMMSG: received PUT request for %s" , r .URL .Path )
527+ data , _ := io .ReadAll (r .Body )
528+ uploadedData = data
529+ w .WriteHeader (http .StatusOK )
530+ return
531+ }
532+ t .Logf ("Mock MMMSG: received unexpected %s request for %s" , r .Method , r .URL .Path )
533+ w .WriteHeader (http .StatusNotFound )
534+ }))
535+ defer mmmsgServer .Close ()
536+
537+ // Set MMMSG_URL to point to our mock server
538+ os .Setenv ("MMMSG_URL" , mmmsgServer .URL )
539+ defer os .Unsetenv ("MMMSG_URL" )
540+
509541 server , err := startTestServer (cfg )
510542 if err != nil {
511543 t .Fatalf ("failed to start test server: %v" , err )
@@ -703,27 +735,42 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
703735 ft .Attachments [0 ].Preview != nil )
704736 })
705737
706- // Step 4: Download the actual image from the proxy URL
707- t .Run ("DownloadImageFromProxy" , func (t * testing.T ) {
708- resp , err := http .Get (attachment .ContentURL )
709- if err != nil {
710- t .Fatalf ("failed to download image from proxy: %v" , err )
711- }
712- defer resp .Body .Close ()
713-
714- if resp .StatusCode != http .StatusOK {
715- t .Errorf ("expected status 200 OK, got %d" , resp .StatusCode )
738+ // Step 4: Verify MMMSG upload and encryption
739+ t .Run ("VerifyMMMSGUpload" , func (t * testing.T ) {
740+ if len (uploadedData ) == 0 {
741+ t .Fatalf ("no data was uploaded to MMMSG" )
716742 }
743+ t .Logf ("Data uploaded to MMMSG: %d bytes" , len (uploadedData ))
717744
718- downloadedData , err := io .ReadAll (resp .Body )
719- if err != nil {
720- t .Fatalf ("failed to read downloaded image data: %v" , err )
745+ if ! strings .HasPrefix (attachment .ContentURL , mmmsgServer .URL ) {
746+ t .Errorf ("attachment content-url %s does not point to mock MMMSG server %s" , attachment .ContentURL , mmmsgServer .URL )
721747 }
722748
723- if len ( downloadedData ) == 0 {
724- t .Errorf ("downloaded image data is empty " )
749+ if attachment . EncryptionKey == "" {
750+ t .Errorf ("encryption key is missing in attachment " )
725751 } else {
726- t .Logf ("Image downloaded successfully from proxy: %d bytes" , len (downloadedData ))
752+ t .Logf ("Encryption key found: %s" , attachment .EncryptionKey )
753+
754+ // Verify that uploaded data is different from original (encrypted)
755+ if bytes .Equal (uploadedData , imageData ) {
756+ t .Errorf ("uploaded data is not encrypted (matches original)" )
757+ } else {
758+ t .Logf ("Uploaded data is encrypted (differs from original)" )
759+ }
760+
761+ // Try to decrypt and verify
762+ key , _ := hex .DecodeString (attachment .EncryptionKey )
763+ block , _ := aes .NewCipher (key )
764+ iv := make ([]byte , aes .BlockSize )
765+ stream := cipher .NewCTR (block , iv )
766+ decrypted := make ([]byte , len (uploadedData ))
767+ stream .XORKeyStream (decrypted , uploadedData )
768+
769+ if ! bytes .Equal (decrypted , imageData ) {
770+ t .Errorf ("decrypted data does not match original image data" )
771+ } else {
772+ t .Logf ("Decrypted data matches original image data successfully" )
773+ }
727774 }
728775 })
729776 })
0 commit comments