Skip to content

Commit

Permalink
use go 1.18 atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
espadolini committed Dec 12, 2023
1 parent 66a3060 commit 81bb2e5
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions ssh/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func TestNewServerConnValidationErrors(t *testing.T) {
if err == nil {
t.Fatal("NewServerConn with invalid public key auth algorithms succeeded")
}
if !c.closed.Load() {
if !c.isClosed() {
t.Fatal("NewServerConn with invalid public key auth algorithms left connection open")
}
if c.used.Load() {
if c.isUsed() {
t.Fatal("NewServerConn with invalid public key auth algorithms used connection")
}

Expand All @@ -88,36 +88,44 @@ func TestNewServerConnValidationErrors(t *testing.T) {
if err == nil {
t.Fatal("NewServerConn with unsupported key exchange succeeded")
}
if !c.closed.Load() {
if !c.isClosed() {
t.Fatal("NewServerConn with unsupported key exchange left connection open")
}
if c.used.Load() {
if c.isUsed() {
t.Fatal("NewServerConn with unsupported key exchange used connection")
}
}

type markerConn struct {
closed atomic.Bool
used atomic.Bool
closed uint32
used uint32
}

func (c *markerConn) isClosed() bool {
return atomic.LoadUint32(&c.closed) != 0
}

func (c *markerConn) isUsed() bool {
return atomic.LoadUint32(&c.used) != 0
}

func (c *markerConn) Close() error {
c.closed.Store(true)
atomic.StoreUint32(&c.closed, 1)
return nil
}

func (c *markerConn) Read(b []byte) (n int, err error) {
c.used.Store(true)
if c.closed.Load() {
atomic.StoreUint32(&c.used, 1)
if atomic.LoadUint32(&c.closed) != 0 {
return 0, net.ErrClosed
} else {
return 0, io.EOF
}
}

func (c *markerConn) Write(b []byte) (n int, err error) {
c.used.Store(true)
if c.closed.Load() {
atomic.StoreUint32(&c.used, 1)
if atomic.LoadUint32(&c.closed) != 0 {
return 0, net.ErrClosed
} else {
return 0, io.ErrClosedPipe
Expand Down

0 comments on commit 81bb2e5

Please sign in to comment.