test/jdk/java/net/httpclient/websocket/java.net.http/jdk/internal/net/http/websocket/HeaderWriterTest.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56369 24a8fafec3ff
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
       
     1 /*
       
     2  * Copyright (c) 2016, 2017, 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.internal.net.http.websocket;
       
    25 
       
    26 import org.testng.annotations.Test;
       
    27 import jdk.internal.net.http.websocket.Frame.HeaderWriter;
       
    28 import jdk.internal.net.http.websocket.Frame.Opcode;
       
    29 
       
    30 import java.nio.ByteBuffer;
       
    31 import java.util.OptionalInt;
       
    32 
       
    33 import static java.util.OptionalInt.empty;
       
    34 import static java.util.OptionalInt.of;
       
    35 import static org.testng.Assert.assertEquals;
       
    36 import static jdk.internal.net.http.websocket.TestSupport.assertThrows;
       
    37 import static jdk.internal.net.http.websocket.TestSupport.forEachPermutation;
       
    38 
       
    39 public class HeaderWriterTest {
       
    40 
       
    41     private long cases, frames;
       
    42 
       
    43     @Test
       
    44     public void negativePayload() {
       
    45         System.out.println("testing negative payload");
       
    46         HeaderWriter w = new HeaderWriter();
       
    47         assertThrows(IllegalArgumentException.class,
       
    48                 ".*(?i)negative.*",
       
    49                 () -> w.payloadLen(-1));
       
    50     }
       
    51 
       
    52     @Test
       
    53     public void test() {
       
    54         System.out.println("testing regular payloads");
       
    55         final long[] payloads = {0, 126, 65536, Integer.MAX_VALUE + 1L};
       
    56         final OptionalInt[] masks = {empty(), of(-1), of(0), of(0xCAFEBABE),
       
    57                 of(Integer.MAX_VALUE), of(Integer.MIN_VALUE)};
       
    58         for (boolean fin : new boolean[]{true, false}) {
       
    59             for (boolean rsv1 : new boolean[]{true, false}) {
       
    60                 for (boolean rsv2 : new boolean[]{true, false}) {
       
    61                     for (boolean rsv3 : new boolean[]{true, false}) {
       
    62                         for (Opcode opcode : Opcode.values()) {
       
    63                             for (long payloadLen : payloads) {
       
    64                                 for (OptionalInt mask : masks) {
       
    65                                     verify(fin, rsv1, rsv2, rsv3, opcode, payloadLen, mask);
       
    66                                 }
       
    67                             }
       
    68                         }
       
    69                     }
       
    70                 }
       
    71             }
       
    72         }
       
    73         System.out.println("Frames: " + frames + ", Total cases: " + cases);
       
    74     }
       
    75 
       
    76     private void verify(boolean fin,
       
    77                         boolean rsv1,
       
    78                         boolean rsv2,
       
    79                         boolean rsv3,
       
    80                         Opcode opcode,
       
    81                         long payloadLen,
       
    82                         OptionalInt mask) {
       
    83         frames++;
       
    84         HeaderWriter writer = new HeaderWriter();
       
    85         ByteBuffer expected = ByteBuffer.allocate(Frame.MAX_HEADER_SIZE_BYTES);
       
    86         writer.fin(fin).rsv1(rsv1).rsv2(rsv2).rsv3(rsv3).opcode(opcode).payloadLen(payloadLen);
       
    87         mask.ifPresentOrElse(writer::mask, writer::noMask);
       
    88         writer.write(expected);
       
    89         expected.flip();
       
    90         verifyPermutations(expected, writer,
       
    91                 () -> writer.fin(fin),
       
    92                 () -> writer.rsv1(rsv1),
       
    93                 () -> writer.rsv2(rsv2),
       
    94                 () -> writer.rsv3(rsv3),
       
    95                 () -> writer.opcode(opcode),
       
    96                 () -> writer.payloadLen(payloadLen),
       
    97                 () -> mask.ifPresentOrElse(writer::mask, writer::noMask));
       
    98     }
       
    99 
       
   100     private void verifyPermutations(ByteBuffer expected,
       
   101                                     HeaderWriter writer,
       
   102                                     Runnable... actions) {
       
   103         forEachPermutation(actions.length,
       
   104                 order -> {
       
   105                     cases++;
       
   106                     for (int i : order) {
       
   107                         actions[i].run();
       
   108                     }
       
   109                     ByteBuffer actual = ByteBuffer.allocate(Frame.MAX_HEADER_SIZE_BYTES + 2);
       
   110                     writer.write(actual);
       
   111                     actual.flip();
       
   112                     assertEquals(actual, expected);
       
   113                 });
       
   114     }
       
   115 }