Skip to content

Commit

Permalink
Merge pull request zeromq#240 from taotetek/master
Browse files Browse the repository at this point in the history
Problem: have places we do not check error returns
  • Loading branch information
c-rack authored Nov 11, 2017
2 parents 50473ee + 96cfbdb commit a399852
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 38 deletions.
132 changes: 108 additions & 24 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ func TestAuthIPAllow(t *testing.T) {
t.Error(err)
}

client.SendFrame([]byte("Hello"), 1)
client.SendFrame([]byte("World"), 0)
err = client.SendFrame([]byte("Hello"), 1)
if err != nil {
t.Error(err)
}

err = client.SendFrame([]byte("World"), 0)
if err != nil {
t.Error(err)
}

poller, err := NewPoller(server)
if err != nil {
Expand Down Expand Up @@ -78,13 +85,27 @@ func TestAuthPlain(t *testing.T) {
}

defer func() {
os.Remove("./password_test.txt")
err = os.Remove("./password_test.txt")
if err != nil {
t.Error(err)
}
}()

w := bufio.NewWriter(pwfile)
w.Write([]byte("admin=Password\n"))
w.Flush()
pwfile.Close()
_, err = w.Write([]byte("admin=Password\n"))
if err != nil {
t.Error(err)
}

err = w.Flush()
if err != nil {
t.Error(err)
}

err = pwfile.Close()
if err != nil {
t.Error(err)
}

auth := NewAuth()
defer auth.Destroy()
Expand Down Expand Up @@ -125,8 +146,15 @@ func TestAuthPlain(t *testing.T) {
t.Error(err)
}

goodClient.SendFrame([]byte("Hello"), 1)
goodClient.SendFrame([]byte("World"), 0)
err = goodClient.SendFrame([]byte("Hello"), 1)
if err != nil {
t.Error(err)
}

err = goodClient.SendFrame([]byte("World"), 0)
if err != nil {
t.Error(err)
}

poller, err := NewPoller(server)
if err != nil {
Expand Down Expand Up @@ -157,8 +185,15 @@ func TestAuthPlain(t *testing.T) {
t.Error(err)
}

badClient.SendFrame([]byte("Hello"), 1)
badClient.SendFrame([]byte("World"), 0)
err = badClient.SendFrame([]byte("Hello"), 1)
if err != nil {
t.Error(err)
}

err = badClient.SendFrame([]byte("World"), 0)
if err != nil {
t.Error(err)
}

s = poller.Wait(200)
if s != nil {
Expand Down Expand Up @@ -203,7 +238,10 @@ func TestAuthCurveAllowAny(t *testing.T) {
badClient := NewSock(Push)
defer badClient.Destroy()

auth.Curve(CurveAllowAny)
err = auth.Curve(CurveAllowAny)
if err != nil {
t.Error(err)
}

port, err := server.Bind("tcp://127.0.0.1:*")
if err != nil {
Expand All @@ -220,8 +258,15 @@ func TestAuthCurveAllowAny(t *testing.T) {
t.Error(err)
}

goodClient.SendFrame([]byte("Hello"), 1)
goodClient.SendFrame([]byte("World"), 0)
err = goodClient.SendFrame([]byte("Hello"), 1)
if err != nil {
t.Error(err)
}

err = goodClient.SendFrame([]byte("World"), 0)
if err != nil {
t.Error(err)
}

poller, err := NewPoller(server)
if err != nil {
Expand All @@ -247,8 +292,15 @@ func TestAuthCurveAllowAny(t *testing.T) {
t.Errorf("want %#v, have %#v", want, have)
}

badClient.SendFrame([]byte("Hello"), 1)
badClient.SendFrame([]byte("Bad World"), 0)
err = badClient.SendFrame([]byte("Hello"), 1)
if err != nil {
t.Error(err)
}

err = badClient.SendFrame([]byte("Bad World"), 0)
if err != nil {
t.Error(err)
}

s = poller.Wait(200)
if s != nil {
Expand Down Expand Up @@ -287,7 +339,10 @@ func TestAuthCurveAllowCertificate(t *testing.T) {
goodClientCert.Apply(goodClient)

certfile := path.Join("testauth", "goodClient.txt")
goodClientCert.SavePublic(certfile)
err = goodClientCert.SavePublic(certfile)
if err != nil {
t.Error(err)
}

badClient := NewSock(Push, SockSetCurveServerkey(serverKey))
defer badClient.Destroy()
Expand Down Expand Up @@ -315,7 +370,10 @@ func TestAuthCurveAllowCertificate(t *testing.T) {
t.Error(err)
}

goodClient.SendFrame([]byte("Hello, Good World!"), 0)
err = goodClient.SendFrame([]byte("Hello, Good World!"), 0)
if err != nil {
t.Error(err)
}

poller, err := NewPoller(server)
if err != nil {
Expand All @@ -337,14 +395,20 @@ func TestAuthCurveAllowCertificate(t *testing.T) {
t.Errorf("want '%#v', have '%#v'", want, have)
}

badClient.SendFrame([]byte("Hello, Bad World"), 0)
err = badClient.SendFrame([]byte("Hello, Bad World"), 0)
if err != nil {
t.Error(err)
}

s = poller.Wait(200)
if s != nil {
t.Errorf("want '%#v', have '%#v", nil, s)
}

os.RemoveAll(testpath)
err = os.RemoveAll(testpath)
if err != nil {
t.Error(err)
}
}

func ExampleAuth() {
Expand All @@ -355,15 +419,29 @@ func ExampleAuth() {
// create a client certificate and save it
clientCert := NewCert()
defer clientCert.Destroy()
clientCert.SavePublic("client_cert")
defer func() { os.Remove("client_cert") }()
err := clientCert.SavePublic("client_cert")
if err != nil {
panic(err)
}

defer func() {
err := os.Remove("client_cert")
if err != nil {
if err != nil {
panic(err)
}
}
}()

// create an auth service
auth := NewAuth()
defer auth.Destroy()

// tell the auth service the client cert is allowed
auth.Curve("client_cert")
err = auth.Curve("client_cert")
if err != nil {
panic(err)
}

// create a server socket and set it to
// use the "global" auth domain
Expand All @@ -377,7 +455,10 @@ func ExampleAuth() {
server.SetOption(SockSetCurveServer(1))

// bind our server to an endpoint
server.Bind("tcp://*:9898")
_, err = server.Bind("tcp://*:9898")
if err != nil {
panic(err)
}

// create a client socket
client := NewSock(Pull)
Expand All @@ -393,5 +474,8 @@ func ExampleAuth() {
client.SetOption(SockSetCurveServerkey(serverCert.PublicText()))

// connect
client.Connect("tcp://127.0.0.1:9898")
err = client.Connect("tcp://127.0.0.1:9898")
if err != nil {
panic(err)
}
}
12 changes: 10 additions & 2 deletions beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func TestBeacon(t *testing.T) {
t.Error(err)
}

speaker.Publish("HI", 100)
err = speaker.Publish("HI", 100)
if err != nil {
t.Error(err)
}

msg := listener.Recv(500)
if len(msg) == 2 {
Expand All @@ -61,6 +64,11 @@ func ExampleBeacon() {
if err != nil {
panic(err)
}

fmt.Printf("started beacon on: %s", address)
beacon.Publish("HI", 100)

err = beacon.Publish("HI", 100)
if err != nil {
panic(err)
}
}
17 changes: 14 additions & 3 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func TestCert(t *testing.T) {
cert.Print()
}

cert.Save("./test_cert")
err := cert.Save("./test_cert")
if err != nil {
t.Error(err)
}

loaded, err := NewCertFromFile("./test_cert")
if err != nil {
t.Error(err)
Expand All @@ -61,8 +65,15 @@ func TestCert(t *testing.T) {
loaded.Print()
}

os.Remove("./test_cert")
os.Remove("./test_cert_secret")
err = os.Remove("./test_cert")
if err != nil {
t.Error(err)
}

err = os.Remove("./test_cert_secret")
if err != nil {
t.Error(err)
}
}

func TestNewCertFromFile(t *testing.T) {
Expand Down
14 changes: 11 additions & 3 deletions certstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (

func TestCertStore(t *testing.T) {
testDir := ".test_zcertstore"
os.Mkdir(testDir, 0777)
err := os.Mkdir(testDir, 0777)
if err != nil {
t.Error(err)
}

certstore := NewCertStore(testDir)
defer certstore.Destroy()
Expand All @@ -24,7 +27,12 @@ func TestCertStore(t *testing.T) {
client1 := NewCert()
client1.SetMeta("name", "Brian")
client1Key := client1.PublicText()
client1.Save(testDir + "/mycert.txt")

err = client1.Save(testDir + "/mycert.txt")
if err != nil {
t.Error(err)
}

client1.Destroy()

client1Loaded := certstore.Lookup(client1Key)
Expand Down Expand Up @@ -52,7 +60,7 @@ func TestCertStore(t *testing.T) {
t.Errorf("want %#v, have %#v", want, have)
}

err := os.RemoveAll(testDir)
err = os.RemoveAll(testDir)
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 13 additions & 3 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ func TestProxy(t *testing.T) {
defer tap.Destroy()

// send some messages and check they arrived
faucet.SendFrame([]byte("Hello"), FlagNone)
faucet.SendFrame([]byte("World"), FlagNone)
err = faucet.SendFrame([]byte("Hello"), FlagNone)
if err != nil {
t.Error(err)
}

err = faucet.SendFrame([]byte("World"), FlagNone)
if err != nil {
t.Error(err)
}

// check the tap
b, f, err := tap.RecvFrame()
Expand Down Expand Up @@ -118,7 +125,10 @@ func TestProxy(t *testing.T) {
t.Error(err)
}

faucet.SendFrame([]byte("Belated Hello"), FlagNone)
err = faucet.SendFrame([]byte("Belated Hello"), FlagNone)
if err != nil {
t.Error(err)
}

if want, have := false, sink.Pollin(); want != have {
t.Errorf("want %#v, have %#v", want, have)
Expand Down
17 changes: 14 additions & 3 deletions sock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func TestSendMessage(t *testing.T) {
t.Error(err)
}

pushSock.SendMessage([][]byte{[]byte("Hello")})
err = pushSock.SendMessage([][]byte{[]byte("Hello")})
if err != nil {
t.Error(err)
}

msg, err := pullSock.RecvMessage()
if err != nil {
t.Error(err)
Expand All @@ -131,7 +135,11 @@ func TestSendMessage(t *testing.T) {
t.Error(err)
}

pushSock.SendMessage([][]byte{[]byte("World")})
err = pushSock.SendMessage([][]byte{[]byte("World")})
if err != nil {
t.Error(err)
}

msg, err = pullSock.RecvMessageNoWait()
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -894,7 +902,10 @@ func ExampleSock_output() {
defer router.Destroy()

// send hello message
dealer.SendFrame([]byte("Hello"), FlagNone)
err = dealer.SendFrame([]byte("Hello"), FlagNone)
if err != nil {
panic(err)
}

// receive hello message
request, err := router.RecvMessage()
Expand Down

0 comments on commit a399852

Please sign in to comment.