github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/welcome_message_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  package libp2p_test
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/p2p/libp2p"
    11  	"github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/handshake"
    12  )
    13  
    14  func TestDynamicWelcomeMessage(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	const TestWelcomeMessage = "Hello World!"
    18  
    19  	t.Run("Get current message - OK", func(t *testing.T) {
    20  		t.Parallel()
    21  		svc, _ := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{WelcomeMessage: TestWelcomeMessage}})
    22  		got := svc.GetWelcomeMessage()
    23  		if got != TestWelcomeMessage {
    24  			t.Fatalf("expected %s, got %s", TestWelcomeMessage, got)
    25  		}
    26  	})
    27  
    28  	t.Run("Set new message", func(t *testing.T) {
    29  		t.Run("OK", func(t *testing.T) {
    30  			t.Parallel()
    31  
    32  			svc, _ := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{WelcomeMessage: TestWelcomeMessage}})
    33  			const testMessage = "I'm the new message!"
    34  
    35  			err := svc.SetWelcomeMessage(testMessage)
    36  			if err != nil {
    37  				t.Fatal("got error:", err)
    38  			}
    39  			got := svc.GetWelcomeMessage()
    40  			if got != testMessage {
    41  				t.Fatalf("expected: %s. got %s", testMessage, got)
    42  			}
    43  		})
    44  		t.Run("error - message too long", func(t *testing.T) {
    45  			t.Parallel()
    46  
    47  			svc, _ := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{WelcomeMessage: TestWelcomeMessage}})
    48  			const testMessage = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    49  			Maecenas eu aliquam enim. Nulla tincidunt arcu nec nulla condimentum nullam sodales` // 141 characters
    50  
    51  			want := handshake.ErrWelcomeMessageLength
    52  			got := svc.SetWelcomeMessage(testMessage)
    53  			if !errors.Is(got, want) {
    54  				t.Fatalf("wrong error: want %v, got %v", want, got)
    55  			}
    56  		})
    57  
    58  	})
    59  }