interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / visits / tests / run.sh
| 1 | #!/bin/sh |
| 2 | # visits/tests/run.sh: gate for visits.om (the owner's view of who visited). |
| 3 | # OMC=/abs/seed/out/omc sh visits/tests/run.sh |
| 4 | # Makes a log of 6,000 requests over three days from 31 addresses to 9 projects, the first 3,000 in |
| 5 | # the seven-field form (before forged logged the agent) and the rest with agent and referrer, with |
| 6 | # seven kinds of line that must be skipped, and requires visits.tsv to equal what awk, sort and uniq |
| 7 | # compute from the same log (the oracle never runs the program under test). Then the page and the |
| 8 | # file modes. Negative controls: each mutant at the end of this file changes one line of visits.om |
| 9 | # and must not pass. |
| 10 | set -u |
| 11 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 12 | V=$HERE/.. |
| 13 | OMC=${OMC:-$HOME/canon/omega-mv/seed/out/omc} |
| 14 | PIN=34d611ca0415eab1728d3234911d90e74af17ba9ab0c581d559f48274d7ca6d3 |
| 15 | T=$(mktemp -d) || exit 2 |
| 16 | trap 'rm -rf "$T"' EXIT |
| 17 | pass=0 |
| 18 | fail=0 |
| 19 | ok() { pass=$((pass + 1)); echo "PASS $1"; } |
| 20 | no() { fail=$((fail + 1)); echo "FAIL $1"; } |
| 21 | eq() { if [ "$2" = "$3" ]; then ok "$1"; else no "$1: got '$2' want '$3'"; fi; } |
| 22 | |
| 23 | got=$(sha256sum "$OMC" 2>/dev/null | cut -d' ' -f1) |
| 24 | if [ "$got" != "$PIN" ]; then |
| 25 | echo "compiler pin mismatch: $got" >&2 |
| 26 | exit 2 |
| 27 | fi |
| 28 | "$OMC" <"$V/visits.om" >"$T/visits" 2>"$T/visits.err" |
| 29 | eq "compile exit" "$?" 0 |
| 30 | chmod +x "$T/visits" |
| 31 | |
| 32 | # the log: deterministic, no clock and no random source |
| 33 | awk 'BEGIN { |
| 34 | split("/ /explore /galaxy /drakestapleton/omega-mv/blob/abc/x.om /drakestapleton/omega-mv /drakestapleton/CUDApp/tree/main?tab=1 /drakestapleton/aienux/ /assets/galaxy.css /drakestapleton/a<b>&\"c/raw /search?q=x", P, " ") |
| 35 | split("200 200 200 303 404 200 500 200 200 304", S, " ") |
| 36 | t = 1794182400 |
| 37 | for (i = 0; i < 6000; i++) { |
| 38 | t += (i * 7) % 61 |
| 39 | ip = (i % 31 < 4) ? sprintf("2001:db8::%x", i % 31) : sprintf("203.0.113.%d", i % 31) |
| 40 | p = P[(i * 7 + int(i / 13)) % 10 + 1]; s = S[(i * 3) % 10 + 1] |
| 41 | split("Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) Mozilla/5.0+(X11;+Linux+x86_64)+Firefox/130.0 - AhrefsBOT/7.0 ExampleCRAWLer/1.0 Baiduspider+2 Yahoo!+Slurp curl/8.5.0 Mozilla/5.0+(compatible;+GoogleOther) Mozilla/5.0+(Macintosh)+Version/17.0+Safari/605.1.15 python-requests/2.32.3 Mozilla/5.0+(X11;+Linux+x86_64)+HeadlessChrome/140.0.0.0+Safari/537.36", A, " ") |
| 42 | m = i % 31; ag = A[m % 12 + 1]; if (m == 9 && i % 2) ag = A[1] |
| 43 | if (i < 3000) printf "%d %s %d %s %s %d %d\n", t, ip, 1 + (i % 3 == 0), p, s, (i * 37) % 9000, i % 50 |
| 44 | else printf "%d %s %d %s %s %d %d %s %s\n", t, ip, 1 + (i % 3 == 0), p, s, (i * 37) % 9000, i % 50, ag, (i % 4) ? "-" : "https://example.org/a+page" |
| 45 | if (i % 1000 == 500) print "forged: reloaded, public repos 30" |
| 46 | if (i % 1000 == 501) printf "%d 198.51.100.9 1 nopath 200 5 1\n", t |
| 47 | if (i % 1000 == 502) printf "soon 198.51.100.9 1 / 200 5 1\n" |
| 48 | if (i % 1000 == 503) printf "%d 198.51.100.9 1 / 200 5 1 extra\n", t |
| 49 | if (i % 1000 == 504) printf "%d 198.51.100.9 1 / 2x0 5 1\n", t |
| 50 | if (i % 1000 == 505) printf "%d 198.51.100.9 1 / 200 5 1 a b c\n", t |
| 51 | if (i == 2505) { printf "%d 198.51.100.9 1 /", t; for (k = 0; k < 5000; k++) printf "a"; print " 200 5 1" } |
| 52 | } |
| 53 | }' >"$T/log" |
| 54 | eq "log lines" "$(wc -l <"$T/log" | tr -d ' ')" 6037 |
| 55 | |
| 56 | # the oracle |
| 57 | awk -F' ' ' |
| 58 | function key(p, q) { q = p; sub(/\?.*/, "", q); if (match(q, /^\/[^\/]*(\/[^\/]*)?/)) q = substr(q, 1, RLENGTH); return q } |
| 59 | (NF != 7 && NF != 9) || $1 !~ /^[0-9]+$/ || $5 !~ /^[0-9]+$/ || $6 !~ /^[0-9]+$/ || substr($4, 1, 1) != "/" || length($0) > 4095 { skip++; next } |
| 60 | { |
| 61 | n++; by += $6; if (!first) first = $1; last = $1; if (n == 1) fd = int($1 / 86400) |
| 62 | c = int($5 / 100); if (c >= 2 && c <= 5) cls[c]++ |
| 63 | if (!($2 in vr)) { nv++; vf[$2] = $1 } |
| 64 | vr[$2]++; vb[$2] += $6; vl[$2] = $1 |
| 65 | d = int($1 / 86400); if (d < fd || d - fd >= 512) out++; else { dr[d]++; if (!((d, $2) in du)) { du[d, $2] = 1; dn[d]++ } } |
| 66 | k = key($4); if (!(k in pr)) np++ |
| 67 | pr[k]++; if (!((k, $2) in pu)) { pu[k, $2] = 1; pn[k]++ } |
| 68 | if (NF == 7) un++ |
| 69 | else if ($8 == "-") nn++ |
| 70 | else if (tolower($8) ~ /bot|crawl|spider|slurp|googleother|google-|mediapartners|feedfetcher|facebookexternalhit|preview|curl|wget|python|go-http|java\/|scrapy|httpclient|libwww|headless|okhttp|aiohttp|axios|node-fetch/) { cr++; kind[$2] = "c" } |
| 71 | else { ot++; if (kind[$2] != "c") kind[$2] = "o" } |
| 72 | } |
| 73 | END { |
| 74 | printf "T\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t0\t0\t%d\n", n, nv, np, by, first, last, cls[2], cls[3], cls[4], cls[5], skip, out |
| 75 | for (d in dr) printf "D\t%d\t%d\t%d\n", d, dr[d], dn[d] |
| 76 | for (k in pr) printf "P\t%s\t%d\t%d\n", k, pr[k], pn[k] |
| 77 | for (a in vr) { k = (a in kind) ? kind[a] : "-"; if (k == "c") kc++; if (k == "o") ko++; printf "V\t%s\t%d\t%d\t%d\t%d\t%s\n", a, vr[a], vb[a], vf[a], vl[a], k } |
| 78 | printf "A\t%d\t%d\t%d\t%d\t%d\t%d\n", cr, ot, nn, un, kc, ko |
| 79 | }' "$T/log" | while IFS=' ' read -r a b rest; do |
| 80 | if [ "$a" = D ]; then printf 'D\t%s\t%s\n' "$(date -u -d "@$((b * 86400))" +%F)" "$rest"; else printf '%s\t%s\t%s\n' "$a" "$b" "$rest"; fi |
| 81 | done | sort >"$T/want.tsv" |
| 82 | |
| 83 | mkdir "$T/w" |
| 84 | (cd "$T/w" && "$T/visits" "$T/log") |
| 85 | eq "run exit" "$?" 0 |
| 86 | sort "$T/w/visits.tsv" >"$T/got.tsv" |
| 87 | if cmp -s "$T/got.tsv" "$T/want.tsv"; then ok "visits.tsv equals the awk oracle"; else no "visits.tsv equals the awk oracle"; diff "$T/got.tsv" "$T/want.tsv" | head -12; fi |
| 88 | eq "oracle saw 31 visitors and 9 projects" "$(grep -c '^V' "$T/want.tsv") $(grep -c '^P' "$T/want.tsv")" "31 9" |
| 89 | eq "seven kinds of bad line were skipped" "$(grep '^T' "$T/want.tsv" | cut -f12)" 37 |
| 90 | AL=$(grep '^A' "$T/want.tsv") |
| 91 | eq "oracle saw all four kinds of request and both kinds of visitor" "$(printf '%s\n' "$AL" | awk -F'\t' '{ print ($2 > 0 && $3 > 0 && $4 > 0 && $5 == 3000 && $6 > 0 && $7 > 0) }')" 1 |
| 92 | eq "an address that used a crawler name and a browser name is a crawler" "$(grep -c "^V 203.0.113.9 .* c$" "$T/want.tsv")" 1 |
| 93 | eq "page names crawlers or tools and browsers" "$(grep -c '<td>crawler or tool</td></tr>' "$T/w/visits.html") $(grep -c '<td>browser</td></tr>' "$T/w/visits.html")" "$(printf '%s\n' "$AL" | cut -f6) $(printf '%s\n' "$AL" | cut -f7)" |
| 94 | |
| 95 | # every word of the list, one request each in upper case inside other text; then two real browsers. |
| 96 | # The gate keeps its own copy of the words: a word altered in the program is then a failed case. |
| 97 | WORDS="bot crawl spider slurp googleother google- mediapartners feedfetcher facebookexternalhit preview curl wget python go-http java/ scrapy httpclient libwww headless okhttp aiohttp axios node-fetch" |
| 98 | NW=$(printf '%s\n' "$WORDS" | wc -w | tr -d ' ') |
| 99 | eq "the program's word list is the gate's" "$(sed -n 's/^text kws "\(.*\)"$/\1/p' "$V/visits.om")" "$WORDS" |
| 100 | n=0 |
| 101 | for w in $WORDS; do n=$((n + 1)); printf '1794182400 10.1.0.%d 1 / 200 5 1 zz%szz -\n' "$n" "$(printf '%s' "$w" | tr 'a-z' 'A-Z')"; done >"$T/wlog" |
| 102 | printf '1794182400 10.2.0.1 1 / 200 5 1 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/141.0.0.0+Safari/537.36 -\n' >>"$T/wlog" |
| 103 | printf '1794182400 10.2.0.2 1 / 200 5 1 Mozilla/5.0+(iPhone;+CPU+iPhone+OS+18_0+like+Mac+OS+X)+AppleWebKit/605.1.15+(KHTML,+like+Gecko)+Version/18.0+Mobile/15E148+Safari/604.1 -\n' >>"$T/wlog" |
| 104 | mkdir "$T/ww" |
| 105 | (cd "$T/ww" && "$T/visits" "$T/wlog") |
| 106 | eq "each word alone makes a crawler or tool; Chrome and Safari are browsers" "$(grep '^A' "$T/ww/visits.tsv")" "$(printf 'A\t%d\t2\t0\t0\t%d\t2' "$NW" "$NW")" |
| 107 | # near misses: each word without its last byte, then without its first, is a browser. A program |
| 108 | # that skips the first or last byte of a word would call all of these crawlers or tools. |
| 109 | n=0 |
| 110 | for w in $WORDS; do |
| 111 | n=$((n + 1)); u=$(printf '%s' "$w" | tr 'a-z' 'A-Z') |
| 112 | printf '1794182400 10.3.0.%d 1 / 200 5 1 zz%szz -\n' "$n" "${u%?}" |
| 113 | printf '1794182400 10.4.0.%d 1 / 200 5 1 zz%szz -\n' "$n" "${u#?}" |
| 114 | done >"$T/nlog" |
| 115 | mkdir "$T/nw" |
| 116 | (cd "$T/nw" && "$T/visits" "$T/nlog") |
| 117 | eq "a word missing its first or last byte is a browser" "$(grep '^A' "$T/nw/visits.tsv")" "$(printf 'A\t0\t%d\t0\t0\t0\t%d' $((2 * NW)) $((2 * NW)))" |
| 118 | eq "files are for the owner only" "$(stat -c %a "$T/w/visits.tsv" "$T/w/visits.html" | tr '\n' ' ')" "600 600 " |
| 119 | eq "no temporary file left" "$(ls "$T/w" | grep -c 'tmp$')" 0 |
| 120 | eq "page has no script" "$(grep -ci '<script' "$T/w/visits.html")" 0 |
| 121 | eq "page keeps markup in a path as text" "$(grep -c 'a<b>&?c' "$T/w/visits.html")" 1 |
| 122 | eq "page is not indexed" "$(grep -c "name='robots' content='noindex'" "$T/w/visits.html")" 1 |
| 123 | eq "page lists every visitor" "$(grep -o '<tr><td>2001:db8::[0-9a-f]*</td>\|<tr><td>203\.0\.113\.[0-9]*</td>' "$T/w/visits.html" | sort -u | wc -l | tr -d ' ')" 31 |
| 124 | # the page groups digits in threes; the TSV keeps plain numbers |
| 125 | grp() { printf '%s\n' "$1" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'; } |
| 126 | REQ=$(grep '^T' "$T/want.tsv" | cut -f2) |
| 127 | eq "grouping oracle" "$(grp 1417491) $(grp 999) $(grp 1000) $(grp 0)" "1,417,491 999 1,000 0" |
| 128 | case "$(grp "$REQ")" in *,*) ok "fixture total has a group mark" ;; *) no "fixture total has a group mark" ;; esac |
| 129 | eq "page shows the request count" "$(grep -c "<dt>Requests</dt><dd>$(grp "$REQ")</dd>" "$T/w/visits.html")" 1 |
| 130 | eq "tsv keeps plain numbers" "$(grep -c ',' "$T/w/visits.tsv")" 0 |
| 131 | DG=$(grep '^D' "$T/want.tsv" | while IFS=' ' read -r _ d r u; do grep -c "<tr><td>$d</td><td>$(grp "$r")</td><td>$(grp "$u")</td></tr>" "$T/w/visits.html"; done | grep -c '^1$') |
| 132 | eq "page shows every day row grouped" "$DG" "$(grep -c '^D' "$T/want.tsv")" |
| 133 | case "$(grep '^D' "$T/want.tsv" | cut -f3 | while read -r r; do grp "$r"; done)" in *,*) ok "a fixture day total has a group mark" ;; *) no "a fixture day total has a group mark" ;; esac |
| 134 | |
| 135 | (cd "$T/w" && "$T/visits") >/dev/null 2>&1; eq "no argument exit" "$?" 1 |
| 136 | (cd "$T/w" && "$T/visits" "$T/none") >/dev/null 2>&1; eq "unreadable log exit" "$?" 2 |
| 137 | mkdir "$T/e"; : >"$T/empty" |
| 138 | (cd "$T/e" && "$T/visits" "$T/empty"); eq "empty log exit" "$?" 0 |
| 139 | eq "empty log totals" "$(cut -f2-4 "$T/e/visits.tsv" | head -n 1)" "$(printf '0\t0\t0')" |
| 140 | |
| 141 | # a request whose day is outside the 512-day table is in the totals and counted, never lost |
| 142 | t0=1794182400 |
| 143 | printf '%d 203.0.113.1 1 /x/y 200 10 1\n%d 203.0.113.2 1 /x/y 200 10 1\n%d 203.0.113.3 1 /x/y 200 10 1\n%d 203.0.113.4 1 /x/y 200 10 1\n' \ |
| 144 | "$t0" "$((t0 + 511 * 86400))" "$((t0 + 512 * 86400))" "$((t0 - 3 * 86400))" >"$T/days.log" |
| 145 | mkdir "$T/d" |
| 146 | (cd "$T/d" && "$T/visits" "$T/days.log"); eq "day window log exit" "$?" 0 |
| 147 | eq "requests and those outside the day table" "$(grep '^T' "$T/d/visits.tsv" | cut -f2,15)" "$(printf '4\t2')" |
| 148 | eq "day lines inside the table" "$(grep -c '^D' "$T/d/visits.tsv")" 2 |
| 149 | eq "page shows requests outside the day table" "$(grep -c '2 requests outside the 512-day table' "$T/d/visits.html")" 1 |
| 150 | |
| 151 | # negative control: every request counts as a new visitor of its day |
| 152 | sed 's/^if sadd1 xor mul vh 1099511628211 day$/if true/' "$V/visits.om" >"$T/mutant.om" |
| 153 | if cmp -s "$V/visits.om" "$T/mutant.om"; then |
| 154 | no "mutant differs from source" |
| 155 | else |
| 156 | "$OMC" <"$T/mutant.om" >"$T/mutant" 2>/dev/null && chmod +x "$T/mutant" |
| 157 | mkdir "$T/m" |
| 158 | (cd "$T/m" && "$T/mutant" "$T/log") |
| 159 | sort "$T/m/visits.tsv" >"$T/mgot.tsv" |
| 160 | if cmp -s "$T/mgot.tsv" "$T/want.tsv"; then no "mutant that overcounts daily visitors is caught"; else ok "mutant that overcounts daily visitors is caught"; fi |
| 161 | fi |
| 162 | |
| 163 | # negative control: the page writes numbers without group marks |
| 164 | sed 's/^if eq rem n 3 0$/if false/' "$V/visits.om" >"$T/mutant2.om" |
| 165 | if cmp -s "$V/visits.om" "$T/mutant2.om"; then |
| 166 | no "mutant 2 differs from source" |
| 167 | else |
| 168 | "$OMC" <"$T/mutant2.om" >"$T/mutant2" 2>/dev/null && chmod +x "$T/mutant2" |
| 169 | mkdir "$T/m2" |
| 170 | (cd "$T/m2" && "$T/mutant2" "$T/log") |
| 171 | if [ "$(grep -c "<dt>Requests</dt><dd>$(grp "$REQ")</dd>" "$T/m2/visits.html")" = 1 ]; then no "mutant without group marks is caught"; else ok "mutant without group marks is caught"; fi |
| 172 | fi |
| 173 | |
| 174 | # negative control: names are matched in lower case only |
| 175 | sed 's/^if and ge x 65 le x 90$/if false/' "$V/visits.om" >"$T/mutant3.om" |
| 176 | if cmp -s "$V/visits.om" "$T/mutant3.om"; then |
| 177 | no "mutant 3 differs from source" |
| 178 | else |
| 179 | "$OMC" <"$T/mutant3.om" >"$T/mutant3" 2>/dev/null && chmod +x "$T/mutant3" |
| 180 | mkdir "$T/m3" |
| 181 | (cd "$T/m3" && "$T/mutant3" "$T/log") |
| 182 | sort "$T/m3/visits.tsv" >"$T/m3got.tsv" |
| 183 | if cmp -s "$T/m3got.tsv" "$T/want.tsv"; then no "mutant that misses AhrefsBOT and ExampleCRAWLer is caught"; else ok "mutant that misses AhrefsBOT and ExampleCRAWLer is caught"; fi |
| 184 | fi |
| 185 | |
| 186 | # negative control: every word after the first is looked for together with the space before it in |
| 187 | # the list, so an agent holding such a word with no space in front of it is no longer caught |
| 188 | sed 's/^s = add i 1$/s = i/' "$V/visits.om" >"$T/mutant4.om" |
| 189 | if cmp -s "$V/visits.om" "$T/mutant4.om"; then |
| 190 | no "mutant 4 differs from source" |
| 191 | else |
| 192 | "$OMC" <"$T/mutant4.om" >"$T/mutant4" 2>/dev/null && chmod +x "$T/mutant4" |
| 193 | mkdir "$T/m4" |
| 194 | (cd "$T/m4" && "$T/mutant4" "$T/wlog") |
| 195 | if [ "$(grep '^A' "$T/m4/visits.tsv" | cut -f2)" = "$NW" ]; then no "mutant that looks for later words with a leading space is caught"; else ok "mutant that looks for later words with a leading space is caught"; fi |
| 196 | fi |
| 197 | |
| 198 | echo "RESULT pass=$pass fail=$fail" |
| 199 | [ "$fail" -eq 0 ] |