Skip to content
Snippets Groups Projects
Commit 479449fd authored by Lukas Braun's avatar Lukas Braun
Browse files

getLine(): Always check for errors

Scan() can return true despite encountering an error. Encountered while
writing a test for the read timeout, also included.
parent 8ff88f52
No related branches found
No related tags found
No related merge requests found
...@@ -99,10 +99,9 @@ func errFatal(err error) { ...@@ -99,10 +99,9 @@ func errFatal(err error) {
} }
func getLine(s *bufio.Scanner) (string, error) { func getLine(s *bufio.Scanner) (string, error) {
if !s.Scan() { s.Scan()
if err := s.Err(); err != nil { if err := s.Err(); err != nil {
return "", err return "", err
}
} }
line := s.Text() line := s.Text()
if len(line) <= 0 { if len(line) <= 0 {
......
...@@ -7,9 +7,11 @@ import ( ...@@ -7,9 +7,11 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"net"
"os" "os"
"sync" "sync"
"testing" "testing"
"time"
"github.com/gokyle/hotp" "github.com/gokyle/hotp"
) )
...@@ -384,6 +386,23 @@ func handleConnT(t *testing.T) { ...@@ -384,6 +386,23 @@ func handleConnT(t *testing.T) {
}) })
} }
func listenT(t *testing.T) {
t.Run("timeout", func(t *testing.T) {
c, err := net.Dial("unix", "@goatherd_test")
tErrFatal(t, err)
t.Log("Waiting for timeout")
start := time.Now()
for {
if _, err = c.Write([]byte("a")); err != nil {
t.Logf("Write failed after %v with %q", time.Now().Sub(start), err)
break
}
time.Sleep(200 * time.Millisecond)
}
})
}
func TestMain(t *testing.T) { func TestMain(t *testing.T) {
if dbURL, ok := os.LookupEnv("DB_URL"); ok { if dbURL, ok := os.LookupEnv("DB_URL"); ok {
cfg.DbURL = dbURL cfg.DbURL = dbURL
...@@ -392,6 +411,7 @@ func TestMain(t *testing.T) { ...@@ -392,6 +411,7 @@ func TestMain(t *testing.T) {
} }
cfg.Lookahead = 3 cfg.Lookahead = 3
cfg.Debug = false cfg.Debug = false
cfg.ReadTimeout.Duration = 1 * time.Second
faildelay.userlocks = make(map[string]*sync.Mutex) faildelay.userlocks = make(map[string]*sync.Mutex)
stdinR, stdinW := io.Pipe() stdinR, stdinW := io.Pipe()
stdinScanner, stdinWriter = bufio.NewScanner(stdinR), bufio.NewWriter(stdinW) stdinScanner, stdinWriter = bufio.NewScanner(stdinR), bufio.NewWriter(stdinW)
...@@ -406,4 +426,12 @@ func TestMain(t *testing.T) { ...@@ -406,4 +426,12 @@ func TestMain(t *testing.T) {
t.Run("checkOffer", checkOfferT) t.Run("checkOffer", checkOfferT)
t.Run("incCount", incCountT) t.Run("incCount", incCountT)
t.Run("handleConn", handleConnT) t.Run("handleConn", handleConnT)
listener, err := net.Listen("unix", "@goatherd_test")
tErrFatal(t, err)
var wg sync.WaitGroup
wg.Add(1)
go listen(&wg, listener)
t.Run("listen", listenT)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment