Something went wrong. Try again.
NETCONF implementation in Go.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160package netconf
import ( "fmt" "io" "testing"
"github.com/stretchr/testify/assert")
type testServer struct { t *testing.T in chan []byte out chan []byte}
func newTestServer(t *testing.T) *testServer { return &testServer{ t: t, in: make(chan []byte), out: make(chan []byte), }}
func (s *testServer) handle(r io.ReadCloser, w io.WriteCloser) { in, err := io.ReadAll(r) if err != nil { panic(fmt.Sprintf("testerver: failed to read incomming message: %v", err)) } s.t.Logf("testserver recv: %s", in) go func() { s.in <- in }()
out, ok := <-s.out if !ok { panic("testserver: no message to send") } s.t.Logf("tesserver send: %s", out)
_, err = w.Write(out) if err != nil { panic(fmt.Sprintf("testserver: failed to write message: %v", err)) }
if err := w.Close(); err != nil { panic("tesserver: failed to close outbound message") }}
func (s *testServer) queueResp(p []byte) { go func() { s.out <- p }() }func (s *testServer) queueRespString(str string) { s.queueResp([]byte(str)) }func (s *testServer) popReq() ([]byte, error) { msg, ok := <-s.in if !ok { return nil, fmt.Errorf("testserver: no message to read:") } return msg, nil}
func (s *testServer) popReqString() (string, error) { p, err := s.popReq() return string(p), err}
func (s *testServer) transport() *testTransport { return newTestTransport(s.handle) }
type testTransport struct { handler func(r io.ReadCloser, w io.WriteCloser) out chan io.ReadCloser}
func newTestTransport(handler func(r io.ReadCloser, w io.WriteCloser)) *testTransport { return &testTransport{ handler: handler, out: make(chan io.ReadCloser), }}
func (s *testTransport) MsgReader() (io.ReadCloser, error) { return <-s.out, nil}
func (s *testTransport) MsgWriter() (io.WriteCloser, error) { inr, inw := io.Pipe() outr, outw := io.Pipe()
go func() { s.out <- outr }() go s.handler(inr, outw)
return inw, nil}
func (s *testTransport) Close() error { if len(s.out) > 0 { return fmt.Errorf("testtransport: remaining outboard messages not sent at close") } return nil}
const ( helloGood = `<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <capabilities> <capability>urn:ietf:params:netconf:base:1.0</capability> <capability>urn:ietf:params:netconf:base:1.1</capability> </capabilities> <session-id>42</session-id></hello>`
helloBadXML = `<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <capabilities//></hello>`
helloNoSessID = `<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <capabilities> <capability>urn:ietf:params:netconf:base:1.0</capability> <capability>urn:ietf:params:netconf:base:1.1</capability> </capabilities></hello>`
helloNoCaps = `<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <capabilities></capabilities> <session-id>42</session-id></hello>`)
func TestHello(t *testing.T) { tt := []struct { name string serverHello string shouldError bool wantID uint64 }{ {"good", helloGood, false, 42}, {"bad xml", helloBadXML, true, 0}, {"no capabilities", helloNoCaps, true, 0}, {"no session-id", helloNoSessID, true, 0}, }
for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { ts := newTestServer(t) sess := &Session{tr: ts.transport()}
ts.queueRespString(tc.serverHello)
err := sess.handshake() if !tc.shouldError { assert.NoError(t, err) }
_, err = ts.popReqString() assert.NoError(t, err) assert.Equal(t, tc.wantID, sess.sessionID) }) }}