### Question:
HashLockedDomainService extends AbstractLockedDomainService { @Override public boolean isGadgetValidForHost(String host, Gadget gadget, String container) { container = getContainer(container); if (isEnabled()) { if (isGadgetReqestingLocking(gadget) || isHostUsingLockedDomain(host) || isDomainLockingEnforced(container)) { if (isRefererCheckEnabled() && !isValidReferer(gadget, container)) { return false; } String neededHost; try { neededHost = getLockedDomain(gadget, container); } catch (GadgetException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Invalid host for call.", e); } return false; } return host.equalsIgnoreCase(neededHost); } } return true; } @Inject  HashLockedDomainService(ContainerConfig config,
          @Named("shindig.locked-domain.enabled") boolean enabled, LockedDomainPrefixGenerator ldGen); @Override String getLockedDomainForGadget(Gadget gadget, String container); @Override boolean isGadgetValidForHost(String host, Gadget gadget, String container); @Override boolean isHostUsingLockedDomain(String host); @Inject(optional = true) void setAuthority(Authority authority); static final String LOCKED_DOMAIN_SUFFIX_KEY; }### Answer:
@Test public void testDisabledGlobally() { replay(); lockedDomainService = new HashLockedDomainService(requiredConfig, false, ldgen); assertTrue(lockedDomainService.isSafeForOpenProxy("anywhere.com")); assertTrue(lockedDomainService.isSafeForOpenProxy("embed.com")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsLocked, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", notLocked, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsSecurityToken, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsBoth, "default")); lockedDomainService = new HashLockedDomainService(enabledConfig, false, ldgen); assertTrue(lockedDomainService.isSafeForOpenProxy("anywhere.com")); assertTrue(lockedDomainService.isSafeForOpenProxy("embed.com")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsLocked, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", notLocked, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsSecurityToken, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("embed.com", wantsBoth, "default")); }
@Test public void testMissingConfig() throws Exception { ContainerConfig containerMissingConfig = new BasicContainerConfig(); containerMissingConfig.newTransaction().addContainer(makeContainer(ContainerConfig.DEFAULT_CONTAINER)).commit(); lockedDomainService = new HashLockedDomainService(containerMissingConfig, true, ldgen); assertFalse(lockedDomainService.isGadgetValidForHost("www.example.com", wantsLocked, "default")); assertTrue(lockedDomainService.isGadgetValidForHost("www.example.com", notLocked, "default")); }
@Test public void testMultiContainer() throws Exception { ContainerConfig inheritsConfig = new BasicContainerConfig(); inheritsConfig .newTransaction() .addContainer( makeContainer(ContainerConfig.DEFAULT_CONTAINER, LOCKED_DOMAIN_SUFFIX_KEY, "-a.example.com:8080", LOCKED_DOMAIN_REQUIRED_KEY, true)) .addContainer(makeContainer("other")) .commit(); lockedDomainService = new HashLockedDomainService(inheritsConfig, true, ldgen); assertFalse(lockedDomainService.isGadgetValidForHost("www.example.com", wantsLocked, "other")); assertFalse(lockedDomainService.isGadgetValidForHost("www.example.com", notLocked, "other")); assertTrue(lockedDomainService.isGadgetValidForHost( "8uhr00296d2o3sfhqilj387krjmgjv3v-a.example.com:8080", wantsLocked, "other")); }