passthrough_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020, 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 obfuscator
  20. import (
  21. "bytes"
  22. "fmt"
  23. "testing"
  24. "time"
  25. )
  26. func TestTLSPassthrough(t *testing.T) {
  27. // Use artificially low time factor period for test
  28. timePeriodSeconds = 2
  29. correctMasterKey := "correct-master-key"
  30. incorrectMasterKey := "incorrect-master-key"
  31. for _, useTimeFactor := range []bool{false, true} {
  32. t.Run(fmt.Sprintf("useTimeFactor: %v", useTimeFactor), func(t *testing.T) {
  33. // test: valid passthrough message
  34. validMessage, err := MakeTLSPassthroughMessage(useTimeFactor, correctMasterKey)
  35. if err != nil {
  36. t.Fatalf("MakeTLSPassthroughMessage failed: %s", err)
  37. }
  38. startTime := time.Now()
  39. if !VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage) {
  40. t.Fatalf("unexpected invalid passthrough message")
  41. }
  42. correctElapsedTime := time.Now().Sub(startTime)
  43. // test: passthrough messages are not identical
  44. anotherValidMessage, err := MakeTLSPassthroughMessage(useTimeFactor, correctMasterKey)
  45. if err != nil {
  46. t.Fatalf("MakeTLSPassthroughMessage failed: %s", err)
  47. }
  48. if bytes.Equal(validMessage, anotherValidMessage) {
  49. t.Fatalf("unexpected identical passthrough messages")
  50. }
  51. // test: valid passthrough message still valid within time factor period
  52. time.Sleep(1 * time.Millisecond)
  53. if !VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage) {
  54. t.Fatalf("unexpected invalid delayed passthrough message")
  55. }
  56. // test: valid passthrough message now invalid after time factor period
  57. time.Sleep(time.Duration(timePeriodSeconds)*time.Second + time.Millisecond)
  58. verified := VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage)
  59. if verified && useTimeFactor {
  60. t.Fatalf("unexpected replayed passthrough message")
  61. }
  62. // test: invalid passthrough message with incorrect key
  63. invalidMessage, err := MakeTLSPassthroughMessage(useTimeFactor, incorrectMasterKey)
  64. if err != nil {
  65. t.Fatalf("MakeTLSPassthroughMessage failed: %s", err)
  66. }
  67. startTime = time.Now()
  68. if VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, invalidMessage) {
  69. t.Fatalf("unexpected valid passthrough message")
  70. }
  71. incorrectElapsedTime := time.Now().Sub(startTime)
  72. // test: valid/invalid elapsed times are nearly identical
  73. timeDiff := correctElapsedTime - incorrectElapsedTime
  74. if timeDiff < 0 {
  75. timeDiff = -timeDiff
  76. }
  77. if timeDiff.Microseconds() > 500 {
  78. t.Fatalf("unexpected elapsed time difference: %v", timeDiff)
  79. }
  80. // test: invalid message length and elapsed time
  81. startTime = time.Now()
  82. if VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, invalidMessage[:16]) {
  83. t.Fatalf("unexpected valid passthrough message with invalid length")
  84. }
  85. incorrectElapsedTime = time.Now().Sub(startTime)
  86. timeDiff = correctElapsedTime - incorrectElapsedTime
  87. if timeDiff < 0 {
  88. timeDiff = -timeDiff
  89. }
  90. if timeDiff.Microseconds() > 100 {
  91. t.Fatalf("unexpected elapsed time difference")
  92. }
  93. })
  94. }
  95. }