Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle print job EOF #19

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lpr_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,15 @@ func (lpr *LprConnection) RunConnection() {
}

if err != nil {
if errors.Is(err, io.EOF) &&
((lpr.dataFileReceived && lpr.controlFileReceived) ||
(!lpr.dataFileReceived && !lpr.controlFileReceived)) {
logDebugf("Got error while reading command, but this is ok, because client has to close the connection: %s", err.Error())
err = nil
} else {
err = fmt.Errorf("got EOF, but either control file was received (%v) or data file was received (%v): %w", lpr.controlFileReceived, lpr.dataFileReceived, err)
if errors.Is(err, io.EOF) {
if lpr.Status == JobSubCommand && (!lpr.dataFileReceived && !lpr.controlFileReceived) {
err = fmt.Errorf("got Print job command, but the connection was closed without receiving a subcommand: %w", err)
} else if (lpr.dataFileReceived && lpr.controlFileReceived) || (!lpr.dataFileReceived && !lpr.controlFileReceived) {
logDebugf("Got error while reading command, but this is ok, because client has to close the connection: %s", err.Error())
err = nil
} else {
err = fmt.Errorf("got EOF, but either control file was received (%v) or data file was received (%v): %w", lpr.controlFileReceived, lpr.dataFileReceived, err)
}
}

lpr.end(err)
Expand Down
63 changes: 63 additions & 0 deletions lpr_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,69 @@ func TestDaemonWithInvalidControlFileContent(t *testing.T) {
lprd.Close()
}

func TestClosedConnectionCases(t *testing.T) {
SetDebugLogger(log.Print)

port := uint16(2345)

text := "Text for the file"
file, err := generateTempFile("", "", text)
require.Nil(t, err)
defer os.Remove(file)

var lprd LprDaemon

nextExternalID := uint64(10)
// set lprd callback function
lprd.GetExternalID = func() uint64 {
nextExternalID++
return nextExternalID
}

err = lprd.Init(port, "")
require.Nil(t, err)

// Connection closed without sending commands
lpr := &LprSend{}
err = lpr.Init("127.0.0.1", file, port, "", "", time.Minute)
require.Nil(t, err)

// Close connection
err = lpr.Close()
require.Nil(t, err)

connection := <-lprd.FinishedConnections()
// Connection results in End
require.Equal(t, End, connection.Status)
// ExternalID is not set
require.Equal(t, uint64(0), connection.ExternalID)
// SaveName is not set
require.Empty(t, connection.SaveName)

//////////////////
// Connection closed after print job command
lpr = &LprSend{}

err = lpr.Init("127.0.0.1", file, port, "", "", time.Minute)
require.Nil(t, err)

// write 0x02 - Print job command
_, err = lpr.writeByte([]byte{'\x02', '\n'})
require.Nil(t, err)

// Close connection without sending subcommand
err = lpr.Close()
require.Nil(t, err)

connection = <-lprd.FinishedConnections()
// Connection results in Error
require.Equal(t, Error, connection.Status)
// ExternalID is set
require.Equal(t, uint64(11), connection.ExternalID)

lprd.Close()
}

func customSendFunc(configPrefix string, file string, hostname string, port uint16, queue string, username string, timeout time.Duration) (err error) {
lpr := &LprSend{}

Expand Down