github.com/ethersphere/bee/v2@v2.2.0/pkg/api/p2p_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"errors"
    10  	"net/http"
    11  	"testing"
    12  
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethersphere/bee/v2/pkg/api"
    15  	"github.com/ethersphere/bee/v2/pkg/crypto"
    16  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    17  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    18  	"github.com/ethersphere/bee/v2/pkg/p2p/mock"
    19  	"github.com/ethersphere/bee/v2/pkg/swarm"
    20  	"github.com/multiformats/go-multiaddr"
    21  )
    22  
    23  func TestAddresses(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	privateKey, err := crypto.GenerateSecp256k1Key()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	pssPrivateKey, err := crypto.GenerateSecp256k1Key()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	overlay := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c")
    35  	addresses := []multiaddr.Multiaddr{
    36  		mustMultiaddr(t, "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"),
    37  		mustMultiaddr(t, "/ip4/192.168.0.101/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"),
    38  	}
    39  
    40  	ethereumAddress := common.HexToAddress("abcd")
    41  
    42  	testServer, _, _, _ := newTestServer(t, testServerOptions{
    43  		PublicKey:       privateKey.PublicKey,
    44  		PSSPublicKey:    pssPrivateKey.PublicKey,
    45  		Overlay:         overlay,
    46  		EthereumAddress: ethereumAddress,
    47  		P2P: mock.New(mock.WithAddressesFunc(func() ([]multiaddr.Multiaddr, error) {
    48  			return addresses, nil
    49  		})),
    50  	})
    51  
    52  	t.Run("ok", func(t *testing.T) {
    53  		t.Parallel()
    54  
    55  		jsonhttptest.Request(t, testServer, http.MethodGet, "/addresses", http.StatusOK,
    56  			jsonhttptest.WithExpectedJSONResponse(api.AddressesResponse{
    57  				Overlay:      &overlay,
    58  				Underlay:     addresses,
    59  				Ethereum:     ethereumAddress,
    60  				PublicKey:    hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&privateKey.PublicKey)),
    61  				PSSPublicKey: hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&pssPrivateKey.PublicKey)),
    62  			}),
    63  		)
    64  	})
    65  
    66  	jsonhttptest.Request(t, testServer, http.MethodGet, "/node", http.StatusOK,
    67  		jsonhttptest.WithExpectedJSONResponse(api.NodeResponse{
    68  			BeeMode:           api.FullMode.String(),
    69  			ChequebookEnabled: true,
    70  			SwapEnabled:       true,
    71  		}),
    72  	)
    73  }
    74  
    75  func TestAddresses_error(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	testErr := errors.New("test error")
    79  
    80  	testServer, _, _, _ := newTestServer(t, testServerOptions{
    81  		P2P: mock.New(mock.WithAddressesFunc(func() ([]multiaddr.Multiaddr, error) {
    82  			return nil, testErr
    83  		})),
    84  	})
    85  
    86  	jsonhttptest.Request(t, testServer, http.MethodGet, "/addresses", http.StatusInternalServerError,
    87  		jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
    88  			Code:    http.StatusInternalServerError,
    89  			Message: testErr.Error(),
    90  		}),
    91  	)
    92  }
    93  
    94  func mustMultiaddr(t *testing.T, s string) multiaddr.Multiaddr {
    95  	t.Helper()
    96  
    97  	a, err := multiaddr.NewMultiaddr(s)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	return a
   102  }