Unverified Commit 549c5571 authored by Philipp Winter's avatar Philipp Winter
Browse files

Show per-test execution times.

parent e99b21d5
package main
import (
"fmt"
"bufio"
"bytes"
"flag"
"io/ioutil"
"log"
"os"
"runtime"
"time"
)
const (
SuccessMessage = "everything as expected"
)
var buf bytes.Buffer
var l = log.New(&buf, "", 0)
func testDefaultBridges() {
fmt.Println("Testing TCP port of default bridges.")
l.Println("Testing default bridges:")
for _, addrTuple := range defaultBridges {
reachable, err := IsTCPPortReachable(addrTuple)
if reachable {
fmt.Printf("\treachable: %s\n", addrTuple)
} else {
fmt.Printf("\tunreachable: %s (%s)\n", addrTuple, err)
}
r := IsTCPPortReachable(addrTuple)
l.Printf(r.String())
}
}
func testDirAuths() {
fmt.Println("Testing TCP port of directory authorities.")
for _, addrTuple := range dirAuths {
reachable, err := IsTCPPortReachable(addrTuple)
if reachable {
fmt.Printf("\treachable: %s\n", addrTuple)
} else {
fmt.Printf("\tunreachable: %s (%s)\n", addrTuple, err)
}
func testWebsites() {
l.Print("Testing websites:")
for url, substring := range websites {
r := DoesWebsiteContainStr(url, substring)
l.Print(r.String())
}
}
func testDomains() {
fmt.Println("Testing *.torproject.org domains.")
var allGood = true
for domain, addrMap := range domains {
err := DoesDomainResolve(domain, addrMap)
if err != nil {
fmt.Printf("\tunexpected: %s (%s)\n", domain, err)
allGood = false
}
}
if allGood {
fmt.Printf("\t%s\n", SuccessMessage)
func testDirAuths() {
l.Println("Testing directory authorities.")
for _, addrTuple := range dirAuths {
r := IsTCPPortReachable(addrTuple)
l.Print(r.String())
}
}
func testWebsites() {
fmt.Println("Testing websites.")
var allGood = true
for url, substring := range websites {
success, err := DoesWebsiteContainStr(url, substring)
func main() {
log.Println("Starting analysis.")
var useStdout bool
flag.BoolVar(&useStdout, "stdout", false, "Write results to stdout instead of a file.")
flag.Parse()
var f *os.File
var err error
if useStdout {
f = os.Stdout
} else {
f, err = ioutil.TempFile("", "emma-")
if err != nil {
fmt.Println(err)
allGood = false
log.Fatal(err)
}
if !success {
fmt.Printf("\tno substring %q in url %q.\n", substring, url)
allGood = false
}
}
if allGood {
fmt.Printf("\t%s\n", SuccessMessage)
}
}
func main() {
l.Printf("Time: %s\n", time.Now())
testDefaultBridges()
testDirAuths()
testDomains()
testWebsites()
testDirAuths()
if _, err := f.WriteString(buf.String()); err != nil {
os.Remove(f.Name())
log.Fatalf("Failed writing output to %q because: %s", f.Name(), err)
} else {
log.Printf("Wrote output to: %s", f.Name())
}
if err = f.Close(); err != nil {
log.Fatal(err)
}
// On Windows, we don't want the terminal window to close after the tool is
// done; otherwise the user won't know where the output file is.
if runtime.GOOS == "windows" {
log.Printf("Press enter to exit.")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
}
......@@ -56,6 +56,7 @@ var domains = map[string]map[string]bool{
"2a01:4f8:fff0:4f:266:37ff:fef9:f825": true},
}
// Websites and a string that's meant to be in the website's body.
var websites = map[string]string{
"https://bridges.torproject.org": "The Tor Project",
"https://torproject.org": "Tor Browser",
......
package main
import (
"errors"
"fmt"
"io/ioutil"
"net"
......@@ -13,17 +14,42 @@ import (
// decide that the given destination is offline.
const timeout time.Duration = 3 * time.Second
type Result struct {
Target string
Error error
ExecutionTime time.Duration
}
func (r Result) String() string {
if r.Error == nil {
return fmt.Sprintf(" ✓ %-35s %6s\n", r.Target, r.ExecutionTime.Round(time.Millisecond))
} else {
return fmt.Sprintf(" ✗ %-35s %6s error: %v\n", r.Target, r.ExecutionTime.Round(time.Millisecond), r.Error)
}
}
func showExecutionTime(start time.Time, r *Result) {
r.ExecutionTime = time.Since(start)
}
// IsTCPPortReachable returns `true' if it can establish a TCP connection with
// the given IP address and port. If not, it returns `false' and the
// respective error, as reported by `net.DialTimeout'.
func IsTCPPortReachable(addrTuple string) (bool, error) {
func IsTCPPortReachable(addrTuple string) (r Result) {
defer func(s time.Time) {
r.ExecutionTime = time.Since(s)
}(time.Now())
r.Target = addrTuple
conn, err := net.DialTimeout("tcp", addrTuple, timeout)
if err != nil {
return false, err
r.Error = err
return
}
conn.Close()
return true, nil
r.Error = nil
return
}
func DoesDomainResolve(domain string, expected map[string]bool) error {
......@@ -34,24 +60,36 @@ func DoesDomainResolve(domain string, expected map[string]bool) error {
for _, addr := range addrs {
if !expected[addr] {
return fmt.Errorf("%s != %s", addrs, expected)
return fmt.Errorf("%q != %q", addrs, expected)
}
}
return nil
}
func DoesWebsiteContainStr(domain, substring string) (bool, error) {
func DoesWebsiteContainStr(domain, substring string) (r Result) {
defer func(s time.Time) {
r.ExecutionTime = time.Since(s)
}(time.Now())
r.Target = domain
resp, err := http.Get(domain)
if err != nil {
return false, err
r.Error = err
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
r.Error = err
return
}
return strings.Contains(string(body), substring), nil
if strings.Contains(string(body), substring) {
r.Error = nil
} else {
r.Error = errors.New("could not find expected string in HTML body")
}
return
}
Supports Markdown
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