github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/airdrop/reg_test.go (about) 1 package stellar 2 3 import ( 4 "context" 5 "net" 6 "testing" 7 8 "github.com/keybase/client/go/kbtest" 9 "github.com/keybase/client/go/libkb" 10 keybase1 "github.com/keybase/client/go/protocol/keybase1" 11 "github.com/keybase/go-framed-msgpack-rpc/rpc" 12 "github.com/stretchr/testify/require" 13 ) 14 15 // ConnPair is like Unix's SocketPair, but uses TCP so it should work on all platforms. 16 // Copy/pasta'ed from: https://gist.github.com/tsavola/cd847385989f1ae497dbbcd2bba68753 17 func connPair() (serverConn, clientConn net.Conn, err error) { 18 l, err := net.Listen("tcp", "localhost:0") 19 if err != nil { 20 return nil, nil, err 21 } 22 defer l.Close() 23 24 addr := l.Addr() 25 var err2 error 26 done := make(chan struct{}) 27 28 go func() { 29 defer close(done) 30 clientConn, err2 = net.Dial(addr.Network(), addr.String()) 31 }() 32 33 serverConn, err = l.Accept() 34 <-done 35 36 if err == nil { 37 err = err2 38 } 39 if err != nil { 40 if clientConn != nil { 41 clientConn.Close() 42 } 43 if serverConn != nil { 44 serverConn.Close() 45 } 46 } 47 return serverConn, clientConn, err 48 } 49 50 func newTestClient(conn net.Conn) *Client { 51 return &Client{ 52 dialFunc: func(m libkb.MetaContext) (net.Conn, error) { return conn, nil }, 53 } 54 } 55 56 type testProcessor struct { 57 details keybase1.AirdropDetails 58 err error 59 doneCh chan struct{} 60 } 61 62 var _ RequestProcessor = (*testProcessor)(nil) 63 64 func newTestProcessor() *testProcessor { 65 return &testProcessor{ 66 doneCh: make(chan struct{}), 67 } 68 } 69 70 func (p *testProcessor) Reg1(c context.Context, uid keybase1.UID, kid keybase1.BinaryKID, err error) { 71 } 72 func (p *testProcessor) Close(ctx context.Context, err error) {} 73 74 func (p *testProcessor) Reg2(ctx context.Context, details keybase1.AirdropDetails, err error) { 75 p.details = details 76 p.err = err 77 p.doneCh <- struct{}{} 78 } 79 80 func TestReg(t *testing.T) { 81 tc := libkb.SetupTest(t, "stellar", 2) 82 defer tc.Cleanup() 83 fu, err := kbtest.CreateAndSignupFakeUser("t", tc.G) 84 require.NoError(t, err) 85 serverConn, clientConn, err := connPair() 86 require.NoError(t, err) 87 tp := newTestProcessor() 88 mctx := tc.MetaContext() 89 xp := libkb.NewTransportFromSocket(mctx.G(), serverConn, keybase1.NetworkSource_REMOTE) 90 srv := rpc.NewServer(xp, libkb.MakeWrapError(mctx.G())) 91 err = HandleRequest(mctx.Ctx(), xp, srv, tp) 92 require.NoError(t, err) 93 go func() { 94 cli := newTestClient(clientConn) 95 err := cli.Register(mctx) 96 require.NoError(t, err) 97 }() 98 <-tp.doneCh 99 require.NoError(t, tp.err) 100 require.Equal(t, tp.details.Uid, fu.GetUID()) 101 }