protocol_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  25. )
  26. func TestTunnelProtocolValidation(t *testing.T) {
  27. validSupportedProtocols := make(TunnelProtocols, 0)
  28. for _, p := range SupportedTunnelProtocols {
  29. if common.Contains(DisabledTunnelProtocols, p) {
  30. continue
  31. }
  32. validSupportedProtocols = append(validSupportedProtocols, p)
  33. }
  34. if len(validSupportedProtocols) == len(SupportedTunnelProtocols) {
  35. err := SupportedTunnelProtocols.Validate()
  36. if err != nil {
  37. t.Errorf("unexpected Validate error: %s", err)
  38. }
  39. } else {
  40. err := SupportedTunnelProtocols.Validate()
  41. if err == nil {
  42. t.Errorf("unexpected Validate success")
  43. }
  44. err = validSupportedProtocols.Validate()
  45. if err != nil {
  46. t.Errorf("unexpected Validate error: %s", err)
  47. }
  48. }
  49. invalidProtocols := TunnelProtocols{"OSSH", "INVALID-PROTOCOL"}
  50. err := invalidProtocols.Validate()
  51. if err == nil {
  52. t.Errorf("unexpected Validate success")
  53. }
  54. pruneProtocols := make(TunnelProtocols, 0)
  55. for i, p := range SupportedTunnelProtocols {
  56. pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", i))
  57. pruneProtocols = append(pruneProtocols, p)
  58. }
  59. pruneProtocols = append(pruneProtocols, fmt.Sprintf("INVALID-PROTOCOL-%d", len(SupportedTunnelProtocols)))
  60. prunedProtocols := pruneProtocols.PruneInvalid()
  61. if !reflect.DeepEqual(prunedProtocols, validSupportedProtocols) {
  62. t.Errorf("unexpected %+v != %+v", prunedProtocols, validSupportedProtocols)
  63. }
  64. }
  65. func TestTLSProfileValidation(t *testing.T) {
  66. // Test: valid profiles
  67. err := SupportedTLSProfiles.Validate(nil)
  68. if err != nil {
  69. t.Errorf("unexpected Validate error: %s", err)
  70. }
  71. // Test: invalid profile
  72. profiles := TLSProfiles{TLS_PROFILE_RANDOMIZED, "INVALID-TLS-PROFILE"}
  73. err = profiles.Validate(nil)
  74. if err == nil {
  75. t.Errorf("unexpected Validate success")
  76. }
  77. // Test: valid custom profile
  78. customProfiles := []string{"CUSTOM-TLS-PROFILE"}
  79. profiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
  80. err = profiles.Validate(customProfiles)
  81. if err != nil {
  82. t.Errorf("unexpected Validate error: %s", err)
  83. }
  84. // Test: prune invalid profiles
  85. pruneProfiles := make(TLSProfiles, 0)
  86. for i, p := range SupportedTLSProfiles {
  87. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", i))
  88. pruneProfiles = append(pruneProfiles, p)
  89. }
  90. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", len(SupportedTLSProfiles)))
  91. prunedProfiles := pruneProfiles.PruneInvalid(nil)
  92. if !reflect.DeepEqual(prunedProfiles, SupportedTLSProfiles) {
  93. t.Errorf("unexpected %+v != %+v", prunedProfiles, SupportedTLSProfiles)
  94. }
  95. // Test: don't prune valid custom profiles
  96. pruneProfiles = TLSProfiles{TLS_PROFILE_RANDOMIZED, "CUSTOM-TLS-PROFILE"}
  97. prunedProfiles = pruneProfiles.PruneInvalid(customProfiles)
  98. if !reflect.DeepEqual(prunedProfiles, pruneProfiles) {
  99. t.Errorf("unexpected %+v != %+v", prunedProfiles, pruneProfiles)
  100. }
  101. }