|
| 1 | +package binder |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/xaionaro-go/binder/parcel" |
| 10 | +) |
| 11 | + |
| 12 | +type testReceiver struct { |
| 13 | + lastCode TransactionCode |
| 14 | +} |
| 15 | + |
| 16 | +func (r *testReceiver) OnTransaction( |
| 17 | + _ context.Context, |
| 18 | + code TransactionCode, |
| 19 | + _ *parcel.Parcel, |
| 20 | +) (*parcel.Parcel, error) { |
| 21 | + r.lastCode = code |
| 22 | + return parcel.New(), nil |
| 23 | +} |
| 24 | + |
| 25 | +func TestStubBinder_CookieZeroBeforeRegistration(t *testing.T) { |
| 26 | + receiver := &testReceiver{} |
| 27 | + stub := NewStubBinder(receiver) |
| 28 | + assert.Equal(t, uintptr(0), stub.Cookie()) |
| 29 | +} |
| 30 | + |
| 31 | +func TestStubBinder_Handle(t *testing.T) { |
| 32 | + receiver := &testReceiver{} |
| 33 | + stub := NewStubBinder(receiver) |
| 34 | + assert.Equal(t, uint32(0), stub.Handle()) |
| 35 | +} |
| 36 | + |
| 37 | +func TestStubBinder_IsAlive(t *testing.T) { |
| 38 | + receiver := &testReceiver{} |
| 39 | + stub := NewStubBinder(receiver) |
| 40 | + assert.True(t, stub.IsAlive(context.Background())) |
| 41 | +} |
| 42 | + |
| 43 | +func TestStubBinder_TransactReturnsError(t *testing.T) { |
| 44 | + receiver := &testReceiver{} |
| 45 | + stub := NewStubBinder(receiver) |
| 46 | + _, err := stub.Transact(context.Background(), 1, 0, parcel.New()) |
| 47 | + require.Error(t, err) |
| 48 | +} |
| 49 | + |
| 50 | +func TestStubBinder_ImplementsIBinder(t *testing.T) { |
| 51 | + receiver := &testReceiver{} |
| 52 | + stub := NewStubBinder(receiver) |
| 53 | + |
| 54 | + var _ IBinder = stub |
| 55 | + assert.NotNil(t, stub) |
| 56 | +} |
| 57 | + |
| 58 | +func TestWriteBinderToParcel_ProxyBinder(t *testing.T) { |
| 59 | + // For a ProxyBinder with a known handle, WriteBinderToParcel should |
| 60 | + // write a BINDER_TYPE_HANDLE object (same as WriteStrongBinder). |
| 61 | + proxy := &ProxyBinder{handle: 42} |
| 62 | + p := parcel.New() |
| 63 | + |
| 64 | + WriteBinderToParcel(context.Background(), p, proxy, nil) |
| 65 | + |
| 66 | + // The parcel should contain a flat_binder_object (24 bytes) + stability int32 (4 bytes). |
| 67 | + assert.Equal(t, 28, p.Len()) |
| 68 | +} |
0 commit comments