diff --git a/app/models/ptr.rb b/app/models/ptr.rb index a61dda3..5817b89 100644 --- a/app/models/ptr.rb +++ b/app/models/ptr.rb @@ -1,6 +1,15 @@ class PTR < Record validates :content, presence: true, hostname: true + validate :no_trailing_dot before_validation :remove_terminating_dot + + def no_trailing_dot + # Do not allow PTR record names that end with a dot + return if !short.end_with?(".") + + errors.add(:name, "PTR record name should not end with a dot") + end + end diff --git a/test/models/ptr_test.rb b/test/models/ptr_test.rb index 82ddf84..26cf87e 100644 --- a/test/models/ptr_test.rb +++ b/test/models/ptr_test.rb @@ -1,50 +1,62 @@ require 'test_helper' class PTRTest < ActiveSupport::TestCase class V4PTRTest < ActiveSupport::TestCase setup do @record = build(:v4_ptr) end test 'save' do @record.save assert_empty @record.errors end test 'guess record' do @record.name = @record.domain.name.split(".")[0..-3].reverse.join(".") + ".1" assert @record.save assert_equal @record.name, "1." + @record.domain.name end test 'chop terminating dot' do @record.content = 'with-dot.example.com.' @record.save! @record.reload assert_equal 'with-dot.example.com', @record.content end + + test "name invalid" do + rec = build(:v4_ptr, name: "195.") + rec.valid? + assert_not_empty rec.errors[:name], "name '195.' should be invalid" + end + + test "name valid" do + rec = build(:v4_ptr, name: "195") + rec.valid? + assert_empty rec.errors[:name], "name '195' should be valid" + end end class V6PTRTest < ActiveSupport::TestCase setup do @record = build(:v6_ptr) end test 'save' do @record.save assert_empty @record.errors end test 'guess record' do @record.name = '2001:db' + @record.domain.name.split(".")[0] + '::2' assert @record.save assert_equal '2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.' + @record.domain.name, @record.name end end end diff --git a/test/models/record_test.rb b/test/models/record_test.rb index 1559023..e31dc90 100644 --- a/test/models/record_test.rb +++ b/test/models/record_test.rb @@ -1,19 +1,20 @@ require 'test_helper' class RecordTest < ActiveSupport::TestCase ['text', -1, 0, 2_147_483_647 + 1].each { |ttl| test "ttl invalid #{ttl}" do rec = build(:a, ttl: ttl) rec.valid? assert_not_empty rec.errors[:ttl], "ttl #{ttl} should be invalid!" end } ['', 1, 2_147_483_647].each { |ttl| test "ttl valid #{ttl}" do rec = build(:a, ttl: ttl) rec.valid? assert_empty rec.errors[:ttl], "ttl #{ttl} should be valid!" end } + end