Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85ec17a

Browse files
authored
[Network] Improve bindings for NWProtocolMetadata. (#6389)
* [Network] Improve bindings for NWProtocolMetadata.It turns out the NWProtocolMetadata can contain metadata for differentprotocols (Ip/Tls/Tcp). This is important; if someone tries to get a value forone protocol and the metadata is for another protocol, then they invoke thewrath of superior beings who will smite that poor someone with uninitializedmemory.At that point there's not much left but to pray.I don't like to depend on divine intervention, so I've modified the API hereto check if the metadata's protocol is the required type for the native APIwe're calling, and if the check fails, we throw a nice and dependable managedexception.This is a functional breaking change; but if there are any lost souls wholikes to pray, they can always re-implement the P/Invokes themselves and skipthe sanity checks.In addition I've renamed a few properties whose name didn't clearly specifywhich protocol type they operate on.Ref: Apple feedback FB6155967.Ref:https://trello.com/c/1TW0BSKJ/145-fb6155967-nwipcreatemetadata-returns-uninitialized-metadata-in-ios-13* Fix casing in exception message.* [tests] Adjust the SecProtocolMetadataTest according to new knowledge about the Network API.* Fix alternative property name in obsolete attribute.
1 parent57d44d1 commit85ec17a

File tree

3 files changed

+123
-46
lines changed

3 files changed

+123
-46
lines changed

‎src/Network/NWProtocolMetadata.cs‎

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,35 @@ public NWProtocolMetadata (IntPtr handle, bool owns) : base (handle, owns) {}
8787
[DllImport(Constants.NetworkLibrary)]
8888
staticexternIntPtrnw_tls_copy_sec_protocol_metadata(IntPtrhandle);
8989

90-
publicSecProtocolMetadataSecProtocolMetadata=>newSecProtocolMetadata(nw_tls_copy_sec_protocol_metadata(GetCheckedHandle()),owns:true);
90+
voidCheckIsIP()
91+
{
92+
if(!IsIP)
93+
thrownewInvalidOperationException("This metadata is not IP metadata.");
94+
}
95+
96+
voidCheckIsTcp()
97+
{
98+
if(!IsTcp)
99+
thrownewInvalidOperationException("This metadata is not TCP metadata.");
100+
}
101+
102+
voidCheckIsTls()
103+
{
104+
if(!IsTls)
105+
thrownewInvalidOperationException("This metadata is not TLS metadata.");
106+
}
107+
108+
#if!XAMCORE_4_0
109+
[Obsolete("Use 'TlsSecProtocolMetadata' instead.")]
110+
publicSecProtocolMetadataSecProtocolMetadata=>TlsSecProtocolMetadata;
111+
#endif
112+
113+
publicSecProtocolMetadataTlsSecProtocolMetadata{
114+
get{
115+
CheckIsTls();
116+
returnnewSecProtocolMetadata(nw_tls_copy_sec_protocol_metadata(GetCheckedHandle()),owns:true);
117+
}
118+
}
91119

92120
[DllImport(Constants.NetworkLibrary)]
93121
staticexternvoidnw_ip_metadata_set_ecn_flag(OS_nw_protocol_metadatametadata,NWIPEcnFlagecn_flag);
@@ -96,15 +124,24 @@ public NWProtocolMetadata (IntPtr handle, bool owns) : base (handle, owns) {}
96124
staticexternNWIPEcnFlagnw_ip_metadata_get_ecn_flag(OS_nw_protocol_metadatametadata);
97125

98126
publicNWIPEcnFlagIPMetadataEcnFlag{
99-
get=>nw_ip_metadata_get_ecn_flag(GetCheckedHandle());
100-
set=>nw_ip_metadata_set_ecn_flag(GetCheckedHandle(),value);
127+
get{
128+
CheckIsIP();
129+
returnnw_ip_metadata_get_ecn_flag(GetCheckedHandle());
130+
}
131+
set{
132+
CheckIsIP();
133+
nw_ip_metadata_set_ecn_flag(GetCheckedHandle(),value);
134+
}
101135
}
102136

103137
[DllImport(Constants.NetworkLibrary)]
104138
staticextern/* uint64_t */ulongnw_ip_metadata_get_receive_time(OS_nw_protocol_metadatametadata);
105139

106140
publiculongIPMetadataReceiveTime{
107-
get=>nw_ip_metadata_get_receive_time(GetCheckedHandle());
141+
get{
142+
CheckIsIP();
143+
returnnw_ip_metadata_get_receive_time(GetCheckedHandle());
144+
}
108145
}
109146

110147
[DllImport(Constants.NetworkLibrary)]
@@ -113,19 +150,41 @@ public ulong IPMetadataReceiveTime {
113150
[DllImport(Constants.NetworkLibrary)]
114151
staticexternNWServiceClassnw_ip_metadata_get_service_class(OS_nw_protocol_metadatametadata);
115152

153+
#if!XAMCORE_4_0
154+
[Obsolete("Use 'IPServiceClass' instead.")]
116155
publicNWServiceClassServiceClass{
117-
get=>nw_ip_metadata_get_service_class(GetCheckedHandle());
118-
set=>nw_ip_metadata_set_service_class(GetCheckedHandle(),value);
156+
get=>IPServiceClass;
157+
set=>IPServiceClass=value;
158+
}
159+
#endif
160+
161+
publicNWServiceClassIPServiceClass{
162+
get{
163+
CheckIsIP();
164+
returnnw_ip_metadata_get_service_class(GetCheckedHandle());
165+
}
166+
set{
167+
CheckIsIP();
168+
nw_ip_metadata_set_service_class(GetCheckedHandle(),value);
169+
}
119170
}
120171

121172
[DllImport(Constants.NetworkLibrary)]
122173
externstatic/* uint32_t */uintnw_tcp_get_available_receive_buffer(IntPtrhandle);
123174

124-
publicuintTcpGetAvailableReceiveBuffer()=>nw_tcp_get_available_receive_buffer(GetCheckedHandle());
175+
publicuintTcpGetAvailableReceiveBuffer()
176+
{
177+
CheckIsTcp();
178+
returnnw_tcp_get_available_receive_buffer(GetCheckedHandle());
179+
}
125180

126181
[DllImport(Constants.NetworkLibrary)]
127182
externstatic/* uint32_t */uintnw_tcp_get_available_send_buffer(IntPtrhandle);
128183

129-
publicuintTcpGetAvailableSendBuffer()=>nw_tcp_get_available_send_buffer(GetCheckedHandle());
184+
publicuintTcpGetAvailableSendBuffer()
185+
{
186+
CheckIsTcp();
187+
returnnw_tcp_get_available_send_buffer(GetCheckedHandle());
188+
}
130189
}
131190
}

