// 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.rdap; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertThat; import static google.registry.rdap.RdapAuthorization.Role.ADMINISTRATOR; import static google.registry.rdap.RdapAuthorization.Role.REGISTRAR; import static google.registry.request.Action.Method.POST; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistDomainAsDeleted; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResources; import static google.registry.testing.DatastoreHelper.persistSimpleResources; import static google.registry.testing.FullFieldsTestEntityHelper.makeAndPersistContactResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeAndPersistHostResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeDomainResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeHistoryEntry; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrarContacts; import static google.registry.testing.TestDataHelper.loadFile; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.google.appengine.api.users.User; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Range; import com.google.common.net.InetAddresses; import com.googlecode.objectify.Key; import google.registry.model.contact.ContactResource; import google.registry.model.domain.DomainResource; import google.registry.model.domain.Period; import google.registry.model.host.HostResource; import google.registry.model.ofy.Ofy; import google.registry.model.registrar.Registrar; import google.registry.model.registry.Registry; import google.registry.model.reporting.HistoryEntry; import google.registry.rdap.RdapMetrics.EndpointType; import google.registry.rdap.RdapMetrics.SearchType; import google.registry.rdap.RdapMetrics.WildcardType; import google.registry.rdap.RdapSearchResults.IncompletenessWarningType; import google.registry.request.Action; import google.registry.request.auth.AuthLevel; import google.registry.request.auth.AuthResult; import google.registry.request.auth.UserAuthInfo; import google.registry.testing.AppEngineRule; import google.registry.testing.FakeClock; import google.registry.testing.FakeResponse; import google.registry.testing.InjectRule; import google.registry.ui.server.registrar.SessionUtils; import google.registry.util.Idn; import java.net.IDN; import java.net.URLDecoder; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import javax.annotation.Nullable; import javax.servlet.http.HttpServletRequest; import org.joda.time.DateTime; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; 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 RdapDomainSearchAction}. */ @RunWith(JUnit4.class) public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { @Rule public final AppEngineRule appEngine = AppEngineRule.builder() .withDatastore() .build(); @Rule public final InjectRule inject = new InjectRule(); private final HttpServletRequest request = mock(HttpServletRequest.class); private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01T00:00:00Z")); private final SessionUtils sessionUtils = mock(SessionUtils.class); private final User user = new User("rdap.user@example.com", "gmail.com", "12345"); private final UserAuthInfo userAuthInfo = UserAuthInfo.create(user, false); private final UserAuthInfo adminUserAuthInfo = UserAuthInfo.create(user, true); private final RdapDomainSearchAction action = new RdapDomainSearchAction(); private FakeResponse response = new FakeResponse(); private Registrar registrar; private DomainResource domainCatLol; private DomainResource domainCatLol2; private DomainResource domainCatExample; private DomainResource domainIdn; private DomainResource domainMultipart; private ContactResource contact1; private ContactResource contact2; private ContactResource contact3; private HostResource hostNs1CatLol; private HostResource hostNs2CatLol; private HistoryEntry historyEntryCatLolCreate; private Map hostNameToHostMap = new HashMap<>(); enum RequestType { NONE, NAME, NS_LDH_NAME, NS_IP } private Object generateActualJson(RequestType requestType, String paramValue) { return generateActualJson(requestType, paramValue, null); } private Object generateActualJson( RequestType requestType, String paramValue, String cursor) { action.requestPath = RdapDomainSearchAction.PATH; String requestTypeParam = null; switch (requestType) { case NAME: action.nameParam = Optional.of(paramValue); action.nsLdhNameParam = Optional.empty(); action.nsIpParam = Optional.empty(); requestTypeParam = "name"; break; case NS_LDH_NAME: action.nameParam = Optional.empty(); action.nsLdhNameParam = Optional.of(paramValue); action.nsIpParam = Optional.empty(); requestTypeParam = "nsLdhName"; break; case NS_IP: action.nameParam = Optional.empty(); action.nsLdhNameParam = Optional.empty(); action.nsIpParam = Optional.of(paramValue); requestTypeParam = "nsIp"; break; default: action.nameParam = Optional.empty(); action.nsLdhNameParam = Optional.empty(); action.nsIpParam = Optional.empty(); requestTypeParam = ""; break; } if (paramValue != null) { if (cursor == null) { action.parameterMap = ImmutableListMultimap.of(requestTypeParam, paramValue); action.cursorTokenParam = Optional.empty(); } else { action.parameterMap = ImmutableListMultimap.of(requestTypeParam, paramValue, "cursor", cursor); action.cursorTokenParam = Optional.of(cursor); } } action.run(); return JSONValue.parse(response.getPayload()); } private HostResource addHostToMap(HostResource host) { hostNameToHostMap.put(host.getFullyQualifiedHostName(), host); return host; } @Before public void setUp() throws Exception { inject.setStaticField(Ofy.class, "clock", clock); // cat.lol and cat2.lol createTld("lol"); registrar = persistResource( makeRegistrar("evilregistrar", "Yes Virginia