diff --git a/cache_test.go b/cache_test.go index 5fad2d62a3ba9a44b6aac72e1a76d8dfadc53da1..33f20dd1873704abbcb17cd6665f3ae6bd33d5a6 100644 --- a/cache_test.go +++ b/cache_test.go @@ -33,6 +33,19 @@ func TestCacheFunctions(t *testing.T) { if e.Error != testError.Error() { t.Errorf("Got test result %q but expected %q.", e.Error, testError) } + + // A bogus bridge line shouldn't make it into the cache. + cache = make(TestCache) + bogusBridgeLine := "bogus-bridge-line" + cache.AddEntry(bogusBridgeLine, errors.New("bogus-error"), time.Now().UTC()) + if len(cache) != 0 { + t.Errorf("Bogus bridge line made it into cache.") + } + + e = cache.IsCached(bogusBridgeLine) + if e != nil { + t.Errorf("Got non-nil cache entry for bogus bridge line.") + } } func TestCacheExpiration(t *testing.T) { @@ -115,6 +128,14 @@ func TestCacheSerialisation(t *testing.T) { if e1.Error != testError.Error() { t.Errorf("Error string expected to be %q but is %q.", testError, e1.Error) } + + // Test errors when reading/writing bogus files. + if err = cache.ReadFromDisk("/f/o/o/b/a/r"); err == nil { + t.Errorf("Failed to return error when reading bogus file.") + } + if err = cache.WriteToDisk("/f/o/o/b/a/r"); err == nil { + t.Errorf("Failed to return error when writing bogus file.") + } } func TestCacheConcurrency(t *testing.T) { diff --git a/tor_test.go b/tor_test.go index d43c3c0e7ca47c0bc0176251364a217dd044a583..4a02cfc7b857c21cf8bb6e278e6f9b1fed04499d 100644 --- a/tor_test.go +++ b/tor_test.go @@ -45,3 +45,35 @@ func TestGetBridgeIdentifier(t *testing.T) { t.Errorf("failed to extract bridge identifier") } } + +func TestBridgeTest(t *testing.T) { + + // Taken from: + // https://gitlab.torproject.org/tpo/anti-censorship/team/-/wikis/Default-Bridges + defaultBridge1 := "obfs4 192.95.36.142:443 cert=qUVQ0srL1JI/vO6V6m/24anYXiJD3QP2HgzUKQtQ7GRqqUvs7P+tG43RtAqdhLOALP7DJQ iat-mode=1" + defaultBridge2 := "obfs4 193.11.166.194:27015 2D82C2E354D531A68469ADF7F878FA6060C6BACA cert=4TLQPJrTSaDffMK7Nbao6LC7G9OW/NHkUwIdjLSS3KYf0Nv4/nQiiI8dY2TcsQx01NniOg iat-mode=0" + bogusBridge := "127.0.0.1:1" + + torCtx = &TorContext{TorBinary: "tor"} + if err := torCtx.Start(); err != nil { + t.Fatalf("Failed to start tor: %s", err) + } + + result := torCtx.TestBridgeLines([]string{defaultBridge1, defaultBridge2, bogusBridge}) + r, _ := result.Bridges[defaultBridge1] + if !r.Functional { + t.Errorf("Default bridge #1 deemed non-functional.") + } + r, _ = result.Bridges[defaultBridge2] + if !r.Functional { + t.Errorf("Default bridge #2 deemed non-functional.") + } + r, _ = result.Bridges[bogusBridge] + if r.Functional { + t.Errorf("Bogus bridge deemed functional.") + } + + if err := torCtx.Stop(); err != nil { + t.Fatalf("Failed to stop tor: %s", err) + } +}