‎tests/monotouch-test/Network/NWProtocolMetadataTest.cs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,29 @@ public void IP ()
2828
Assert.False(m.IsTcp,"IsTcp");
2929
Assert.False(m.IsUdp,"IsUdp");
3030
Assert.NotNull(m.ProtocolDefinition,"ProtocolDefinition");
31-
Assert.NotNull(m.SecProtocolMetadata,"SecProtocolMetadata");
31+
Assert.Throws<InvalidOperationException>(()=>{varx=m.SecProtocolMetadata;},"SecProtocolMetadata");
32+
Assert.Throws<InvalidOperationException>(()=>{varx=m.TlsSecProtocolMetadata;},"TlsSecProtocolMetadata");
3233
Assert.That(m.ServiceClass,Is.EqualTo(NWServiceClass.BestEffort),"ServiceClass");
34+
Assert.That(m.IPServiceClass,Is.EqualTo(NWServiceClass.BestEffort),"IPServiceClass");
3335
}
3436
}
3537

3638
[Test]
3739
publicvoidUdp()
3840
{
3941
using(varm=NWProtocolMetadata.CreateUdpMetadata()){
40-
Assert.That(m.IPMetadataEcnFlag,Is.EqualTo(NWIPEcnFlag.NonEct),"IPMetadataEcnFlag");
41-
Assert.That(m.IPMetadataReceiveTime,Is.EqualTo(0),"IPMetadataReceiveTime");
42+
Assert.Throws<InvalidOperationException>(()=>{varx=m.IPMetadataEcnFlag;},"IPMetadataEcnFlag");
43+
Assert.Throws<InvalidOperationException>(()=>{varx=m.IPMetadataReceiveTime;},"IPMetadataReceiveTime");
4244
Assert.False(m.IsIP,"IsIP");
4345
Assert.False(m.IsTcp,"IsTcp");
4446
Assert.True(m.IsUdp,"IsUdp");
4547
Assert.NotNull(m.ProtocolDefinition,"ProtocolDefinition");
46-
Assert.NotNull(m.SecProtocolMetadata,"SecProtocolMetadata");
47-
Assert.That(m.ServiceClass,Is.EqualTo(NWServiceClass.BestEffort),"ServiceClass");
48+
Assert.Throws<InvalidOperationException>(()=>{varx=m.SecProtocolMetadata;},"SecProtocolMetadata");
49+
Assert.Throws<InvalidOperationException>(()=>{varx=m.TlsSecProtocolMetadata;},"TlsSecProtocolMetadata");
50+
Assert.Throws<InvalidOperationException>(()=>{varx=m.ServiceClass;},"ServiceClass");
51+
Assert.Throws<InvalidOperationException>(()=>{varx=m.IPServiceClass;},"IPServiceClass");
4852
}
4953
}
5054
}
5155
}
52-
#endif
56+
#endif
Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#if!__WATCHOS__
22
usingSystem;
33
usingSystem.Runtime.InteropServices;
4+
usingSystem.Threading;
5+
6+
usingCoreFoundation;
47
usingFoundation;
58
usingNetwork;
69
usingObjCRuntime;
@@ -21,44 +24,55 @@ public void SetUp ()
2124
}
2225

