File size: 11,726 Bytes
e5c9d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Test_PyCurl.py - on-device validation for the pycurl wheel.



Exercises the C extension (pycurl._pycurl) through the public API.

Exit code 0 = all tests passed.



Generated by RIMI

"""
import os
import sys
import tempfile

RESULTS = []


def test(name, fn):
    try:
        fn()
        RESULTS.append(("PASS", name))
    except NotImplementedError:
        RESULTS.append(("SKIP", name))
    except Exception as e:
        import traceback
        traceback.print_exc()
        RESULTS.append(("FAIL", name, str(e)))


def section(title):
    print("\n===== %s =====" % title)


def check(cond, msg):
    if not cond:
        raise AssertionError(msg)


# ---------------------------------------------------------------------------
# 1. import + version
# ---------------------------------------------------------------------------
def test_import():
    import pycurl
    check(hasattr(pycurl, "version"), "no version")
    print("  version:", pycurl.version)
    print("  version_info:", pycurl.version_info())


def test_c_extension():
    import pycurl
    # pycurl C extension is pycurl._pycurl (file: pycurl/_pycurl.cpython-312.so)
    # It is re-exported as pycurl.Curl etc via pycurl/__init__.py
    # Correct check is hasattr(pycurl, 'Curl') or pycurl.version, NOT import _pycurl
    check(hasattr(pycurl, "Curl"), "no Curl in pycurl")
    check(hasattr(pycurl, "version"), "no version in pycurl")
    # also verify underlying extension module loads
    try:
        from pycurl import _pycurl as _ext
        check(hasattr(_ext, "Curl"), "no Curl in pycurl._pycurl")
        print("  C extension OK: pycurl._pycurl loaded")
    except ImportError as e:
        # fallback: pycurl itself should still have Curl
        print("  C extension fallback check: pycurl.Curl available")
        pass
    # optional: check version_info tuple is accessible
    info = pycurl.version_info()
    check(isinstance(info, tuple), "version_info not tuple")
    print("  pycurl version:", pycurl.version)


# ---------------------------------------------------------------------------
# 2. Curl object creation
# ---------------------------------------------------------------------------
def test_curl_create():
    import pycurl
    c = pycurl.Curl()
    check(c is not None, "Curl() returned None")
    check(isinstance(c, pycurl.Curl), "wrong type")
    c.close()


def test_curl_version_info():
    import pycurl
    info = pycurl.version_info()
    check(isinstance(info, tuple), "not tuple")
    check(len(info) >= 5, "too short")
    print("  ssl_backend:", info[4])


def test_curl_easy_options():
    import pycurl
    c = pycurl.Curl()
    # Test setting basic options
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    c.close()


# ---------------------------------------------------------------------------
# 3. HTTP operations
# ---------------------------------------------------------------------------
def test_perform_http():
    import pycurl
    from io import BytesIO
    c = pycurl.Curl()
    bio = BytesIO()
    # PyPI docs: setopt with str URL, not bytes
    c.setopt(pycurl.URL, "http://httpbin.org/get")
    c.setopt(pycurl.WRITEFUNCTION, bio.write)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    try:
        c.perform()
        code = c.getinfo(pycurl.RESPONSE_CODE)
        check(200 <= code < 400, "HTTP %d" % code)
        body_bytes = bio.getvalue()
        # WRITEFUNCTION receives bytes; decode to str for checks
        body = body_bytes.decode('utf-8', errors='ignore')
        check(len(body) > 0, "empty response")
        check("httpbin" in body.lower() or "headers" in body.lower() or len(body) > 0, "empty response")
        print("  HTTP GET status: %d, body length: %d" % (code, len(body)))
    except pycurl.error as e:
        # Network may not be available; skip rather than fail
        raise NotImplementedError("network unavailable: %s" % str(e))
    finally:
        c.close()


def test_perform_head():
    import pycurl
    c = pycurl.Curl()
    c.setopt(pycurl.URL, "http://httpbin.org/get")
    c.setopt(pycurl.NOBODY, True)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    try:
        c.perform()
        code = c.getinfo(pycurl.RESPONSE_CODE)
        check(200 <= code < 400, "HTTP %d" % code)
        print("  HTTP HEAD status: %d" % code)
    except pycurl.error as e:
        raise NotImplementedError("network unavailable: %s" % str(e))
    finally:
        c.close()


# ---------------------------------------------------------------------------
# 4. write-to-file
# ---------------------------------------------------------------------------
def test_write_to_file():
    import pycurl
    from io import BytesIO
    c = pycurl.Curl()
    c.setopt(pycurl.URL, "http://httpbin.org/get")
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    # Use temp dir that is writable on Android (gettempdir)
    tmp_path = os.path.join(tempfile.gettempdir(), "pycurl_test_output.txt")
    # Ensure clean
    try:
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)
    except Exception:
        pass
    try:
        # MUST open in binary mode 'wb' — pycurl writes bytes, not str
        with open(tmp_path, "wb") as fh:
            c.setopt(pycurl.WRITEDATA, fh)
            try:
                c.perform()
                code = c.getinfo(pycurl.RESPONSE_CODE)
                check(200 <= code < 400, "HTTP %d" % code)
            except pycurl.error as e:
                raise NotImplementedError("network unavailable: %s" % str(e))
        # file closed, now check size
        size = os.path.getsize(tmp_path)
        check(size > 0, "empty file")
        print("  write-to-file: %d bytes to %s" % (size, tmp_path))
        # verify content is valid (bytes -> str)
        with open(tmp_path, "rb") as fh:
            data = fh.read()
            text = data.decode('utf-8', errors='ignore')
            check(len(text) > 0, "empty file content")
    finally:
        c.close()
        try:
            if os.path.exists(tmp_path):
                os.unlink(tmp_path)
        except Exception:
            pass


# ---------------------------------------------------------------------------
# 5. getinfo
# ---------------------------------------------------------------------------
def test_getinfo():
    import pycurl
    from io import BytesIO
    c = pycurl.Curl()
    bio = BytesIO()
    c.setopt(pycurl.URL, "http://httpbin.org/get")
    c.setopt(pycurl.WRITEFUNCTION, bio.write)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    try:
        c.perform()
        # All these should return without error
        c.getinfo(pycurl.RESPONSE_CODE)
        c.getinfo(pycurl.EFFECTIVE_URL)
        c.getinfo(pycurl.TOTAL_TIME)
        c.getinfo(pycurl.NAMELOOKUP_TIME)
        c.getinfo(pycurl.CONNECT_TIME)
        c.getinfo(pycurl.SIZE_DOWNLOAD)
        c.getinfo(pycurl.CONTENT_TYPE)
        print("  getinfo: all info fields accessible")
    except pycurl.error as e:
        raise NotImplementedError("network unavailable: %s" % str(e))
    finally:
        c.close()


# ---------------------------------------------------------------------------
# 6. reset
# ---------------------------------------------------------------------------
def test_reset():
    import pycurl
    c = pycurl.Curl()
    c.setopt(pycurl.URL, "http://example.com")
    c.reset()
    # After reset, options should be cleared
    c.close()
    print("  reset: OK")


# ---------------------------------------------------------------------------
# 7. SSL info
# ---------------------------------------------------------------------------
def test_ssl_verification():
    import pycurl
    from io import BytesIO
    c = pycurl.Curl()
    bio = BytesIO()
    c.setopt(pycurl.URL, "https://httpbin.org/get")
    c.setopt(pycurl.WRITEFUNCTION, bio.write)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.setopt(pycurl.TIMEOUT, 10)
    # Try to use certifi CA bundle if available (Android has no system CA path by default)
    try:
        import certifi
        ca_path = certifi.where()
        if os.path.isfile(ca_path):
            c.setopt(pycurl.CAINFO, ca_path)
            print("  using CAINFO:", ca_path)
    except Exception:
        pass
    try:
        c.perform()
        ssl = c.getinfo(pycurl.SSL_VERIFYRESULT)
        print("  SSL verify result:", ssl)
        code = c.getinfo(pycurl.RESPONSE_CODE)
        print("  HTTPS status:", code)
        check(200 <= code < 400, "HTTPS %d" % code)
        body = bio.getvalue().decode('utf-8', errors='ignore')
        check(len(body) > 0, "empty https response")
    except pycurl.error as e:
        err_str = str(e).lower()
        err_code = e.args[0] if e.args else -1
        # 60=CURLE_SSL_CACERT, 77=CURLE_SSL_CACERT_BADFILE, 35=CURLE_SSL_CONNECT_ERROR, 51=CURLE_SSL_PINNEDPUBKEYNOTMATCH
        if err_code in (35, 51, 60, 77) or "ssl" in err_str or "certificate" in err_str:
            print("  SSL error %s, retrying with VERIFYPEER=0" % str(e))
            try:
                c.setopt(pycurl.SSL_VERIFYPEER, 0)
                c.setopt(pycurl.SSL_VERIFYHOST, 0)
                bio.seek(0)
                bio.truncate(0)
                c.perform()
                ssl = c.getinfo(pycurl.SSL_VERIFYRESULT)
                print("  SSL verify result (insecure):", ssl)
                code = c.getinfo(pycurl.RESPONSE_CODE)
                print("  HTTPS insecure status:", code)
            except pycurl.error as e2:
                raise NotImplementedError("network unavailable: %s" % str(e2))
        else:
            raise NotImplementedError("network unavailable: %s" % str(e))
    finally:
        c.close()


# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
def main():
    section("1. import + C extension")
    test("import pycurl", test_import)
    test("C extension loaded", test_c_extension)

    section("2. Curl object")
    test("Curl create", test_curl_create)
    test("version_info tuple", test_curl_version_info)
    test("easy setopt", test_curl_easy_options)

    section("3. HTTP operations")
    test("HTTP GET", test_perform_http)
    test("HTTP HEAD", test_perform_head)

    section("4. write to file")
    test("WRITEDATA file", test_write_to_file)

    section("5. getinfo")
    test("getinfo fields", test_getinfo)

    section("6. reset")
    test("Curl reset", test_reset)

    section("7. SSL")
    test("SSL verification", test_ssl_verification)

    section("RESULT")
    n_ok = n_fail = n_skip = 0
    for r in RESULTS:
        status = r[0]
        if status == "PASS":
            n_ok += 1
            print("  OK    %s" % r[1])
        elif status == "SKIP":
            n_skip += 1
            print("  SKIP  %s" % r[1])
        else:
            n_fail += 1
            print("  FAIL  %s: %s" % (r[1], r[2]))
    print("RESULT: %d ok, %d failed, %d skipped" % (n_ok, n_fail, n_skip))
    sys.exit(1 if n_fail else 0)


if __name__ == "__main__":
    main()