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
|
# Copyright 2016 Christoph Reiter
# 2023 Nick Boultbee
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
import os
import pytest
from gi.repository import Gio, Soup, GLib
from urllib.request import urlopen, build_opener
from tests import TestCase, skipIf
from quodlibet.util import is_linux, get_ca_file
@pytest.mark.network
# See https://stackoverflow.com/questions/75274925
@skipIf(is_linux() and not os.environ.get("container"), "Only on Flatpak linux")
class Thttps(TestCase):
"""For Windows/OSX/Flatpak to check if we can create a TLS connection
using both openssl and whatever backend soup/gio uses.
"""
GOOD = ["https://sha256.badssl.com/"]
BAD = [
"https://expired.badssl.com/",
"https://wrong.host.badssl.com/",
"https://self-signed.badssl.com/",
]
def test_urllib(self):
for url in self.GOOD:
urlopen(url, cafile=get_ca_file()).close()
for url in self.BAD:
with self.assertRaises(Exception):
urlopen(url, cafile=get_ca_file()).close()
def test_urllib_default(self):
for url in self.GOOD:
urlopen(url).close()
for url in self.BAD:
with self.assertRaises(Exception):
urlopen(url).close()
def test_urllib_build_opener(self):
for url in self.GOOD:
build_opener().open(url).close()
for url in self.BAD:
with self.assertRaises(Exception):
build_opener().open(url).close()
def test_gio(self):
for url in self.GOOD:
client = Gio.SocketClient()
client.set_tls(True)
client.connect_to_uri(url, 443, None).close()
for url in self.BAD:
with self.assertRaises(GLib.GError):
client = Gio.SocketClient()
client.set_tls(True)
client.connect_to_uri(url, 443, None).close()
def test_soup(self):
for url in self.GOOD:
session = Soup.Session()
msg = Soup.Message.new("GET", url)
session.send_and_read(msg)
for url in self.BAD:
with self.assertRaises(GLib.GError):
session = Soup.Session()
msg = Soup.Message.new("GET", url)
session.send_and_read(msg)
|