Files
ghostwire/whatsapp_suite/webview.py
T

89 lines
3.7 KiB
Python
Raw Normal View History

"""Isolated WebView2 test host."""
import json
import pathlib
import shutil
import subprocess
import urllib.request
import zipfile
from .common import BASE
ROOT = BASE / "webview2-harmless-test"
VERSION = "1.0.2903.40"
SOURCE = r'''
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
class Probe : Form {
WebView2 view = new WebView2();
string report;
Timer deadline = new Timer();
bool completed;
Probe(string path) {
report = path; ShowInTaskbar = false; Opacity = 0;
Controls.Add(view); view.Dock = DockStyle.Fill;
deadline.Interval = 45000;
deadline.Tick += (s,e) => Finish("{\"passed\":false,\"error\":\"timeout\"}", 2);
Shown += async (s,e) => {
deadline.Start();
try {
var env = await CoreWebView2Environment.CreateAsync(null,
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test-profile"));
await view.EnsureCoreWebView2Async(env);
var result = await view.CoreWebView2.ExecuteScriptAsync(
"({arithmetic:1+1,marker:'isolated-webview2-test',url:location.href})");
bool ok = result.Contains("\"arithmetic\":2") && result.Contains("isolated-webview2-test");
Finish("{\"passed\":" + (ok ? "true" : "false") +
",\"method\":\"CoreWebView2.ExecuteScriptAsync\",\"external_process_injection\":false," +
"\"result\":" + result + "}", ok ? 0 : 1);
} catch(Exception ex) {
File.WriteAllText(report + ".error.txt", ex.ToString());
Finish("{\"passed\":false,\"error\":\"See .error.txt\"}", 1);
}
};
}
void Finish(string data, int code) {
if(completed) return; completed=true; deadline.Stop();
File.WriteAllText(report,data); Environment.ExitCode=code;
view.Dispose(); Close();
}
[STAThread] static void Main(string[] args) {
Application.EnableVisualStyles(); Application.Run(new Probe(args[0]));
}
}
'''
def run_webview_test():
ROOT.mkdir(exist_ok=True)
package = ROOT / ('webview2-' + VERSION + '.nupkg')
if not package.exists():
urllib.request.urlretrieve(
'https://api.nuget.org/v3-flatcontainer/microsoft.web.webview2/' + VERSION +
'/microsoft.web.webview2.' + VERSION + '.nupkg', package)
with zipfile.ZipFile(package) as archive:
for name in ('lib/net462/Microsoft.Web.WebView2.Core.dll',
'lib/net462/Microsoft.Web.WebView2.WinForms.dll',
'runtimes/win-x64/native/WebView2Loader.dll'):
with archive.open(name) as src, (ROOT / pathlib.Path(name).name).open('wb') as dest:
shutil.copyfileobj(src, dest)
source = ROOT / 'Probe.cs'
source.write_text(SOURCE, encoding='utf-8')
exe = ROOT / 'Probe.exe'
compiler = r'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe'
subprocess.run([compiler, '/nologo', '/target:winexe', '/platform:x64',
'/out:' + str(exe), '/r:System.Windows.Forms.dll',
'/r:System.Drawing.dll',
'/r:' + str(ROOT / 'Microsoft.Web.WebView2.Core.dll'),
'/r:' + str(ROOT / 'Microsoft.Web.WebView2.WinForms.dll'),
str(source)], check=True, creationflags=subprocess.CREATE_NO_WINDOW)
report = ROOT / 'result.json'
report.unlink(missing_ok=True)
run = subprocess.run([str(exe), str(report)], cwd=ROOT, timeout=60,
creationflags=subprocess.CREATE_NO_WINDOW)
if report.exists():
print(json.dumps(json.loads(report.read_text(encoding='utf-8-sig')), indent=2))
else:
raise RuntimeError('Test host exited without a report: ' + str(run.returncode))
return run.returncode