github.com/status-im/status-go@v1.1.0/server/pairing/config_test.go (about)

     1  package pairing
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  	"gopkg.in/go-playground/validator.v9"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/status-im/status-go/protocol/requests"
    14  )
    15  
    16  func TestConfigTestSuite(t *testing.T) {
    17  	suite.Run(t, new(ConfigTestSuite))
    18  }
    19  
    20  type ConfigTestSuite struct {
    21  	suite.Suite
    22  	validate *validator.Validate
    23  }
    24  
    25  func (s *ConfigTestSuite) SetupTest() {
    26  	var err error
    27  	s.validate, err = newValidate()
    28  	require.NoError(s.T(), err, "newValidate should not return error")
    29  }
    30  
    31  func (s *ConfigTestSuite) TestValidationKeystorePath() {
    32  	s.T().Run("Valid keystore path with keyUID", func(t *testing.T) {
    33  		sc := &SenderConfig{
    34  			KeystorePath: "some/path/0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    35  			DeviceType:   "phone",
    36  			KeyUID:       "0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    37  			Password:     "password",
    38  		}
    39  
    40  		assert.NoError(t, s.validate.Struct(sc), "SenderConfig validation should pass")
    41  	})
    42  
    43  	s.T().Run("Invalid keystore path without keyUID", func(t *testing.T) {
    44  		sc := &SenderConfig{
    45  			KeystorePath: "some/path/",
    46  			DeviceType:   "phone",
    47  			KeyUID:       "0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    48  			Password:     "password",
    49  		}
    50  
    51  		assert.Error(t, s.validate.Struct(sc), "SenderConfig validation should fail")
    52  	})
    53  }
    54  
    55  func (s *ConfigTestSuite) TestValidationKeyUID() {
    56  	s.T().Run("Valid keyUID", func(t *testing.T) {
    57  		sc := &SenderConfig{
    58  			KeystorePath: "some/path/0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    59  			DeviceType:   "phone",
    60  			KeyUID:       "0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    61  			Password:     "password",
    62  		}
    63  
    64  		assert.NoError(t, s.validate.Struct(sc), "SenderConfig validation should pass")
    65  	})
    66  
    67  	s.T().Run("Invalid keyUID", func(t *testing.T) {
    68  		sc := &SenderConfig{
    69  			KeystorePath: "some/path/0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54e",
    70  			DeviceType:   "phone",
    71  			KeyUID:       "0x130cc0ebdaecd220c1d6dea0ef01d575ef5364506785745049eb98ddf49cb54",
    72  			Password:     "password",
    73  		}
    74  
    75  		assert.Error(t, s.validate.Struct(sc), "SenderConfig validation should fail")
    76  	})
    77  }
    78  
    79  func (s *ConfigTestSuite) TestValidationNotEndKeyUID() {
    80  	keyUIDPattern := regexp.MustCompile(`^0x[0-9a-fA-F]{64}$`)
    81  
    82  	r := &ReceiverConfig{
    83  		CreateAccount: &requests.CreateAccount{
    84  			RootDataDir:   "/tmp",
    85  			KdfIterations: 1,
    86  			DeviceName:    "device-1",
    87  		},
    88  	}
    89  	keystorePath := r.AbsoluteKeystorePath()
    90  	s.Require().True(len(keystorePath) <= 66 || !keyUIDPattern.MatchString(keystorePath[len(keystorePath)-66:]))
    91  
    92  	s.Require().NoError(validateReceiverConfig(r, r), "ReceiverConfig validation should pass")
    93  }