GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Add more unit tests.

This patch bumps our code coverage to 48.6%.
parent 69592d78
...@@ -33,6 +33,19 @@ func TestCacheFunctions(t *testing.T) { ...@@ -33,6 +33,19 @@ func TestCacheFunctions(t *testing.T) {
if e.Error != testError.Error() { if e.Error != testError.Error() {
t.Errorf("Got test result %q but expected %q.", e.Error, testError) 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) { func TestCacheExpiration(t *testing.T) {
...@@ -115,6 +128,14 @@ func TestCacheSerialisation(t *testing.T) { ...@@ -115,6 +128,14 @@ func TestCacheSerialisation(t *testing.T) {
if e1.Error != testError.Error() { if e1.Error != testError.Error() {
t.Errorf("Error string expected to be %q but is %q.", testError, e1.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) { func TestCacheConcurrency(t *testing.T) {
......
...@@ -45,3 +45,35 @@ func TestGetBridgeIdentifier(t *testing.T) { ...@@ -45,3 +45,35 @@ func TestGetBridgeIdentifier(t *testing.T) {
t.Errorf("failed to extract bridge identifier") 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)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment