Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix_: ensure generated identity-images have a valid clock value #6239

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions images/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package images
import (
"bytes"
"image"
"time"
)

func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
Expand All @@ -25,6 +26,7 @@ func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
Height: rImg.Bounds().Dy(),
FileSize: bb.Len(),
ResizeTarget: int(s),
Clock: uint64(time.Now().UnixMilli()),
}

iis = append(iis, ii)
Expand Down
24 changes: 24 additions & 0 deletions images/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ func TestGenerateBannerImage_ShrinkOnly(t *testing.T) {
require.Exactly(t, identityImage.Width, int(BannerDim))
require.Exactly(t, identityImage.Height, 805)
}

func TestGenerateIdentityImages(t *testing.T) {
// Create image data
testImage := image.NewRGBA(image.Rect(0, 0, int(BannerDim)+10, int(BannerDim)+20))

// Create a temporary file for storing the image data
tmpTestFilePath := t.TempDir() + "/test.png"
file, err := os.Create(tmpTestFilePath)
require.NoError(t, err)
defer file.Close()

// Save the image data to the temporary file
err = png.Encode(file, testImage)
require.NoError(t, err)

// Generate the identity images
identityImages, err := GenerateIdentityImages(tmpTestFilePath, 100, 100, 500, 500)
require.NoError(t, err)

// Check the identity images have a valid clock value
for _, image := range identityImages {
require.NotEqual(t, image.Clock, uint64(0))
}
}
2 changes: 1 addition & 1 deletion protocol/messenger_contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func (m *Messenger) updateContactImagesURL(contact *Contact) error {
if err != nil {
return err
}
v.LocalURL = m.httpServer.MakeContactImageURL(common.PubkeyToHex(publicKey), k)
v.LocalURL = m.httpServer.MakeContactImageURL(common.PubkeyToHex(publicKey), k, v.Clock)
contact.Images[k] = v
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/server_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func (s *MediaServer) MakeQRURL(qurul string,
return u.String()
}

func (s *MediaServer) MakeContactImageURL(publicKey string, imageType string) string {
func (s *MediaServer) MakeContactImageURL(publicKey string, imageType string, imageClock uint64) string {
u := s.MakeBaseURL()
u.Path = contactImagesPath
u.RawQuery = url.Values{"publicKey": {publicKey}, "imageName": {imageType}}.Encode()
u.RawQuery = url.Values{"publicKey": {publicKey}, "imageName": {imageType}, "clock": {fmt.Sprint(imageClock)}}.Encode()

return u.String()
}
Expand Down
9 changes: 9 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func (s *ServerURLSuite) TestServer_MakeStickerURL() {
s.serverNoPort.MakeStickerURL("0xdeadbeef4ac0"))
}

func (s *ServerURLSuite) TestServer_MakeContactImageURL() {
s.Require().Equal(
baseURLWithCustomPort+"/contactImages?clock=1&imageName=Test&publicKey=0x1",
s.server.MakeContactImageURL("0x1", "Test", uint64(1)))
s.testNoPort(
baseURLWithDefaultPort+"/contactImages?clock=1&imageName=Test&publicKey=0x1",
s.serverNoPort.MakeContactImageURL("0x1", "Test", uint64(1)))
}

// TestQRCodeGeneration tests if we provide all the correct parameters to the media server
// do we get a valid QR code or not as part of the response payload.
// we have stored a generated QR code in tests folder, and we compare their bytes.
Expand Down
Loading