// Copyright 2017 The Nomulus Authors. All Rights Reserved. // // Licensed 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 google.registry.whois; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.registrar.Registrar.State.ACTIVE; import static google.registry.model.registrar.Registrar.Type.PDT; import static google.registry.model.registry.Registries.getTlds; import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistSimpleResources; import static google.registry.testing.FullFieldsTestEntityHelper.makeContactResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeDomainResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeHostResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrarContacts; import static google.registry.whois.WhoisHelper.loadWhoisTestFile; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; import static javax.servlet.http.HttpServletResponse.SC_OK; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.appengine.api.datastore.DatastoreFailureException; import com.google.appengine.api.datastore.DatastoreTimeoutException; import google.registry.model.domain.DomainResource; import google.registry.model.ofy.Ofy; import google.registry.model.registrar.Registrar; import google.registry.model.registry.Registry; import google.registry.testing.AppEngineRule; import google.registry.testing.FakeClock; import google.registry.testing.FakeResponse; import google.registry.testing.FakeSleeper; import google.registry.testing.InjectRule; import google.registry.util.Retrier; import google.registry.whois.WhoisMetrics.WhoisMetric; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import org.joda.time.DateTime; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Unit tests for {@link WhoisServer}. */ @RunWith(JUnit4.class) public class WhoisServerTest { @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); @Rule public final InjectRule inject = new InjectRule(); private final FakeResponse response = new FakeResponse(); private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z")); private WhoisServer newWhoisServer(String input) { WhoisServer whoisServer = new WhoisServer(); whoisServer.clock = clock; whoisServer.input = new StringReader(input); whoisServer.response = response; whoisServer.whoisReader = new WhoisReader(new WhoisCommandFactory()); whoisServer.whoisMetrics = new WhoisMetrics(); whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock); whoisServer.disclaimer = "Doodle Disclaimer\nI exist so that carriage return\nin disclaimer can be tested."; whoisServer.retrier = new Retrier(new FakeSleeper(clock), 3); return whoisServer; } @Before public void setUp() throws Exception { createTlds("lol", "xn--q9jyb4c", "1.test"); inject.setStaticField(Ofy.class, "clock", clock); } @Test public void testRun_badRequest_stillSends200() throws Exception { newWhoisServer("\r\n").run(); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getPayload()).isEqualTo(loadWhoisTestFile("whois_server_no_command.txt")); } @Test public void testRun_domainQuery_works() throws Exception { Registrar registrar = persistResource(makeRegistrar( "evilregistrar", "Yes Virginia