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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import Dispatch
class TestHTTPCookieStorage: XCTestCase {
enum StorageType {
case shared
case groupContainer(String)
}
override func setUp() {
// Delete any cookies in the storage
cookieStorage(for: .shared).removeCookies(since: Date(timeIntervalSince1970: 0))
cookieStorage(for: .groupContainer("test")).removeCookies(since: Date(timeIntervalSince1970: 0))
}
func test_sharedCookieStorageAccessedFromMultipleThreads() {
let q = DispatchQueue.global()
let syncQ = DispatchQueue(label: "TestHTTPCookieStorage.syncQ")
// Protected by `g` dispatchGroup
nonisolated(unsafe) var allCookieStorages: [HTTPCookieStorage] = []
let g = DispatchGroup()
for _ in 0..<64 {
g.enter()
q.async {
let mySharedCookieStore = HTTPCookieStorage.shared
syncQ.async {
allCookieStorages.append(mySharedCookieStore)
g.leave()
}
}
}
g.wait()
let cookieStorages = syncQ.sync { allCookieStorages }
let mySharedCookieStore = HTTPCookieStorage.shared
XCTAssertTrue(cookieStorages.reduce(true, { $0 && $1 === mySharedCookieStore }), "\(cookieStorages)")
}
func test_BasicStorageAndRetrieval() {
basicStorageAndRetrieval(with: .shared)
basicStorageAndRetrieval(with: .groupContainer("test"))
}
func test_deleteCookie() {
deleteCookie(with: .shared)
deleteCookie(with: .groupContainer("test"))
}
func test_removeCookies() {
removeCookies(with: .shared)
removeCookies(with: .groupContainer("test"))
}
func test_cookiesForURL() {
setCookiesForURL(with: .shared)
checkCookiesForURL(with: .shared)
setCookiesForURL(with: .groupContainer("test"))
checkCookiesForURL(with: .groupContainer("test"))
}
func test_cookiesForURLWithMainDocumentURL() {
setCookiesForURLWithMainDocumentURL(with: .shared)
setCookiesForURLWithMainDocumentURL(with: .groupContainer("test"))
}
func test_descriptionCookie() {
checkCookieDescription(for: .shared)
checkCookieDescription(for: .groupContainer("test"))
}
func test_cookieDomainMatching() {
checkCookieDomainMatching(for: .shared)
checkCookieDomainMatching(for: .groupContainer("test"))
}
func cookieStorage(for type: StorageType) -> HTTPCookieStorage {
switch type {
case .shared:
return HTTPCookieStorage.shared
case .groupContainer(let identifier):
return HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: identifier)
}
}
func basicStorageAndRetrieval(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test value @#$%^$&*99",
.path: "/",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: 1475767775) //expired cookie
])!
storage.setCookie(simpleCookie)
XCTAssertEqual(storage.cookies!.count, 0)
let simpleCookie0 = HTTPCookie(properties: [ //no expiry date
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "swift.org",
])!
storage.setCookie(simpleCookie0)
XCTAssertEqual(storage.cookies!.count, 1)
let simpleCookie1 = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "swift.org",
])!
storage.setCookie(simpleCookie1)
XCTAssertEqual(storage.cookies!.count, 1) //test for replacement
let simpleCookie2 = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "example.com",
])!
storage.setCookie(simpleCookie2)
XCTAssertEqual(storage.cookies!.count, 2)
}
func deleteCookie(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let simpleCookie2 = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "example.com",
])!
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test value @#$%^$&*99",
.path: "/",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 1000)
])!
storage.setCookie(simpleCookie)
storage.setCookie(simpleCookie2)
XCTAssertEqual(storage.cookies!.count, 2)
storage.deleteCookie(simpleCookie)
XCTAssertEqual(storage.cookies!.count, 1)
storage.deleteCookie(simpleCookie2)
XCTAssertEqual(storage.cookies!.count, 0)
}
func removeCookies(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let past = Date(timeIntervalSinceReferenceDate: Date().timeIntervalSinceReferenceDate - 120)
let future = Date(timeIntervalSinceReferenceDate: Date().timeIntervalSinceReferenceDate + 120)
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test value @#$%^$&*99",
.path: "/",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 1000)
])!
storage.setCookie(simpleCookie)
XCTAssertEqual(storage.cookies!.count, 1)
storage.removeCookies(since: future)
XCTAssertEqual(storage.cookies!.count, 1)
storage.removeCookies(since: past)
XCTAssertEqual(storage.cookies!.count, 0)
}
func setCookiesForURL(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let url = URL(string: "https://swift.org")
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "example.com",
])!
let simpleCookie1 = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test value @#$%^$&*99",
.path: "/",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 1000)
])!
storage.setCookies([simpleCookie, simpleCookie1], for: url, mainDocumentURL: nil)
XCTAssertEqual(storage.cookies!.count, 1)
}
func checkCookiesForURL(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let url = URL(string: "https://swift.org")
XCTAssertEqual(storage.cookies(for: url!)!.count, 1)
}
func setCookiesForURLWithMainDocumentURL(with storageType: StorageType) {
let storage = cookieStorage(for: storageType)
storage.cookieAcceptPolicy = .onlyFromMainDocumentDomain
let url = URL(string: "https://swift.org/downloads")
let mainUrl = URL(string: "http://ci.swift.org")
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie2",
.value: "Test@#$%^$&*99khnia",
.path: "/",
.domain: "swift.org",
])!
storage.setCookies([simpleCookie], for: url, mainDocumentURL: mainUrl)
XCTAssertEqual(storage.cookies(for: url!)!.count, 1)
let url1 = URL(string: "https://dt.swift.org/downloads")
let simpleCookie1 = HTTPCookie(properties: [
.name: "TestCookie3",
.value: "Test@#$%^$&*999189",
.path: "/",
.domain: "swift.org",
])!
storage.setCookies([simpleCookie1], for: url1, mainDocumentURL: mainUrl)
XCTAssertEqual(storage.cookies(for: url1!)!.count, 0)
}
func checkCookieDescription(for storageType: StorageType) {
let storage = cookieStorage(for: storageType)
guard let cookies = storage.cookies else {
XCTFail("No cookies")
return
}
XCTAssertEqual(storage.description, "<NSHTTPCookieStorage cookies count:\(cookies.count)>")
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test value @#$%^$&*99",
.path: "/",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 1000)
])!
storage.setCookie(simpleCookie)
guard let cookies0 = storage.cookies else {
XCTFail("No cookies")
return
}
XCTAssertEqual(storage.description, "<NSHTTPCookieStorage cookies count:\(cookies0.count)>")
storage.deleteCookie(simpleCookie)
guard let cookies1 = storage.cookies else {
XCTFail("No cookies")
return
}
XCTAssertEqual(storage.description, "<NSHTTPCookieStorage cookies count:\(cookies1.count)>")
}
func checkCookieDomainMatching(for storageType: StorageType) {
let storage = cookieStorage(for: storageType)
let simpleCookie1 = HTTPCookie(properties: [ // swift.org domain only
.name: "TestCookie1",
.value: "TestValue1",
.path: "/",
.domain: "swift.org",
])!
storage.setCookie(simpleCookie1)
let simpleCookie2 = HTTPCookie(properties: [ // *.swift.org
.name: "TestCookie2",
.value: "TestValue2",
.path: "/",
.domain: ".SWIFT.org",
])!
storage.setCookie(simpleCookie2)
let simpleCookie3 = HTTPCookie(properties: [ // bugs.swift.org
.name: "TestCookie3",
.value: "TestValue3",
.path: "/",
.domain: "bugs.swift.org",
])!
storage.setCookie(simpleCookie3)
XCTAssertEqual(storage.cookies!.count, 3)
let swiftOrgUrl = URL(string: "https://swift.ORG")!
let ciSwiftOrgUrl = URL(string: "https://CI.swift.ORG")!
let bugsSwiftOrgUrl = URL(string: "https://BUGS.swift.org")!
let exampleComUrl = URL(string: "http://www.example.com")!
let superSwiftOrgUrl = URL(string: "https://superswift.org")!
XCTAssertEqual(Set(storage.cookies(for: swiftOrgUrl)!), Set([simpleCookie1, simpleCookie2]))
XCTAssertEqual(storage.cookies(for: ciSwiftOrgUrl)!, [simpleCookie2])
XCTAssertEqual(Set(storage.cookies(for: bugsSwiftOrgUrl)!), Set([simpleCookie2, simpleCookie3]))
XCTAssertEqual(storage.cookies(for: exampleComUrl)!, [])
XCTAssertEqual(storage.cookies(for: superSwiftOrgUrl)!, [])
}
func test_cookieInXDGSpecPath() throws {
#if !os(Android) && !DARWIN_COMPATIBILITY_TESTS && !os(Windows)// No XDG on native Foundation
//Test without setting the environment variable
let testCookie = HTTPCookie(properties: [
.name: "TestCookie0",
.value: "Test @#$%^$&*99mam",
.path: "/",
.domain: "sample.com",
])!
let storage = HTTPCookieStorage.shared
storage.setCookie(testCookie)
XCTAssertEqual(storage.cookies!.count, 1)
let destPath: String
let bundleName = "/" + Bundle.main.bundlePath.split(separator: "/").last!.prefix(while: { $0 != "." })
if let xdg_data_home = getenv("XDG_DATA_HOME") {
destPath = String(utf8String: xdg_data_home)! + bundleName + "/.cookies.shared"
} else {
destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared"
}
let fm = FileManager.default
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
XCTAssertTrue(exists)
// Test by setting the environmental variable
let task = Process()
task.executableURL = xdgTestHelperURL()
task.arguments = ["--xdgcheck"]
var environment = ProcessInfo.processInfo.environment
let testPath = NSHomeDirectory() + "/TestXDG"
environment["XDG_DATA_HOME"] = testPath
task.environment = environment
// Launch the task
try task.run()
task.waitUntilExit()
let status = task.terminationStatus
XCTAssertEqual(status, 0)
let terminationReason = task.terminationReason
XCTAssertEqual(terminationReason, Process.TerminationReason.exit)
try? fm.removeItem(atPath: testPath)
#endif
}
func test_sorting() {
let storage = HTTPCookieStorage.shared
let url = URL(string: "https://swift.org")
let cookie = HTTPCookie(properties: [
.name: "A",
.value: "3",
.path: "/1",
.domain: "swift.org",
])!
let cookie2 = HTTPCookie(properties: [
.name: "B",
.value: "2",
.path: "/2",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 1000)
])!
let cookie3 = HTTPCookie(properties: [
.name: "C",
.value: "1",
.path: "/2",
.domain: "swift.org",
.expires: Date(timeIntervalSince1970: Date().timeIntervalSince1970 + 2000)
])!
storage.setCookies([cookie, cookie2, cookie3], for: url, mainDocumentURL: url)
let result = storage.sortedCookies(using: [
NSSortDescriptor(keyPath: \HTTPCookie.path, ascending: true),
NSSortDescriptor(keyPath: \HTTPCookie.name, ascending: false),
])
XCTAssertEqual(result, [cookie, cookie3, cookie2])
}
}
|