github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/protocol/protocol_test.go (about)

     1  /*
     2   * Copyright (c) 2018, Psiphon Inc.
     3   * All rights reserved.
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package protocol
    21  
    22  import (
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  )
    27  
    28  func TestTunnelProtocolValidation(t *testing.T) {
    29  
    30  	err := SupportedTunnelProtocols.Validate()
    31  	if err != nil {
    32  		t.Errorf("unexpected Validate error: %s", err)
    33  	}
    34  
    35  	invalidProtocols := TunnelProtocols{"OSSH", "INVALID-PROTOCOL"}
    36  	err = invalidProtocols.Validate()
    37  	if err == nil {
    38  		t.Errorf("unexpected Validate success")
    39  	}
    40  
    41  	pruneProtocols := make(TunnelProtocols, 0)
    42  	for i, p := range SupportedTunnelProtocols {
    43  		pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", i))
    44  		pruneProtocols = append(pruneProtocols, p)
    45  	}
    46  	pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", len(SupportedTunnelProtocols)))
    47  
    48  	prunedProtocols := pruneProtocols.PruneInvalid()
    49  
    50  	if !reflect.DeepEqual(prunedProtocols, SupportedTunnelProtocols) {
    51  		t.Errorf("unexpected %+v != %+v", prunedProtocols, SupportedTunnelProtocols)
    52  	}
    53  }
    54  
    55  func TestTLSProfileValidation(t *testing.T) {
    56  
    57  	// Test: valid profiles
    58  
    59  	err := SupportedTLSProfiles.Validate(nil)
    60  	if err != nil {
    61  		t.Errorf("unexpected Validate error: %s", err)
    62  	}
    63  
    64  	// Test: invalid profile
    65  
    66  	profiles := TLSProfiles{TLS_PROFILE_RANDOMIZED, "INVALID-TLS-PROFILE"}
    67  	err = profiles.Validate(nil)
    68  	if err == nil {
    69  		t.Errorf("unexpected Validate success")
    70  	}
    71  
    72  	// Test: valid custom profile
    73  
    74  	customProfiles := []string{"CUSTOM-TLS-PROFILE"}
    75  
    76  	profiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
    77  	err = profiles.Validate(customProfiles)
    78  	if err != nil {
    79  		t.Errorf("unexpected Validate error: %s", err)
    80  	}
    81  
    82  	// Test: prune invalid profiles
    83  
    84  	pruneProfiles := make(TLSProfiles, 0)
    85  	for i, p := range SupportedTLSProfiles {
    86  		pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", i))
    87  		pruneProfiles = append(pruneProfiles, p)
    88  	}
    89  	pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", len(SupportedTLSProfiles)))
    90  
    91  	prunedProfiles := pruneProfiles.PruneInvalid(nil)
    92  
    93  	if !reflect.DeepEqual(prunedProfiles, SupportedTLSProfiles) {
    94  		t.Errorf("unexpected %+v != %+v", prunedProfiles, SupportedTLSProfiles)
    95  	}
    96  
    97  	// Test: don't prune valid custom profiles
    98  
    99  	pruneProfiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
   100  
   101  	prunedProfiles = pruneProfiles.PruneInvalid(customProfiles)
   102  
   103  	if !reflect.DeepEqual(prunedProfiles, pruneProfiles) {
   104  		t.Errorf("unexpected %+v != %+v", prunedProfiles, pruneProfiles)
   105  	}
   106  }