Bug 1522705 - Add /data/<count> path to HttpBinHandler r=geckoview-reviewers,esawin
authorJames Willcox <snorp@snorp.net>
Fri, 22 Feb 2019 21:45:57 +0000
changeset 518542 69196a59302d12241df753ed581f899e0e0ba217
parent 518541 e657fe2ca2a3d8fe935d3ea7308938b40f94a17c
child 518543 fd8e8a3cbfacf64f6eae71ef13524db194c8c8ec
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, esawin
bugs1522705
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1522705 - Add /data/<count> path to HttpBinHandler r=geckoview-reviewers,esawin This simply lets you request 'count' random bytes. A SHA-256 digest is included for verifying the integrity of the response. Differential Revision: https://phabricator.services.mozilla.com/D19503
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/HttpBinHandler.java
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/HttpBinHandler.java
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/HttpBinHandler.java
@@ -21,17 +21,21 @@ import android.support.annotation.Nullab
 
 import android.util.Log;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Enumeration;
+import java.util.Random;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.handler.AbstractHandler;
 
@@ -158,21 +162,46 @@ class HttpBinHandler extends AbstractHan
 
                 // Body data
                 final ByteArrayOutputStream data = new ByteArrayOutputStream();
                 pipe(is, data);
 
                 response.put("data", data.toString("UTF-8"));
                 respondJSON(servletResponse, os, response);
                 baseRequest.setHandled(true);
+            } else if (uri.startsWith("/bytes")) {
+                pipe(is);
+
+                final int count = Integer.parseInt(uri.substring("/bytes/".length()));
+
+                final Random random = new Random(System.currentTimeMillis());
+                final byte[] payload = new byte[count];
+                random.nextBytes(payload);
+
+                servletResponse.setStatus(HttpServletResponse.SC_OK);
+                servletResponse.setContentLength(count);
+                servletResponse.setContentType("application/octet-stream");
+
+                final byte[] digest = MessageDigest.getInstance("SHA-256").digest(payload);
+                servletResponse.addHeader("X-SHA-256",
+                        String.format("%064x", new BigInteger(1, digest)));
+
+                os.write(payload);
+                os.flush();
+
+                baseRequest.setHandled(true);
             }
 
             if (!baseRequest.isHandled()) {
                 servletResponse.setStatus(501);
                 baseRequest.setHandled(true);
             }
         } catch (JSONException e) {
             Log.e(LOGTAG, "JSON error while handling response", e);
             servletResponse.setStatus(500);
             baseRequest.setHandled(true);
+        } catch (NoSuchAlgorithmException e) {
+            Log.e(LOGTAG, "Failed to generate digest", e);
+            servletResponse.setStatus(500);
+            baseRequest.setHandled(true);
         }
     }
 }