github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/meekConn_test.go (about) 1 /* 2 * Copyright (c) 2021, 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 psiphon 21 22 import ( 23 "context" 24 "io/ioutil" 25 "net/http" 26 "os" 27 "testing" 28 "time" 29 30 "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters" 31 ) 32 33 // MeekModeRelay and MeekModeObfuscatedRoundTrip are tested via meek protocol 34 // and tactics test cases. 35 36 func TestMeekModePlaintextRoundTrip(t *testing.T) { 37 38 testDataDirName, err := ioutil.TempDir("", "psiphon-meek-mode-plaintext-round-trip-test") 39 if err != nil { 40 t.Fatalf("TempDir failed: %v", err) 41 } 42 defer os.RemoveAll(testDataDirName) 43 44 serverName := "example.org" 45 46 rootCAsFileName, 47 rootCACertificatePin, 48 serverCertificatePin, 49 shutdown, 50 serverAddr, 51 dialer := initTestCertificatesAndWebServer( 52 t, testDataDirName, serverName) 53 defer shutdown() 54 55 params, err := parameters.NewParameters(nil) 56 if err != nil { 57 t.Fatalf("parameters.NewParameters failed: %v", err) 58 } 59 60 meekConfig := &MeekConfig{ 61 Parameters: params, 62 Mode: MeekModePlaintextRoundTrip, 63 DialAddress: serverAddr, 64 UseHTTPS: true, 65 SNIServerName: "not-" + serverName, 66 VerifyServerName: serverName, 67 VerifyPins: []string{rootCACertificatePin, serverCertificatePin}, 68 } 69 70 dialConfig := &DialConfig{ 71 TrustedCACertificatesFilename: rootCAsFileName, 72 CustomDialer: dialer, 73 } 74 75 ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second) 76 defer cancelFunc() 77 78 meekConn, err := DialMeek(ctx, meekConfig, dialConfig) 79 if err != nil { 80 t.Fatalf("DialMeek failed: %v", err) 81 } 82 83 client := &http.Client{ 84 Transport: meekConn, 85 } 86 87 response, err := client.Get("https://" + serverAddr + "/") 88 if err != nil { 89 t.Fatalf("http.Client.Get failed: %v", err) 90 } 91 response.Body.Close() 92 93 if response.StatusCode != http.StatusOK { 94 t.Fatalf("unexpected response code: %v", response.StatusCode) 95 } 96 97 err = meekConn.Close() 98 if err != nil { 99 t.Fatalf("MeekConn.Close failed: %v", err) 100 } 101 }