|
| 1 | +package org.dcache.oncrpc4j.rpc; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.MoreExecutors; |
| 4 | +import java.io.IOException; |
| 5 | +import java.security.AccessController; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.concurrent.atomic.AtomicReference; |
| 9 | +import java.util.function.Consumer; |
| 10 | +import javax.security.auth.Subject; |
| 11 | +import org.dcache.oncrpc4j.xdr.XdrVoid; |
| 12 | +import org.glassfish.grizzly.filterchain.FilterChainContext; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.Before; |
| 15 | + |
| 16 | +import static org.junit.Assert.*; |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +public class RpcDispatcherTest { |
| 20 | + |
| 21 | + private Map<OncRpcProgram, RpcDispatchable> programs; |
| 22 | + private RpcDispatcher dispatcher; |
| 23 | + |
| 24 | + private FilterChainContext context; |
| 25 | + |
| 26 | + private RpcCall call; |
| 27 | + |
| 28 | + private Consumer callInterceptor; |
| 29 | + |
| 30 | + private OncRpcProgram PROG_ONE = new OncRpcProgram(1, 1); |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + programs = new HashMap<>(); |
| 35 | + callInterceptor = mock(Consumer.class); |
| 36 | + dispatcher = new RpcDispatcher(MoreExecutors.newDirectExecutorService(), programs, true, |
| 37 | + callInterceptor); |
| 38 | + |
| 39 | + Subject s = new Subject(); |
| 40 | + RpcAuth auth = mock(RpcAuth.class); |
| 41 | + when(auth.getSubject()).thenReturn(s); |
| 42 | + |
| 43 | + call = mock(RpcCall.class); |
| 44 | + when(call.getProgramVersion()).thenReturn(1); |
| 45 | + when(call.getProgram()).thenReturn(1); |
| 46 | + when(call.getCredential()).thenReturn(auth); |
| 47 | + |
| 48 | + context = mock(FilterChainContext.class); |
| 49 | + when(context.getMessage()).thenReturn(call); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSubjectPropagation() throws IOException { |
| 54 | + |
| 55 | + AtomicReference<Subject> callSubject = new AtomicReference<>(); |
| 56 | + |
| 57 | + programs.put(PROG_ONE, (call) -> { |
| 58 | + Subject subject = Subject.getSubject(AccessController.getContext()); |
| 59 | + callSubject.set(subject); |
| 60 | + }); |
| 61 | + |
| 62 | + dispatcher.handleRead(context); |
| 63 | + |
| 64 | + assertSame("subject not propagated", call.getCredential().getSubject(), callSubject.get()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testProgramUnavailable() throws IOException { |
| 69 | + |
| 70 | + dispatcher.handleRead(context); |
| 71 | + |
| 72 | + verify(call).failProgramUnavailable(); |
| 73 | + verify(call).failProgramUnavailable(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testRejected() throws IOException { |
| 78 | + |
| 79 | + programs.put(PROG_ONE, (call) -> { |
| 80 | + throw new RpcException(RpcRejectStatus.RPC_MISMATCH, "", XdrVoid.XDR_VOID); |
| 81 | + }); |
| 82 | + |
| 83 | + dispatcher.handleRead(context); |
| 84 | + verify(call, atLeastOnce()).reject(anyInt(), any()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testRpcGarbage() throws IOException { |
| 89 | + |
| 90 | + programs.put(PROG_ONE, (call) -> { |
| 91 | + throw new OncRpcRejectedException(1); |
| 92 | + }); |
| 93 | + |
| 94 | + dispatcher.handleRead(context); |
| 95 | + verify(call).failRpcGarbage(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testRpcGarbageOnIOException() throws IOException { |
| 100 | + |
| 101 | + programs.put(PROG_ONE, (call) -> { |
| 102 | + throw new IOException(); |
| 103 | + }); |
| 104 | + |
| 105 | + dispatcher.handleRead(context); |
| 106 | + verify(call).failRpcGarbage(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSystemError() throws IOException { |
| 111 | + |
| 112 | + programs.put(PROG_ONE, (call) -> { |
| 113 | + throw new RuntimeException(); |
| 114 | + }); |
| 115 | + |
| 116 | + try { |
| 117 | + dispatcher.handleRead(context); |
| 118 | + fail("runtime exception not propagated"); |
| 119 | + } catch (RuntimeException e) { |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + verify(call).failRpcSystem(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testCallInterceptor() throws IOException { |
| 128 | + |
| 129 | + programs.put(PROG_ONE, (call) -> { |
| 130 | + }); |
| 131 | + |
| 132 | + dispatcher.handleRead(context); |
| 133 | + verify(callInterceptor).accept(any()); |
| 134 | + } |
| 135 | +} |
0 commit comments