1 /*
   2  * Copyright (c) 2016, 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 package jdk.incubator.http.internal.websocket;
  24 
  25 import org.testng.annotations.DataProvider;
  26 
  27 import jdk.incubator.http.internal.websocket.TestSupport.F5;
  28 import java.nio.ByteBuffer;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 import java.util.stream.Stream;
  32 
  33 import static jdk.incubator.http.internal.websocket.TestSupport.cartesianIterator;
  34 import static jdk.incubator.http.internal.websocket.TestSupport.concat;
  35 import static jdk.incubator.http.internal.websocket.TestSupport.iteratorOf;
  36 import static jdk.incubator.http.internal.websocket.TestSupport.iteratorOf1;
  37 import static java.util.List.of;
  38 
  39 /*
  40  * Data providers for WebSocket tests
  41  */
  42 public final class DataProviders {
  43 
  44     /*
  45      * Various ByteBuffer-s to be passed to sendPing/sendPong.
  46      *
  47      * Actual data is put in the middle of the buffer to make sure the code under
  48      * test relies on position/limit rather than on 0 and capacity.
  49      *
  50      *     +-------------------+-------~ ~-------------+--------------+
  51      *     |<---- leading ---->|<------~ ~--- data --->|<- trailing ->|
  52      *     +-------------------+-------~ ~-------------+--------------+
  53      *     ^0                   ^position               ^limit         ^capacity
  54      */
  55     @DataProvider(name = "outgoingData", parallel = true)
  56     public static Iterator<Object[]> outgoingData() {
  57         List<Integer> leading  = of(0, 1, 17, 125);
  58         List<Integer> trailing = of(0, 1, 19, 123);
  59         List<Integer> sizes    = of(0, 1, 2, 17, 32, 64, 122, 123, 124, 125, 126, 127, 128, 256);
  60         List<Boolean> direct   = of(true, false);
  61         List<Boolean> readonly = of(false); // TODO: return readonly (true)
  62         F5<Integer, Integer, Integer, Boolean, Boolean, Object[]> f =
  63                 (l, t, s, d, r) -> {
  64                     ByteBuffer b;
  65                     if (d) {
  66                         b = ByteBuffer.allocateDirect(l + t + s);
  67                     } else {
  68                         b = ByteBuffer.allocate(l + t + s);
  69                     }
  70                     fill(b);
  71                     if (r) {
  72                         b = b.asReadOnlyBuffer();
  73                     }
  74                     b.position(l).limit(l + s);
  75                     return new ByteBuffer[]{b};
  76                 };
  77         Iterator<Object[]> product = cartesianIterator(leading, trailing, sizes, direct, readonly, f);
  78         Iterator<Object[]> i = iteratorOf1(new Object[]{null});
  79         return concat(iteratorOf(i, product));
  80     }
  81 
  82     @DataProvider(name = "incomingData", parallel = true)
  83     public static Iterator<Object[]> incomingData() {
  84         return Stream.of(0, 1, 2, 17, 63, 125)
  85                 .map(i -> new Object[]{fill(ByteBuffer.allocate(i))})
  86                 .iterator();
  87     }
  88 
  89     @DataProvider(name = "incorrectFrame")
  90     public static Iterator<Object[]> incorrectFrame() {
  91         List<Boolean> fin   = of(true, false );
  92         List<Boolean> rsv1  = of(true, false );
  93         List<Boolean> rsv2  = of(true, false );
  94         List<Boolean> rsv3  = of(true, false );
  95         List<Integer> sizes = of(0, 126, 1024);
  96         return cartesianIterator(fin, rsv1, rsv2, rsv3, sizes,
  97                 (a, b, c, d, e) -> new Object[]{a, b, c, d, ByteBuffer.allocate(e)});
  98     }
  99 
 100     private static ByteBuffer fill(ByteBuffer b) {
 101         int i = 0;
 102         while (b.hasRemaining()) {
 103             b.put((byte) (++i & 0xff));
 104         }
 105         return b;
 106     }
 107 }