文件
dayu-color/tools/verify_beta.mm
T

43 行
4.5 KiB
Plaintext

#import <AppKit/AppKit.h>
#import "../大鱼调色/Shared/DYBetaDiagnostics.h"
#import "../大鱼调色/Wrapper Application/DYBetaUpdater.h"
static NSUInteger checks;
static void check(BOOL pass,NSString *name){if(!pass){fprintf(stderr,"FAIL %s\n",name.UTF8String);exit(1);}checks++;}
int main(int argc,char **argv){@autoreleasepool{
if(argc==2&&!strcmp(argv[1],"--live-update")){
DYBetaUpdater *updater=[DYBetaUpdater new];NSMutableArray *phases=[NSMutableArray array];__block BOOL done=NO,passed=NO,started=NO;
updater.changed=^(NSDictionary *state){NSString *phase=state[@"phase"];[phases addObject:phase];
if([phase isEqual:@"available"]){if(started){done=YES;fprintf(stderr,"%s\n",[state description].UTF8String);}else{started=YES;dispatch_async(dispatch_get_main_queue(),^{[updater download];});}}
if([phase isEqual:@"ready"]){done=YES;passed=YES;}
if([@[@"failed",@"latest",@"integrity_rejected"] containsObject:phase]){
done=YES;fprintf(stderr,"%s\n",[state description].UTF8String);
if([phase isEqual:@"integrity_rejected"])fprintf(stderr,"Run the live update check in the officially installed app.\n");
}
};
[updater check];NSDate *deadline=[NSDate dateWithTimeIntervalSinceNow:360];
while(!done&&deadline.timeIntervalSinceNow>0)[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.1]];
NSDictionary *result=@{@"passed":@(passed),@"phases":phases,@"source":@"production HTTPS beta endpoint and download",@"installation_performed":@NO};
printf("%s\n",[[[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:result options:0 error:nil] encoding:NSUTF8StringEncoding]autorelease].UTF8String);
updater.changed=nil;[updater cancel];return passed?0:1;
}
NSString *base=@"https://dayu.media/wp-content/uploads/dayu-color-beta/updates/";
NSMutableDictionary *manifest=[@{@"bundle_id":@"com.dayu.color",@"channel":@"beta",@"version":@"1.7.0",@"build":@45,@"size":@123,
@"sha256":[@"a" stringByPaddingToLength:64 withString:@"a" startingAtIndex:0],@"package_type":@"pkg",@"install_mode":@"pkg_installer",@"download_url":[base stringByAppendingString:@"dayu-color-1-7-0-45.pkg"]}mutableCopy];
check([DYBetaUpdater validateManifest:manifest error:nil],@"valid manifest");
for(NSString *key in manifest.allKeys){for(id bad in @[[NSNull null],@[],@{},@YES,@"",@(-1)]){NSMutableDictionary *changed=[[manifest mutableCopy]autorelease];changed[key]=bad;
check(![DYBetaUpdater validateManifest:changed error:nil],[NSString stringWithFormat:@"reject %@ %@",key,bad]);}}
check(![DYBetaUpdater validateManifest:(id)@[] error:nil],@"non dictionary manifest");
for(NSString *bad in @[@"http://dayu.media/wp-json/dayu-color/v1/update",@"https://dayu.media.evil.test/wp-json/dayu-color/v1/update",@"https://dayu.media:444/wp-json/dayu-color/v1/update",@"https://user@dayu.media/wp-json/dayu-color/v1/update",@"https://dayu.media/wp-json/other/v1/update",@"https://dayu.media/wp-json/dayu-color/v1/../other"])
check(!DYBetaAllowedURL([NSURL URLWithString:bad],NO),@"reject foreign URL");
NSDictionary *safe=DYBetaSafeFields(@{@"phase":@"end",@"frames":@"/Users/test/secret.mov",@"error_domain":@"token=secret",@"email":@"private@example.invalid",@"duration_ms":@(NAN)});
check([safe[@"frames"]isEqual:@"[REDACTED]"]&&[safe[@"error_domain"]isEqual:@"[REDACTED]"]&&!safe[@"email"]&&!safe[@"duration_ms"],@"privacy allowlist and redaction");
check(DYBetaSafeFields((id)@[]).count==0,@"bad fields do not crash");
if(argc==3){NSData *data=[NSData dataWithContentsOfFile:[NSString stringWithUTF8String:argv[2]]];NSDictionary *real=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];NSError *error=nil;
BOOL verified=[DYBetaUpdater validatePackage:[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]] manifest:real error:&error];
if(!verified)fprintf(stderr,"%s\n",error.localizedDescription.UTF8String);check(verified,@"signed notarized full product package");
NSMutableDictionary *wrong=[[real mutableCopy]autorelease];wrong[@"sha256"]=[@"0" stringByPaddingToLength:64 withString:@"0" startingAtIndex:0];
check(![DYBetaUpdater validatePackage:[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]] manifest:wrong error:nil],@"corrupt checksum refused before install");
}
printf("{\"passed\":true,\"checks\":%lu}\n",(unsigned long)checks);[manifest release];
}return 0;}