diff --git a/BitTorrent.xcodeproj/project.pbxproj b/BitTorrent.xcodeproj/project.pbxproj index e2e52da..b3f192c 100644 --- a/BitTorrent.xcodeproj/project.pbxproj +++ b/BitTorrent.xcodeproj/project.pbxproj @@ -79,6 +79,7 @@ B5AF7AAA1F252A79003FD66F /* MultFileHandleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5AF7AA81F252A70003FD66F /* MultFileHandleTests.swift */; }; B5AF7AAD1F253055003FD66F /* FileHandleFake.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5AF7AAC1F253055003FD66F /* FileHandleFake.swift */; }; B5BD7FD61F03032400621BC2 /* TorrentHTTPTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BD7FD51F03032400621BC2 /* TorrentHTTPTrackerTests.swift */; }; + B5BF03EC1FADC54900DF13D5 /* IndexCorrectedDataSlice.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BF03EB1FADC54900DF13D5 /* IndexCorrectedDataSlice.swift */; }; B5BFD9CC1F3FAF9A00CE0186 /* TorrentTrackerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BFD9CB1F3FAF9A00CE0186 /* TorrentTrackerManager.swift */; }; B5BFD9CE1F3FAFA500CE0186 /* TorrentTrackerManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BFD9CD1F3FAFA500CE0186 /* TorrentTrackerManagerTests.swift */; }; B5BFD9D01F3FB13E00CE0186 /* TorrentTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BFD9CF1F3FB13E00CE0186 /* TorrentTracker.swift */; }; @@ -245,6 +246,7 @@ B5AF7AA81F252A70003FD66F /* MultFileHandleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultFileHandleTests.swift; sourceTree = ""; }; B5AF7AAC1F253055003FD66F /* FileHandleFake.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileHandleFake.swift; sourceTree = ""; }; B5BD7FD51F03032400621BC2 /* TorrentHTTPTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TorrentHTTPTrackerTests.swift; sourceTree = ""; }; + B5BF03EB1FADC54900DF13D5 /* IndexCorrectedDataSlice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexCorrectedDataSlice.swift; sourceTree = ""; }; B5BFD9CB1F3FAF9A00CE0186 /* TorrentTrackerManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TorrentTrackerManager.swift; sourceTree = ""; }; B5BFD9CD1F3FAFA500CE0186 /* TorrentTrackerManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TorrentTrackerManagerTests.swift; sourceTree = ""; }; B5BFD9CF1F3FB13E00CE0186 /* TorrentTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TorrentTracker.swift; sourceTree = ""; }; @@ -435,6 +437,7 @@ B514DD8E1F40C53C00C932F8 /* NetworkSpeedTrackerTests.swift */, B514DD9B1F40DD1B00C932F8 /* URL.swift */, B501A5571F557E9E00B87911 /* RandomEnumeration.swift */, + B5BF03EB1FADC54900DF13D5 /* IndexCorrectedDataSlice.swift */, ); path = Utilities; sourceTree = ""; @@ -1019,6 +1022,7 @@ B5C06F131F12CDD8005730B3 /* TorrentPeer.swift in Sources */, B5F81E4B1F04399800B25C70 /* TorrentTrackerResponse.swift in Sources */, B501A5661F55B24B00B87911 /* TorrentServer.swift in Sources */, + B5BF03EC1FADC54900DF13D5 /* IndexCorrectedDataSlice.swift in Sources */, B5AF7AA61F252A66003FD66F /* MultiFileHandle.swift in Sources */, B54D0C7B1CA69FD8004343BD /* Data+sha1.swift in Sources */, B5F27C5D1F3F7BDD0040589C /* TorrentPeerManager.swift in Sources */, diff --git a/BitTorrent/File Manager/FileHandleFake.swift b/BitTorrent/File Manager/FileHandleFake.swift index 045fb16..06ecf7e 100644 --- a/BitTorrent/File Manager/FileHandleFake.swift +++ b/BitTorrent/File Manager/FileHandleFake.swift @@ -24,7 +24,7 @@ class FileHandleFake: FileHandleProtocol { func readData(ofLength length: Int) -> Data { let beginOffset = currentOffset currentOffset += length - return data[beginOffset ..< currentOffset] + return data.correctingIndicies[beginOffset ..< currentOffset] } func write(_ data: Data) { diff --git a/BitTorrent/File Manager/MultiFileHandle.swift b/BitTorrent/File Manager/MultiFileHandle.swift index f64edce..55cfd35 100644 --- a/BitTorrent/File Manager/MultiFileHandle.swift +++ b/BitTorrent/File Manager/MultiFileHandle.swift @@ -87,8 +87,8 @@ class MultiFileHandle: FileHandleProtocol { private func writeDataToEndOfCurrentFile(_ data: Data) -> Data? { guard remainingInCurrentFile >= data.count else { - let dataToWrite = data[0 ..< Int(remainingInCurrentFile)] - let remaining = data[Int(remainingInCurrentFile) ..< data.count] + let dataToWrite = data.correctingIndicies[0 ..< Int(remainingInCurrentFile)] + let remaining = data.correctingIndicies[Int(remainingInCurrentFile) ..< data.count] currentFile.handle.write(dataToWrite) incrementCurrentFile() return remaining diff --git a/BitTorrent/File Manager/TorrentFileManagerTests.swift b/BitTorrent/File Manager/TorrentFileManagerTests.swift index 6f0967e..d542d6f 100644 --- a/BitTorrent/File Manager/TorrentFileManagerTests.swift +++ b/BitTorrent/File Manager/TorrentFileManagerTests.swift @@ -42,7 +42,7 @@ class TorrentFileManagerTests: XCTestCase { sut.setPiece(at: 1, data: piece1) // Then - XCTAssertEqualData(fileHandle.data[pieceLength..= expectedLength { - let message = buffer[0..) -> Data { + let actualLowerBound = originalData.startIndex + range.lowerBound + let actualUpperBound = originalData.startIndex + range.upperBound + return originalData[actualLowerBound ..< actualUpperBound] + } + + subscript(_ index: Int) -> UInt8 { + let correctedIndex = originalData.startIndex + index + return originalData[correctedIndex] + } + + func index(after i: Int) -> Int { + return i + 1 + } +} + +extension Data { + var correctingIndicies: IndexCorrectedDataSlice { + return IndexCorrectedDataSlice(originalData: self) + } +} diff --git a/Submodules/BEncodeSwift b/Submodules/BEncodeSwift index 5818b8f..20a7a09 160000 --- a/Submodules/BEncodeSwift +++ b/Submodules/BEncodeSwift @@ -1 +1 @@ -Subproject commit 5818b8f70ff4753a71469c6d21d67854a6a737bb +Subproject commit 20a7a09b2f64d785240f961aaf91a3ab956c06f2