package signature_test import ( "errors" "fmt" "os" "testing" "time" fakebsl "edge-infra.dev/pkg/edge/bsl/fake" "edge-infra.dev/pkg/edge/datasync/internal/config" "edge-infra.dev/pkg/edge/datasync/internal/signature" ) func TestSignature(t *testing.T) { payload := []byte("hello from data-sync...testing message signature!") messageContxt := signature.MessageSignatureContext{ MessageID: "123", MessageType: "tlog", Payload: payload, Timestamp: time.Date(2021, 4, 28, 17, 59, 59, 0, time.Now().UTC().Location()), } bslInfo := fakebsl.GetBSLInfo() t.Run("error when missing ENV vars", func(t *testing.T) { sharedKey := config.GetSharedKey() secretKey := config.GetSecretKey() _, gotErr := signature.Generate(sharedKey, secretKey, messageContxt, bslInfo.OrganizationName) wantErr := errors.New("failed to sign message. missing keys") if gotErr == nil { t.Errorf("expected to get exception, got nil") } if gotErr.Error() != wantErr.Error() { t.Errorf("got %q, want %q", gotErr.Error(), wantErr.Error()) } }) os.Setenv("DATA_SYNC_SHARED_KEY", "some-SHARED-key") os.Setenv("DATA_SYNC_SECRET_KEY", "some-SECRET-key") os.Setenv("DATA_SYNC_APP_KEY", "some-application-key") os.Setenv("MESSAGE_ROUTING", `{"routes":[{"id":"tlog", "topic":"tlog", "bulk_size": 10}, {"id":"public", "topic":"public", "bulk_size": 20}], "messages":[{"type": "tlog", "compression": true, "signing": true, "route": "tlog"}, {"type": "audit", "compression": true, "signing": false, "route": "public"}, {"type": "*", "compression": false, "signing": false, "route": "public"}]}`) t.Run("success to sign compressed message", func(t *testing.T) { sharedKey := config.GetSharedKey() secretKey := config.GetSecretKey() organizationName := bslInfo.OrganizationName fmt.Print(organizationName) got, _ := signature.Generate(sharedKey, secretKey, messageContxt, organizationName) want := "some-SHARED-key:1aa81b47f5eb56dc0885a4a287740703e846346e69a78f9cef710428b19165185a4f51941fa971a53f7c4714340d5745742b53f0b2134328c98a3ed73be32dec" if got != want { t.Errorf("got %q, want %q", got, want) } }) t.Run("success to sign non-compressed message", func(t *testing.T) { messageContxt.MessageType = "non-compressed-message-type" sharedKey := config.GetSharedKey() secretKey := config.GetSecretKey() organizationName := bslInfo.OrganizationName got, _ := signature.Generate(sharedKey, secretKey, messageContxt, organizationName) want := "some-SHARED-key:1aa81b47f5eb56dc0885a4a287740703e846346e69a78f9cef710428b19165185a4f51941fa971a53f7c4714340d5745742b53f0b2134328c98a3ed73be32dec" if got != want { t.Errorf("got %q, want %q", got, want) } }) } func BenchmarkSignMessage(b *testing.B) { os.Setenv("DATA_SYNC_SHARED_KEY", "some-SHARED-key") os.Setenv("DATA_SYNC_SECRET_KEY", "some-SECRET-key") os.Setenv("DATA_SYNC_APP_KEY", "some-application-key") os.Setenv("MESSAGE_ROUTING", `{"routes":[{"id":"tlog", "topic":"tlog", "bulk_size": 10}, {"id":"public", "topic":"public", "bulk_size": 20}], "messages":[{"type": "tlog", "compression": true, "signing": true, "route": "tlog"}, {"type": "audit", "compression": true, "signing": false, "route": "public"}, {"type": "*", "compression": false, "signing": false, "route": "public"}]}`) payload, _ := os.ReadFile("test-data/tlog.json") messageContxt := signature.MessageSignatureContext{ MessageID: "123", MessageType: "tlog", Payload: payload, Timestamp: time.Date(2021, 4, 28, 17, 59, 59, 0, time.Local), } sharedKey := config.GetSharedKey() secretKey := config.GetSecretKey() organizationName := config.OrganizationName() for i := 0; i < b.N; i++ { _, err := signature.Generate(sharedKey, secretKey, messageContxt, organizationName) if err != nil { b.Log(err) } } }