github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/obfuscator/passthrough_test.go (about) 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 20 package obfuscator 21 22 import ( 23 "bytes" 24 "fmt" 25 "testing" 26 "time" 27 ) 28 29 func TestTLSPassthrough(t *testing.T) { 30 31 // Use artificially low time factor period for test 32 timePeriodSeconds = 2 33 34 correctMasterKey := "correct-master-key" 35 incorrectMasterKey := "incorrect-master-key" 36 37 for _, useTimeFactor := range []bool{false, true} { 38 39 t.Run(fmt.Sprintf("useTimeFactor: %v", useTimeFactor), func(t *testing.T) { 40 41 // test: valid passthrough message 42 43 validMessage, err := MakeTLSPassthroughMessage(useTimeFactor, correctMasterKey) 44 if err != nil { 45 t.Fatalf("MakeTLSPassthroughMessage failed: %s", err) 46 } 47 48 startTime := time.Now() 49 50 if !VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage) { 51 t.Fatalf("unexpected invalid passthrough message") 52 } 53 54 correctElapsedTime := time.Now().Sub(startTime) 55 56 // test: passthrough messages are not identical 57 58 anotherValidMessage, err := MakeTLSPassthroughMessage(useTimeFactor, correctMasterKey) 59 if err != nil { 60 t.Fatalf("MakeTLSPassthroughMessage failed: %s", err) 61 } 62 63 if bytes.Equal(validMessage, anotherValidMessage) { 64 t.Fatalf("unexpected identical passthrough messages") 65 } 66 67 // test: valid passthrough message still valid within time factor period 68 69 time.Sleep(1 * time.Millisecond) 70 71 if !VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage) { 72 t.Fatalf("unexpected invalid delayed passthrough message") 73 } 74 75 // test: valid passthrough message now invalid after time factor period 76 77 time.Sleep(time.Duration(timePeriodSeconds)*time.Second + time.Millisecond) 78 79 verified := VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, validMessage) 80 81 if verified && useTimeFactor { 82 t.Fatalf("unexpected replayed passthrough message") 83 } 84 85 // test: invalid passthrough message with incorrect key 86 87 invalidMessage, err := MakeTLSPassthroughMessage(useTimeFactor, incorrectMasterKey) 88 if err != nil { 89 t.Fatalf("MakeTLSPassthroughMessage failed: %s", err) 90 } 91 92 startTime = time.Now() 93 94 if VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, invalidMessage) { 95 t.Fatalf("unexpected valid passthrough message") 96 } 97 98 incorrectElapsedTime := time.Now().Sub(startTime) 99 100 // test: valid/invalid elapsed times are nearly identical 101 102 timeDiff := correctElapsedTime - incorrectElapsedTime 103 if timeDiff < 0 { 104 timeDiff = -timeDiff 105 } 106 107 if timeDiff.Microseconds() > 500 { 108 t.Fatalf("unexpected elapsed time difference: %v", timeDiff) 109 } 110 111 // test: invalid message length and elapsed time 112 113 startTime = time.Now() 114 115 if VerifyTLSPassthroughMessage(useTimeFactor, correctMasterKey, invalidMessage[:16]) { 116 t.Fatalf("unexpected valid passthrough message with invalid length") 117 } 118 119 incorrectElapsedTime = time.Now().Sub(startTime) 120 121 timeDiff = correctElapsedTime - incorrectElapsedTime 122 if timeDiff < 0 { 123 timeDiff = -timeDiff 124 } 125 126 if timeDiff.Microseconds() > 100 { 127 t.Fatalf("unexpected elapsed time difference") 128 } 129 }) 130 } 131 }