Add number conversion, validation and fallback for parsing conditions.

Fix #710.
This commit is contained in:
FelisCatus 2016-03-08 15:30:46 +08:00
parent eb3951512d
commit 4bdc62ddff
2 changed files with 107 additions and 7 deletions

View File

@ -524,7 +524,10 @@ module.exports = exports =
fromStr: (str, condition) ->
[ip, prefixLength] = str.split('/')
condition.ip = ip
condition.prefixLength = parseInt(prefixLength)
addr = @parseIp ip
condition.ip = '0.0.0.0' unless addr?
condition.prefixLength = parseInt(prefixLength, 10)
condition.prefixLength = 0 unless condition.prefixLength >= 0
condition
'HostLevelsCondition':
@ -555,8 +558,10 @@ module.exports = exports =
str: (condition) -> condition.minValue + '~' + condition.maxValue
fromStr: (str, condition) ->
[minValue, maxValue] = str.split('~')
condition.minValue = minValue
condition.maxValue = maxValue
condition.minValue = parseInt(minValue, 10)
condition.maxValue = parseInt(maxValue, 10)
condition.minValue = 1 unless condition.minValue > 0
condition.maxValue = 1 unless condition.maxValue > 0
condition
'WeekdayCondition':
@ -580,8 +585,10 @@ module.exports = exports =
str: (condition) -> condition.startDay + '~' + condition.endDay
fromStr: (str, condition) ->
[startDay, endDay] = str.split('~')
condition.startDay = startDay
condition.endDay = endDay
condition.startDay = parseInt(startDay, 10)
condition.endDay = parseInt(endDay, 10)
condition.startDay = 0 unless 0 <= condition.startDay <= 6
condition.endDay = 0 unless 0 <= condition.endDay <= 6
condition
'TimeCondition':
abbrs: ['T', 'Time', 'Hour']
@ -604,7 +611,9 @@ module.exports = exports =
str: (condition) -> condition.startHour + '~' + condition.endHour
fromStr: (str, condition) ->
[startHour, endHour] = str.split('~')
condition.startHour = startHour
condition.endHour = endHour
condition.startHour = parseInt(startHour, 10)
condition.endHour = parseInt(endHour, 10)
condition.startHour = 0 unless 0 <= condition.startHour < 24
condition.endHour = 0 unless 0 <= condition.endHour < 24
condition
# coffeelint: enable=missing_fat_arrows

View File

@ -383,7 +383,98 @@ describe 'Conditions', ->
result = Conditions.str(condition)
result.should.equal('Ip: 127.0.0.1/16')
cond = Conditions.fromStr(result)
console.log typeof cond.prefixLength
cond.should.eql(condition)
it 'should provide sensible fallbacks for invalid IpCondition', ->
cond = Conditions.fromStr('Ip: foo/-233')
cond.should.eql(
conditionType: 'IpCondition'
ip: '0.0.0.0'
prefixLength: 0
)
cond = Conditions.fromStr('Ip: nonsense stuff')
cond.should.eql(
conditionType: 'IpCondition'
ip: '0.0.0.0'
prefixLength: 0
)
it 'should provide sensible fallbacks for invalid IpCondition', ->
cond = Conditions.fromStr('Ip: 0.0.0.0/-233')
cond.should.eql(
conditionType: 'IpCondition'
ip: '0.0.0.0'
prefixLength: 0
)
it 'should encode & decode HostLevelsCondition correctly', ->
condition =
conditionType: 'HostLevelsCondition'
minValue: 4
maxValue: 7
result = Conditions.str(condition)
result.should.equal('HostLevels: 4~7')
cond = Conditions.fromStr(result)
cond.should.eql(condition)
it 'should provide sensible fallbacks for HostLevels out of range', ->
cond = Conditions.fromStr('HostLevels: A~-1')
cond.should.eql(
conditionType: 'HostLevelsCondition'
minValue: 1
maxValue: 1
)
cond = Conditions.fromStr('HostLevels: nonsense')
cond.should.eql(
conditionType: 'HostLevelsCondition'
minValue: 1
maxValue: 1
)
it 'should encode & decode WeekdayCondition correctly', ->
condition =
conditionType: 'WeekdayCondition'
startDay: 3
endDay: 6
result = Conditions.str(condition)
result.should.equal('Weekday: 3~6')
cond = Conditions.fromStr(result)
cond.should.eql(condition)
it 'should provide sensible fallbacks for Weekday out of range', ->
cond = Conditions.fromStr('Weekday: -1~100')
cond.should.eql(
conditionType: 'WeekdayCondition'
startDay: 0
endDay: 0
)
cond = Conditions.fromStr('Weekday: nonsense')
cond.should.eql(
conditionType: 'WeekdayCondition'
startDay: 0
endDay: 0
)
it 'should encode & decode TimeCondition correctly', ->
condition =
conditionType: 'TimeCondition'
startHour: 7
endHour: 23
result = Conditions.str(condition)
result.should.equal('Hour: 7~23')
cond = Conditions.fromStr(result)
cond.should.eql(condition)
it 'should provide sensible fallbacks for Hour out of range', ->
cond = Conditions.fromStr('Hour: -1~100')
cond.should.eql(
conditionType: 'TimeCondition'
startHour: 0
endHour: 0
)
cond = Conditions.fromStr('Hour: nonsense')
cond.should.eql(
conditionType: 'TimeCondition'
startHour: 0
endHour: 0
)
it 'should parse conditions with extra spaces correctly', ->
Conditions.fromStr('url: *abcde* ').should.eql({
conditionType: 'UrlWildcardCondition'