protocol_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package protocol
  20. import (
  21. "fmt"
  22. "reflect"
  23. "testing"
  24. )
  25. func TestTunnelProtocolValidation(t *testing.T) {
  26. err := SupportedTunnelProtocols.Validate()
  27. if err != nil {
  28. t.Errorf("unexpected Validate error: %s", err)
  29. }
  30. invalidProtocols := TunnelProtocols{"OSSH", "INVALID-PROTOCOL"}
  31. err = invalidProtocols.Validate()
  32. if err == nil {
  33. t.Errorf("unexpected Validate success")
  34. }
  35. pruneProtocols := make(TunnelProtocols, 0)
  36. for i, p := range SupportedTunnelProtocols {
  37. pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", i))
  38. pruneProtocols = append(pruneProtocols, p)
  39. }
  40. pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", len(SupportedTunnelProtocols)))
  41. prunedProtocols := pruneProtocols.PruneInvalid()
  42. if !reflect.DeepEqual(prunedProtocols, SupportedTunnelProtocols) {
  43. t.Errorf("unexpected %+v != %+v", prunedProtocols, SupportedTunnelProtocols)
  44. }
  45. }
  46. func TestTLSProfileValidation(t *testing.T) {
  47. // Test: valid profiles
  48. err := SupportedTLSProfiles.Validate(nil)
  49. if err != nil {
  50. t.Errorf("unexpected Validate error: %s", err)
  51. }
  52. // Test: invalid profile
  53. profiles := TLSProfiles{TLS_PROFILE_RANDOMIZED, "INVALID-TLS-PROFILE"}
  54. err = profiles.Validate(nil)
  55. if err == nil {
  56. t.Errorf("unexpected Validate success")
  57. }
  58. // Test: valid custom profile
  59. customProfiles := []string{"CUSTOM-TLS-PROFILE"}
  60. profiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
  61. err = profiles.Validate(customProfiles)
  62. if err != nil {
  63. t.Errorf("unexpected Validate error: %s", err)
  64. }
  65. // Test: prune invalid profiles
  66. pruneProfiles := make(TLSProfiles, 0)
  67. for i, p := range SupportedTLSProfiles {
  68. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", i))
  69. pruneProfiles = append(pruneProfiles, p)
  70. }
  71. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", len(SupportedTLSProfiles)))
  72. prunedProfiles := pruneProfiles.PruneInvalid(nil)
  73. if !reflect.DeepEqual(prunedProfiles, SupportedTLSProfiles) {
  74. t.Errorf("unexpected %+v != %+v", prunedProfiles, SupportedTLSProfiles)
  75. }
  76. // Test: don't prune valid custom profiles
  77. pruneProfiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
  78. prunedProfiles = pruneProfiles.PruneInvalid(customProfiles)
  79. if !reflect.DeepEqual(prunedProfiles, pruneProfiles) {
  80. t.Errorf("unexpected %+v != %+v", prunedProfiles, pruneProfiles)
  81. }
  82. }