use of com.gargoylesoftware.htmlunit.html.HtmlHiddenInput in project htmlunit by HtmlUnit.
the class ComputedCSSStyleDeclaration method getEmptyHeight.
/**
* Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
* elements.
*
* @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
* elements
*/
private int getEmptyHeight() {
if (height2_ != null) {
return height2_.intValue();
}
final DomNode node = getElement().getDomNodeOrDie();
if (!node.mayBeDisplayed()) {
height2_ = Integer.valueOf(0);
return 0;
}
final String display = getDisplay();
if (NONE.equals(display)) {
height2_ = Integer.valueOf(0);
return 0;
}
final Element elem = getElement();
final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();
if (elem instanceof HTMLBodyElement) {
height2_ = windowHeight;
return windowHeight;
}
final boolean isInline = "inline".equals(display) && !(node instanceof HtmlInlineFrame);
// height is ignored for inline elements
final boolean explicitHeightSpecified = !isInline && !super.getHeight().isEmpty();
int defaultHeight;
if ((elem.getClass() == HTMLElement.class || elem instanceof HTMLDivElement || elem instanceof HTMLUnknownElement || elem instanceof HTMLDataElement || elem instanceof HTMLTimeElement || elem instanceof HTMLOutputElement || elem instanceof HTMLSlotElement || elem instanceof HTMLLegendElement) && StringUtils.isBlank(node.getTextContent())) {
defaultHeight = 0;
} else if (elem.getFirstChild() == null) {
if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
final BrowserVersion browser = getBrowserVersion();
if (browser.hasFeature(JS_CLIENTHEIGHT_RADIO_CHECKBOX_10)) {
defaultHeight = 10;
} else {
defaultHeight = 13;
}
} else if (node instanceof HtmlButton) {
defaultHeight = 20;
} else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
final BrowserVersion browser = getBrowserVersion();
if (browser.hasFeature(JS_CLIENTHEIGHT_INPUT_17)) {
defaultHeight = 17;
} else if (browser.hasFeature(JS_CLIENTHEIGHT_INPUT_18)) {
defaultHeight = 18;
} else {
defaultHeight = 20;
}
} else if (node instanceof HtmlSelect) {
defaultHeight = 20;
} else if (node instanceof HtmlTextArea) {
defaultHeight = 49;
} else if (node instanceof HtmlInlineFrame) {
defaultHeight = 154;
} else {
defaultHeight = 0;
}
} else {
final String fontSize = getFontSize();
defaultHeight = getBrowserVersion().getFontHeight(fontSize);
if (node instanceof HtmlDivision || node instanceof HtmlSpan) {
String width = getStyleAttribute(WIDTH, false);
// maybe we are enclosed something that forces a width
Element parent = getElement().getParentElement();
while (width.isEmpty() && parent != null) {
width = getWindow().getComputedStyle(parent, null).getStyleAttribute(WIDTH, false);
parent = parent.getParentElement();
}
final int pixelWidth = pixelValue(width);
final String content = node.getVisibleText();
if (pixelWidth > 0 && !width.isEmpty() && StringUtils.isNotBlank(content)) {
final String[] lines = StringUtils.split(content, '\n');
int lineCount = 0;
final int fontSizeInt = Integer.parseInt(fontSize.substring(0, fontSize.length() - 2));
final FontRenderContext fontRenderCtx = new FontRenderContext(null, false, true);
for (final String line : lines) {
if (StringUtils.isBlank(line)) {
lineCount++;
} else {
// width is specified, we have to to some line breaking
final AttributedString attributedString = new AttributedString(line);
attributedString.addAttribute(TextAttribute.SIZE, fontSizeInt / 1.1);
final LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedString.getIterator(), fontRenderCtx);
lineBreakMeasurer.nextLayout(pixelWidth);
lineCount++;
while (lineBreakMeasurer.getPosition() < line.length() && lineCount < 1000) {
lineBreakMeasurer.nextLayout(pixelWidth);
lineCount++;
}
}
}
defaultHeight *= lineCount;
} else {
if (node instanceof HtmlSpan && StringUtils.isEmpty(content)) {
defaultHeight = 0;
} else {
defaultHeight *= StringUtils.countMatches(content, '\n') + 1;
}
}
}
}
final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;
int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
@Override
public String get(final ComputedCSSStyleDeclaration style) {
final Element element = style.getElement();
if (element instanceof HTMLBodyElement) {
return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
}
// height is ignored for inline elements
if (isInline) {
return "";
}
return style.getStyleAttribute(HEIGHT, true);
}
});
if (height == 0 && !explicitHeightSpecified) {
height = defaultHeight;
}
height2_ = Integer.valueOf(height);
return height;
}
use of com.gargoylesoftware.htmlunit.html.HtmlHiddenInput in project jenkins by jenkinsci.
the class PasswordTest method testBackgroundSecretConversion.
@Test
public void testBackgroundSecretConversion() throws Exception {
final JenkinsRule.WebClient wc = j.createWebClient();
j.configRoundtrip();
// empty default values
assertEquals("", PasswordHolderConfiguration.getInstance().secretWithSecretGetterAndSetter.getPlainText());
assertEquals("", PasswordHolderConfiguration.getInstance().secretWithStringGetterAndSetter.getPlainText());
assertEquals("", PasswordHolderConfiguration.getInstance().stringWithSecretGetterAndSetter);
assertEquals("", PasswordHolderConfiguration.getInstance().stringWithStringGetterAndSetter);
// set some values and expect them to remain after round-trip
final Secret secretWithSecretGetterAndSetter = Secret.fromString("secretWithSecretGetterAndSetter");
// ensure IV is set so the encrypted value is stable
secretWithSecretGetterAndSetter.getEncryptedValue();
PasswordHolderConfiguration.getInstance().secretWithSecretGetterAndSetter = secretWithSecretGetterAndSetter;
final Secret secretWithStringGetterAndSetter = Secret.fromString("secretWithStringGetterAndSetter");
// ensure IV is set so the encrypted value is stable
secretWithStringGetterAndSetter.getEncryptedValue();
PasswordHolderConfiguration.getInstance().secretWithStringGetterAndSetter = secretWithStringGetterAndSetter;
PasswordHolderConfiguration.getInstance().stringWithSecretGetterAndSetter = "stringWithSecretGetterAndSetter";
PasswordHolderConfiguration.getInstance().stringWithStringGetterAndSetter = "stringWithStringGetterAndSetter";
final HtmlPage configPage = wc.goTo("configure");
for (DomElement element : configPage.getElementsByTagName("input")) {
if ("hidden".equals(element.getAttribute("type")) && element.getAttribute("class").contains("complex-password-field")) {
final HtmlHiddenInput input = (HtmlHiddenInput) element;
// assert that all password fields contain encrypted values after we set plain values
assertTrue(input.getValueAttribute().startsWith("{"));
assertTrue(input.getValueAttribute().endsWith("}"));
}
}
j.configRoundtrip();
// confirm round-trip did not change effective values
assertEquals("secretWithSecretGetterAndSetter", PasswordHolderConfiguration.getInstance().secretWithSecretGetterAndSetter.getPlainText());
assertEquals("secretWithStringGetterAndSetter", PasswordHolderConfiguration.getInstance().secretWithStringGetterAndSetter.getPlainText());
assertEquals("stringWithSecretGetterAndSetter", PasswordHolderConfiguration.getInstance().stringWithSecretGetterAndSetter);
assertEquals("stringWithStringGetterAndSetter", PasswordHolderConfiguration.getInstance().stringWithStringGetterAndSetter);
assertEquals(secretWithSecretGetterAndSetter.getEncryptedValue(), PasswordHolderConfiguration.getInstance().secretWithSecretGetterAndSetter.getEncryptedValue());
// The following is because the serialized "Secret" value in the form gets decrypted, losing IV, to be passed as String into the setter, to be converted to Secret, getting new IV in #getEncryptedValue call.
assertNotEquals(secretWithStringGetterAndSetter.getEncryptedValue(), PasswordHolderConfiguration.getInstance().secretWithStringGetterAndSetter.getEncryptedValue());
}
use of com.gargoylesoftware.htmlunit.html.HtmlHiddenInput in project jenkins by jenkinsci.
the class PasswordTest method testStringlyTypedSecrets.
@Test
public void testStringlyTypedSecrets() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new StringlyTypedSecretsBuilder(""));
project.save();
assertEquals(1, project.getBuilders().size());
j.configRoundtrip(project);
// empty default values after initial form submission
StringlyTypedSecretsBuilder buildStep = (StringlyTypedSecretsBuilder) project.getBuildersList().get(0);
assertNotNull(buildStep);
assertTrue(buildStep.mySecret.startsWith("{"));
assertTrue(buildStep.mySecret.endsWith("}"));
assertTrue(Secret.fromString(buildStep.mySecret).getPlainText().isEmpty());
// set a value and expect it to remain after round-trip
final Secret stringlyTypedSecret = Secret.fromString("stringlyTypedSecret");
// ensure IV is set so the encrypted value is stable
stringlyTypedSecret.getEncryptedValue();
buildStep.mySecret = stringlyTypedSecret.getEncryptedValue();
project.save();
final HtmlPage configPage = j.createWebClient().goTo(project.getUrl() + "/configure");
for (DomElement element : configPage.getElementsByTagName("input")) {
if ("hidden".equals(element.getAttribute("type")) && element.getAttribute("class").contains("complex-password-field")) {
final HtmlHiddenInput input = (HtmlHiddenInput) element;
// assert that all password fields contain encrypted values after we set plain values
assertTrue(input.getValueAttribute().startsWith("{"));
assertTrue(input.getValueAttribute().endsWith("}"));
}
}
j.configRoundtrip(project);
buildStep = (StringlyTypedSecretsBuilder) project.getBuildersList().get(0);
// confirm round-trip did not change effective values
assertEquals("stringlyTypedSecret", Secret.fromString(buildStep.mySecret).getPlainText());
// Unfortunately the constructor parameter will be decrypted transparently now, so this is sort of a minor regression with this enhancement.
// Note that it's not enough to just undo the related changes to core/src/main to try this; as Functions#getPasswordValue will throw a SecurityException during tests only and break the previous assertion.
assertNotEquals(stringlyTypedSecret.getEncryptedValue(), buildStep.mySecret);
}
use of com.gargoylesoftware.htmlunit.html.HtmlHiddenInput in project jenkins by jenkinsci.
the class PasswordTest method testBuildStep.
@Test
public void testBuildStep() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new PasswordHolderBuildStep());
project.save();
assertEquals(1, project.getBuilders().size());
j.configRoundtrip(project);
// empty default values after initial form submission
PasswordHolderBuildStep buildStep = (PasswordHolderBuildStep) project.getBuildersList().get(0);
assertNotNull(buildStep);
assertEquals("", buildStep.secretWithSecretGetterSecretSetter.getPlainText());
assertEquals("", buildStep.secretWithSecretGetterStringSetter.getPlainText());
assertEquals("", buildStep.secretWithStringGetterSecretSetter.getPlainText());
assertEquals("", buildStep.secretWithStringGetterStringSetter.getPlainText());
assertEquals("", buildStep.stringWithSecretGetterSecretSetter);
assertEquals("", buildStep.stringWithSecretGetterStringSetter);
assertEquals("", buildStep.stringWithStringGetterSecretSetter);
assertEquals("", buildStep.stringWithStringGetterStringSetter);
buildStep = (PasswordHolderBuildStep) project.getBuildersList().get(0);
assertNotNull(buildStep);
// set some values and expect them to remain after round-trip
final Secret secretWithSecretGetterSecretSetter = Secret.fromString("secretWithSecretGetterSecretSetter");
// ensure IV is set so the encrypted value is stable
secretWithSecretGetterSecretSetter.getEncryptedValue();
buildStep.secretWithSecretGetterSecretSetter = secretWithSecretGetterSecretSetter;
final Secret secretWithStringGetterStringSetter = Secret.fromString("secretWithStringGetterStringSetter");
// ensure IV is set so the encrypted value is stable
secretWithStringGetterStringSetter.getEncryptedValue();
buildStep.secretWithStringGetterStringSetter = secretWithStringGetterStringSetter;
final Secret secretWithStringGetterSecretSetter = Secret.fromString("secretWithStringGetterSecretSetter");
// ensure IV is set so the encrypted value is stable
secretWithStringGetterSecretSetter.getEncryptedValue();
buildStep.secretWithStringGetterSecretSetter = secretWithStringGetterSecretSetter;
final Secret secretWithSecretGetterStringSetter = Secret.fromString("secretWithSecretGetterStringSetter");
// ensure IV is set so the encrypted value is stable
secretWithSecretGetterStringSetter.getEncryptedValue();
buildStep.secretWithSecretGetterStringSetter = secretWithSecretGetterStringSetter;
buildStep.stringWithSecretGetterSecretSetter = "stringWithSecretGetterSecretSetter";
buildStep.stringWithStringGetterStringSetter = "stringWithStringGetterStringSetter";
buildStep.stringWithStringGetterSecretSetter = "stringWithStringGetterSecretSetter";
buildStep.stringWithSecretGetterStringSetter = "stringWithSecretGetterStringSetter";
project.save();
final HtmlPage configPage = j.createWebClient().goTo(project.getUrl() + "/configure");
int i = 0;
for (DomElement element : configPage.getElementsByTagName("input")) {
if ("hidden".equals(element.getAttribute("type")) && element.getAttribute("class").contains("complex-password-field")) {
final HtmlHiddenInput input = (HtmlHiddenInput) element;
// assert that all password fields contain encrypted values after we set plain values
assertTrue(input.getValueAttribute().startsWith("{"));
assertTrue(input.getValueAttribute().endsWith("}"));
i++;
}
}
// at least 8 password fields expected on that job config form
assertTrue(i >= 8);
j.configRoundtrip(project);
buildStep = (PasswordHolderBuildStep) project.getBuildersList().get(0);
// confirm round-trip did not change effective values
assertEquals("secretWithSecretGetterSecretSetter", buildStep.secretWithSecretGetterSecretSetter.getPlainText());
assertEquals("secretWithStringGetterStringSetter", buildStep.secretWithStringGetterStringSetter.getPlainText());
assertEquals("secretWithStringGetterSecretSetter", buildStep.secretWithStringGetterSecretSetter.getPlainText());
assertEquals("secretWithSecretGetterStringSetter", buildStep.secretWithSecretGetterStringSetter.getPlainText());
assertEquals("stringWithSecretGetterSecretSetter", buildStep.stringWithSecretGetterSecretSetter);
assertEquals("stringWithStringGetterStringSetter", buildStep.stringWithStringGetterStringSetter);
assertEquals("stringWithStringGetterSecretSetter", buildStep.stringWithStringGetterSecretSetter);
assertEquals("stringWithSecretGetterStringSetter", buildStep.stringWithSecretGetterStringSetter);
// confirm the Secret getter/setter will not change encrypted value (keeps IV)
assertEquals(secretWithSecretGetterSecretSetter.getEncryptedValue(), buildStep.secretWithSecretGetterSecretSetter.getEncryptedValue());
// This depends on implementation; if the Getter returns the plain text (to be re-encrypted by Functions#getPasswordValue), then this won't work,
// but if the getter returns #getEncrytedValue (as implemented in the build step here), it does.
// While clever, would recommend fixing mismatched getters/setters here
assertEquals(secretWithStringGetterSecretSetter.getEncryptedValue(), buildStep.secretWithStringGetterSecretSetter.getEncryptedValue());
// This isn't equal because we expect that the code cannot handle an encrypted secret value passed to the setter, so we decrypt it
assertNotEquals(secretWithStringGetterStringSetter.getEncryptedValue(), buildStep.secretWithStringGetterStringSetter.getEncryptedValue());
}
use of com.gargoylesoftware.htmlunit.html.HtmlHiddenInput in project jenkins by jenkinsci.
the class PasswordTest method testBlankoutOfStringBackedPasswordFieldWithoutItemConfigure.
@Test
public void testBlankoutOfStringBackedPasswordFieldWithoutItemConfigure() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
JenkinsRule.WebClient wc = j.createWebClient();
HtmlPage htmlPage = wc.goTo(p.getUrl() + "/passwordFields");
for (DomElement element : htmlPage.getElementsByTagName("input")) {
if ("hidden".equals(element.getAttribute("type")) && element.getAttribute("class").contains("complex-password-field")) {
final HtmlHiddenInput input = (HtmlHiddenInput) element;
// assert that all password fields contain encrypted values after we set plain values
assertTrue(input.getValueAttribute().startsWith("{"));
assertTrue(input.getValueAttribute().endsWith("}"));
}
}
final MockAuthorizationStrategy a = new MockAuthorizationStrategy();
a.grant(Jenkins.READ, Item.READ, Item.EXTENDED_READ).everywhere().toEveryone();
j.jenkins.setAuthorizationStrategy(a);
/* Now go to the page without Item/Configure and expect asterisks */
htmlPage = wc.goTo(p.getUrl() + "/passwordFields");
for (DomElement element : htmlPage.getElementsByTagName("input")) {
if ("hidden".equals(element.getAttribute("type")) && element.getAttribute("class").contains("complex-password-field")) {
final HtmlHiddenInput input = (HtmlHiddenInput) element;
assertEquals("********", input.getValueAttribute());
}
}
}
Aggregations