Merge pull request #1019 from mamabusi/swift-3.1-branch

diff --git a/Foundation/NSHTTPCookieStorage.swift b/Foundation/NSHTTPCookieStorage.swift
index f659d31..a96d770 100644
--- a/Foundation/NSHTTPCookieStorage.swift
+++ b/Foundation/NSHTTPCookieStorage.swift
@@ -38,7 +38,6 @@
 
     private static var sharedStorage: HTTPCookieStorage?
     private static var sharedCookieStorages: [String: HTTPCookieStorage] = [:] //for group storage containers
-
     private var cookieFilePath: String!
     private let workQueue: DispatchQueue = DispatchQueue(label: "HTTPCookieStorage.workqueue")
     var allCookies: [String: HTTPCookie]
@@ -47,7 +46,13 @@
         allCookies = [:]
         cookieAcceptPolicy = .always
         super.init()
-        cookieFilePath = filePath(path: _CFXDGCreateDataHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName)
+        let bundlePath = Bundle.main.bundlePath
+        var bundleName = bundlePath.components(separatedBy: "/").last!
+        if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) {
+            bundleName = bundleName.substring(to: range.lowerBound)
+        }
+        let cookieFolderPath = _CFXDGCreateDataHomePath()._swiftObject + "/" + bundleName
+        cookieFilePath = filePath(path: cookieFolderPath, fileName: "/.cookies." + cookieStorageName, bundleName: bundleName)
         loadPersistedCookies()
     }
 
@@ -72,12 +77,13 @@
         }
     }
 
-    private func filePath(path: String, fileName: String) -> String {
+    private func filePath(path: String, fileName: String, bundleName: String) -> String {
         if directory(with: path) {
             return path + fileName
         }
-        //if we were unable to create the desired directory, create the cookie file in the `pwd`
-        return FileManager.default.currentDirectoryPath + fileName
+        //if we were unable to create the desired directory, create the cookie file
+        //in a subFolder (named after the bundle) of the `pwd`
+        return FileManager.default.currentDirectoryPath + "/" + bundleName + fileName
     }
 
     open var cookies: [HTTPCookie]? {
diff --git a/TestFoundation/TestNSHTTPCookieStorage.swift b/TestFoundation/TestNSHTTPCookieStorage.swift
index 133cbae..d31f766 100644
--- a/TestFoundation/TestNSHTTPCookieStorage.swift
+++ b/TestFoundation/TestNSHTTPCookieStorage.swift
@@ -227,19 +227,22 @@
         storage.setCookie(testCookie)
         XCTAssertEqual(storage.cookies!.count, 3)
         var destPath: String
+        let bundlePath = Bundle.main.bundlePath
+        var bundleName = "/" + bundlePath.components(separatedBy: "/").last!
+        if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) {
+            bundleName = bundleName.substring(to: range.lowerBound)
+        }
         if let xdg_data_home = getenv("XDG_DATA_HOME") {
-            destPath = String(utf8String: xdg_data_home)! + "/.cookies.shared"
+            destPath = String(utf8String: xdg_data_home)! + bundleName + "/.cookies.shared"
         } else {
-            destPath = NSHomeDirectory() + "/.local/share/.cookies.shared"
+            destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared"
         }
         let fm = FileManager.default
         var isDir = false
         let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
         XCTAssertTrue(exists)
         //Test by setting the environmental variable
-        let bundle = Bundle.main
-        let bundlePath = bundle.bundlePath
-        var pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
+        let pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
         let task = Process()
         task.launchPath = bundlePath.substring(to: pathIndex!) + "/xdgTestHelper/xdgTestHelper"
         var environment = ProcessInfo.processInfo.environment
diff --git a/TestFoundation/XDGTestHelper.swift b/TestFoundation/XDGTestHelper.swift
index 422d725..167b63c 100644
--- a/TestFoundation/XDGTestHelper.swift
+++ b/TestFoundation/XDGTestHelper.swift
@@ -11,7 +11,7 @@
 let xdg_data_home = String(utf8String: rawValue!)
 storage.setCookie(simpleCookie)
 let fm = FileManager.default
-let destPath = xdg_data_home! + "/.cookies.shared"
+let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
 var isDir = false
 let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir) 
 if (!exists) {