github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/env_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "github.com/stretchr/testify/require" 8 "os" 9 "path/filepath" 10 "testing" 11 ) 12 13 func TestEnvDarwin(t *testing.T) { 14 env := newEnv(nil, nil, "darwin", makeLogGetter(t)) 15 16 sockFile, err := env.GetSocketBindFile() 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 cacheDir := env.GetSandboxCacheDir() 22 expectedSockFile := filepath.Join(cacheDir, "keybased.sock") 23 if sockFile != expectedSockFile { 24 t.Fatalf("Clients expect sock file to be %s", expectedSockFile) 25 } 26 } 27 28 type MockedConfigReader struct { 29 NullConfiguration 30 } 31 32 var globalTorMode = TorNone 33 34 func (nc MockedConfigReader) GetTorMode() (TorMode, error) { 35 return globalTorMode, nil 36 } 37 38 var globalProxyType = "" 39 40 func (nc MockedConfigReader) GetProxyType() string { 41 return globalProxyType 42 } 43 44 var globalProxyAddress = "" 45 46 func (nc MockedConfigReader) GetProxy() string { 47 return globalProxyAddress 48 } 49 50 var globalIsCertPinningEnabled = true 51 52 func (nc MockedConfigReader) IsCertPinningEnabled() bool { 53 return globalIsCertPinningEnabled 54 } 55 56 func resetGlobals() { 57 globalTorMode = TorNone 58 globalProxyType = "" 59 globalProxyAddress = "" 60 globalIsCertPinningEnabled = true 61 } 62 63 func TestTorMode(t *testing.T) { 64 resetGlobals() 65 66 mockedEnv := NewEnv(MockedConfigReader{}, MockedConfigReader{}, makeLogGetter(t)) 67 68 // Test that when tor mode is enabled, a Socks proxy is properly configured 69 require.Equal(t, NoProxy, mockedEnv.GetProxyType()) 70 require.Equal(t, "", mockedEnv.GetProxy()) 71 72 globalTorMode = TorLeaky 73 require.Equal(t, Socks, mockedEnv.GetProxyType()) 74 require.Equal(t, "localhost:9050", mockedEnv.GetProxy()) 75 76 globalTorMode = TorStrict 77 require.Equal(t, Socks, mockedEnv.GetProxyType()) 78 require.Equal(t, "localhost:9050", mockedEnv.GetProxy()) 79 80 // Test that tor mode overrides proxy settings 81 globalProxyType = "http" 82 globalProxyAddress = "localhost:8080" 83 require.Equal(t, Socks, mockedEnv.GetProxyType()) 84 require.Equal(t, "localhost:9050", mockedEnv.GetProxy()) 85 } 86 87 func TestGetProxyType(t *testing.T) { 88 resetGlobals() 89 90 defaultEnv := NewEnv(nil, nil, makeLogGetter(t)) 91 require.Equal(t, NoProxy, defaultEnv.GetProxyType()) 92 93 mockedEnv := NewEnv(MockedConfigReader{}, MockedConfigReader{}, makeLogGetter(t)) 94 require.Equal(t, NoProxy, mockedEnv.GetProxyType()) 95 96 globalProxyType = "Socks" 97 require.Equal(t, Socks, mockedEnv.GetProxyType()) 98 globalProxyType = "SOCKS" 99 require.Equal(t, Socks, mockedEnv.GetProxyType()) 100 globalProxyType = "SoCkS" 101 require.Equal(t, Socks, mockedEnv.GetProxyType()) 102 103 globalProxyType = "http_connect" 104 require.Equal(t, HTTPConnect, mockedEnv.GetProxyType()) 105 globalProxyType = "HTTP_CONNECT" 106 require.Equal(t, HTTPConnect, mockedEnv.GetProxyType()) 107 108 globalProxyType = "BOGUS" 109 require.Equal(t, NoProxy, mockedEnv.GetProxyType()) 110 111 resetGlobals() 112 require.Equal(t, NoProxy, mockedEnv.GetProxyType()) 113 114 orig := os.Getenv("PROXY_TYPE") 115 116 os.Setenv("PROXY_TYPE", "socks") 117 require.Equal(t, Socks, mockedEnv.GetProxyType()) 118 os.Setenv("PROXY_TYPE", "http_connect") 119 require.Equal(t, HTTPConnect, mockedEnv.GetProxyType()) 120 121 os.Setenv("PROXY_TYPE", orig) 122 } 123 124 func TestGetProxy(t *testing.T) { 125 resetGlobals() 126 127 defaultEnv := NewEnv(nil, nil, makeLogGetter(t)) 128 require.Equal(t, "", defaultEnv.GetProxy()) 129 130 mockedEnv := NewEnv(MockedConfigReader{}, MockedConfigReader{}, makeLogGetter(t)) 131 require.Equal(t, "", mockedEnv.GetProxy()) 132 133 globalProxyAddress = "localhost:8090" 134 require.Equal(t, "localhost:8090", mockedEnv.GetProxy()) 135 136 resetGlobals() 137 require.Equal(t, "", defaultEnv.GetProxy()) 138 139 orig := os.Getenv("PROXY") 140 os.Setenv("PROXY", "localhost:8080") 141 require.Equal(t, "localhost:8080", mockedEnv.GetProxy()) 142 os.Setenv("PROXY", orig) 143 144 orig = os.Getenv("HTTP_PROXY") 145 os.Setenv("HTTP_PROXY", "localhost:8081") 146 require.Equal(t, "localhost:8081", mockedEnv.GetProxy()) 147 os.Setenv("HTTP_PROXY", orig) 148 149 orig = os.Getenv("HTTPS_PROXY") 150 os.Setenv("HTTPS_PROXY", "localhost:8082") 151 require.Equal(t, "localhost:8082", mockedEnv.GetProxy()) 152 os.Setenv("HTTPS_PROXY", orig) 153 } 154 155 func TestIsCertPinningEnabled(t *testing.T) { 156 resetGlobals() 157 158 defaultEnv := NewEnv(nil, nil, makeLogGetter(t)) 159 require.Equal(t, true, defaultEnv.IsCertPinningEnabled()) 160 161 mockedEnv := NewEnv(MockedConfigReader{}, MockedConfigReader{}, makeLogGetter(t)) 162 require.Equal(t, true, mockedEnv.IsCertPinningEnabled()) 163 164 globalIsCertPinningEnabled = false 165 require.Equal(t, false, mockedEnv.IsCertPinningEnabled()) 166 167 globalIsCertPinningEnabled = true 168 require.Equal(t, true, mockedEnv.IsCertPinningEnabled()) 169 170 orig := os.Getenv("DISABLE_SSL_PINNING") 171 os.Setenv("DISABLE_SSL_PINNING", "true") 172 require.Equal(t, false, mockedEnv.IsCertPinningEnabled()) 173 os.Setenv("DISABLE_SSL_PINNING", "no") 174 require.Equal(t, true, mockedEnv.IsCertPinningEnabled()) 175 os.Setenv("DISABLE_SSL_PINNING", orig) 176 require.Equal(t, true, mockedEnv.IsCertPinningEnabled()) 177 }