p2p-swift / Tests / IrdP2PCryptoTests / BytesTests.swift
116 words
 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
import Foundation
import Testing
@testable import IrdP2PCrypto

@Suite struct BytesTests {
    @Test func base64VectorsRoundTrip() throws {
        for v in try vector("room-crypto")["b64"]!.arr {
            let data = v["bytes"]!.bytes
            #expect(Bytes.base64(data) == v["b64"]!.str)
            #expect(Bytes.base64URL(data) == v["b64url"]!.str)
            #expect(Bytes.fromBase64(v["b64"]!.str) == data)
            #expect(Bytes.fromBase64URL(v["b64url"]!.str) == data)
        }
    }

    @Test func base64URLRejectsGarbage() {
        #expect(Bytes.fromBase64URL("A") == nil)
        #expect(Bytes.fromBase64URL("!!!!") == nil)
        #expect(Bytes.fromBase64URL("AA==") == Data([0]))   // padding tolerated on input
    }

    @Test func hexRoundTrip() {
        let d = Data([0x00, 0x0f, 0xf0, 0xff])
        #expect(Bytes.hex(d) == "000ff0ff")
        #expect(Bytes.fromHex("000ff0ff") == d)
        #expect(Bytes.fromHex("000FF0FF") == d)
        #expect(Bytes.fromHex("abc") == nil)
        #expect(Bytes.fromHex("zz") == nil)
    }

    @Test func randomIsFreshAndSized() {
        #expect(Bytes.random(16).count == 16)
        #expect(Bytes.random(16) != Bytes.random(16))
    }

    @Test func constantTimeEquality() {
        #expect(Bytes.constantTimeEquals(Data([1, 2]), Data([1, 2])))
        #expect(!Bytes.constantTimeEquals(Data([1, 2]), Data([1, 3])))
        #expect(!Bytes.constantTimeEquals(Data([1, 2]), Data([1, 2, 3])))
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/p2p-swift.git