From 6fcda304d637cf17a8fcefb5da2d255844ce9322 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Sun, 29 Dec 2024 19:52:33 +0900 Subject: [PATCH 1/2] Fix DocumentLen() out-of-range checks Fix DocumentLen() out-of-range checks that were incorrect. --- oviewer/event.go | 7 ++++--- oviewer/event_test.go | 40 +++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/oviewer/event.go b/oviewer/event.go index e3e3a552..b5cf8522 100644 --- a/oviewer/event.go +++ b/oviewer/event.go @@ -259,11 +259,12 @@ type eventDocument struct { } // SetDocument fires the eventDocument event. -func (root *Root) SetDocument(docNum int) { - if docNum < 0 || docNum < root.DocumentLen() { - return +func (root *Root) SetDocument(docNum int) error { + if docNum < 0 || docNum >= root.DocumentLen() { + return ErrInvalidDocumentNum } root.sendDocument(docNum) + return nil } func (root *Root) sendDocument(docNum int) { diff --git a/oviewer/event_test.go b/oviewer/event_test.go index a81dd438..c3816dbe 100644 --- a/oviewer/event_test.go +++ b/oviewer/event_test.go @@ -1,10 +1,7 @@ package oviewer import ( - "bytes" "context" - "log" - "os" "testing" "github.com/gdamore/tcell/v2" @@ -38,40 +35,37 @@ func TestRoot_SetDocument(t *testing.T) { docNum int } tests := []struct { - name string - args args - want string + name string + args args + wantErr bool }{ { - name: "set document", + name: "valid document number", args: args{ - docNum: 1, + docNum: 0, }, - want: "", + wantErr: false, }, { - name: "set document", + name: "invalid document number - negative", args: args{ docNum: -1, }, - want: "", + wantErr: true, + }, + { + name: "invalid document number - out of range", + args: args{ + docNum: 100, + }, + wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { root := rootHelper(t) - var buf bytes.Buffer - log.SetOutput(&buf) - defaultFlags := log.Flags() - log.SetFlags(0) - defer func() { - log.SetOutput(os.Stderr) - log.SetFlags(defaultFlags) - }() - root.SetDocument(tt.args.docNum) - got := buf.String() - if got != tt.want { - t.Errorf("Root.SetDocument() = %v, want %v", got, tt.want) + if err := root.SetDocument(tt.args.docNum); (err != nil) != tt.wantErr { + t.Errorf("Root.SetDocument() error = %v, wantErr %v", err, tt.wantErr) } }) } From 0e60fe9660403f18e03fa66f796f15c2a3a0472d Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Sun, 29 Dec 2024 19:57:46 +0900 Subject: [PATCH 2/2] Add error message for incorrect number of specified Document --- oviewer/oviewer.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index 822510e1..0af91b58 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -390,6 +390,8 @@ var ( ErrInvalidSGR = errors.New("invalid SGR") // ErrNotSuuport indicates that it is not supported. ErrNotSuuport = errors.New("not support") + // ErrInvalidDocumentNum indicates that the document number is invalid. + ErrInvalidDocumentNum = errors.New("invalid document number") ) // This is a function of tcell.NewScreen but can be replaced with mock.