github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/e2e/mobile_entrypoint_test.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package e2e
    19  
    20  import (
    21  	"encoding/json"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  
    32  	"github.com/mysteriumnetwork/node/config"
    33  	"github.com/mysteriumnetwork/node/mobile/mysterium"
    34  	"github.com/mysteriumnetwork/payments/crypto"
    35  )
    36  
    37  func TestMobileNodeConsumer(t *testing.T) {
    38  	dir, err := os.MkdirTemp("", "mobileEntryPoint")
    39  	require.NoError(t, err)
    40  	defer os.RemoveAll(dir)
    41  
    42  	err = os.Mkdir(filepath.Join(dir, ".mysterium"), 0777)
    43  	require.NoError(t, err)
    44  
    45  	options := &mysterium.MobileNodeOptions{
    46  		Network:                        string(config.Mainnet),
    47  		DiscoveryAddress:               "http://discovery:8080/api/v4",
    48  		BrokerAddresses:                []string{"broker"},
    49  		EtherClientRPCL1:               []string{"http://ganache:8545"},
    50  		EtherClientRPCL2:               []string{"ws://ganache2:8545"},
    51  		FeedbackURL:                    "TODO",
    52  		QualityOracleURL:               "http://morqa:8085/api/v3",
    53  		IPDetectorURL:                  "http://ipify:3000/?format=json",
    54  		LocationDetectorURL:            "https://location.mysterium.network/api/v1/location",
    55  		TransactorEndpointAddress:      "http://transactor:8888/api/v1",
    56  		HermesEndpointAddress:          "http://hermes:8889/api/v1",
    57  		ActiveChainID:                  80001,
    58  		Chain1ID:                       5,
    59  		Chain2ID:                       80001,
    60  		MystSCAddress:                  "0xaa9c4e723609cb913430143fbc86d3cbe7adca21",
    61  		RegistrySCAddress:              "0x427c2bad22335710aec5e477f3e3adcd313a9bcb",
    62  		HermesSCAddress:                "0xd68defb97d0765741f8ecf179df2f9564e1466a3",
    63  		ChannelImplementationSCAddress: "0x599d43715df3070f83355d9d90ae62c159e62a75",
    64  	}
    65  
    66  	node, err := mysterium.NewNode(dir, options)
    67  	require.NoError(t, err)
    68  	require.NotNil(t, node)
    69  
    70  	t.Run("Test status", func(t *testing.T) {
    71  		resp, err := node.GetStatus()
    72  		require.NoError(t, err)
    73  
    74  		var status mysterium.GetStatusResponse
    75  		err = json.Unmarshal(resp, &status)
    76  
    77  		require.NoError(t, err)
    78  
    79  		require.Equal(t, "NotConnected", status.State)
    80  		require.Equal(t, "", status.Proposal.ProviderID)
    81  		require.Equal(t, "", status.Proposal.ServiceType)
    82  	})
    83  
    84  	t.Run("Test identity registration", func(t *testing.T) {
    85  		identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{})
    86  		require.NoError(t, err)
    87  
    88  		require.NotNil(t, identity)
    89  		require.Equal(t, "Unregistered", identity.RegistrationStatus)
    90  
    91  		topUpConsumer(t, identity.IdentityAddress, common.HexToAddress(hermesID), registrationFee)
    92  
    93  		err = node.RegisterIdentity(&mysterium.RegisterIdentityRequest{
    94  			IdentityAddress: identity.IdentityAddress,
    95  		})
    96  		require.NoError(t, err)
    97  
    98  		require.Eventually(t, func() bool {
    99  			identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{})
   100  			require.NoError(t, err)
   101  			return identity.RegistrationStatus == "Registered"
   102  		}, 15*time.Second, 1*time.Second)
   103  	})
   104  
   105  	t.Run("Test balance", func(t *testing.T) {
   106  		identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{})
   107  		require.NoError(t, err)
   108  
   109  		assert.Eventually(t, func() bool {
   110  			balance, err := node.GetBalance(&mysterium.GetBalanceRequest{IdentityAddress: identity.IdentityAddress})
   111  			require.NoError(t, err)
   112  			return crypto.BigMystToFloat(balanceAfterRegistration) == balance.Balance
   113  		}, time.Second*5, 100*time.Millisecond)
   114  	})
   115  
   116  	t.Run("Test identity export", func(t *testing.T) {
   117  		identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{})
   118  		require.NoError(t, err)
   119  		// without '0x' prefix
   120  		hexAddress := strings.ToLower(identity.IdentityAddress[2:])
   121  
   122  		exportBytes, err := node.ExportIdentity(identity.IdentityAddress, "secret_pass")
   123  		require.NoError(t, err)
   124  
   125  		var ks identityKeystore
   126  		err = json.Unmarshal(exportBytes, &ks)
   127  		require.NoError(t, err)
   128  		require.Equal(t, ks.Address, hexAddress)
   129  		require.NotEmpty(t, ks.Version)
   130  		require.NotEmpty(t, ks.ID)
   131  		require.NotEmpty(t, ks.Crypto)
   132  	})
   133  
   134  	t.Run("Test identity import", func(t *testing.T) {
   135  		keystoreString := "{\"address\":\"2574e9053c104f5e6012cbb0aa457318339d8a7f\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"ciphertext\":\"772b3df26635c50fccf26350c6530c4216e2d78b4836105475f2876dc0704810\",\"cipherparams\":{\"iv\":\"1b96fb8b5614f5b46f1e1e0327f370ed\"},\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"6978a44ba80d588aacf497d2b042948bdbf74aefa22b715ab863647511236f17\"},\"mac\":\"77b896027172c9dc68d64f15d6450492bd92a57b994734fd147769a580e02ef6\"},\"id\":\"d18381e4-2011-48c7-97cf-84ccc3882c87\",\"version\":3}"
   136  		keystorePass := "fhHGF12G2g"
   137  
   138  		address, err := node.ImportIdentity([]byte(keystoreString), keystorePass)
   139  		require.NoError(t, err)
   140  		require.NotEmpty(t, address)
   141  
   142  		identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{Address: address})
   143  		require.NoError(t, err)
   144  		require.Equal(t, address, identity.IdentityAddress)
   145  		require.NotEmpty(t, identity.ChannelAddress)
   146  		require.Equal(t, "Unregistered", identity.RegistrationStatus)
   147  	})
   148  
   149  	t.Run("Test resident country", func(t *testing.T) {
   150  		// given
   151  		identity, err := node.GetIdentity(&mysterium.GetIdentityRequest{})
   152  		require.NoError(t, err)
   153  
   154  		// when
   155  		err = node.UpdateResidentCountry(&mysterium.ResidentCountryUpdateRequest{IdentityAddress: identity.IdentityAddress, Country: "AU"})
   156  		require.NoError(t, err)
   157  
   158  		// then
   159  		require.Equal(t, "AU", node.ResidentCountry(), "default country should be set")
   160  
   161  		// and
   162  		err = node.UpdateResidentCountry(&mysterium.ResidentCountryUpdateRequest{IdentityAddress: identity.IdentityAddress})
   163  		require.Error(t, err, "country is required")
   164  		err = node.UpdateResidentCountry(&mysterium.ResidentCountryUpdateRequest{Country: "UK"})
   165  		require.Error(t, err, "identity is required")
   166  	})
   167  
   168  	t.Run("Test filter preset", func(t *testing.T) {
   169  		// given
   170  		byName := func(presets []mysterium.ProposalFilterPreset, name string) bool {
   171  			for _, p := range presets {
   172  				if p.Name == name {
   173  					return true
   174  				}
   175  			}
   176  			return false
   177  		}
   178  
   179  		// when
   180  		bytes, err := node.ListProposalFilterPresets()
   181  		require.NoError(t, err)
   182  
   183  		var presets []mysterium.ProposalFilterPreset
   184  		err = json.Unmarshal(bytes, &presets)
   185  		require.NoError(t, err)
   186  
   187  		// when
   188  		for _, name := range []string{"Media Streaming", "Browsing", "Download"} {
   189  			require.True(t, byName(presets, name), "missing name '%s' in preset filters", name)
   190  		}
   191  	})
   192  
   193  	t.Run("Test shutdown", func(t *testing.T) {
   194  		err := node.Shutdown()
   195  		require.NoError(t, err)
   196  	})
   197  }
   198  
   199  type identityKeystore struct {
   200  	Address string                 `json:"address"`
   201  	Crypto  map[string]interface{} `json:"crypto"`
   202  	ID      string                 `json:"id"`
   203  	Version int                    `json:"version"`
   204  }