<commit_before><commit_msg>initial test set<commit_after>//
//  JWTTests.swift
//  JWTTests
//
//  Created by <NAME> on 3/31/15.
//  Copyright (c) 2015 RoundZero bv. All rights reserved.
//

import XCTest
import JWT
import Sodium

class JWTTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func test_HS256_JWT_from_dicts() {
        // This is an example of a functional test case.
        let expected_jwt = "<KEY>"
        let jwt = JWT(header: ["alg":"HS256"], body: ["hello":"world"])
        let s = jwt.dumps("secret")
        XCTAssert(s != nil, "JWT.dumps() failed")
        XCTAssert(s! == expected_jwt, "JWT.dumps() unexpected jwt")
    }

    func test_NaCl_JWT_from_string() {
        let jwt_ed = "<KEY>"
        let sk = "YHWUUc0P6SY46WaDdnssE8NpFsQQ<KEY>"
        let pk = "<KEY>"
        let jwt = JWTNaCl()
        XCTAssert(jwt.loads(jwt_ed, b64key: pk, verify: true), "loading a NaCl signed JWT and specified public (verification) key")
        XCTAssert(jwt.loads(jwt_ed, key: nil, verify: true), "loading a NaCl signed JWT and with implicit (verification) key 'kid'")
        let kid = jwt.header["kid"] as? String
        XCTAssert(kid != nil && kid! == pk, "mismatch PK / kid in loaded JWT")
    }

    func test_HS256_JWT_from_string() {
        let jwt_hs256 = "<KEY>"
        let jwt = JWT()
        XCTAssert(jwt.loads(jwt_hs256, key: "secret", verify: true), "loading a HS256 signed JWT and specified (verification) hash")
        XCTAssert(jwt.body["phone_number"] as? String == "+306947898605", "wrong 'phone' scope in loaded JWT")
        var jwtn = JWTNaCl()
        XCTAssert(jwt.loads(jwt_hs256, key: "secret", verify: true), "loading a HS256 signed JWT in a NaCl subclass should work too")
    }

    func test_random_string_as_JWT() {
        var error: NSError?
        var jwt = JWT()
        XCTAssert(jwt.loads("randomstring", key: nil, verify: false, error: &error) == false, "random string should not load")
        XCTAssert(jwt.body.count == 0, "loading garbage should leave body empty")
    }

    func test_alg_none_with_sig() {
        let jwt_none_sig = "<KEY>"
        var jwt = JWT()
        XCTAssert(jwt.loads(jwt_none_sig, key: nil, verify: true) == false, "alg=none with a signature is not valid")
        jwt = JWT(blacklist: ["none"])
        XCTAssert(jwt.loads(jwt_none_sig, key: nil, verify: false) == false, "none is on blacklist")
    }

    func test_timestamps() {
        let date = NSDate()
        let now = UInt(date.timeIntervalSince1970)
        let jwt = JWT()
        var jwt_dated = JWT()
        var s = ""
        jwt_dated.body["exp"] = now-100
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == false, "exp in past \(s)")
        jwt_dated.body["exp"] = now+100 // and leave it there for next tests
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == true, "exp in future \(s)")
        jwt_dated.body["nbf"] = now+100
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == false, "nbf in future \(s)")
        jwt_dated.body["nbf"] = now-100 // and leave it there for next tests
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == true, "nbf in past \(s)")
        jwt_dated.body["iat"] = now+100
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == false, "iat in future \(s)")
        jwt_dated.body["iat"] = now-100 // and leave it there for next tests
        s = jwt_dated.dumps("")!
        XCTAssert(jwt.loads(s, key: nil, verify: true) == true, "iat in past \(s)")
    }

    func test_NaCl_JWT_load_dump_load() {
        let jwt_ed = "<KEY>"
        // generate keys
        let sodium = Sodium()!
        let seed = NSMutableData(length: sodium.sign.SeedBytes)!
        SecRandomCopyBytes(kSecRandomDefault, UInt(sodium.sign.SeedBytes), UnsafeMutablePointer<UInt8>(seed.mutableBytes))
        let kp = sodium.sign.keyPair(seed: seed)
        // This is an example of a functional test case.
        XCTAssert(kp != nil, "Key pair generation")
        var jwt = JWTNaCl()
        XCTAssert(jwt.loads(jwt_ed, key: kp!.publicKey, verify: true) == false, "NaCl JWT should not validate with wrong key")
        // but is stiil loaded
        let jwt_str = jwt.dumps(kp!.secretKey)! // valid Ed25519 signed token
        jwt.header = [:]
        XCTAssert(jwt.header["alg"] as? String == "none", "after reset of header, alg=none")
        jwt.body = [:]
        XCTAssert(jwt.loads(jwt_str, key: nil, verify: true) == false, "verify a generated JWT to kid when signed with fresh key")
        XCTAssert(jwt.loads(jwt_str, key: kp!.publicKey, verify: true), "verify a generated JWT with its public key")
    }

    func testPerformanceVerify() {
        // This is an example of a performance test case.
        let jwt_str = "<KEY>"
        var jwt = JWTNaCl()
        self.measureBlock() {
            let ok = jwt.loads(jwt_str, key: nil, verify: true)
        }
    }
    
}

{'fraction_non_alphanumeric': 0.13215613382899627, 'fraction_numerical': 0.015055762081784387, 'mean_word_length': 2.589726484322882, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, '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': '7028716', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1843, 'n_words': 528}