Commit b992fb57 authored by David Valleau's avatar David Valleau Committed by Commit Bot

Adding verification of user-entered addresses

R=hcarmona@chromium.org, skau@chromium.org

Bug: 780284
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: Ia9508803a34cde2c957cf4ca03047990039e3a29
Reviewed-on: https://chromium-review.googlesource.com/809536Reviewed-by: default avatarHector Carmona <hcarmona@chromium.org>
Reviewed-by: default avatarXiaoqian Dai <xdai@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Commit-Queue: David Valleau <valleau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524185}
parent 62d87e5d
......@@ -183,9 +183,10 @@
on-tap="onCancelTap_">
$i18n{cancelButtonText}
</paper-button>
<paper-button class="action-button"
<paper-button id="addPrinterButton" class="action-button"
on-tap="addPressed_"
disabled="[[!newPrinter.printerName]]">
disabled="[[!canAddPrinter_(newPrinter.printerName,
newPrinter.printerAddress)]]">
$i18n{addPrinterButtonText}
</paper-button>
</div>
......
......@@ -198,6 +198,52 @@ Polymer({
onProtocolChange_: function(event) {
this.set('newPrinter.printerProtocol', event.target.value);
},
/**
* This function uses regular expressions to determine whether the provided
* printer address is valid. Address can be either an ipv4/6 address or a
* hostname followed by an optional port.
* NOTE: The regular expression for hostnames will allow hostnames that are
* over 255 characters.
* @param {String} name
* @param {String} address
* @return {boolean} Whether the add printer button is enabled.
* @private
*/
canAddPrinter_: function(name, address) {
if (!name || !address)
return false;
var hostnamePrefix = '([a-z\\d]|[a-z\\d][a-z\\d\\-]{0,61}[a-z\\d])';
// Matches an arbitrary number of 'prefix patterns' which are separated by a
// dot.
var hostnameSuffix = `(\\.${hostnamePrefix})*`;
// Matches an optional port at the end of the address.
var portNumber = '(:\\d+)?';
var ipv6Full = '(([a-f\\d]){1,4}(:(:)?([a-f\\d]){1,4}){1,7})';
// Special cases for addresses using a shorthand notation.
var ipv6Prefix = '(::([a-f\\d]){1,4})';
var ipv6Suffix = '(([a-f\\d]){1,4}::)';
var ipv6Combined = `(${ipv6Full}|${ipv6Prefix}|${ipv6Suffix})`;
var ipv6WithPort = `(\\[${ipv6Combined}\\]${portNumber})`;
// Matches valid hostnames and ipv4 addresses.
var hostnameRegex =
new RegExp(`^${hostnamePrefix}${hostnameSuffix}${portNumber}$`, 'i');
// Matches valid ipv6 addresses.
var ipv6AddressRegex =
new RegExp(`^(${ipv6Combined}|${ipv6WithPort})$`, 'i');
var invalidIpv6Regex = new RegExp('.*::.*::.*');
return hostnameRegex.test(address) ||
(ipv6AddressRegex.test(address) && !invalidIpv6Regex.test(address));
},
});
Polymer({
......
......@@ -73,10 +73,14 @@ suite('CupsAddPrinterDialogTests', function() {
var address = addDialog.$$('#printerAddressInput');
assertTrue(!!name);
name.value = 'Test Printer';
name.value = 'Test printer';
assertTrue(!!address);
address.value = '127.0.0.1';
var addButton = addDialog.$$('#addPrinterButton');
assertTrue(!!addButton);
assertFalse(addButton.disabled);
}
function clickAddButton(dialog) {
......@@ -141,6 +145,62 @@ suite('CupsAddPrinterDialogTests', function() {
});
});
test('ValidIPV4', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectTrue(dialog.canAddPrinter_('Test printer', '127.0.0.1'));
});
test('ValidIPV4WithPort', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectTrue(dialog.canAddPrinter_('Test printer', '127.0.1.183:1234'));
});
test('ValidIPV6', function() {
var dialog = document.createElement('add-printer-manually-dialog');
// Test the full ipv6 address scheme.
expectTrue(dialog.canAddPrinter_('Test printer', '1:2:a3:ff4:5:6:7:8'));
// Test the shorthand prefix scheme.
expectTrue(dialog.canAddPrinter_('Test printer', '::255'));
// Test the shorthand suffix scheme.
expectTrue(dialog.canAddPrinter_('Test printer', '1::'));
});
test('ValidIPV6WithPort', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectTrue(dialog.canAddPrinter_('Test printer', '[1:2:aa2:4]:12'));
expectTrue(dialog.canAddPrinter_('Test printer', '[::255]:54'));
expectTrue(dialog.canAddPrinter_('Test printer', '[1::]:7899'));
});
test('InvalidIPV6', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectFalse(dialog.canAddPrinter_('Test printer', '1:2:3:4:5:6:7:8:9'));
expectFalse(dialog.canAddPrinter_('Test printer', '1:2:3:aa:a1245:2'));
expectFalse(dialog.canAddPrinter_('Test printer', '1:2:3:za:2'));
expectFalse(dialog.canAddPrinter_('Test printer', '1:::22'));
expectFalse(dialog.canAddPrinter_('Test printer', '1::2::3'));
});
test('ValidHostname', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectTrue(dialog.canAddPrinter_('Test printer', 'hello-world.com'));
expectTrue(dialog.canAddPrinter_('Test printer', 'hello.world.com:12345'));
});
test('InvalidHostname', function() {
var dialog = document.createElement('add-printer-manually-dialog');
expectFalse(dialog.canAddPrinter_('Test printer', 'helloworld!123.com'));
expectFalse(dialog.canAddPrinter_('Test printer', 'helloworld123-.com'));
expectFalse(dialog.canAddPrinter_('Test printer', '-helloworld123.com'));
});
/**
* Test that clicking on Add opens the model select page.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment