-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathurl_encoding_utf8_codepage_semantic.dpr
More file actions
113 lines (105 loc) · 4.16 KB
/
Copy pathurl_encoding_utf8_codepage_semantic.dpr
File metadata and controls
113 lines (105 loc) · 4.16 KB
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
program url_encoding_utf8_codepage_semantic;
{$mode delphi}{$H+}
uses
mormot.core.fpcx64mm,
{$ifdef UNIX}
cthreads,
cwstring,
{$endif UNIX}
System.SysUtils,
System.NetEncoding;
procedure Check(ACondition: Boolean; const AMessage: string);
begin
if not ACondition then
raise Exception.Create('URL_ENCODING_UTF8_CODEPAGE_FAIL: '+AMessage);
end;
function BinanceUnicodeSymbol: UnicodeString;
begin
SetLength(Result,8);
Result[1]:=WideChar($5e01);
Result[2]:=WideChar($5b89);
Result[3]:=WideChar($4eba);
Result[4]:=WideChar($751f);
Result[5]:='U';
Result[6]:='S';
Result[7]:='D';
Result[8]:='T';
end;
procedure CheckEncoding(const AContext: string);
const
ExpectedUnicode = '%E5%B8%81%E5%AE%89%E4%BA%BA%E7%94%9FUSDT';
ExpectedAscii = 'AZaz09-_.!*''()@$+%2F%3F%3D%26%2B%25%00';
var
Decoded: UnicodeString;
RawBinary: RawByteString;
RawInput: RawByteString;
begin
Check(TNetEncoding.URL.Encode(BinanceUnicodeSymbol)=ExpectedUnicode,
AContext+' UnicodeString');
RawInput:=UTF8Encode(BinanceUnicodeSymbol);
Check(TNetEncoding.URL.Encode(RawInput)=ExpectedUnicode,
AContext+' RawByteString');
{ Length-aware encoding of an embedded NUL is the existing FPC raw-byte
contract. Delphi's PChar-based implementation stops at NUL; this check
prevents the byte-safe repair from regressing the FPC extension. }
Check(TNetEncoding.URL.Encode('AZaz09-_.!*''()@$ /?=&+%'+#0)=ExpectedAscii,
AContext+' ASCII, reserved bytes and embedded NUL');
SetLength(RawBinary,4);
RawBinary[1]:=AnsiChar(0);
RawBinary[2]:=AnsiChar($7f);
RawBinary[3]:=AnsiChar($80);
RawBinary[4]:=AnsiChar($ff);
Check(TNetEncoding.URL.Encode(RawBinary)='%00%7F%80%FF',
AContext+' arbitrary raw bytes');
Check(TNetEncoding.URL.Encode('')='',AContext+' empty input');
Decoded:=TNetEncoding.URL.Decode('Moon%20%D0%96');
Check(Decoded='Moon '+WideChar($0416),AContext+' UTF-8 decode');
Check(TNetEncoding.URL.Decode('a+b')='a b',AContext+' form-style plus decode');
Check(TURLEncoding.URIDecode('a+b',False)='a+b',
AContext+' URI plus preservation');
Check(TURLEncoding.URIDecode('a+b',True)='a b',
AContext+' URI plus conversion');
Check(TURLEncoding.URIDecode('%ZZ',False)='%ZZ',
AContext+' invalid percent escape preservation');
Check(TNetEncoding.URL.Encode('a+b c',[],
[TURLEncoding.TEncodeOption.SpacesAsPlus])='a%2Bb+c',
AContext+' plus is data when spaces use plus');
Check(TNetEncoding.URL.Encode('%2F',[],[])='%2F',
AContext+' existing escape preservation');
Check(TNetEncoding.URL.EncodeQuery('a+b#c',[])='a+b%23c',
AContext+' query unsafe set');
Check(TNetEncoding.URL.EncodePath(WideChar($0416)+'/'+WideChar($20ac),[])=
'/%D0%96/%E2%82%AC',AContext+' Unicode path bytes');
Check(TNetEncoding.URL.EncodePath('a//b/',[])='/a/b',
AContext+' path normalization');
Check(TNetEncoding.URL.EncodePath('',[])='/',AContext+' empty path');
Check(TNetEncoding.URL.EncodePath('///',[])='',AContext+' separators-only path');
Check(TNetEncoding.URL.EncodePath('a b?c',[])='/a+b%3Fc',
AContext+' path unsafe and space policy');
Check(TNetEncoding.URL.EncodePath('a'+#0+'b',[])='/a%00b',
AContext+' path embedded NUL');
Check(TNetEncoding.URL.EncodePath('a@b',[Ord('@')])='/a%40b',
AContext+' path custom unsafe set');
Check(TNetEncoding.URL.EncodePath('%2F',[])='/%2F',
AContext+' path existing escape preservation');
Check(TNetEncoding.URL.EncodePath('%2F',[Ord('%')])='/%2F',
AContext+' path escaped percent precedes custom unsafe policy');
Check(TNetEncoding.URL.EncodePath('%2F',[Ord('2')])='/%2F',
AContext+' path escaped hex digit precedes custom unsafe policy');
Check(TNetEncoding.URL.EncodePath('%ZZ',[Ord('%')])='/%25ZZ',
AContext+' path invalid escape obeys custom unsafe policy');
end;
var
OriginalCodePage: TSystemCodePage;
begin
OriginalCodePage:=DefaultSystemCodePage;
try
CheckEncoding('system code page');
SetMultiByteConversionCodePage(CP_UTF8);
Check(DefaultSystemCodePage=CP_UTF8,'UTF-8 code page setup');
CheckEncoding('UTF-8 default code page');
finally
SetMultiByteConversionCodePage(OriginalCodePage);
end;
WriteLn('URL_ENCODING_UTF8_CODEPAGE_PASS');
end.