accumulo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] ctubbsii commented on a change in pull request #289: ACCUMULO-4677 Sanitizing PathParam values in REST-based Monitor
Date Thu, 01 Jan 1970 00:00:00 GMT
ctubbsii commented on a change in pull request #289: ACCUMULO-4677 Sanitizing PathParam values
in REST-based Monitor
URL: https://github.com/apache/accumulo/pull/289#discussion_r151855370
 
 

 ##########
 File path: server/monitor/src/test/java/org/apache/accumulo/monitor/view/WebViewsTest.java
 ##########
 @@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.accumulo.monitor.view;
+
+import static org.easymock.EasyMock.expect;
+import static org.powermock.api.easymock.PowerMock.mockStatic;
+import static org.powermock.api.easymock.PowerMock.replayAll;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.accumulo.core.client.Instance;
+import org.apache.accumulo.core.client.impl.Table;
+import org.apache.accumulo.core.client.impl.Tables;
+import org.apache.accumulo.monitor.Monitor;
+import org.apache.accumulo.server.AccumuloServerContext;
+import org.easymock.EasyMock;
+import org.glassfish.jersey.client.ClientConfig;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.TestProperties;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+/**
+ * Basic unit test for parameter validation constraints
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({Monitor.class, Tables.class})
+public class WebViewsTest extends JerseyTest {
+
+  @Override
+  public Application configure() {
+    enable(TestProperties.LOG_TRAFFIC);
+    enable(TestProperties.DUMP_ENTITY);
+    ResourceConfig config = new ResourceConfig(WebViews.class);
+    config.register(org.apache.accumulo.monitor.view.WebViewsTest.HashMapWriter.class);
+    return config;
+  }
+
+  @Override
+  protected void configureClient(ClientConfig config) {
+    super.configureClient(config);
+    config.register(org.apache.accumulo.monitor.view.WebViewsTest.HashMapWriter.class);
+  }
+
+  /**
+   * Expect to fail the constraint validation on the REST endpoint. The constraint is the
pre-defined word character class Pattern so passing a table name with
+   * punctuation will cause a 400 response code.
+   */
+  @Test
+  public void testGetTablesConstraintViolations() {
+    Response output = target("tables/f+o*o").request().get();
+    Assert.assertEquals("should return status 400", 400, output.getStatus());
+  }
+
+  /**
+   * Test path tables/{tableID} which passes constraints. On passing constraints underlying
logic will be executed so we need to mock a certain amount of it.
+   * Note: to get the proper response code back, you need to make sure jersey has a registered
MessageBodyWriter capable of serializing/writing the object
+   * returned from your endpoint. We're using a simple stubbed out inner class HashMapWriter
for this.
+   *
+   * @throws Exception
+   *           not expected
+   */
+  @Test
+  public void testGetTablesConstraintPassing() throws Exception {
+    Instance instanceMock = EasyMock.createMock(Instance.class);
+    expect(instanceMock.getInstanceID()).andReturn("foo").anyTimes();
+    AccumuloServerContext contextMock = EasyMock.createMock(AccumuloServerContext.class);
+    expect(contextMock.getInstance()).andReturn(instanceMock).anyTimes();
+
+    mockStatic(Monitor.class);
+    expect(Monitor.getContext()).andReturn(contextMock).anyTimes();
+
+    mockStatic(Tables.class);
+    expect(Tables.getTableName(instanceMock, new Table.ID("foo"))).andReturn("bar");
+    replayAll();
+    org.easymock.EasyMock.replay(instanceMock, contextMock);
+
+    // Using the mocks we can verify that the getModel method gets called via debugger
+    // however it's difficult to continue to mock through the jersey MVC code for the properly
built response.
+    // Our silly HashMapWriter registered in the configure method gets wired in and used
here.
+    Response output = target("tables/foo").request().get();
+    Assert.assertEquals("should return status 200", 200, output.getStatus());
+    String responseBody = output.readEntity(String.class);
+    Assert.assertTrue(responseBody.contains("tableID=foo") && responseBody.contains("table=bar"));
+  }
+
+  /**
+   * Test minutes parameter constraints. When outside range we should get a 400 response.
+   */
+  @Test
+  public void testGetTracesSummaryValidationConstraint() {
+    // Test upper bounds of constraint
+    Response output = target("trace/summary").queryParam("minutes", 5000000).request().get();
+    Assert.assertEquals("should return status 400", 400, output.getStatus());
+
+    // Test lower bounds of constraint
+    output = target("trace/summary").queryParam("minutes", -27).request().get();
+    Assert.assertEquals("should return status 400", 400, output.getStatus());
+  }
+
+  /**
+   * Silly stub to handle MessageBodyWriter for Hashmap. Registered in configure method and
auto-wired by Jersey.
+   */
+  public static class HashMapWriter implements MessageBodyWriter<HashMap> {
 
 Review comment:
   Should specify generic type parameters here on HashMap (and also line 140 and 145) for
type safety and to avoid introducing new warnings.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message