Add test for valid username

Like a MAC address, a valid username can only contain 6 pairs of
hexadecimal characters, and for homebridge purposes must be
colon-separated. This adds a simple regex test for a proper username,
since I just spent nearly 24h trying to debug an issue that all came
down to not realizing that a MAC address could only have letters A-F
(see https://github.com/nfarina/homebridge/issues/385).
This commit is contained in:
Nathan Henrie
2015-12-27 10:29:21 -07:00
parent f5b790ff35
commit 15bf22805c
+9
View File
@@ -124,6 +124,15 @@ Server.prototype._loadConfig = function() {
throw err; throw err;
} }
var accessoryCount = (config.accessories && config.accessories.length) || 0;
var username = config.bridge.username;
var validMac = /^([0-9A-F]{2}:){5}([0-9A-F]{2})$/;
if (!validMac.test(username)){
throw new Error('Not a valid username: ' + username + '. Must be 6 pairs of colon-' +
'separated hexadecimal chars (A-F 0-9), like a MAC address.');
}
var accessoryCount = (config.accessories && config.accessories.length) || 0; var accessoryCount = (config.accessories && config.accessories.length) || 0;
var platformCount = (config.platforms && config.platforms.length) || 0; var platformCount = (config.platforms && config.platforms.length) || 0;
log.info("Loaded config.json with %s accessories and %s platforms.", accessoryCount, platformCount); log.info("Loaded config.json with %s accessories and %s platforms.", accessoryCount, platformCount);