]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Avoid assert statement outside of tests
authorDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sat, 20 Nov 2021 08:34:14 +0000 (09:34 +0100)
committerDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sat, 26 Feb 2022 15:51:07 +0000 (16:51 +0100)
This fixes DeepSource alerts:

Assert statement used outside of tests

Usage of assert statement in application logic is discouraged. assert is
removed with compiling to optimized byte code. Consider raising an
exception instead. Ideally, assert statement should be used only in tests.

Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
trojans/tncc-emulate.py

index dd443e0d006cb70232605dd60130c5a56576a53d..88a8d6cdaae1c74a3b8ca8678078f13977ffd1e1 100755 (executable)
@@ -570,16 +570,17 @@ class tncc:
             for cert in self.avail_certs:
                 fail = False
                 for dn_name, dn_vals in req_dns.items():
-                    for name, val in dn_vals.items():
-                        try:
-                            if dn_name == 'IssuerDN':
-                                assert val in cert.issuer[name]
-                            else:
-                                logging.warning('Unknown DN type %s', str(dn_name))
-                                raise Exception()
-                        except Exception:
-                            fail = True
-                            break
+                    if dn_name == 'IssuerDN':
+                        for name, val in dn_vals.items():
+                            if (
+                                name not in cert.issuer
+                                or val not in cert.issuer[name]
+                            ):
+                                fail = True
+                                break
+                    else:
+                        logging.warning('Unknown DN type %s', str(dn_name))
+                        fail = True
                     if fail:
                         break
                 if not fail:
@@ -678,10 +679,11 @@ if __name__ == "__main__":
             for iface in netifaces.interfaces():
                 try:
                     mac = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr']
-                    assert mac != '00:00:00:00:00:00'
-                    mac_addrs.append(mac)
-                except Exception:
+                except (IndexError, KeyError):
                     pass
+                else:
+                    if mac != '00:00:00:00:00:00':
+                        mac_addrs.append(mac)
 
     hostname = os.environ.get('TNCC_HOSTNAME', socket.gethostname())