1 /*
  2  * Copyright (c) 2018, 2023, 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 /*
 25  * @test
 26  * @summary Tests response body subscribers's onNext's Lists are unmodifiable,
 27  *          and that the buffers are read-only
 28  * @library /test/lib /test/jdk/java/net/httpclient/lib
 29  * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
 30  * @run testng/othervm ImmutableFlowItems
 31  */
 32 
 33 import java.io.IOException;
 34 import java.io.InputStream;
 35 import java.io.OutputStream;
 36 import java.net.InetAddress;
 37 import java.net.InetSocketAddress;
 38 import java.net.URI;
 39 import java.nio.ByteBuffer;
 40 import java.util.List;
 41 import java.util.concurrent.CompletionStage;
 42 import java.util.concurrent.Flow;
 43 import com.sun.net.httpserver.HttpExchange;
 44 import com.sun.net.httpserver.HttpHandler;
 45 import com.sun.net.httpserver.HttpServer;
 46 import com.sun.net.httpserver.HttpsConfigurator;
 47 import com.sun.net.httpserver.HttpsServer;
 48 import java.net.http.HttpClient;
 49 import java.net.http.HttpHeaders;
 50 import java.net.http.HttpRequest;
 51 import java.net.http.HttpResponse;
 52 import java.net.http.HttpResponse.BodyHandler;
 53 import java.net.http.HttpResponse.BodySubscriber;
 54 import java.net.http.HttpResponse.BodySubscribers;
 55 import javax.net.ssl.SSLContext;
 56 import jdk.httpclient.test.lib.http2.Http2TestServer;
 57 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 58 import jdk.httpclient.test.lib.http2.Http2Handler;
 59 import jdk.test.lib.net.SimpleSSLContext;
 60 import org.testng.annotations.AfterTest;
 61 import org.testng.annotations.BeforeTest;
 62 import org.testng.annotations.DataProvider;
 63 import org.testng.annotations.Test;
 64 import static java.lang.System.out;
 65 import static java.nio.charset.StandardCharsets.UTF_8;
 66 import static org.testng.Assert.*;
 67 
 68 public class ImmutableFlowItems {
 69 
 70     SSLContext sslContext;
 71     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 72     HttpsServer httpsTestServer;       // HTTPS/1.1
 73     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 74     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
 75     String httpURI_fixed;
 76     String httpURI_chunk;
 77     String httpsURI_fixed;
 78     String httpsURI_chunk;
 79     String http2URI_fixed;
 80     String http2URI_chunk;
 81     String https2URI_fixed;
 82     String https2URI_chunk;
 83 
 84     @DataProvider(name = "variants")
 85     public Object[][] variants() {
 86         return new Object[][]{
 87                 { httpURI_fixed   },
 88                 { httpURI_chunk   },
 89                 { httpsURI_fixed  },
 90                 { httpsURI_chunk  },
 91                 { http2URI_fixed  },
 92                 { http2URI_chunk  },
 93                 { https2URI_fixed },
 94                 { https2URI_chunk },
 95         };
 96     }
 97 
 98     static final String BODY = "You'll never plough a field by turning it over in your mind.";
 99 
100     HttpClient newHttpClient() {
101         return HttpClient.newBuilder()
102                 .sslContext(sslContext)
103                 .build();
104     }
105 
106     @Test(dataProvider = "variants")
107     public void testAsString(String uri) throws Exception {
108         HttpClient client = newHttpClient();
109 
110         HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
111                 .build();
112 
113         BodyHandler<String> handler = new CRSBodyHandler();
114         client.sendAsync(req, handler)
115                 .thenApply(HttpResponse::body)
116                 .thenAccept(body -> assertEquals(body, BODY))
117                 .join();
118     }
119 
120     static class CRSBodyHandler implements BodyHandler<String> {
121         @Override
122         public BodySubscriber<String> apply(HttpResponse.ResponseInfo rinfo) {
123             assertEquals(rinfo.statusCode(), 200);
124             return new CRSBodySubscriber();
125         }
126     }
127 
128     static class CRSBodySubscriber implements BodySubscriber<String> {
129         private final BodySubscriber<String> ofString = BodySubscribers.ofString(UTF_8);
130 
131         @Override
132         public void onSubscribe(Flow.Subscription subscription) {
133             ofString.onSubscribe(subscription);
134         }
135 
136         @Override
137         public void onNext(List<ByteBuffer> item) {
138             assertUnmodifiableList(item);
139             long c = item.stream().filter(ByteBuffer::isReadOnly).count();
140             assertEquals(c, item.size(), "Unexpected writable buffer in: " +item);
141             ofString.onNext(item);
142         }
143 
144         @Override
145         public void onError(Throwable throwable) {
146             ofString.onError(throwable);
147         }
148 
149         @Override
150         public void onComplete() {
151             ofString.onComplete();
152         }
153 
154         @Override
155         public CompletionStage<String> getBody() {
156             return ofString.getBody();
157         }
158     }
159 
160     static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
161 
162     static void assertUnmodifiableList(List<ByteBuffer> list) {
163         assertNotNull(list);
164         ByteBuffer b = list.get(0);
165         assertNotNull(b);
166         assertThrows(UOE, () -> list.add(ByteBuffer.wrap(new byte[0])));
167         assertThrows(UOE, () -> list.remove(b));
168     }
169 
170     static String serverAuthority(HttpServer server) {
171         return InetAddress.getLoopbackAddress().getHostName() + ":"
172                 + server.getAddress().getPort();
173     }
174 
175     @BeforeTest
176     public void setup() throws Exception {
177         sslContext = new SimpleSSLContext().get();
178         if (sslContext == null)
179             throw new AssertionError("Unexpected null sslContext");
180 
181         // HTTP/1.1
182         HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
183         HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
184         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
185         httpTestServer = HttpServer.create(sa, 0);
186         httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
187         httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
188         httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/fixed";
189         httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/chunk";
190 
191         httpsTestServer = HttpsServer.create(sa, 0);
192         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
193         httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
194         httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
195         httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/fixed";
196         httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/chunk";
197 
198         // HTTP/2
199         Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
200         Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
201 
202         http2TestServer = new Http2TestServer("localhost", false, 0);
203         http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
204         http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
205         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
206         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
207 
208         https2TestServer = new Http2TestServer("localhost", true, sslContext);
209         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
210         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
211         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
212         https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";
213 
214         httpTestServer.start();
215         httpsTestServer.start();
216         http2TestServer.start();
217         https2TestServer.start();
218     }
219 
220     @AfterTest
221     public void teardown() throws Exception {
222         httpTestServer.stop(0);
223         httpsTestServer.stop(0);
224         http2TestServer.stop();
225         https2TestServer.stop();
226     }
227 
228     static class HTTP1_FixedLengthHandler implements HttpHandler {
229         @Override
230         public void handle(HttpExchange t) throws IOException {
231             out.println("HTTP1_FixedLengthHandler received request to " + t.getRequestURI());
232             try (InputStream is = t.getRequestBody();
233                  OutputStream os = t.getResponseBody()) {
234                 is.readAllBytes();
235                 byte[] bytes = BODY.getBytes(UTF_8);
236                 t.sendResponseHeaders(200, bytes.length);
237                 os.write(bytes);
238             }
239         }
240     }
241 
242     static class HTTP1_ChunkedHandler implements HttpHandler {
243         @Override
244         public void handle(HttpExchange t) throws IOException {
245             out.println("HTTP1_ChunkedHandler received request to " + t.getRequestURI());
246             try (InputStream is = t.getRequestBody();
247                  OutputStream os = t.getResponseBody()) {
248                 is.readAllBytes();
249                 byte[] bytes = BODY.getBytes(UTF_8);
250                 t.sendResponseHeaders(200, 0);
251                 os.write(bytes);
252             }
253         }
254     }
255 
256     static class HTTP2_FixedLengthHandler implements Http2Handler {
257         @Override
258         public void handle(Http2TestExchange t) throws IOException {
259             out.println("HTTP2_FixedLengthHandler received request to " + t.getRequestURI());
260             try (InputStream is = t.getRequestBody();
261                  OutputStream os = t.getResponseBody()) {
262                 is.readAllBytes();
263                 byte[] bytes = BODY.getBytes(UTF_8);
264                 t.sendResponseHeaders(200, bytes.length);
265                 os.write(bytes);
266             }
267         }
268     }
269 
270     static class HTTP2_VariableHandler implements Http2Handler {
271         @Override
272         public void handle(Http2TestExchange t) throws IOException {
273             out.println("HTTP2_VariableHandler received request to " + t.getRequestURI());
274             try (InputStream is = t.getRequestBody();
275                  OutputStream os = t.getResponseBody()) {
276                 is.readAllBytes();
277                 byte[] bytes = BODY.getBytes(UTF_8);
278                 t.sendResponseHeaders(200, 0); // variable
279                 os.write(bytes);
280             }
281         }
282     }
283 }