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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
#!/usr/bin/env python3
"""BNW HTTP functional tests -- no browser required, just HTTP requests.
Tests the full user workflow of a running BNW instance:
static pages -> key validation -> data upload ->
input preparation (octave) -> structure learning (C binaries) ->
network rendering
Usage:
BASE_URL=http://127.0.0.1:8881 ./test/bnw_http_tests.py
Only Python >= 3.8 standard library is required (urllib, no requests).
NOTE: the two test keys are written into the BNW data directory
(normally /var/lib/genenet/bnw) and stay there; use clear.php or
restart the container to get rid of them.
"""
import os
from functools import partial
import sys
import tempfile
import urllib.error
import urllib.parse
import urllib.request
import uuid
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8881").rstrip("/")
# Keys must be exactly 3 letters (input_validate.php:valid_keyval).
KEY_D = os.environ.get("KEY_D", "tst") # discrete 8-node dataset
KEY_C = os.environ.get("KEY_C", "ctn") # continuous 5-node dataset
HERE = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(HERE, "..", "sourcecodes", "example_datasets")
RESULTS = []
class Response:
def __init__(self, status, headers, body):
self.status = status
self.headers = headers
self.body = body
def has(self, pattern):
return pattern in self.body
def header(self, name):
return self.headers.get(name)
def request(path, post=None, files=None, timeout=180):
"""HTTP GET (or multipart POST if files/post given). Redirects are
NOT followed so we can assert on them."""
url = BASE_URL + path
headers = {}
data = None
if files is not None or post is not None:
boundary = "----bnwtest" + uuid.uuid4().hex
parts = []
fields = dict(post or {})
fields.update(files or {})
for name, value in fields.items():
if isinstance(value, tuple): # (filename, bytes) file upload
filename, content = value
parts.append(
f"--{boundary}\r\n"
f'Content-Disposition: form-data; name="{name}"; '
f'filename="{filename}"\r\n'
f"Content-Type: application/octet-stream\r\n\r\n"
f"{content.decode('utf-8', 'replace')}\r\n")
else:
parts.append(
f"--{boundary}\r\n"
f'Content-Disposition: form-data; name="{name}"\r\n\r\n'
f"{value}\r\n")
parts.append(f"--{boundary}--\r\n")
body = "".join(parts).encode()
headers["Content-Type"] = f"multipart/form-data; boundary={boundary}"
data = body
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
opener = urllib.request.build_opener(NoRedirect)
req = urllib.request.Request(url, data=data, headers=headers)
try:
with opener.open(req, timeout=timeout) as resp:
return Response(resp.status, dict(resp.headers),
resp.read().decode("utf-8", "replace"))
except urllib.error.HTTPError as err:
return Response(err.code, dict(err.headers),
err.read().decode("utf-8", "replace"))
# --- tests ------------------------------------------------------------------
def t01_index():
"""web root serves the upload form (index -> bn_genenet.php)"""
r = request("/")
return r.status == 200 and r.has("key_search")
def t02_home():
"""home.php serves the BNW application home page"""
r = request("/home.php")
return r.status == 200 and r.has("Bayesian")
def t03_help():
"""help.php served"""
return request("/help.php").status == 200
def t04_asset():
"""javascript asset served"""
return request("/scripts/d3.v4.min.js").status == 200
def t05_key_roundtrip():
"""the user-supplied 3-letter key is echoed back into the form
NOTE: upstream key *validation* is broken under PHP 8
(valid_keyval sends its error header after output has started, so
invalid keys are silently accepted); what we can test over HTTP is
that a well-formed key is used as the session key."""
r = request("/bn_genenet.php", post={"My_key": KEY_D,
"MyUpload": "NO",
"searchkey": ""})
return r.status == 200 and f'value={KEY_D}' in r.body
def t06_upload_discrete():
"""upload discrete 8-node data + octave input preparation
After a successful upload bn_genenet.php renders the data
properties table; 'var data1 = [' only appears when the octave
preparation (run_prep_input/prepareInput.m) produced
<key>input_table.txt."""
with open(os.path.join(DATA_DIR, "example_data_8nodes.txt"), "rb") as fh:
data = fh.read()
r = request("/bn_genenet.php",
post={"My_key": KEY_D, "MyUpload": "YES", "searchkey": ""},
files={"MyFile": ("example_data_8nodes.txt", data)})
return (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = ["))
def t07_settings_pages():
"""structure settings + remove-variables pages for the uploaded key"""
return (request(f"/create_tiers_gom_part1.php?My_key={KEY_D}").status == 200
and request(f"/remove_variables.php?My_key={KEY_D}").status == 200)
def t08_default_settings():
"""executionprogress_default.php writes the default settings"""
r = request(f"/executionprogress_default.php?My_key={KEY_D}")
return r.status == 200 and r.has("Computing")
def t09_structure_learning():
"""execute_bn_gom.php runs the structure learning binaries"""
return request(f"/execute_bn_gom.php?My_key={KEY_D}").status == 200
def t10_network_rendered():
"""graphviz_structure.php renders the learned network"""
r = request(f"/graphviz_structure.php?My_key={KEY_D}")
return r.status == 200 and ("digraph" in r.body or "svg" in r.body)
def t11_upload_continuous():
"""upload continuous 5-node data + octave input preparation"""
with open(os.path.join(DATA_DIR, "sci_5node_input_data.txt"), "rb") as fh:
data = fh.read()
r = request("/bn_genenet.php",
post={"My_key": KEY_C, "MyUpload": "YES", "searchkey": ""},
files={"MyFile": ("sci_5node_input_data.txt", data)})
return (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = ["))
def t12_default_settings_continuous():
"""default settings for the continuous data set"""
return request(f"/executionprogress_default.php?My_key={KEY_C}").status == 200
def t13_structure_learning_continuous():
"""structure learning for the continuous data set"""
return request(f"/execute_bn_gom.php?My_key={KEY_C}").status == 200
def t14_network_rendered_continuous():
"""network rendering for the continuous data set"""
r = request(f"/graphviz_structure.php?My_key={KEY_C}")
return r.status == 200 and ("digraph" in r.body or "svg" in r.body)
EXAMPLES = [
# (example dir, old key) -- the pairs linked from workflow.php
("example2", "hQG"),
("example_sci", "Llu"),
("example_chr2_spleen", "cuL"),
("example_chl", "bWR"),
("example_time_series", "TEb"),
("examplecar15node", "eIA"),
]
def t15_example_workflow(example, oldkey):
"""the worked examples linked from workflow.php load end-to-end
workflow.php links e.g. example.php?My_key=example2|hQG;
example.php copies the pre-computed example state to a fresh
random 3-letter key and redirects (JS) to layout_svg_no.php. We
extract the fresh key from the response and follow the chain."""
import re
r = request(f"/example.php?My_key={example}%7C{oldkey}")
if r.status != 200:
return False
m = re.search(r'layout_svg_no\.php\?My_key=([A-Za-z]{3})', r.body)
if not m:
return False
key = m.group(1)
r2 = request(f"/layout_svg_no.php?My_key={key}")
# 'var data1 = false' means the SVG file was not found server-side
# (json_encode(file(...)) returning false), which crashes the page
# with 'data1.join is not a function' in the browser.
return (r2.status == 200
and "var data1 = [" in r2.body
and "var data1 = false" not in r2.body)
def t16_svg_download():
"""the rendered network SVG is downloadable via /bnw-data/"""
import re
r = request("/example.php?My_key=example2%7ChQG")
m = re.search(r'layout_svg_no\.php\?My_key=([A-Za-z]{3})', r.body)
if not m:
return False
r2 = request(f"/bnw-data/{m.group(1)}network_no_edge.svg")
return r2.status == 200 and "<svg" in r2.body
def t17_script_assets():
"""the javascript/css assets referenced by the pages download
with real content (d3.v4.min.js, jquery, font-awesome, ...)"""
ok = True
for asset, marker, minlen in [
("scripts/d3.v4.min.js", "d3", 50000),
("scripts/jquery.min.js", "jQuery", 20000),
("scripts/font-awesome.css", "font-family", 5000),
("scripts/accordion.js", "function", 200),
("scripts/create_table.js", "function", 200)]:
r = request("/" + asset)
ok = ok and r.status == 200 and marker in r.body and len(r.body) >= minlen
return ok
# --- GN2 (genenetwork.org) integration ---------------------------------------
#
# GN2 posts My_Gennenet to bn_genenet.php: columns separated by commas,
# rows by semicolons. bn_genenet.php embeds the payload in
# genenetwork_data(), which (client-side) converts , -> TAB and ; ->
# newline into the hidden 'searchkey' textarea and auto-submits the
# form. A browserless emulation performs that conversion itself and
# posts searchkey/My_key/MyUpload=NO directly.
GN2_KEY = "gna" # 3 letters
GN2_DATA = ("Geno1,Geno2,Trait1,Trait2;"
"1,2,0.1,0.2;"
"2,1,0.3,0.4;"
"1,2,0.5,0.6")
def t18_gn2_entry_point():
"""GN2 posts My_Gennenet; the server converts it (commas -> tabs,
semicolons -> newlines) and serves the prepared-data page in ONE
request. The old release echoed the raw payload into a JS string
(XSS on public deployments) -- that must be gone."""
r = request("/bn_genenet.php", post={"My_Gennenet": GN2_DATA,
"My_key": GN2_KEY})
return (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = [")
and "genenetwork_data" not in r.body)
def t18b_gn2_template_spelling():
"""GN2's bnw_page.html posts the field as 'My_Genenet' (one 'n'
in 'Genenet'), while BNW historically read 'My_Gennenet' -- accept
both spellings, through the legacy /sourcecodes/ action path the
template posts to."""
r = request("/sourcecodes/bn_genenet.php",
post={"My_Genenet": GN2_DATA, "My_key": GN2_KEY})
return (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = "))
def t19_gn2_data_loaded():
"""the auto-submit POST loads the converted data and prepares it
This emulates what the browser does with genenetwork_data(): comma
-> tab, semicolon -> newline."""
tsv = GN2_DATA.replace(",", "\t").replace(";", "\n")
r = request("/bn_genenet.php", post={"searchkey": tsv,
"My_key": GN2_KEY,
"MyUpload": "NO"})
return (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = ["))
def t20_gn2_structure_learning():
"""a network is learned and rendered for the GN2-posted data"""
r = request(f"/executionprogress_default.php?My_key={GN2_KEY}")
if r.status != 200:
return False
r = request(f"/execute_bn_gom.php?My_key={GN2_KEY}")
if r.status != 200:
return False
r = request(f"/graphviz_structure.php?My_key={GN2_KEY}")
return r.status == 200 and ("digraph" in r.body or "svg" in r.body)
def t21_gn2_spaced_names():
"""GN2 trait display names may contain spaces ("Age at death");
BNW's file format does not allow them and the octave parsers split
on whitespace, so a spaced name corrupts the header ("A(0,_):
subscripts must be ... integers" in mk_adj_mat). bn_genenet.php
must sanitize the name row: whitespace inside a name becomes an
underscore."""
spaced = "_Trait 1,_Trait 2,_Trait 3;0.123,3.456,9.1;0.972,1.500,2.2;2.269,4.053,1.7"
r = request("/bn_genenet.php", post={"My_Gennenet": spaced,
"My_key": KEY_C})
if not (r.status == 200
and r.has("Perform Bayesian network modeling")
and r.has("var data1 = ")):
return False
# structure learning must complete and render the network
request(f"/executionprogress_default.php?My_key={KEY_C}")
request(f"/execute_bn_gom.php?My_key={KEY_C}")
r2 = request(f"/layout_svg_no.php?My_key={KEY_C}")
return r2.status == 200 and "<svg" in r2.body
# --- input validation (fail fast with informative errors) -------------
# Real-world failure: GeneNetwork sent a header with NO data rows (the
# selected traits had no sample values loaded); the payload flowed
# through prep and died deep inside octave with "Could not open
# <key>continuous_input.txt". bn_genenet.php must reject bad payloads
# with a plain-English problem list instead.
def _post_gn2(payload, key):
return request("/bn_genenet.php", post={"My_Gennenet": payload,
"My_key": key})
def t22_gn2_header_only_rejected():
"""header without data rows -> clear error, no octave crash"""
r = _post_gn2("_A,_B,_C,_D,_E,_F,_G,_H;", "q22")
return (r.status == 200 and r.has("could not be used")
and r.has("No sample data rows were received")
and not r.has("Perform Bayesian network modeling"))
def t23_gn2_ragged_row_rejected():
"""a data row with the wrong number of values -> clear error"""
r = _post_gn2("_A,_B,_C;1,2,3;4,5;7,8,9;10,11,12", "q23")
return (r.status == 200 and r.has("could not be used")
and r.has("value(s) but the header names"))
def t24_gn2_single_variable_rejected():
"""one variable alone cannot be learned -> clear error"""
r = _post_gn2("_A;1;2;3;4;5", "q24")
return (r.status == 200 and r.has("could not be used")
and r.has("at least 2 variables"))
def t25_gn2_single_sample_rejected():
"""one sample row cannot be learned -> clear error"""
r = _post_gn2("_A,_B,_C;1,2,3", "q25")
return (r.status == 200 and r.has("could not be used")
and r.has("Only 1 usable sample row"))
def t26_gn2_nonnumeric_rejected():
"""a non-numeric cell -> clear error naming row, column and value"""
r = _post_gn2("_A,_B,_C;1,2,3;4,x,6;7,8,9", "q26")
return (r.status == 200 and r.has("could not be used")
and r.has("'x' in row 3, column 2 is not a number"))
def t27_gn2_duplicate_names_renamed():
"""duplicate trait names (same trait from two data sets) are
renamed with a note instead of crashing octave"""
r = _post_gn2("_A,_B,_A,_D;1,2,3,4;5,6,7,8;9,1,2,3", "q27")
return (r.status == 200
and r.has("Duplicate variable name(s) renamed")
and r.has("A_2")
and r.has("Perform Bayesian network modeling"))
def t28_gn2_crlf_payload_accepted():
"""CRLF line endings (Windows-style payload) must not break prep"""
r = _post_gn2("_A,_B,_C;\r\n1,2,3;\r\n4,5,6;\r\n7,8,9;\r\n", "q28")
return (r.status == 200
and r.has("Perform Bayesian network modeling"))
def t29_gn2_all_NA_rows_rejected():
"""rows that are all-NA are removed by prep; if nothing is left
that is a clear input problem, not an octave crash"""
r = _post_gn2("_A,_B,_C;NA,NA,NA;NA,NA,NA", "q29")
return (r.status == 200 and r.has("could not be used")
and r.has("contain only missing values (NA)"))
def t30_gn2_empty_payload_rejected():
"""an empty payload -> clear error"""
r = _post_gn2("", "q30")
return (r.status == 200 and r.has("could not be used")
and r.has("The posted data is empty"))
def t31_gn2_all_NA_column_rejected():
"""a variable with only NA values crashes prep ("Could not open
<key>continuous_input.txt") -- reject it before prep runs"""
r = _post_gn2("_a,_b,_c;1,NA,2;2,NA,3;3,NA,4;4,NA,5;5,NA,6;6,NA,7;7,NA,8",
"q31")
return (r.status == 200 and r.has("could not be used")
and r.has("has no measured values"))
def t32_gn2_every_row_NA_rejected():
"""when every row carries an NA, prep removes all rows and learning
dies with "fscanf: invalid stream number = -1" -- reject up front"""
r = _post_gn2("_a,_b,_c;NA,1,2;1,NA,3;2,3,NA;NA,4,5;1,NA,6;2,5,NA;3,6,NA",
"q32")
return (r.status == 200 and r.has("could not be used")
and r.has("Every sample row contains at least one missing value"))
def t33_gn2_cyclic_failure_has_hint():
"""cyclic/mirrored columns can produce a network BNW cannot lay out
(toposort "subscripts must be ..."); the FAILED page must explain
that in plain English instead of only showing the octave trace"""
import time as _time
pl = "_a,_b,_c;1,4,6;2,5,7;3,6,1;4,7,2;5,1,3;6,2,4;7,3,5"
r = _post_gn2(pl, "q33")
if not (r.status == 200 and r.has("Perform Bayesian network modeling")):
return False
request(f"/executionprogress_default.php?My_key=q33")
for _ in range(30):
b = request(f"/execute_bn_gom.php?My_key=q33")
if b.has("Structure learning FAILED"):
return b.has("Hint:")
if "Forwarding" in b:
_time.sleep(2)
continue
_time.sleep(2)
return False # neither learned nor failed within the wait window
TESTS = [
("t01 index page served", t01_index),
("t02 home.php served", t02_home),
("t03 help.php served", t03_help),
("t04 javascript asset served", t04_asset),
("t05 key round-trip", t05_key_roundtrip),
("t06 upload + preparation (discrete)", t06_upload_discrete),
("t07 settings pages (discrete)", t07_settings_pages),
("t08 default settings written (discrete)", t08_default_settings),
("t09 structure learning runs (discrete)", t09_structure_learning),
("t10 network graph rendered (discrete)", t10_network_rendered),
("t11 upload + preparation (continuous)", t11_upload_continuous),
("t12 default settings written (continuous)", t12_default_settings_continuous),
("t13 structure learning runs (continuous)", t13_structure_learning_continuous),
("t14 network graph rendered (continuous)", t14_network_rendered_continuous),
("t15 example workflow (example2)", partial(t15_example_workflow, "example2", "hQG")),
("t15 example workflow (example_sci)", partial(t15_example_workflow, "example_sci", "Llu")),
("t15 example workflow (example_chr2_spleen)", partial(t15_example_workflow, "example_chr2_spleen", "cuL")),
("t15 example workflow (example_chl)", partial(t15_example_workflow, "example_chl", "bWR")),
("t15 example workflow (example_time_series)", partial(t15_example_workflow, "example_time_series", "TEb")),
("t15 example workflow (examplecar15node)", partial(t15_example_workflow, "examplecar15node", "eIA")),
("t16 network svg downloadable", t16_svg_download),
("t17 script assets download with content", t17_script_assets),
("t18 GN2 single-POST handshake (no XSS)", t18_gn2_entry_point),
("t18b GN2 template spelling My_Genenet (legacy path)", t18b_gn2_template_spelling),
("t19 GN2 data load + preparation", t19_gn2_data_loaded),
("t20 GN2 structure learning + rendering", t20_gn2_structure_learning),
("t21 GN2 trait names with spaces sanitized", t21_gn2_spaced_names),
("t22 GN2 header-only payload rejected", t22_gn2_header_only_rejected),
("t23 GN2 ragged row rejected", t23_gn2_ragged_row_rejected),
("t24 GN2 single variable rejected", t24_gn2_single_variable_rejected),
("t25 GN2 single sample row rejected", t25_gn2_single_sample_rejected),
("t26 GN2 non-numeric cell rejected", t26_gn2_nonnumeric_rejected),
("t27 GN2 duplicate names renamed", t27_gn2_duplicate_names_renamed),
("t28 GN2 CRLF payload accepted", t28_gn2_crlf_payload_accepted),
("t29 GN2 all-NA rows rejected", t29_gn2_all_NA_rows_rejected),
("t30 GN2 empty payload rejected", t30_gn2_empty_payload_rejected),
("t31 GN2 all-NA column rejected", t31_gn2_all_NA_column_rejected),
("t32 GN2 every-row-NA rejected", t32_gn2_every_row_NA_rejected),
("t33 GN2 cyclic-failure page has hint", t33_gn2_cyclic_failure_has_hint),
]
def main():
print(f"== BNW HTTP functional tests against {BASE_URL} ==")
failures = []
for name, fn in TESTS:
try:
ok = fn()
detail = ""
except Exception as err: # noqa: BLE001
ok, detail = False, f" ({err})"
if ok:
RESULTS.append(f"PASS: {name}")
print(f"PASS: {name}")
else:
RESULTS.append(f"FAIL: {name}{detail}")
failures.append(name)
print(f"FAIL: {name}{detail}")
passed = len(RESULTS) - len(failures)
print(f"\n== {passed} passed, {len(failures)} failed ==")
if failures:
print("Failed tests: " + ", ".join(failures))
return 1
return 0
if __name__ == "__main__":
sys.exit(main())
|