Commit 2ab31c2d authored by Drew's avatar Drew

Linux port

parent fb2d046a
Pipeline #778 skipped
Subproject commit d748db6908297a7af63f52c09185c085eed88fc3
Subproject commit 0f13c69416a261590ac379e5435d6bea07cb54f8
......@@ -13,13 +13,23 @@ extension String {
public func cStringUsingEncoding(encoding: NSStringEncoding) -> [CChar]? {
let strNS = self.bridge()
let bytes = strNS.cStringUsingEncoding(NSUTF8StringEncoding)
let bfrPointer = UnsafeBufferPointer(start: bytes, count: strNS.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let bfrPointer = UnsafeBufferPointer(start: bytes, count: strNS.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)+1)
var arr = [CChar](bfrPointer)
strNS.description //don't optimize out
return arr
}
}
#endif
//SR-138
extension NSString {
var toString: String {
#if os(Linux)
return self.bridge()
#else
return self as String
#endif
}
}
//who defines NSTemporaryDirectory?
#if os(Linux)
......@@ -27,3 +37,44 @@ extension String {
return "/tmp/"
}
#endif
#if os(Linux)
import Glibc //⛏567
#endif
//setAttributesOfItemAtPath isn't implemented
extension NSFileManager {
func setSWIFTBUGAttributes(attributes: [String : AnyObject], ofItemAtPath path: String) throws {
for attribute in attributes.keys {
switch attribute {
case NSFilePosixPermissions:
guard let number = attributes[attribute] as? NSNumber else {
fatalError("Can't set file permissions to \(attributes[attribute])")
}
#if os(OSX)
let modeT = number.unsignedShortValue
#elseif os(Linux)
let modeT = number.unsignedIntValue
#endif
if chmod(path, modeT) != 0 {
fatalError("errno \(errno)")
}
default:
fatalError("Attribute type not implemented: \(attribute)")
}
}
}
}
//missing constructor
#if os(Linux)
extension NSString {
convenience init(format: String, _ args: CVarArgType...) {
let str = withVaList(args) { (listPtr) -> NSString in
return NSString(format: format, arguments: listPtr)
}
self.init(string: str.bridge())
}
}
#endif
\ No newline at end of file
......@@ -30,7 +30,7 @@ extension Key {
//create a locked down file
//so that we only write if everything's good
try NSData().writeToFile(file, options: NSDataWritingOptions())
try NSFileManager.defaultManager().setAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: file)
try NSFileManager.defaultManager().setSWIFTBUGAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: file)
//with that out of the way
try self.unlock()
......
......@@ -64,7 +64,7 @@ public final class Key {
}
var str = ""
for char in hash {
str += String(format: "%02X", arguments: [char]) as String
str += NSString(format: "%02X", char).toString
}
return str
}
......@@ -103,7 +103,7 @@ extension Key {
public convenience init(password: String, salt: String, keySize: KeySizes) throws {
sodium_init_wrap()
let saltBytes = salt.cStringUsingEncoding(NSUTF8StringEncoding)!
precondition(saltBytes.count == Int(crypto_pwhash_scryptsalsa208sha256_SALTBYTES))
precondition(saltBytes.count == Int(crypto_pwhash_scryptsalsa208sha256_SALTBYTES), "\(saltBytes.count) is different than \(crypto_pwhash_scryptsalsa208sha256_SALTBYTES)")
var bytes = password.cStringUsingEncoding(NSUTF8StringEncoding)!
......
......@@ -31,8 +31,8 @@ class CryptoBoxTests :XCTestCase {
#endif
//fix the permsisions on this key so we don't freak out the security goalie
try! NSFileManager.defaultManager().setAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: alicePath)
try! NSFileManager.defaultManager().setAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: bobPath)
try! NSFileManager.defaultManager().setSWIFTBUGAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: alicePath)
try! NSFileManager.defaultManager().setSWIFTBUGAttributes([NSFilePosixPermissions: NSNumber(short: 0o0600)], ofItemAtPath: bobPath)
let alice = try! PublicKey(readFromFile: alicePath)
let bob = try! PublicKey(readFromFile: bobPath)
......
......@@ -41,7 +41,7 @@ class CryptoSecretBoxTests : XCTestCase {
}
}
#if ATBUILD
extension CryptoSecretBoxTests : XCTestCaseProvider {
var allTests : [(String, () -> Void)] {
return [
......@@ -51,3 +51,4 @@ extension CryptoSecretBoxTests : XCTestCaseProvider {
]
}
}
#endif
......@@ -24,10 +24,12 @@ class CryptoStreamTestsTests : XCTestCase {
XCTAssert(plaintext == [1,2,3])
}
}
#if ATBUILD
extension CryptoStreamTestsTests : XCTestCaseProvider {
var allTests : [(String, () -> Void)] {
return [
("testChaCha20", testChaCha20)
]
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -27,11 +27,12 @@ class GenericHashTests : XCTestCase {
XCTAssert(hash1 != ([0,1,2,3] as [UInt8]).genericHash)
}
}
#if ATBUILD
extension GenericHashTests : XCTestCaseProvider {
var allTests : [(String, () -> Void)] {
return [
("testGenericHash", testGenericHash)
]
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -35,6 +35,7 @@ class KeyFileTests : XCTestCase {
}
}
#if ATBUILD
extension KeyFileTests : XCTestCaseProvider {
var allTests : [(String, () -> Void)] {
return [
......@@ -42,4 +43,5 @@ extension KeyFileTests : XCTestCaseProvider {
("testPublicKeyLoadSave",testPublicKeyLoadSave)
]
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -17,7 +17,7 @@ import XCTest
class KeyTests : XCTestCase {
func testKey() {
let k = try! Key(password: "My password", salt: "My salt is 32 characters sjej", keySize: KeySizes.crypto_box_seed)
XCTAssert(k.hash == "C2D877E295C0070384F1486F18CE136C72B050EFAB71D2830260F2A062B9E2AC")
XCTAssert(k.hash == "C2D877E295C0070384F1486F18CE136C72B050EFAB71D2830260F2A062B9E2AC", "k.hash isn't the right value: \(k.hash)")
}
func testZeroImport() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment