<commit_before>//
//  SFTPFileTests.swift
//  SSHKitCore
//
//  Created by vicalloy on 6/17/16.
//
//

import XCTest

class SFTPFileTests: SFTPTests {
    
    private var readFileExpectation: XCTestExpectation?

    let lsFolderPathForTest = "./ls"
    let filePathForWriteTest = "./test_write.txt"
    let filePathForReadTest = "./test_read.txt"

    // MARK: - setUp
    override func setUp() {
        super.setUp()
        
        mkdir(lsFolderPathForTest)
        createEmptyFile(lsFolderPathForTest.stringByAppendingString("/1"))
        createEmptyFile(lsFolderPathForTest.stringByAppendingString("/2"))
        
        unlink(filePathForWriteTest)
        unlink(filePathForReadTest)
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        unlink(lsFolderPathForTest.stringByAppendingString("/1"))
        unlink(lsFolderPathForTest.stringByAppendingString("/2"))
        rmdir(lsFolderPathForTest)
        
        unlink(filePathForWriteTest)
        unlink(filePathForReadTest)
        
        super.tearDown()
    }
    
    // MARK: - helper function
    func createFile(filename: String, content: String) {
        
        do {
            let file = try SSHKitSFTPFile.openFileForWrite(channel, path: filename, shouldResume: false, mode: 0o755)
            let length = content.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
            let data = content.cStringUsingEncoding(NSUTF8StringEncoding)
            let error: NSErrorPointer = nil
            let writeLength = file.write(data!, size: length, errorPtr: error)
            XCTAssertEqual(length, writeLength)
            file.close()
        } catch let error as NSError {
            XCTFail(error.description)
        }
    }

    // MARK: - test
    func testListDirectory() {
        do {
            var dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            var files = dir.listDirectory(nil)
            XCTAssertEqual(files.count, 4)
            dir.close()
            
            dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            files = dir.listDirectory({ (filename) -> SSHKitSFTPListDirFilterCode in
                if filename == "." {
                    return .Ignore;
                }
                return .Add;
            })
            XCTAssertEqual(files.count, 3)
            dir.close()
            
            dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            files = dir.listDirectory({ (filename) -> SSHKitSFTPListDirFilterCode in
                return .Cancel
            })
            XCTAssertEqual(files.count, 0)
            dir.close()
            
        } catch let error as NSError {
            XCTFail(error.description)
        }
    }
    
    func testWrite() {
        let filename = filePathForWriteTest
        
        var i = 0
        var content = "0123456789abcd"
        while i < 10 {
            content = content.stringByAppendingString(content)
            i += 1
        }
        
        createFile(filename, content: content)
    }
    
    func testRead() {
        let filename = filePathForReadTest
        
        var i = 0
        var content = "0123456789abcd"
        while i < 10 {
            content = content.stringByAppendingString(content)
            i += 1<commit_msg>add test for SFTP listDirectory (ls a link)<commit_after>//
//  SFTPFileTests.swift
//  SSHKitCore
//
//  Created by vicalloy on 6/17/16.
//
//

import XCTest

class SFTPFileTests: SFTPTests {
    
    private var readFileExpectation: XCTestExpectation?

    let lsFolderPathForTest = "./ls"
    let lnFolderPathForTest = "./ln"
    let lnFilePathForTest = "./lnFile"
    let filePathForWriteTest = "./test_write.txt"
    let filePathForReadTest = "./test_read.txt"

    // MARK: - setUp
    override func setUp() {
        super.setUp()
        
        mkdir(lsFolderPathForTest)
        createEmptyFile(lsFolderPathForTest.stringByAppendingString("/1"))
        createEmptyFile(lsFolderPathForTest.stringByAppendingString("/2"))
        channel!.symlink(lsFolderPathForTest, destination: lnFolderPathForTest)
        channel!.symlink(lsFolderPathForTest.stringByAppendingString("/1"), destination: lnFilePathForTest)
        
        unlink(filePathForWriteTest)
        unlink(filePathForReadTest)
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        unlink(lsFolderPathForTest.stringByAppendingString("/1"))
        unlink(lsFolderPathForTest.stringByAppendingString("/2"))
        unlink(lnFolderPathForTest)
        unlink(lnFilePathForTest)
        rmdir(lsFolderPathForTest)
        
        unlink(filePathForWriteTest)
        unlink(filePathForReadTest)
        
        super.tearDown()
    }
    
    // MARK: - helper function
    func createFile(filename: String, content: String) {
        
        do {
            let file = try SSHKitSFTPFile.openFileForWrite(channel, path: filename, shouldResume: false, mode: 0o755)
            let length = content.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
            let data = content.cStringUsingEncoding(NSUTF8StringEncoding)
            let error: NSErrorPointer = nil
            let writeLength = file.write(data!, size: length, errorPtr: error)
            XCTAssertEqual(length, writeLength)
            file.close()
        } catch let error as NSError {
            XCTFail(error.description)
        }
    }

    // MARK: - test
    func testListDirectory() {
        do {
            var dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            var files = dir.listDirectory(nil)
            XCTAssertEqual(files.count, 4)
            dir.close()
            
            dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            files = dir.listDirectory({ (filename) -> SSHKitSFTPListDirFilterCode in
                if filename == "." {
                    return .Ignore;
                }
                return .Add;
            })
            XCTAssertEqual(files.count, 3)
            dir.close()
            
            dir = try SSHKitSFTPFile.openDirectory(channel, path: lsFolderPathForTest)
            files = dir.listDirectory({ (filename) -> SSHKitSFTPListDirFilterCode in
                return .Cancel
            })
            XCTAssertEqual(files.count, 0)
            dir.close()
            
            // test symlink
            dir = try SSHKitSFTPFile.openDirectory(channel, path: lnFolderPathForTest)
            files = dir.listDirectory(nil)
            XCTAssertEqual(files.count, 4)
            dir.close()
            
        } catch let error as NSError {
            XCTFail(error.description)
        }
        do {
            // try to ls symlink file
            let dir = try SSHKitSFTPFile.openDirectory(channel, path: lnFilePathForTest)
            let files = dir.listDirectory(nil)
            XCTAssertEqual(files.count, 4)
            dir.close()
        } catch let error as NSError {
            if error.code != SSHKitSFTPErrorCode.NoSuchFile.rawValue {
                XCTFail(error.description)
            }
        }
    }
    
    func testWrite() {
        let filename = filePathForWriteTest
        
        var i = 0
        var content = "0123456789abcd"
        while i < 10 {
            content = content.stringByAppendingString(content)
            i += 1
        }
        
        createFile(filename, content: content)
    }
    
    func testRead() {
        let filename = filePathForReadTest
        
        var i = 0
        var content = "0123456789abcd"
        while i < 10 {
            content = content.stringByAppendingString(content)
            i += 1
{'fraction_non_alphanumeric': 0.08376434969092973, 'fraction_numerical': 0.011984357259997478, 'mean_word_length': 1.8839578028373953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5089704', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2202, 'n_words': 469}