lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
nathanmittler | 29cbef1 | 2014-10-27 11:33:19 -0700 | [diff] [blame] | 32 | package com.google.net.stubby.transport; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 33 | |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.junit.Assert.assertTrue; |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 36 | import static org.mockito.Matchers.anyInt; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 37 | import static org.mockito.Matchers.eq; |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.atLeastOnce; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 39 | import static org.mockito.Mockito.mock; |
| 40 | import static org.mockito.Mockito.verify; |
| 41 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 42 | |
| 43 | import com.google.common.io.ByteStreams; |
| 44 | import com.google.common.primitives.Bytes; |
simonma | f589a4a | 2015-01-15 16:23:32 -0800 | [diff] [blame] | 45 | import com.google.net.stubby.transport.MessageDeframer.Listener; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 46 | |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.junit.runners.JUnit4; |
| 50 | import org.mockito.ArgumentCaptor; |
| 51 | |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 52 | import java.io.ByteArrayOutputStream; |
| 53 | import java.io.IOException; |
| 54 | import java.io.InputStream; |
nathanmittler | 28fb46e | 2014-10-18 06:33:19 -0700 | [diff] [blame] | 55 | import java.util.List; |
| 56 | import java.util.zip.GZIPOutputStream; |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
simonma | f589a4a | 2015-01-15 16:23:32 -0800 | [diff] [blame] | 59 | * Tests for {@link MessageDeframer}. |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 60 | */ |
| 61 | @RunWith(JUnit4.class) |
simonma | f589a4a | 2015-01-15 16:23:32 -0800 | [diff] [blame] | 62 | public class MessageDeframerTest { |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 63 | private Listener listener = mock(Listener.class); |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 64 | private MessageDeframer deframer = new MessageDeframer(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 65 | private ArgumentCaptor<InputStream> messages = ArgumentCaptor.forClass(InputStream.class); |
| 66 | |
| 67 | @Test |
| 68 | public void simplePayload() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 69 | deframer.request(1); |
| 70 | deframer.deframe(buffer(new byte[]{0, 0, 0, 0, 2, 3, 14}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 71 | verify(listener).messageRead(messages.capture(), eq(2)); |
| 72 | assertEquals(Bytes.asList(new byte[]{3, 14}), bytes(messages)); |
| 73 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 74 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void smallCombinedPayloads() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 79 | deframer.request(2); |
| 80 | deframer.deframe(buffer(new byte[]{0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 2, 14, 15}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 81 | verify(listener).messageRead(messages.capture(), eq(1)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 82 | assertEquals(Bytes.asList(new byte[] {3}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 83 | verify(listener).messageRead(messages.capture(), eq(2)); |
| 84 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 85 | assertEquals(Bytes.asList(new byte[] {14, 15}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 86 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
ejona | 9d50299 | 2014-09-22 12:23:19 -0700 | [diff] [blame] | 90 | public void endOfStreamWithPayloadShouldNotifyEndOfStream() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 91 | deframer.request(1); |
| 92 | deframer.deframe(buffer(new byte[] {0, 0, 0, 0, 1, 3}), true); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 93 | verify(listener).messageRead(messages.capture(), eq(1)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 94 | assertEquals(Bytes.asList(new byte[] {3}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 95 | verify(listener).endOfStream(); |
| 96 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 97 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
ejona | 9d50299 | 2014-09-22 12:23:19 -0700 | [diff] [blame] | 101 | public void endOfStreamShouldNotifyEndOfStream() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 102 | deframer.deframe(buffer(new byte[0]), true); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 103 | verify(listener).endOfStream(); |
| 104 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void payloadSplitBetweenBuffers() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 109 | deframer.request(1); |
| 110 | deframer.deframe(buffer(new byte[] {0, 0, 0, 0, 7, 3, 14, 1, 5, 9}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 111 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 112 | verifyNoMoreInteractions(listener); |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 113 | deframer.deframe(buffer(new byte[] {2, 6}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 114 | verify(listener).messageRead(messages.capture(), eq(7)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 115 | assertEquals(Bytes.asList(new byte[] {3, 14, 1, 5, 9, 2, 6}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 116 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 117 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void frameHeaderSplitBetweenBuffers() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 122 | deframer.request(1); |
| 123 | |
| 124 | deframer.deframe(buffer(new byte[] {0, 0}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 125 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 126 | verifyNoMoreInteractions(listener); |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 127 | deframer.deframe(buffer(new byte[] {0, 0, 1, 3}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 128 | verify(listener).messageRead(messages.capture(), eq(1)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 129 | assertEquals(Bytes.asList(new byte[] {3}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 130 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 131 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void emptyPayload() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 136 | deframer.request(1); |
| 137 | deframer.deframe(buffer(new byte[] {0, 0, 0, 0, 0}), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 138 | verify(listener).messageRead(messages.capture(), eq(0)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 139 | assertEquals(Bytes.asList(), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 140 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 141 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void largerFrameSize() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 146 | deframer.request(1); |
| 147 | deframer.deframe( |
| 148 | Buffers.wrap(Bytes.concat(new byte[] {0, 0, 0, 3, (byte) 232}, new byte[1000])), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 149 | verify(listener).messageRead(messages.capture(), eq(1000)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 150 | assertEquals(Bytes.asList(new byte[1000]), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 151 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 152 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 156 | public void endOfStreamCallbackShouldWaitForMessageDelivery() { |
| 157 | deframer.deframe(buffer(new byte[] {0, 0, 0, 0, 1, 3}), true); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 158 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 159 | |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 160 | deframer.request(1); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 161 | verify(listener).messageRead(messages.capture(), eq(1)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 162 | assertEquals(Bytes.asList(new byte[] {3}), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 163 | verify(listener).endOfStream(); |
| 164 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 165 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void compressed() { |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 170 | deframer = new MessageDeframer(listener, MessageDeframer.Compression.GZIP); |
| 171 | deframer.request(1); |
| 172 | |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 173 | byte[] payload = compress(new byte[1000]); |
| 174 | assertTrue(payload.length < 100); |
| 175 | byte[] header = new byte[] {1, 0, 0, 0, (byte) payload.length}; |
| 176 | deframer.deframe(buffer(Bytes.concat(header, payload)), false); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 177 | verify(listener).messageRead(messages.capture(), eq(1000)); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 178 | assertEquals(Bytes.asList(new byte[1000]), bytes(messages)); |
nathanmittler | 11c363a | 2015-01-09 11:22:19 -0800 | [diff] [blame] | 179 | verify(listener, atLeastOnce()).bytesRead(anyInt()); |
| 180 | verifyNoMoreInteractions(listener); |
ejona | 913de47 | 2014-09-18 07:48:43 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private static List<Byte> bytes(ArgumentCaptor<InputStream> captor) { |
| 184 | try { |
| 185 | return Bytes.asList(ByteStreams.toByteArray(captor.getValue())); |
| 186 | } catch (IOException ex) { |
| 187 | throw new AssertionError(ex); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private static Buffer buffer(byte[] bytes) { |
| 192 | return Buffers.wrap(bytes); |
| 193 | } |
| 194 | |
| 195 | private static byte[] compress(byte[] bytes) { |
| 196 | try { |
| 197 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 198 | GZIPOutputStream zip = new GZIPOutputStream(baos); |
| 199 | zip.write(bytes); |
| 200 | zip.close(); |
| 201 | return baos.toByteArray(); |
| 202 | } catch (IOException ex) { |
| 203 | throw new RuntimeException(ex); |
| 204 | } |
| 205 | } |
| 206 | } |