Skip to content

Commit

Permalink
Add number conversion, validation and fallback for parsing conditions.
Browse files Browse the repository at this point in the history
Fix #710.
  • Loading branch information
FelisCatus committed Mar 8, 2016
1 parent eb39515 commit 4bdc62d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
23 changes: 16 additions & 7 deletions omega-pac/src/conditions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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':
Expand All @@ -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']
Expand All @@ -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
91 changes: 91 additions & 0 deletions omega-pac/test/conditions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 4bdc62d

Please sign in to comment.