| 1 | import pytest |
| 2 | import base64 |
| 3 | import os |
| 4 | |
| 5 | from io import StringIO, BytesIO |
| 6 | from bonsai import LDIFReader, LDIFError |
| 7 | |
| 8 | |
| 9 | def test_init_params(): |
| 10 | """Test constructor parameters for LDIFReader.""" |
| 11 | with pytest.raises(TypeError): |
| 12 | _ = LDIFReader("wrong") |
| 13 | with pytest.raises(TypeError): |
| 14 | _ = LDIFReader(StringIO(), max_length=None) |
| 15 | with pytest.raises(TypeError): |
| 16 | _ = LDIFReader(BytesIO()) |
| 17 | inp = StringIO() |
| 18 | ldif = LDIFReader(inp, max_length=100) |
| 19 | assert ldif.input_file == inp |
| 20 | assert ldif.max_length == 100 |
| 21 | |
| 22 | |
| 23 | def test_version(): |
| 24 | """Test setting version attribute from LDIF.""" |
| 25 | text = "version: 1\ndn: cn=test\ncn: test\n" |
| 26 | with StringIO(text) as ldif: |
| 27 | reader = LDIFReader(ldif) |
| 28 | ent = next(reader) |
| 29 | assert reader.version == 1 |
| 30 | assert ent.dn == "cn=test" |
| 31 | |
| 32 | |
| 33 | def test_missing_dn(): |
| 34 | """Test missing distinguished name in LDIF entry.""" |
| 35 | text = "changetype: add\nsn: test\ncn: test\n" |
| 36 | with StringIO(text) as ldif: |
| 37 | reader = LDIFReader(ldif) |
| 38 | with pytest.raises(LDIFError) as excinfo: |
| 39 | _ = next(reader) |
| 40 | assert "Missing distinguished name" in str(excinfo.value) |
| 41 | assert "entry #1" in str(excinfo.value) |
| 42 | |
| 43 | |
| 44 | def test_invalid_file(): |
| 45 | """Test invalid lines.""" |
| 46 | text = " invalid\n" |
| 47 | with StringIO(text) as ldif: |
| 48 | reader = LDIFReader(ldif) |
| 49 | with pytest.raises(LDIFError) as excinfo: |
| 50 | _ = next(reader) |
| 51 | assert "Parser error" in str(excinfo.value) |
| 52 | assert isinstance(excinfo.value.__context__, IndexError) |
| 53 | text = "dn: cn=test\nnotvalid attribute\n" |
| 54 | with StringIO(text) as ldif: |
| 55 | reader = LDIFReader(ldif) |
| 56 | with pytest.raises(LDIFError) as excinfo: |
| 57 | _ = next(reader) |
| 58 | assert "Invalid attribute value pair:" in str(excinfo.value) |
| 59 | assert "entry #1" in str(excinfo.value) |
| 60 | assert "value separator" in str(excinfo.value.__context__) |
| 61 | text = "dn: :cn=test notvalid attribute\n" |
| 62 | with StringIO(text) as ldif: |
| 63 | reader = LDIFReader(ldif) |
| 64 | with pytest.raises(LDIFError) as excinfo: |
| 65 | _ = next(reader) |
| 66 | assert "Invalid attribute value pair:" in str(excinfo.value) |
| 67 | assert "entry #1" in str(excinfo.value) |
| 68 | assert "safe first character" in str(excinfo.value.__context__) |
| 69 | |
| 70 | |
| 71 | def test_invalid_base64(): |
| 72 | """Test LDIF line with invalid base64 data.""" |
| 73 | text = "dn:: cn=test notvalid: attribute\n" |
| 74 | with StringIO(text) as ldif: |
| 75 | reader = LDIFReader(ldif) |
| 76 | with pytest.raises(LDIFError) as excinfo: |
| 77 | _ = next(reader) |
| 78 | assert "Invalid attribute value pair:" in str(excinfo.value) |
| 79 | assert "entry #1" in str(excinfo.value) |
| 80 | assert "Incorrect padding" in str(excinfo.value.__context__) |
| 81 | text = "cn:: dGV4dCx2YWx1ZSxkYXRhNNO" |
| 82 | with StringIO(text) as ldif: |
| 83 | reader = LDIFReader(ldif) |
| 84 | with pytest.raises(LDIFError) as excinfo: |
| 85 | _ = next(reader) |
| 86 | assert "Invalid attribute value pair:" in str(excinfo.value) |
| 87 | assert "entry #1" in str(excinfo.value) |
| 88 | assert "Incorrect padding" in str(excinfo.value.__context__) |
| 89 | |
| 90 | |
| 91 | def test_too_long_line(): |
| 92 | """Test LDIF input with too long line.""" |
| 93 | text = "dn: cn=toolong\n" |
| 94 | with StringIO(text) as ldif: |
| 95 | reader = LDIFReader(ldif, max_length=12) |
| 96 | with pytest.raises(LDIFError) as excinfo: |
| 97 | _ = next(reader) |
| 98 | assert "too long" in str(excinfo.value) |
| 99 | assert "Line 1" in str(excinfo.value) |
| 100 | |
| 101 | |
| 102 | def test_comment(): |
| 103 | """Test parsing comment lines in LDIF files.""" |
| 104 | ldif = "# DN: cn=test\ndn: cn=test\n#Other comment line.\ncn: test\n" |
| 105 | with StringIO(ldif) as test: |
| 106 | reader = LDIFReader(test) |
| 107 | ent = next(reader) |
| 108 | assert ent.dn == "cn=test" |
| 109 | assert ent["cn"] == ["test"] |
| 110 | multiline = "# A long multiline comment\n in an LDIF file.\ndn: cn=test\n" |
| 111 | with StringIO(multiline) as test: |
| 112 | reader = LDIFReader(test) |
| 113 | ent = next(reader) |
| 114 | assert ent.dn == "cn=test" |
| 115 | |
| 116 | |
| 117 | def test_input_file(): |
| 118 | """Test input_file property.""" |
| 119 | inp = StringIO() |
| 120 | ldif = LDIFReader(inp) |
| 121 | assert ldif.input_file == inp |
| 122 | with pytest.raises(TypeError): |
| 123 | ldif.input_file = None |
| 124 | inp2 = StringIO() |
| 125 | ldif.input_file = inp2 |
| 126 | assert ldif.input_file == inp2 |
| 127 | |
| 128 | |
| 129 | def test_autoload(): |
| 130 | """Test autoload property.""" |
| 131 | inp = StringIO() |
| 132 | ldif = LDIFReader(inp) |
| 133 | assert ldif.autoload == True |
| 134 | with pytest.raises(TypeError): |
| 135 | ldif.autoload = "Yes" |
| 136 | ldif.autoload = False |
| 137 | assert ldif.autoload == False |
| 138 | |
| 139 | |
| 140 | def test_resource_handlers(): |
| 141 | """Test resource_handlers property.""" |
| 142 | inp = StringIO() |
| 143 | ldif = LDIFReader(inp) |
| 144 | assert isinstance(ldif.resource_handlers, dict) |
| 145 | assert "file" in ldif.resource_handlers.keys() |
| 146 | with pytest.raises(AttributeError): |
| 147 | ldif.resource_handlers = {"New": "dict"} |
| 148 | ldif.resource_handlers["http"] = lambda x: x |
| 149 | assert "http" in ldif.resource_handlers.keys() |
| 150 | |
| 151 | |
| 152 | def test_multiline_attribute(): |
| 153 | """Test parsing multiline attributes in LDIF.""" |
| 154 | text = "dn: cn=unimaginably+sn=very,ou=very,dc=very,dc=long,\n dc=line\ncn: unimaginably\nsn: very\nsn: long\n" |
| 155 | with StringIO(text) as test: |
| 156 | reader = LDIFReader(test) |
| 157 | ent = next(reader) |
| 158 | assert ent.dn == "cn=unimaginably+sn=very,ou=very,dc=very,dc=long,dc=line" |
| 159 | assert ent["cn"][0] == "unimaginably" |
| 160 | assert ent["sn"][0] == "very" |
| 161 | assert ent["sn"][1] == "long" |
| 162 | |
| 163 | |
| 164 | def test_multiple_entries(): |
| 165 | """Test parsing multiple entries in one LDIF.""" |
| 166 | text = "dn: cn=test1\ncn: test1\n\ndn: cn=test2\ncn: test2\n" |
| 167 | with StringIO(text) as test: |
| 168 | reader = LDIFReader(test) |
| 169 | entries = list(reader) |
| 170 | assert len(entries) == 2 |
| 171 | assert entries[0].dn == "cn=test1" |
| 172 | assert entries[1]["cn"][0] == "test2" |
| 173 | |
| 174 | |
| 175 | def test_encoded_attributes(): |
| 176 | """Test parsing base64 encoded attributes.""" |
| 177 | attr = "test" |
| 178 | text = f"version: 1\ndn: cn=test\ncn:: {base64.b64encode(attr.encode('UTF-8')).decode('UTF-8')}\n" |
| 179 | with StringIO(text) as test: |
| 180 | reader = LDIFReader(test) |
| 181 | ent = next(reader) |
| 182 | assert ent.dn == "cn=test" |
| 183 | assert ent["cn"][0] == attr |
| 184 | |
| 185 | |
| 186 | def test_load_resource(): |
| 187 | """Test load_resource method.""" |
| 188 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 189 | with StringIO() as test: |
| 190 | test.name = "dummy" |
| 191 | reader = LDIFReader(test) |
| 192 | with pytest.raises(LDIFError) as err: |
| 193 | reader.load_resource("ftp://dummy.com") |
| 194 | assert "Unsupported URL format" in str(err) |
| 195 | with pytest.raises(LDIFError) as err: |
| 196 | reader.load_resource("ftp//dummy.com") |
| 197 | assert "Unsupported URL format" in str(err) |
| 198 | url = f"file://{os.path.join(curdir, os.path.join('testenv', 'test.jpeg'))}" |
| 199 | content = reader.load_resource(url) |
| 200 | assert len(content) != 0 |
| 201 | assert isinstance(content, bytes) |
| 202 | |
| 203 | |
| 204 | def test_url_attribute(): |
| 205 | """Test URL attribute in LDIF file.""" |
| 206 | text = "dn: cn=test\ncn: test1\njpegPhoto:< file://./testenv/test.jpeg\n" |
| 207 | with StringIO(text) as test: |
| 208 | test.name = __file__ |
| 209 | reader = LDIFReader(test) |
| 210 | ent = next(reader) |
| 211 | assert ent.dn == "cn=test" |
| 212 | assert len(ent["jpegPhoto"][0]) == 1959 |
| 213 | assert isinstance(ent["jpegPhoto"][0], bytes) |
| 214 | |
| 215 | |
| 216 | def test_changetype(): |
| 217 | """Test changetype attribute in LDIF file.""" |
| 218 | text = "dn: cn=test\nchangetype: add\ncn: test\n" |
| 219 | with StringIO(text) as test: |
| 220 | reader = LDIFReader(test) |
| 221 | ent = next(reader) |
| 222 | assert ent.dn == "cn=test" |
| 223 | assert "cn" in ent |
| 224 | assert "changetype" not in ent |
| 225 | |
| 226 | |
| 227 | def test_missing_attribute(): |
| 228 | """Test missing attribute in LDIF-CHANGE.""" |
| 229 | text = "dn: cn=test\nchangetype: modify\nadd: sn\ncn: test\n" |
| 230 | with StringIO(text) as test: |
| 231 | reader = LDIFReader(test) |
| 232 | with pytest.raises(LDIFError): |
| 233 | _ = next(reader) |
| 234 | |
| 235 | |
| 236 | def test_value_with_colon(): |
| 237 | """Test attribute value with containing colon.""" |
| 238 | text = "dn: cn=test\npostaladdress: p.o. box: 1234\ncn: test\n" |
| 239 | with StringIO(text) as test: |
| 240 | test.name = __file__ |
| 241 | reader = LDIFReader(test) |
| 242 | ent = next(reader) |
| 243 | assert ent.dn == "cn=test" |
| 244 | assert ent["postaladdress"][0] == "p.o. box: 1234" |
| 245 | assert ent["cn"][0] == "test" |
| 246 | |
| 247 | |
| 248 | def test_modify_change(): |
| 249 | """Test loading modified attributes from LDIF-CHANGE.""" |
| 250 | text = """dn: cn=test |
| 251 | changetype: modify |
| 252 | add: sn |
| 253 | sn: testing1 |
| 254 | sn: testing2 |
| 255 | - |
| 256 | replace: uid |
| 257 | uid: tester |
| 258 | - |
| 259 | delete: gidNumber |
| 260 | - |
| 261 | delete: objectclass |
| 262 | objectClass: posixUser |
| 263 | |
| 264 | """ |
| 265 | with StringIO(text) as test: |
| 266 | reader = LDIFReader(test) |
| 267 | ent = next(reader) |
| 268 | status = ent._status() |
| 269 | assert status["sn"]["@status"] == 1 |
| 270 | assert status["sn"]["@added"] == ["testing1", "testing2"] |
| 271 | assert status["sn"]["@deleted"] == [] |
| 272 | assert status["uid"]["@status"] == 2 |
| 273 | assert status["uid"]["@added"] == ["tester"] |
| 274 | assert status["uid"]["@deleted"] == [] |
| 275 | assert status["objectClass"]["@status"] == 1 |
| 276 | assert status["objectClass"]["@added"] == [] |
| 277 | assert status["objectClass"]["@deleted"] == ["posixUser"] |
| 278 | assert status["@deleted_keys"] == ["gidNumber"] |