aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_utils.py
blob: 434d6b267666ef00fb29d65d250f13bfa32c3b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import textwrap, cStringIO, os, time, re
import libpry
from libmproxy import utils


class uformat_timestamp(libpry.AutoTree):
    def test_simple(self):
        assert utils.format_timestamp(utils.timestamp())


class uisBin(libpry.AutoTree):
    def test_simple(self):
        assert not utils.isBin("testing\n\r")
        assert utils.isBin("testing\x01")
        assert utils.isBin("testing\x0e")
        assert utils.isBin("testing\x7f")


class uhexdump(libpry.AutoTree):
    def test_simple(self):
        assert utils.hexdump("one\0"*10)


class upretty_size(libpry.AutoTree):
    def test_simple(self):
        assert utils.pretty_size(100) == "100B"
        assert utils.pretty_size(1024) == "1kB"
        assert utils.pretty_size(1024 + (1024/2)) == "1.5kB"
        assert utils.pretty_size(1024*1024) == "1M"


class uData(libpry.AutoTree):
    def test_nonexistent(self):
        libpry.raises("does not exist", utils.data.path, "nonexistent")


class uMultiDict(libpry.AutoTree):
    def setUp(self):
        self.md = utils.MultiDict()

    def test_setget(self):
        assert not self.md.has_key("foo")
        self.md.append("foo", 1)
        assert self.md["foo"] == [1]
        assert self.md.has_key("foo")

    def test_del(self):
        self.md.append("foo", 1)
        del self.md["foo"]
        assert not self.md.has_key("foo")

    def test_extend(self):
        self.md.append("foo", 1)
        self.md.extend("foo", [2, 3])
        assert self.md["foo"] == [1, 2, 3]

    def test_extend_err(self):
        self.md.append("foo", 1)
        libpry.raises("not iterable", self.md.extend, "foo", 2)

    def test_get(self):
        self.md.append("foo", 1)
        self.md.append("foo", 2)
        assert self.md.get("foo") == [1, 2]
        assert self.md.get("bar") == None

    def test_caseSensitivity(self):
        self.md._helper = (utils._caseless,)
        self.md["foo"] = [1]
        self.md.append("FOO", 2)
        assert self.md["foo"] == [1, 2]
        assert self.md["FOO"] == [1, 2]
        assert self.md.has_key("FoO")

    def test_dict(self):
        self.md.append("foo", 1)
        self.md.append("foo", 2)
        self.md["bar"] = [3]
        assert self.md == self.md
        assert dict(self.md) == self.md

    def test_copy(self):
        self.md["foo"] = [1, 2]
        self.md["bar"] = [3, 4]
        md2 = self.md.copy()
        assert md2 == self.md
        assert id(md2) != id(self.md)

    def test_clear(self):
        self.md["foo"] = [1, 2]
        self.md["bar"] = [3, 4]
        self.md.clear()
        assert not self.md.keys()

    def test_setitem(self):
        libpry.raises(ValueError, self.md.__setitem__, "foo", "bar")
        self.md["foo"] = ["bar"]
        assert self.md["foo"] == ["bar"]

    def test_itemPairs(self):
        self.md.append("foo", 1)
        self.md.append("foo", 2)
        self.md.append("bar", 3)
        l = list(self.md.itemPairs())
        assert len(l) == 3
        assert ("foo", 1) in l
        assert ("foo", 2) in l
        assert ("bar", 3) in l

    def test_getset_state(self):
        self.md.append("foo", 1)
        self.md.append("foo", 2)
        self.md.append("bar", 3)
        state = self.md.get_state()
        nd = utils.MultiDict.from_state(state)
        assert nd == self.md


class uHeaders(libpry.AutoTree):
    def setUp(self):
        self.hd = utils.Headers()

    def test_read_simple(self):
        data = """
            Header: one
            Header2: two
            \r\n
        """
        data = textwrap.dedent(data)
        data = data.strip()
        s = cStringIO.StringIO(data)
        self.hd.read(s)
        assert self.hd["header"] == ["one"]
        assert self.hd["header2"] == ["two"]

    def test_read_multi(self):
        data = """
            Header: one
            Header: two
            \r\n
        """
        data = textwrap.dedent(data)
        data = data.strip()
        s = cStringIO.StringIO(data)
        self.hd.read(s)
        assert self.hd["header"] == ["one", "two"]

    def test_read_continued(self):
        data = """
            Header: one
            \ttwo
            Header2: three
            \r\n
        """
        data = textwrap.dedent(data)
        data = data.strip()
        s = cStringIO.StringIO(data)
        self.hd.read(s)
        assert self.hd["header"] == ['one\r\n two']

    def test_dictToHeader1(self):
        self.hd.append("one", "uno")
        self.hd.append("two", "due")
        self.hd.append("two", "tre")
        expected = [
            "one: uno\r\n",
            "two: due\r\n",
            "two: tre\r\n",
            "\r\n"
        ]
        out = repr(self.hd)
        for i in expected:
            assert out.find(i) >= 0
        
    def test_dictToHeader2(self):
        self.hd["one"] = ["uno"]
        expected1 = "one: uno\r\n"
        expected2 = "\r\n"
        out = repr(self.hd)
        assert out.find(expected1) >= 0
        assert out.find(expected2) >= 0

    def test_match_re(self):
        h = utils.Headers()
        h.append("one", "uno")
        h.append("two", "due")
        h.append("two", "tre")
        assert h.match_re("uno")
        assert h.match_re("two: due")
        assert not h.match_re("nonono")

    def test_getset_state(self):
        self.hd.append("foo", 1)
        self.hd.append("foo", 2)
        self.hd.append("bar", 3)
        state = self.hd.get_state()
        nd = utils.Headers.from_state(state)
        assert nd == self.hd


