|
6 | 6 | using ObjCRuntime; |
7 | 7 | #endif |
8 | 8 |
|
| 9 | +#if ENABLE_RUNTIME_DRIFT_CASE_CLOUDFUNCTIONS_USEFUNCTIONSEMULATORORIGIN |
| 10 | +using Firebase.CloudFunctions; |
| 11 | +using Foundation; |
| 12 | +using ObjCRuntime; |
| 13 | +#endif |
| 14 | + |
9 | 15 | namespace FirebaseFoundationE2E; |
10 | 16 |
|
11 | 17 | static class FirebaseRuntimeDriftCases |
@@ -136,6 +142,95 @@ void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEven |
136 | 142 | } |
137 | 143 | #endif |
138 | 144 |
|
| 145 | +#if ENABLE_RUNTIME_DRIFT_CASE_CLOUDFUNCTIONS_USEFUNCTIONSEMULATORORIGIN |
| 146 | + static Task<string> VerifyCloudFunctionsUseFunctionsEmulatorOriginAsync() |
| 147 | + { |
| 148 | + const string staleSelector = "useFunctionsEmulatorOrigin:"; |
| 149 | + const string liveSelector = "useEmulatorWithHost:port:"; |
| 150 | + |
| 151 | + var functions = CloudFunctions.DefaultInstance; |
| 152 | + if (functions is null) |
| 153 | + { |
| 154 | + throw new InvalidOperationException("Firebase.CloudFunctions.CloudFunctions.DefaultInstance returned null after App.Configure()."); |
| 155 | + } |
| 156 | + |
| 157 | + var staleMethod = typeof(CloudFunctions).GetMethod( |
| 158 | + "UseFunctionsEmulatorOrigin", |
| 159 | + BindingFlags.Instance | BindingFlags.Public, |
| 160 | + binder: null, |
| 161 | + types: new[] { typeof(string) }, |
| 162 | + modifiers: null); |
| 163 | + if (staleMethod is not null) |
| 164 | + { |
| 165 | + throw new InvalidOperationException( |
| 166 | + $"CloudFunctions still exposes stale managed API '{staleMethod.Name}', so callers can still reach selector '{staleSelector}'."); |
| 167 | + } |
| 168 | + |
| 169 | + var staleProperty = typeof(CloudFunctions).GetProperty("EmulatorOrigin", BindingFlags.Instance | BindingFlags.Public); |
| 170 | + if (staleProperty is not null) |
| 171 | + { |
| 172 | + throw new InvalidOperationException( |
| 173 | + $"CloudFunctions still exposes stale managed property '{staleProperty.Name}', which no longer maps to a real ObjC surface."); |
| 174 | + } |
| 175 | + |
| 176 | + if (functions.RespondsToSelector(new Selector(staleSelector))) |
| 177 | + { |
| 178 | + throw new InvalidOperationException( |
| 179 | + $"Native FIRFunctions still responds to stale selector '{staleSelector}', so the runtime drift would still be reachable."); |
| 180 | + } |
| 181 | + |
| 182 | + if (!functions.RespondsToSelector(new Selector(liveSelector))) |
| 183 | + { |
| 184 | + throw new InvalidOperationException( |
| 185 | + $"Native FIRFunctions does not respond to expected live selector '{liveSelector}'."); |
| 186 | + } |
| 187 | + |
| 188 | + NSException? marshaledException = null; |
| 189 | + MarshalObjectiveCExceptionMode? marshaledExceptionMode = null; |
| 190 | + |
| 191 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 192 | + { |
| 193 | + marshaledException ??= args.Exception; |
| 194 | + marshaledExceptionMode ??= args.ExceptionMode; |
| 195 | + } |
| 196 | + |
| 197 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 198 | + try |
| 199 | + { |
| 200 | + try |
| 201 | + { |
| 202 | + functions.UseEmulatorOriginWithHost("127.0.0.1", 5002); |
| 203 | + } |
| 204 | + catch (ObjCException ex) |
| 205 | + { |
| 206 | + throw new InvalidOperationException( |
| 207 | + $"Selector '{liveSelector}' should not throw after the binding fix, but observed {ex.GetType().FullName}. " + |
| 208 | + $"Runtime host argument type: {typeof(string).FullName}. Runtime port argument type: {typeof(uint).FullName}. " + |
| 209 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 210 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 211 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 212 | + ex); |
| 213 | + } |
| 214 | + |
| 215 | + if (marshaledException is not null) |
| 216 | + { |
| 217 | + throw new InvalidOperationException( |
| 218 | + $"Cloud Functions emulator API completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 219 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 220 | + } |
| 221 | + |
| 222 | + return Task.FromResult( |
| 223 | + $"Removed stale managed selector '{staleSelector}' and property 'EmulatorOrigin'. " + |
| 224 | + $"Live selector '{liveSelector}' completed without ObjC exception after the binding fix. " + |
| 225 | + $"Runtime host argument type: {typeof(string).FullName}. Runtime port argument type: {typeof(uint).FullName}."); |
| 226 | + } |
| 227 | + finally |
| 228 | + { |
| 229 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 230 | + } |
| 231 | + } |
| 232 | +#endif |
| 233 | + |
139 | 234 | static string FormatDetail(string? value) |
140 | 235 | { |
141 | 236 | return string.IsNullOrWhiteSpace(value) ? "<empty>" : value; |
|
0 commit comments