test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/internal/frame/FramesDecoderTest.java
changeset 48703 3eae36c6baa5
equal deleted inserted replaced
48702:81eb51edf2cb 48703:3eae36c6baa5
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package jdk.incubator.http.internal.frame;
       
    25 
       
    26 import java.io.IOException;
       
    27 import java.nio.ByteBuffer;
       
    28 import java.util.ArrayList;
       
    29 import java.util.List;
       
    30 import org.testng.Assert;
       
    31 import org.testng.annotations.Test;
       
    32 import static java.lang.System.out;
       
    33 import static java.nio.charset.StandardCharsets.UTF_8;
       
    34 import static org.testng.Assert.*;
       
    35 
       
    36 public class FramesDecoderTest {
       
    37 
       
    38     abstract class TestFrameProcessor implements FramesDecoder.FrameProcessor {
       
    39         protected volatile int count;
       
    40         public int numberOfFramesDecoded() { return count; }
       
    41     }
       
    42 
       
    43     /**
       
    44      * Verifies that a ByteBuffer containing more that one frame, destined
       
    45      * to be returned to the user's subscriber, i.e. a data frame, does not
       
    46      * inadvertently expose the following frame ( between its limit and
       
    47      * capacity ).
       
    48      */
       
    49     @Test
       
    50     public void decodeDataFrameFollowedByAnother() throws Exception {
       
    51         // input frames for to the decoder
       
    52         List<ByteBuffer> data1 = List.of(ByteBuffer.wrap("XXXX".getBytes(UTF_8)));
       
    53         DataFrame dataFrame1 = new DataFrame(1, 0, data1);
       
    54         List<ByteBuffer> data2 = List.of(ByteBuffer.wrap("YYYY".getBytes(UTF_8)));
       
    55         DataFrame dataFrame2 = new DataFrame(1, 0, data2);
       
    56 
       
    57         List<ByteBuffer> buffers = new ArrayList<>();
       
    58         FramesEncoder encoder = new FramesEncoder();
       
    59         buffers.addAll(encoder.encodeFrame(dataFrame1));
       
    60         buffers.addAll(encoder.encodeFrame(dataFrame2));
       
    61 
       
    62         ByteBuffer combined = ByteBuffer.allocate(1024);
       
    63         buffers.stream().forEach(combined::put);
       
    64         combined.flip();
       
    65 
       
    66         TestFrameProcessor testFrameProcessor = new TestFrameProcessor() {
       
    67             @Override
       
    68             public void processFrame(Http2Frame frame) throws IOException {
       
    69                 assertTrue(frame instanceof DataFrame);
       
    70                 DataFrame dataFrame = (DataFrame) frame;
       
    71                 List<ByteBuffer> list = dataFrame.getData();
       
    72                 assertEquals(list.size(), 1);
       
    73                 ByteBuffer data = list.get(0);
       
    74                 byte[] bytes = new byte[data.remaining()];
       
    75                 data.get(bytes);
       
    76                 if (count == 0) {
       
    77                     assertEquals(new String(bytes, UTF_8), "XXXX");
       
    78                     out.println("First data received:" + data);
       
    79                     assertEquals(data.position(), data.limit());  // since bytes read
       
    80                     assertEquals(data.limit(), data.capacity());
       
    81                 } else {
       
    82                     assertEquals(new String(bytes, UTF_8), "YYYY");
       
    83                     out.println("Second data received:" + data);
       
    84                 }
       
    85                 count++;
       
    86             }
       
    87         };
       
    88         FramesDecoder decoder = new FramesDecoder(testFrameProcessor);
       
    89 
       
    90         out.println("Sending " + combined + " to decoder: ");
       
    91         decoder.decode(combined);
       
    92         Assert.assertEquals(testFrameProcessor.numberOfFramesDecoded(), 2);
       
    93     }
       
    94 
       
    95 
       
    96     /**
       
    97      * Verifies that a ByteBuffer containing ONLY data one frame, destined
       
    98      * to be returned to the user's subscriber, does not restrict the capacity.
       
    99      * The complete buffer ( all its capacity ), since no longer used by the
       
   100      * HTTP Client, should be returned to the user.
       
   101      */
       
   102     @Test
       
   103     public void decodeDataFrameEnsureNotCapped() throws Exception {
       
   104         // input frames for to the decoder
       
   105         List<ByteBuffer> data1 = List.of(ByteBuffer.wrap("XXXX".getBytes(UTF_8)));
       
   106         DataFrame dataFrame1 = new DataFrame(1, 0, data1);
       
   107 
       
   108         List<ByteBuffer> buffers = new ArrayList<>();
       
   109         FramesEncoder encoder = new FramesEncoder();
       
   110         buffers.addAll(encoder.encodeFrame(dataFrame1));
       
   111 
       
   112         ByteBuffer combined = ByteBuffer.allocate(1024);
       
   113         buffers.stream().forEach(combined::put);
       
   114         combined.flip();
       
   115 
       
   116         TestFrameProcessor testFrameProcessor = new TestFrameProcessor() {
       
   117             @Override
       
   118             public void processFrame(Http2Frame frame) throws IOException {
       
   119                 assertTrue(frame instanceof DataFrame);
       
   120                 DataFrame dataFrame = (DataFrame) frame;
       
   121                 List<ByteBuffer> list = dataFrame.getData();
       
   122                 assertEquals(list.size(), 1);
       
   123                 ByteBuffer data = list.get(0);
       
   124                 byte[] bytes = new byte[data.remaining()];
       
   125                 data.get(bytes);
       
   126                 assertEquals(new String(bytes, UTF_8), "XXXX");
       
   127                 out.println("First data received:" + data);
       
   128                 assertEquals(data.position(), data.limit());  // since bytes read
       
   129                 //assertNotEquals(data.limit(), data.capacity());
       
   130                 assertEquals(data.capacity(), 1024 - 9 /*frame header*/);
       
   131                 count++;
       
   132             }
       
   133         };
       
   134         FramesDecoder decoder = new FramesDecoder(testFrameProcessor);
       
   135 
       
   136         out.println("Sending " + combined + " to decoder: ");
       
   137         decoder.decode(combined);
       
   138         Assert.assertEquals(testFrameProcessor.numberOfFramesDecoded(), 1);
       
   139     }
       
   140 }