2326
[Test]
24-
publicvoidIPDefaults()
27+
publicvoidTlsDefaults()
2528
{
26-
if(TestRuntime.CheckXcodeVersion(11,0))
27-
Assert.Ignore("NWProtocolMetadata.CreateIPMetadata () returns a metadata object with uninitialized metadata, which means the asserts here fail randomly.");
29+
using(varep=NWEndpoint.Create("www.microsoft.com","https"))
30+
using(varparameters=NWParameters.CreateSecureTcp())
31+
using(varqueue=newDispatchQueue(GetType().FullName)){
32+
varconnection=newNWConnection(ep,parameters);
2833

29-
using(varm=NWProtocolMetadata.CreateIPMetadata()){
30-
vars=m.SecProtocolMetadata;
31-
// This is mostly, but not always, returning false
32-
// Assert.False (s.EarlyDataAccepted, "EarlyDataAccepted");
33-
Assert.That(s.NegotiatedCipherSuite,Is.EqualTo(SslCipherSuite.SSL_NULL_WITH_NULL_NULL),"NegotiatedCipherSuite");
34-
Assert.Null(s.NegotiatedProtocol,"NegotiatedProtocol");
35-
Assert.That(s.NegotiatedProtocolVersion,Is.EqualTo(SslProtocol.Unknown),"NegotiatedProtocolVersion");
36-
Assert.Null(s.PeerPublicKey,"PeerPublicKey");
37-
#iffalse
38-
Assert.True(SecProtocolMetadata.ChallengeParametersAreEqual(s,s),"ChallengeParametersAreEqual");
39-
Assert.True(SecProtocolMetadata.PeersAreEqual(s,s),"PeersAreEqual");
40-
#endif
41-
}
42-
}
34+
varready=newManualResetEvent(false);
35+
connection.SetStateChangeHandler((state,error)=>{
36+
Console.WriteLine(state);
37+
switch(state){
38+
caseNWConnectionState.Cancelled:
39+
caseNWConnectionState.Failed:
40+
// We can't dispose until the connection has been closed or it failed.
41+
connection.Dispose();
42+
break;
43+
caseNWConnectionState.Invalid:
44+
caseNWConnectionState.Preparing:
45+
caseNWConnectionState.Waiting:
46+
break;
47+
caseNWConnectionState.Ready:
48+
ready.Set();
49+
break;
50+
default:
51+
break;
52+
}
53+
});
4354

44-
#iffalse
45-
[DllImport(Constants.CoreFoundationLibrary)]
46-
externstaticnintCFGetRetainCount(IntPtrhandle);
55+
connection.SetQueue(queue);
56+
connection.Start();
4757

48-
[Test]
49-
publicvoidCreateSecret()
50-
{
51-
using(varnpm=NWProtocolMetadata.CreateIPMetadata()){
52-
// `npm` and `spm` have the same handle - same internal object satistfy both protocols
53-
Console.WriteLine($"{CFGetRetainCount(npm.Handle)}");
54-
using(varspm=npm.SecProtocolMetadata){
55-
Console.WriteLine($"{CFGetRetainCount(npm.Handle)}");
56-
Console.WriteLine($"{CFGetRetainCount(spm.Handle)}");
57-
varsecret=spm.CreateSecret("test",16);// crash
58+
// Wait until the connection is ready.
59+
Assert.True(ready.WaitOne(TimeSpan.FromSeconds(10)),"Connection is ready");
60+
61+
using(varm=connection.GetProtocolMetadata(NWProtocolDefinition.TlsDefinition)){
62+
vars=m.TlsSecProtocolMetadata;
63+
Assert.False(s.EarlyDataAccepted,"EarlyDataAccepted");
64+
Assert.That(s.NegotiatedCipherSuite,Is.Not.EqualTo(SslCipherSuite.SSL_NULL_WITH_NULL_NULL),"NegotiatedCipherSuite");
65+
Assert.Null(s.NegotiatedProtocol,"NegotiatedProtocol");
66+
Assert.That(s.NegotiatedProtocolVersion,Is.EqualTo(SslProtocol.Tls_1_2).Or.EqualTo(SslProtocol.Tls_1_3),"NegotiatedProtocolVersion");
67+
Assert.NotNull(s.PeerPublicKey,"PeerPublicKey");
68+
69+
Assert.True(SecProtocolMetadata.ChallengeParametersAreEqual(s,s),"ChallengeParametersAreEqual");
70+
Assert.True(SecProtocolMetadata.PeersAreEqual(s,s),"PeersAreEqual");
5871
}
72+
73+
connection.Cancel();
5974
}
6075
}
61-
#endif
6276
}
6377
}
64-
#endif
78+
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp