func TestReadAnnotation(t *testing.T) {
	var expected = "12345678\n"
	var goodInput = "@type test 1.0\n" + expected
	var badInput = "bad first line\nmore data\n"
	_, r, err := readAnnotation(bytes.NewBufferString(goodInput))
	if err != nil {
		t.Fatalf("%q resulted in an error: %s", goodInput, err)
	}
	body, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("error reading after type annotation: %s", err)
	}
	if !bytes.Equal(body, []byte(expected)) {
		t.Errorf("got %q, expected %q", body, expected)
	}
	_, _, err = readAnnotation(bytes.NewBufferString(badInput))
	if err == nil {
		t.Errorf("%q resulted in no error", badInput)
	}
}
Test the function readAnnotation().
package zoossh
import (
	"bytes"
	"io/ioutil"
	"os"
	"testing"
	"time"
)
const (
	serverDescriptorDir  = "testdata/collector-descriptors/"
	serverDescriptorFile = "testdata/server-descriptors"
	consensusFile        = "testdata/consensus"
	sharedRandConsensusFile = "testdata/2017-04-15-00-00-00-consensus"
)
func BenchmarkDescriptorLookup(b *testing.B) {
	digest := "88827c73d5fd35e9638f820c44187ccdf8403b0f"
	date := time.Date(2014, time.December, 10, 0, 0, 0, 0, time.UTC)
	if _, err := os.Stat(serverDescriptorDir); os.IsNotExist(err) {
		b.Skipf("skipping because of missing %s", serverDescriptorDir)
	}
	for i := 0; i < b.N; i++ {
		if _, err := LoadDescriptorFromDigest(serverDescriptorDir, digest, date); err != nil {
			b.Fatal(err)
		}
	}
}
func TestBase64ToString(t *testing.T) {
	dec, err := Base64ToString("OVSyFvUCAKNSYpz8ZPArMLqf0Ds=")
	if err != nil {
		t.Error("Failed to decode Base64.")
	}
	if dec != "3954b216f50200a352629cfc64f02b30ba9fd03b" {
		t.Error("Base64 chunk decoded incorrectly.")
	}
	dec, err = Base64ToString("OVSyFvUCAKNSYpz8ZPArMLqf0Ds")
	if err != nil {
		t.Error("Failed to decode Base64 (with missing padding).")
	}
	if dec != "3954b216f50200a352629cfc64f02b30ba9fd03b" {
		t.Error("Base64 chunk decoded incorrectly.")
	}
}
func TestStringToPort(t *testing.T) {
	port := StringToPort("65536")
	if port != 0 {
		t.Error("Bad return value for invalid port.")
	}
	port = StringToPort("65535")
	if port != 65535 {
		t.Error("Bad return value for valid port.")
	}
	port = StringToPort("foobar")
	if port != 0 {
		t.Error("Bad return value for invalid input.")
	}
}
func TestAnnotationString(t *testing.T) {
	a := Annotation{"foobar", "0", "0"}
	s := a.String()
	if s != "@type foobar 0.0" {
		t.Errorf("Badly formatted annotation: %s", s)
	}
}
func TestAnnotationEquals(t *testing.T) {
	a := Annotation{"a", "b", "c"}
	b := Annotation{"a", "b", "c"}
	z := Annotation{"x", "y", "z"}
	if !a.Equals(&b) || !b.Equals(&a) {
		t.Error("Equals() incorrectly classified annotations as unequal.")
	}
	if a.Equals(&z) || z.Equals(&a) || b.Equals(&z) || z.Equals(&b) {
		t.Error("Equals() incorrectly classified annotations as equal.")
	}
}
func TestParseAnnotation(t *testing.T) {
	goodTests := []struct {
		s        string
		expected Annotation
	}{
		{"@type server-descriptor 1.0", Annotation{"server-descriptor", "1", "0"}},
		{"@type server-descriptor 1.2", Annotation{"server-descriptor", "1", "2"}},
		{"@type server-descriptor 2.0", Annotation{"server-descriptor", "2", "0"}},
		{"@type extra-info 2.0", Annotation{"extra-info", "2", "0"}},
		{"@type CASE 1.0", Annotation{"CASE", "1", "0"}},
	}
	badTests := []string{
		"",
		"@type test",
		"@type 1.0",
		"@type test 1",
		"@type test 1.",
		"@type test .0",
		"@type test 1.0 more",
		"@TYPE test 1.0",
		"@typo test 1.0",
		"type test 1.0",
	}
	for _, test := range goodTests {
		annotation, err := parseAnnotation(test.s)
		if err != nil {
			t.Errorf("%q resulted in an error: %s", test.s, err)
		}
		if !annotation.Equals(&test.expected) {
			t.Errorf("%q did not compare equal to %q", test.s, test.expected)
		}
	}
	for _, s := range badTests {
		_, err := parseAnnotation(s)
		if err == nil {
			t.Errorf("%q resulted in no error", s)
		}
	}
}
func BenchmarkParseAnnotation(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if _, err := parseAnnotation("@type server-descriptor 1.0"); err != nil {
			b.Fatal(err)
		}
	}
}