source: ntrip/trunk/BNC/tests/tools/cred_check.py

Last change on this file was 11160, checked in by stuerze, 3 weeks ago

screenshots are updated

  • Property svn:executable set to *
File size: 9.3 KB
Line 
1#!/usr/bin/env python3
2"""Checks of the credentials files (option credentialsFile, ~/.netrc) with
3BNC as Ntrip client and as uploading source against tools/fakecaster.py
4(--auth: only the expected user and password are accepted). Every scenario
5runs with its own HOME directory, so ~/.netrc is under control.
6
7 tests/tools/cred_check.py [--bnc PATH] exit code 1 if a check fails
8
9Download (Ntrip 2 unless noted):
10 password in the configuration file (as before, hint in the log)
11 user only, ~/.netrc with two users for the host (and a macdef)
12 $NETRC: entry 'machine host:port' preferred over 'machine host'
13 BNC format (credentialsFile): exact port preferred over '*', quoted password
14 BNC format without port: '<host> <user> <password>'
15 URL without user: no credentials sent although ~/.netrc has the host
16 user without entry: logged, rejected by the caster
17 ~/.netrc readable for others: not used (logged)
18 netrc 'default' entry: not used
19 Ntrip 1 with ~/.netrc
20Upload:
21 Ntrip 2, user with empty password, BNC format file
22 Ntrip 1, no user and empty password, BNC format entry with user '-'
23"""
24
25import argparse
26import glob
27import os
28import shutil
29import subprocess
30import sys
31import time
32
33TOOLS = os.path.dirname(os.path.abspath(__file__))
34TESTS = os.path.dirname(TOOLS)
35ROOT = os.path.dirname(TESTS)
36WORK = os.path.join(TESTS, "work", "cred_check")
37SERVE_RAW = os.path.join(TESTS, "data", "crafted_limits.raw")
38UPLOAD_RAW = os.path.join(TESTS, "data", "upload_rtnet_5min.raw")
39
40
41def run(bnc, name, port, auth, bnc_args, files=None, env_extra=None, upload=False):
42 """Fake caster on 127.0.0.1:port, BNC with HOME=<work>/home; returns
43 (caster output, BNC log)."""
44 d = os.path.join(WORK, name)
45 home = os.path.join(d, "home")
46 os.makedirs(os.path.join(d, "Output"))
47 os.makedirs(home)
48 os.symlink(os.path.join(ROOT, "Example_Configs", "Input"), os.path.join(d, "Input"))
49 for path, (text, mode) in (files or {}).items():
50 path = path.replace("~", home)
51 with open(path, "w") as f:
52 f.write(text.replace("PORT", str(port)))
53 os.chmod(path, mode)
54 caster = [sys.executable, "-I", os.path.join(TOOLS, "fakecaster.py"), "--port", str(port),
55 "--out", os.path.join(d, "Output", "upload.raw"), "--time", "2026-10-01T09:33:00",
56 "--timeout", "6"]
57 if not upload:
58 caster += ["--serve", SERVE_RAW]
59 for a in auth:
60 caster += ["--auth", a]
61 fc = subprocess.Popen(caster, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
62 fc.stdout.readline() # "listening"
63 env = dict(os.environ, QT_QPA_PLATFORM="offscreen", LC_ALL="C", TZ="UTC", HOME=home)
64 env.pop("NETRC", None)
65 env.update({k: v.replace("~", home) for k, v in (env_extra or {}).items()})
66 args = [a.replace("~", home).replace("PORT", str(port)) for a in bnc_args]
67 cmd = [bnc, "--nw", "--platform", "offscreen"] + args
68 with open(os.path.join(d, "bnc.out"), "wb") as out:
69 if upload:
70 subprocess.run(cmd, cwd=d, stdout=out, stderr=subprocess.STDOUT, env=env, timeout=300)
71 else:
72 p = subprocess.Popen(cmd, cwd=d, stdout=out, stderr=subprocess.STDOUT, env=env)
73 time.sleep(8)
74 p.send_signal(2)
75 p.wait(30)
76 out = fc.communicate(timeout=120)[0]
77 with open(os.path.join(d, "caster.out"), "w") as f:
78 f.write(out)
79 log = "".join(open(f, errors="replace").read() for f in glob.glob(os.path.join(d, "Output", "*.log*")))
80 return out, log
81
82
83def download(bnc, name, port, url_user, auth, files=None, env_extra=None, extra=(), version="2"):
84 mp = "//%s127.0.0.1:%d/TEST RTCM_3 DEU 50 8 no %s" % (url_user, port, version)
85 return run(bnc, name, port, auth,
86 ["--conf", os.path.join(ROOT, "Example_Configs", "22_Empty.bnc"),
87 "-key", "mountPoints", mp, "-key", "logFile", "Output/bnc.log"] + list(extra),
88 files, env_extra)
89
90
91def upload(bnc, name, port, version, user, auth, files, extra=()):
92 mp = "127.0.0.1,%d,CLOCK,%s,%s,,IGS20,IGS-SSR,0,,,,,,,0 byte(s)," % (port, version, user)
93 return run(bnc, name, port, auth,
94 ["--conf", os.path.join(ROOT, "Example_Configs", "16_Upload.bnc"),
95 "--file", UPLOAD_RAW, "-key", "uploadMountpointsOut", mp] + list(extra),
96 files, upload=True)
97
98
99def main():
100 ap = argparse.ArgumentParser(description=__doc__,
101 formatter_class=argparse.RawDescriptionHelpFormatter)
102 ap.add_argument("--bnc", default=os.path.join(ROOT, "bnc"))
103 args = ap.parse_args()
104 bnc = os.path.abspath(args.bnc)
105 shutil.rmtree(WORK, ignore_errors=True)
106 failed = 0
107
108 def check(name, ok, caster):
109 nonlocal failed
110 lines = [l for l in caster.splitlines() if l.startswith(("auth", "served", "received"))]
111 summary = ", ".join(sorted(set(lines)))
112 print("%-6s %-46s %s" % ("PASS" if ok else "FAIL", name, summary), flush=True)
113 failed += not ok
114
115 def served(c):
116 return any(l.startswith("served ") and int(l.split()[1]) > 0 for l in c.splitlines())
117
118 def rejected(c):
119 return any(l.startswith("auth ") and l.endswith(" rejected") for l in c.splitlines())
120
121 def received(c):
122 return any(l.startswith("received ") and int(l.split()[1]) > 0 for l in c.splitlines())
123
124 netrc_two_users = ("macdef init\ncd /tmp\n\n"
125 "# two accounts on the same caster\n"
126 "machine 127.0.0.1 login other password x\n"
127 "machine 127.0.0.1 login bnc password right\n")
128
129 c, log = download(bnc, "config_password", 21301, "bnc:right@", ["bnc:right"])
130 check("download: password in the configuration file", served(c) and "auth bnc accepted" in c
131 and "password in the configuration file" in log, c)
132
133 c, log = download(bnc, "netrc_two_users", 21302, "bnc@", ["bnc:right"],
134 {"~/.netrc": (netrc_two_users, 0o600)})
135 check("download: ~/.netrc, two users for the host", served(c) and "auth bnc accepted" in c
136 and not rejected(c) and "configuration file" not in log, c)
137
138 c, log = download(bnc, "netrc_port", 21303, "bnc@", ["bnc:right"],
139 {"~/other_netrc": ("machine 127.0.0.1 login bnc password wrong\n"
140 "machine 127.0.0.1:PORT login bnc password right\n", 0o600)},
141 {"NETRC": "~/other_netrc"})
142 check("download: $NETRC, machine host:port preferred", served(c) and not rejected(c), c)
143
144 c, log = download(bnc, "bnc_format", 21304, "bnc@", ["bnc:my secret"],
145 {"~/bnc.credentials": ("# host port user password\n"
146 "127.0.0.1 * bnc wrong\n"
147 "127.0.0.1 PORT bnc \"my secret\"\n", 0o600)},
148 extra=["-key", "credentialsFile", "~/bnc.credentials"])
149 check("download: BNC format, exact port, quoted", served(c) and not rejected(c), c)
150
151 c, log = download(bnc, "bnc_format_3", 21312, "bnc@", ["bnc:three"],
152 {"~/bnc.credentials": ("127.0.0.1 other x\n127.0.0.1 bnc three\n", 0o600)},
153 extra=["-key", "credentialsFile", "~/bnc.credentials"])
154 check("download: BNC format without port", served(c) and not rejected(c), c)
155
156 c, log = download(bnc, "anonymous", 21305, "", [],
157 {"~/.netrc": (netrc_two_users, 0o600)})
158 check("download: URL without user stays anonymous", served(c) and "auth none" in c
159 and "auth bnc" not in c and "auth other" not in c, c)
160
161 c, log = download(bnc, "no_entry", 21306, "bnc@", ["bnc:right"],
162 {"~/.netrc": ("machine 127.0.0.1 login other password x\n", 0o600)})
163 check("download: no entry for the user", not served(c) and "auth bnc rejected" in c
164 and "no password" in log, c)
165
166 c, log = download(bnc, "netrc_readable", 21307, "bnc@", ["bnc:right"],
167 {"~/.netrc": ("machine 127.0.0.1 login bnc password right\n", 0o644)})
168 check("download: ~/.netrc readable for others", not served(c) and "readable for other users" in log, c)
169
170 c, log = download(bnc, "netrc_default", 21308, "bnc@", ["bnc:right"],
171 {"~/.netrc": ("default login bnc password right\n", 0o600)})
172 check("download: netrc default entry not used", not served(c) and "no password" in log, c)
173
174 c, log = download(bnc, "ntrip1", 21309, "bnc@", ["bnc:right"],
175 {"~/.netrc": (netrc_two_users, 0o600)}, version="1")
176 check("download: Ntrip 1, ~/.netrc", served(c) and "auth bnc accepted" in c and not rejected(c), c)
177
178 c, log = upload(bnc, "upload_v2", 21310, "2", "bnc", ["bnc:up2"],
179 {"~/bnc.credentials": ("127.0.0.1 PORT bnc up2\n", 0o600)},
180 ["-key", "credentialsFile", "~/bnc.credentials"])
181 check("upload: Ntrip 2, password from file", received(c) and not rejected(c), c)
182
183 c, log = upload(bnc, "upload_v1", 21311, "1", "", [":up1"],
184 {"~/bnc.credentials": ("127.0.0.1 PORT - up1\n", 0o600)},
185 ["-key", "credentialsFile", "~/bnc.credentials"])
186 check("upload: Ntrip 1, entry without user", received(c) and not rejected(c), c)
187
188 print("%d check(s) failed, details in %s" % (failed, os.path.relpath(WORK, ROOT)))
189 return 1 if failed else 0
190
191
192if __name__ == "__main__":
193 sys.exit(main())
Note: See TracBrowser for help on using the repository browser.