| 1 | package tests |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | "os" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/cretz/bine/tor" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | ) |
| 13 | |
| 14 | var torEnabled bool |
| 15 | var torExePath string |
| 16 | var torVerbose bool |
| 17 | var torIncludeNetworkTests bool |
| 18 | var globalEnabledNetworkContext *TestContext |
| 19 | |
| 20 | func TestMain(m *testing.M) { |
| 21 | flag.BoolVar(&torEnabled, "tor", false, "Whether any of the integration tests are enabled") |
| 22 | flag.StringVar(&torExePath, "tor.path", "tor", "The Tor exe path") |
| 23 | flag.BoolVar(&torVerbose, "tor.verbose", false, "Show verbose test info") |
| 24 | flag.BoolVar(&torIncludeNetworkTests, "tor.network", false, "Include network tests") |
| 25 | flag.Parse() |
| 26 | exitCode := m.Run() |
| 27 | if globalEnabledNetworkContext != nil { |
| 28 | globalEnabledNetworkContext.CloseTorOnClose = true |
| 29 | globalEnabledNetworkContext.Close() |
| 30 | } |
| 31 | os.Exit(exitCode) |
| 32 | } |
| 33 | |
| 34 | func GlobalEnabledNetworkContext(t *testing.T) *TestContext { |
| 35 | if !torEnabled || !torIncludeNetworkTests { |
| 36 | t.Skip("Only runs if -tor and -tor.network are set") |
| 37 | } |
| 38 | if globalEnabledNetworkContext == nil { |
| 39 | ctx := NewTestContext(t, nil) |
| 40 | ctx.CloseTorOnClose = false |
| 41 | // 45 second wait for enable network |
| 42 | enableCtx, enableCancel := context.WithTimeout(ctx, 45*time.Second) |
| 43 | defer enableCancel() |
| 44 | ctx.Require.NoError(ctx.EnableNetwork(enableCtx, true)) |
| 45 | globalEnabledNetworkContext = ctx |
| 46 | } else { |
| 47 | globalEnabledNetworkContext.T = t |
| 48 | globalEnabledNetworkContext.Require = require.New(t) |
| 49 | } |
| 50 | return globalEnabledNetworkContext |
| 51 | } |
| 52 | |
| 53 | type TestContext struct { |
| 54 | context.Context |
| 55 | *testing.T |
| 56 | *tor.Tor |
| 57 | Require *require.Assertions |
| 58 | CloseTorOnClose bool |
| 59 | } |
| 60 | |
| 61 | func NewTestContext(t *testing.T, conf *tor.StartConf) *TestContext { |
| 62 | if !torEnabled { |
| 63 | t.Skip("Only runs if -tor is set") |
| 64 | } |
| 65 | // Build start conf |
| 66 | if conf == nil { |
| 67 | conf = &tor.StartConf{} |
| 68 | } |
| 69 | conf.ExePath = torExePath |
| 70 | if torVerbose { |
| 71 | conf.DebugWriter = os.Stdout |
| 72 | conf.NoHush = true |
| 73 | } else { |
| 74 | conf.ExtraArgs = append(conf.ExtraArgs, "--quiet") |
| 75 | } |
| 76 | ret := &TestContext{Context: context.Background(), T: t, Require: require.New(t), CloseTorOnClose: true} |
| 77 | // Start tor |
| 78 | var err error |
| 79 | if ret.Tor, err = tor.Start(ret.Context, conf); err != nil { |
| 80 | defer ret.Close() |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | return ret |
| 84 | } |
| 85 | |
| 86 | // Deadline to disambiguate context and test deadline and use test one. |
| 87 | func (t *TestContext) Deadline() (time.Time, bool) { return t.T.Deadline() } |
| 88 | |
| 89 | func (t *TestContext) Close() { |
| 90 | if t.CloseTorOnClose { |
| 91 | if err := t.Tor.Close(); err != nil { |
| 92 | if t.Failed() { |
| 93 | t.Logf("Failure on close: %v", err) |
| 94 | } else { |
| 95 | t.Errorf("Failure on close: %v", err) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |