github.com/pion/dtls/v2@v2.2.12/pkg/protocol/extension/alpn_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package extension
     5  
     6  import (
     7  	"errors"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestALPN(t *testing.T) {
    13  	extension := ALPN{
    14  		ProtocolNameList: []string{"http/1.1", "spdy/1", "spdy/2", "spdy/3"},
    15  	}
    16  
    17  	raw, err := extension.Marshal()
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	newExtension := ALPN{}
    23  	err = newExtension.Unmarshal(raw)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	if !reflect.DeepEqual(newExtension.ProtocolNameList, extension.ProtocolNameList) {
    29  		t.Errorf("extensionALPN marshal: got %s expected %s", newExtension.ProtocolNameList, extension.ProtocolNameList)
    30  	}
    31  }
    32  
    33  func TestALPNProtocolSelection(t *testing.T) {
    34  	s, err := ALPNProtocolSelection([]string{"http/1.1", "spd/1"}, []string{"spd/1"})
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if s != "spd/1" {
    39  		t.Errorf("expected: spd/1, got: %v", s)
    40  	}
    41  	_, err = ALPNProtocolSelection([]string{"http/1.1"}, []string{"spd/1"})
    42  	if !errors.Is(err, errALPNNoAppProto) {
    43  		t.Fatal("expected to fail negotiating an application protocol")
    44  	}
    45  	s, err = ALPNProtocolSelection([]string{"http/1.1", "spd/1"}, []string{})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if s != "" {
    50  		t.Errorf("expected not to negotiate a protocol, got: %v", s)
    51  	}
    52  }