class uisStringLike(libpry.AutoTree):
    def test_all(self):
        assert utils.isStringLike("foo")
        assert not utils.isStringLike([1, 2, 3])
        assert not utils.isStringLike((1, 2, 3))
        assert not utils.isStringLike(["1", "2", "3"])


class uisSequenceLike(libpry.AutoTree):
    def test_all(self):
        assert utils.isSequenceLike([1, 2, 3])
        assert utils.isSequenceLike((1, 2, 3))
        assert not utils.isSequenceLike("foobar")
        assert utils.isSequenceLike(["foobar", "foo"])
        x = iter([1, 2, 3])
        assert utils.isSequenceLike(x)
        assert not utils.isSequenceLike(1)



class upretty_xmlish(libpry.AutoTree):
    def test_tagre(self):
        def f(s):
            return re.search(utils.TAG, s, re.VERBOSE|re.MULTILINE)
        assert f(r"<body>")
        assert f(r"<body/>")
        assert f(r"< body/>")
        assert f(r"< body/ >")
        assert f(r"< body / >")
        assert f(r"<foo a=b>")
        assert f(r"<foo a='b'>")
        assert f(r"<foo a='b\"'>")
        assert f(r'<a b=(a.b) href="foo">')
        assert f('<td width=25%>')
        assert f('<form name="search" action="/search.php" method="get" accept-charset="utf-8" class="search">')
        assert f('<img src="gif" width="125" height="16" alt=&quot;&quot; />')


    def test_all(self):
        def isbalanced(ret):
            # The last tag should have no indent
            assert ret[-1].strip() == ret[-1]

        s = "<html><br><br></br><p>one</p></html>"
        ret = utils.pretty_xmlish(s)
        isbalanced(ret)

        s = r"""
<body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="document.f.q.focus();if(document.images)new Image().src='/images/srpr/nav_logo27.png'" ><textarea id=csi style=display:none></textarea></body>
        """
        isbalanced(utils.pretty_xmlish(textwrap.dedent(s)))

        s = r"""
                <a href="http://foo.com" target="">
                   <img src="http://foo.gif" alt="bar" height="25" width="132">
                </a>
            """
        isbalanced(utils.pretty_xmlish(textwrap.dedent(s)))

        s = r"""
            <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
            \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
            <html></html>
        """
        ret = utils.pretty_xmlish(textwrap.dedent(s))
        isbalanced(ret)

        s = "<html><br/><p>one</p></html>"
        ret = utils.pretty_xmlish(s)
        assert len(ret) == 6
        isbalanced(ret)

        s = "gobbledygook"
        assert utils.pretty_xmlish(s) == ["gobbledygook"]


class udummy_ca(libpry.AutoTree):
    def test_all(self):
        d = self.tmpdir()
        path = os.path.join(d, "foo/cert.cnf")
        assert utils.dummy_ca(path)
        assert os.path.exists(path)


class udummy_cert(libpry.AutoTree):
    def test_with_ca(self):
        d = self.tmpdir()
        cacert = os.path.join(d, "foo/cert.cnf")
        assert utils.dummy_ca(cacert)
        assert utils.dummy_cert(
            os.path.join(d, "foo"),
            cacert,
            "foo.com"
        )
        assert os.path.exists(os.path.join(d, "foo", "foo.com.pem"))
        # Short-circuit
        assert utils.dummy_cert(
            os.path.join(d, "foo"),
            cacert,
            "foo.com"
        )

    def test_no_ca(self):
        d = self.tmpdir()
        assert utils.dummy_cert(
            d,
            None,
            "foo.com"
        )
        assert os.path.exists(os.path.join(d, "foo.com.pem"))


class uLRUCache(libpry.AutoTree):
    def test_one(self):
        class Foo:
            ran = False
            @utils.LRUCache(2)
            def one(self, x):
                self.ran = True
                return x

        f = Foo()
        assert f.one(1) == 1
        assert f.ran
        f.ran = False
        assert f.one(1) == 1
        assert not f.ran

        f.ran = False
        assert f.one(1) == 1
        assert not f.ran
        assert f.one(2) == 2
        assert f.one(3) == 3
        assert f.ran

        f.ran = False
        assert f.one(1) == 1
        assert f.ran

        assert len(f._cached_one) == 2
        assert len(f._cachelist_one) == 2


tests = [
    uformat_timestamp(),
    uisBin(),
    uhexdump(),
    upretty_size(),
    uisStringLike(),
    uisSequenceLike(),
    uMultiDict(),
    uHeaders(),
    uData(),
    upretty_xmlish(),
    udummy_ca(),
    udummy_cert(),
    uLRUCache(),
]