...

Source file src/edge-infra.dev/pkg/edge/datasync/internal/signature/signature_test.go

Documentation: edge-infra.dev/pkg/edge/datasync/internal/signature

     1  package signature_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	fakebsl "edge-infra.dev/pkg/edge/bsl/fake"
    11  	"edge-infra.dev/pkg/edge/datasync/internal/config"
    12  	"edge-infra.dev/pkg/edge/datasync/internal/signature"
    13  )
    14  
    15  func TestSignature(t *testing.T) {
    16  	payload := []byte("hello from data-sync...testing message signature!")
    17  	messageContxt := signature.MessageSignatureContext{
    18  		MessageID:   "123",
    19  		MessageType: "tlog",
    20  		Payload:     payload,
    21  		Timestamp:   time.Date(2021, 4, 28, 17, 59, 59, 0, time.Now().UTC().Location()),
    22  	}
    23  
    24  	bslInfo := fakebsl.GetBSLInfo()
    25  
    26  	t.Run("error when missing ENV vars", func(t *testing.T) {
    27  		sharedKey := config.GetSharedKey()
    28  		secretKey := config.GetSecretKey()
    29  		_, gotErr := signature.Generate(sharedKey, secretKey, messageContxt, bslInfo.OrganizationName)
    30  		wantErr := errors.New("failed to sign message. missing keys")
    31  
    32  		if gotErr == nil {
    33  			t.Errorf("expected to get exception, got nil")
    34  		}
    35  
    36  		if gotErr.Error() != wantErr.Error() {
    37  			t.Errorf("got %q, want %q", gotErr.Error(), wantErr.Error())
    38  		}
    39  	})
    40  
    41  	os.Setenv("DATA_SYNC_SHARED_KEY", "some-SHARED-key")
    42  	os.Setenv("DATA_SYNC_SECRET_KEY", "some-SECRET-key")
    43  	os.Setenv("DATA_SYNC_APP_KEY", "some-application-key")
    44  
    45  	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"}]}`)
    46  
    47  	t.Run("success to sign compressed message", func(t *testing.T) {
    48  		sharedKey := config.GetSharedKey()
    49  		secretKey := config.GetSecretKey()
    50  		organizationName := bslInfo.OrganizationName
    51  		fmt.Print(organizationName)
    52  		got, _ := signature.Generate(sharedKey, secretKey, messageContxt, organizationName)
    53  		want := "some-SHARED-key:1aa81b47f5eb56dc0885a4a287740703e846346e69a78f9cef710428b19165185a4f51941fa971a53f7c4714340d5745742b53f0b2134328c98a3ed73be32dec"
    54  
    55  		if got != want {
    56  			t.Errorf("got %q, want %q", got, want)
    57  		}
    58  	})
    59  
    60  	t.Run("success to sign non-compressed message", func(t *testing.T) {
    61  		messageContxt.MessageType = "non-compressed-message-type"
    62  		sharedKey := config.GetSharedKey()
    63  		secretKey := config.GetSecretKey()
    64  		organizationName := bslInfo.OrganizationName
    65  		got, _ := signature.Generate(sharedKey, secretKey, messageContxt, organizationName)
    66  		want := "some-SHARED-key:1aa81b47f5eb56dc0885a4a287740703e846346e69a78f9cef710428b19165185a4f51941fa971a53f7c4714340d5745742b53f0b2134328c98a3ed73be32dec"
    67  
    68  		if got != want {
    69  			t.Errorf("got %q, want %q", got, want)
    70  		}
    71  	})
    72  }
    73  
    74  func BenchmarkSignMessage(b *testing.B) {
    75  	os.Setenv("DATA_SYNC_SHARED_KEY", "some-SHARED-key")
    76  	os.Setenv("DATA_SYNC_SECRET_KEY", "some-SECRET-key")
    77  	os.Setenv("DATA_SYNC_APP_KEY", "some-application-key")
    78  	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"}]}`)
    79  
    80  	payload, _ := os.ReadFile("test-data/tlog.json")
    81  
    82  	messageContxt := signature.MessageSignatureContext{
    83  		MessageID:   "123",
    84  		MessageType: "tlog",
    85  		Payload:     payload,
    86  		Timestamp:   time.Date(2021, 4, 28, 17, 59, 59, 0, time.Local),
    87  	}
    88  
    89  	sharedKey := config.GetSharedKey()
    90  	secretKey := config.GetSecretKey()
    91  	organizationName := config.OrganizationName()
    92  	for i := 0; i < b.N; i++ {
    93  		_, err := signature.Generate(sharedKey, secretKey, messageContxt, organizationName)
    94  		if err != nil {
    95  			b.Log(err)
    96  		}
    97  	}
    98  }
    99  

View as plain text