1require 'psych/helper' 2 3module Psych 4 class TestStringTainted < TestCase 5 class Tainted < Handler 6 attr_reader :tc 7 8 def initialize tc 9 @tc = tc 10 end 11 12 def start_document version, tags, implicit 13 tags.flatten.each do |tag| 14 assert_taintedness tag 15 end 16 end 17 18 def alias name 19 assert_taintedness name 20 end 21 22 def scalar value, anchor, tag, plain, quoted, style 23 assert_taintedness value 24 assert_taintedness tag if tag 25 assert_taintedness anchor if anchor 26 end 27 28 def start_sequence anchor, tag, implicit, style 29 assert_taintedness tag if tag 30 assert_taintedness anchor if anchor 31 end 32 33 def start_mapping anchor, tag, implicit, style 34 assert_taintedness tag if tag 35 assert_taintedness anchor if anchor 36 end 37 38 def assert_taintedness thing, message = "'#{thing}' should be tainted" 39 tc.assert thing.tainted?, message 40 end 41 end 42 43 class Untainted < Tainted 44 def assert_taintedness thing, message = "'#{thing}' should not be tainted" 45 tc.assert !thing.tainted?, message 46 end 47 end 48 49 50 def setup 51 handler = Tainted.new self 52 @parser = Psych::Parser.new handler 53 end 54 55 def test_tags_are_tainted 56 assert_taintedness "%TAG !yaml! tag:yaml.org,2002:\n---\n!yaml!str \"foo\"" 57 end 58 59 def test_alias 60 assert_taintedness "--- &ponies\n- foo\n- *ponies" 61 end 62 63 def test_scalar 64 assert_taintedness "--- ponies" 65 end 66 67 def test_anchor 68 assert_taintedness "--- &hi ponies" 69 end 70 71 def test_scalar_tag 72 assert_taintedness "--- !str ponies" 73 end 74 75 def test_seq_start_tag 76 assert_taintedness "--- !!seq [ a ]" 77 end 78 79 def test_seq_start_anchor 80 assert_taintedness "--- &zomg [ a ]" 81 end 82 83 def test_seq_mapping_tag 84 assert_taintedness "--- !!map { a: b }" 85 end 86 87 def test_seq_mapping_anchor 88 assert_taintedness "--- &himom { a: b }" 89 end 90 91 def assert_taintedness string 92 @parser.parse string.taint 93 end 94 end 95 96 class TestStringUntainted < TestStringTainted 97 def setup 98 handler = Untainted.new self 99 @parser = Psych::Parser.new handler 100 end 101 102 def assert_taintedness string 103 @parser.parse string 104 end 105 end 106 107 class TestStringIOUntainted < TestStringTainted 108 def setup 109 handler = Untainted.new self 110 @parser = Psych::Parser.new handler 111 end 112 113 def assert_taintedness string 114 @parser.parse StringIO.new(string) 115 end 116 end 117 118 class TestIOTainted < TestStringTainted 119 def assert_taintedness string 120 t = Tempfile.new(['something', 'yml']) 121 t.binmode 122 t.write string 123 t.close 124 File.open(t.path, 'r:bom|utf-8') { |f| 125 @parser.parse f 126 } 127 t.close(true) 128 end 129 end 130end 131