Files

29 lines
3.5 KiB
Java

package cn.ucvl.zhaodisplay;
/** Executes real URL construction and WebView allowlists without network or Android runtime. */
public class ServerEndpointTest {
static int assertions;
static void check(boolean condition,String message){assertions++;if(!condition)throw new AssertionError(message);}
public static void main(String[] args){
check(ServerEndpoint.api("/api/phone/home").equals("https://home.ucvl.cn/api/phone/home"),"phone API uses the official domain");
check(ServerEndpoint.api("/api/display/heartbeat").equals("https://home.ucvl.cn/api/display/heartbeat"),"display heartbeat shares the same endpoint");
check(ServerEndpoint.MAP_PHONE.equals("https://home.ucvl.cn/maps/phone"),"first-party map entry uses official origin");
check(ServerEndpoint.tvNavigation(ServerEndpoint.TV+"?client=android&load=42#display=fixture"),"display pairing fragment stays on official TV origin");
check(ServerEndpoint.tvNavigation("https://home.ucvl.cn:443/tv/child"),"explicit HTTPS default port and TV child allowed");
for(String url:new String[]{"https://home.ucvl.cn/maps/phone","https://home.ucvl.cn:443/maps/tile?z=1&x=1&y=0","https://home.ucvl.cn/ui/assets/map-abc.js"})check(ServerEndpoint.mapResource(url),"allow only first-party map resources: "+url);
check(ServerEndpoint.mapPage("https://home.ucvl.cn/maps/phone?theme=day"),"ready callback accepts exact map document with query");
check(!ServerEndpoint.mapPage("https://home.ucvl.cn/maps/phone-evil"),"map ready is not a prefix match");
check(!ServerEndpoint.mapPage("https://home.ucvl.cn/maps/tile"),"tile cannot mark map document ready");
for(String url:new String[]{null,"","http://home.ucvl.cn/maps/phone","https://zhao.ucvl.cn/maps/phone","https://home.ucvl.cn.evil.invalid/maps/phone","https://evil.invalid@home.ucvl.cn/maps/phone","https://home.ucvl.cn@evil.invalid/maps/phone","https://home.ucvl.cn:444/maps/phone","https://home.ucvl.cn./maps/phone","//home.ucvl.cn/maps/phone","file:///maps/phone","javascript:alert(1)","https://home.ucvl.cn/maps/../api/private","https://home.ucvl.cn/maps/%2e%2e/api/private","https://home.ucvl.cn/maps/%252e%252e/api/private","https://home.ucvl.cn/maps/%2f..%2fapi/private","https://home.ucvl.cn/maps\\phone","https://home.ucvl.cn/maps//phone"}){
check(!ServerEndpoint.mapResource(url),"reject unsafe map resource: "+url);
check(!ServerEndpoint.mapPage(url),"reject unsafe map completion URL: "+url);
}
for(String url:new String[]{"https://home.ucvl.cn/api/phone/home","https://home.ucvl.cn/maps","https://home.ucvl.cn/ui/assets-evil/map.js","https://home.ucvl.cn/ui/private"})check(!ServerEndpoint.mapResource(url),"reject unrelated first-party path: "+url);
for(String url:new String[]{"https://home.ucvl.cn/tv-evil","https://home.ucvl.cn/tv/../api","https://home.ucvl.cn:8443/tv","https://evil.invalid/tv","https://zhao.ucvl.cn/tv","https://home.ucvl.cn/maps/phone","https://user@home.ucvl.cn/tv","http://home.ucvl.cn/tv"})check(!ServerEndpoint.tvNavigation(url),"reject unsafe TV navigation: "+url);
for(String path:new String[]{null,"","api/voice","//evil.invalid/api/voice","https://evil.invalid/api/voice","/maps/phone","/api/../tv","/api/%2e%2e/tv","/api/voice#token"}){
try{ServerEndpoint.api(path);throw new AssertionError("unsafe API path accepted: "+path);}catch(IllegalArgumentException expected){assertions++;}
}
System.out.println("Server endpoint allowlist: "+assertions+" assertions passed");
}
}