Local Network access permission is not taken into account inside a UI Testing Bundle

Originator:aciobanu
Number:rdar://FB983096 Date Originated:5/1/2022
Status:Open Resolved:
Product:Network Framework Product Version:Xcode 13.2.1
Classification: Reproducible:Yes
 
Making a local network request from a test inside a UI Testing Bundle (using URLSession) fails with the following error:

```
Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2824fa670 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Local network prohibited), interface: en0, ipv4, _kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5E125442-C766-46B1-A172-6CF9DA76DC85>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <5E125442-C766-46B1-A172-6CF9DA76DC85>.<1>
```

The same code works from a Unit Testing Bundle or from the main iOS app target.

Running the unit test for the first time (clean install) triggers the local network privacy prompt, while running the UI test does not trigger the privacy prompt and the request fails with the error above. 

STEPS TO REPRODUCE 
1. Start a local server using `python -m http.server`;
2. From a test inside a UI Testing Bundle, make a call to the local server using URLSession:

```
func testLocalNetworkRequest() throws {
  var localNetworkUrlComponents = URLComponents()
  localNetworkUrlComponents.scheme = "http"
  localNetworkUrlComponents.host = "[YOUR_IP_HERE]"
  localNetworkUrlComponents.port = 8000 // this is the default port, change it if needed

  let localNetworkURL = localNetworkUrlComponents.url!
  let localNetworkRequestExpectation = XCTestExpectation(description: "Local network request.")

  URLSession.shared.dataTask(with: localNetworkURL) { data, response, error in
    if let error = error {
      XCTFail("Failed local network call: \(String(describing: error)).")
    } 

    if let data = data, let html = String(data: data, encoding: .utf8) {
      dump(html)
      XCTAssert(!html.isEmpty)
    }

    localNetworkRequestExpectation.fulfill()
  }.resume()

  wait(for: [localNetworkRequestExpectation], timeout: 100)
}
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!