protocol_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. err := SupportedTLSProfiles.Validate()
  48. if err != nil {
  49. t.Errorf("unexpected Validate error: %s", err)
  50. }
  51. invalidProfiles := TLSProfiles{"OSSH", "INVALID-PROTOCOL"}
  52. err = invalidProfiles.Validate()
  53. if err == nil {
  54. t.Errorf("unexpected Validate success")
  55. }
  56. pruneProfiles := make(TLSProfiles, 0)
  57. for i, p := range SupportedTLSProfiles {
  58. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", i))
  59. pruneProfiles = append(pruneProfiles, p)
  60. }
  61. pruneProfiles = append(pruneProfiles, fmt.Sprintf("INVALID-PROFILE-%d", len(SupportedTLSProfiles)))
  62. prunedProfiles := pruneProfiles.PruneInvalid()
  63. if !reflect.DeepEqual(prunedProfiles, SupportedTLSProfiles) {
  64. t.Errorf("unexpected %+v != %+v", prunedProfiles, SupportedTLSProfiles)
  65. }
  66. }