mirror of https://github.com/oxen-io/session-ios
Added more unit tests
Removed an unused endpoint Moved 'Dependencies' into the Utilities folder (also out from being nested within 'OpenGroupAPI' since it can be broader than that) Finished adding unit tests for the OpenGroupAPIpull/592/head
parent
b6a6f77d4e
commit
65f14cf0a1
@ -1,148 +0,0 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
import Sodium
|
||||
import SessionSnodeKit
|
||||
import SessionUtilitiesKit
|
||||
|
||||
// MARK: - Dependencies
|
||||
|
||||
extension OpenGroupAPI {
|
||||
public class Dependencies {
|
||||
private var _api: OnionRequestAPIType.Type?
|
||||
public var api: OnionRequestAPIType.Type {
|
||||
get { getValueSettingIfNull(&_api) { OnionRequestAPI.self } }
|
||||
set { _api = newValue }
|
||||
}
|
||||
|
||||
private var _storage: SessionMessagingKitStorageProtocol?
|
||||
public var storage: SessionMessagingKitStorageProtocol {
|
||||
get { getValueSettingIfNull(&_storage) { SNMessagingKitConfiguration.shared.storage } }
|
||||
set { _storage = newValue }
|
||||
}
|
||||
|
||||
private var _sodium: SodiumType?
|
||||
public var sodium: SodiumType {
|
||||
get { getValueSettingIfNull(&_sodium) { Sodium() } }
|
||||
set { _sodium = newValue }
|
||||
}
|
||||
|
||||
private var _aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType?
|
||||
public var aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType {
|
||||
get { getValueSettingIfNull(&_aeadXChaCha20Poly1305Ietf) { sodium.getAeadXChaCha20Poly1305Ietf() } }
|
||||
set { _aeadXChaCha20Poly1305Ietf = newValue }
|
||||
}
|
||||
|
||||
private var _sign: SignType?
|
||||
public var sign: SignType {
|
||||
get { getValueSettingIfNull(&_sign) { sodium.getSign() } }
|
||||
set { _sign = newValue }
|
||||
}
|
||||
|
||||
private var _genericHash: GenericHashType?
|
||||
public var genericHash: GenericHashType {
|
||||
get { getValueSettingIfNull(&_genericHash) { sodium.getGenericHash() } }
|
||||
set { _genericHash = newValue }
|
||||
}
|
||||
|
||||
private var _ed25519: Ed25519Type.Type?
|
||||
public var ed25519: Ed25519Type.Type {
|
||||
get { getValueSettingIfNull(&_ed25519) { Ed25519.self } }
|
||||
set { _ed25519 = newValue }
|
||||
}
|
||||
|
||||
private var _nonceGenerator16: NonceGenerator16ByteType?
|
||||
public var nonceGenerator16: NonceGenerator16ByteType {
|
||||
get { getValueSettingIfNull(&_nonceGenerator16) { NonceGenerator16Byte() } }
|
||||
set { _nonceGenerator16 = newValue }
|
||||
}
|
||||
|
||||
private var _nonceGenerator24: NonceGenerator24ByteType?
|
||||
public var nonceGenerator24: NonceGenerator24ByteType {
|
||||
get { getValueSettingIfNull(&_nonceGenerator24) { NonceGenerator24Byte() } }
|
||||
set { _nonceGenerator24 = newValue }
|
||||
}
|
||||
|
||||
private var _standardUserDefaults: UserDefaultsType?
|
||||
public var standardUserDefaults: UserDefaultsType {
|
||||
get { getValueSettingIfNull(&_standardUserDefaults) { UserDefaults.standard } }
|
||||
set { _standardUserDefaults = newValue }
|
||||
}
|
||||
|
||||
private var _date: Date?
|
||||
public var date: Date {
|
||||
get { getValueSettingIfNull(&_date) { Date() } }
|
||||
set { _date = newValue }
|
||||
}
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
public init(
|
||||
api: OnionRequestAPIType.Type? = nil,
|
||||
storage: SessionMessagingKitStorageProtocol? = nil,
|
||||
sodium: SodiumType? = nil,
|
||||
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
|
||||
sign: SignType? = nil,
|
||||
genericHash: GenericHashType? = nil,
|
||||
ed25519: Ed25519Type.Type? = nil,
|
||||
nonceGenerator16: NonceGenerator16ByteType? = nil,
|
||||
nonceGenerator24: NonceGenerator24ByteType? = nil,
|
||||
standardUserDefaults: UserDefaultsType? = nil,
|
||||
date: Date? = nil
|
||||
) {
|
||||
_api = api
|
||||
_storage = storage
|
||||
_sodium = sodium
|
||||
_aeadXChaCha20Poly1305Ietf = aeadXChaCha20Poly1305Ietf
|
||||
_sign = sign
|
||||
_genericHash = genericHash
|
||||
_ed25519 = ed25519
|
||||
_nonceGenerator16 = nonceGenerator16
|
||||
_nonceGenerator24 = nonceGenerator24
|
||||
_standardUserDefaults = standardUserDefaults
|
||||
_date = date
|
||||
}
|
||||
|
||||
// MARK: - Convenience
|
||||
|
||||
public func with(
|
||||
api: OnionRequestAPIType.Type? = nil,
|
||||
storage: SessionMessagingKitStorageProtocol? = nil,
|
||||
sodium: SodiumType? = nil,
|
||||
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
|
||||
sign: SignType? = nil,
|
||||
genericHash: GenericHashType? = nil,
|
||||
ed25519: Ed25519Type.Type? = nil,
|
||||
nonceGenerator16: NonceGenerator16ByteType? = nil,
|
||||
nonceGenerator24: NonceGenerator24ByteType? = nil,
|
||||
standardUserDefaults: UserDefaultsType? = nil,
|
||||
date: Date? = nil
|
||||
) -> Dependencies {
|
||||
return Dependencies(
|
||||
api: (api ?? self._api),
|
||||
storage: (storage ?? self._storage),
|
||||
sodium: (sodium ?? self._sodium),
|
||||
aeadXChaCha20Poly1305Ietf: (aeadXChaCha20Poly1305Ietf ?? self._aeadXChaCha20Poly1305Ietf),
|
||||
sign: (sign ?? self._sign),
|
||||
genericHash: (genericHash ?? self._genericHash),
|
||||
ed25519: (ed25519 ?? self._ed25519),
|
||||
nonceGenerator16: (nonceGenerator16 ?? self._nonceGenerator16),
|
||||
nonceGenerator24: (nonceGenerator24 ?? self._nonceGenerator24),
|
||||
standardUserDefaults: (standardUserDefaults ?? self._standardUserDefaults),
|
||||
date: (date ?? self._date)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Convenience
|
||||
|
||||
fileprivate func getValueSettingIfNull<T>(_ maybeValue: inout T?, _ valueGenerator: () -> T) -> T {
|
||||
guard let value: T = maybeValue else {
|
||||
let value: T = valueGenerator()
|
||||
maybeValue = value
|
||||
return value
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
import Sodium
|
||||
import SessionSnodeKit
|
||||
import SessionUtilitiesKit
|
||||
|
||||
// MARK: - Dependencies
|
||||
|
||||
public class Dependencies {
|
||||
private var _api: OnionRequestAPIType.Type?
|
||||
public var api: OnionRequestAPIType.Type {
|
||||
get { getValueSettingIfNull(&_api) { OnionRequestAPI.self } }
|
||||
set { _api = newValue }
|
||||
}
|
||||
|
||||
private var _storage: SessionMessagingKitStorageProtocol?
|
||||
public var storage: SessionMessagingKitStorageProtocol {
|
||||
get { getValueSettingIfNull(&_storage) { SNMessagingKitConfiguration.shared.storage } }
|
||||
set { _storage = newValue }
|
||||
}
|
||||
|
||||
private var _sodium: SodiumType?
|
||||
public var sodium: SodiumType {
|
||||
get { getValueSettingIfNull(&_sodium) { Sodium() } }
|
||||
set { _sodium = newValue }
|
||||
}
|
||||
|
||||
private var _aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType?
|
||||
public var aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType {
|
||||
get { getValueSettingIfNull(&_aeadXChaCha20Poly1305Ietf) { sodium.getAeadXChaCha20Poly1305Ietf() } }
|
||||
set { _aeadXChaCha20Poly1305Ietf = newValue }
|
||||
}
|
||||
|
||||
private var _sign: SignType?
|
||||
public var sign: SignType {
|
||||
get { getValueSettingIfNull(&_sign) { sodium.getSign() } }
|
||||
set { _sign = newValue }
|
||||
}
|
||||
|
||||
private var _genericHash: GenericHashType?
|
||||
public var genericHash: GenericHashType {
|
||||
get { getValueSettingIfNull(&_genericHash) { sodium.getGenericHash() } }
|
||||
set { _genericHash = newValue }
|
||||
}
|
||||
|
||||
private var _ed25519: Ed25519Type.Type?
|
||||
public var ed25519: Ed25519Type.Type {
|
||||
get { getValueSettingIfNull(&_ed25519) { Ed25519.self } }
|
||||
set { _ed25519 = newValue }
|
||||
}
|
||||
|
||||
private var _nonceGenerator16: NonceGenerator16ByteType?
|
||||
public var nonceGenerator16: NonceGenerator16ByteType {
|
||||
get { getValueSettingIfNull(&_nonceGenerator16) { OpenGroupAPI.NonceGenerator16Byte() } }
|
||||
set { _nonceGenerator16 = newValue }
|
||||
}
|
||||
|
||||
private var _nonceGenerator24: NonceGenerator24ByteType?
|
||||
public var nonceGenerator24: NonceGenerator24ByteType {
|
||||
get { getValueSettingIfNull(&_nonceGenerator24) { OpenGroupAPI.NonceGenerator24Byte() } }
|
||||
set { _nonceGenerator24 = newValue }
|
||||
}
|
||||
|
||||
private var _standardUserDefaults: UserDefaultsType?
|
||||
public var standardUserDefaults: UserDefaultsType {
|
||||
get { getValueSettingIfNull(&_standardUserDefaults) { UserDefaults.standard } }
|
||||
set { _standardUserDefaults = newValue }
|
||||
}
|
||||
|
||||
private var _date: Date?
|
||||
public var date: Date {
|
||||
get { getValueSettingIfNull(&_date) { Date() } }
|
||||
set { _date = newValue }
|
||||
}
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
public init(
|
||||
api: OnionRequestAPIType.Type? = nil,
|
||||
storage: SessionMessagingKitStorageProtocol? = nil,
|
||||
sodium: SodiumType? = nil,
|
||||
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
|
||||
sign: SignType? = nil,
|
||||
genericHash: GenericHashType? = nil,
|
||||
ed25519: Ed25519Type.Type? = nil,
|
||||
nonceGenerator16: NonceGenerator16ByteType? = nil,
|
||||
nonceGenerator24: NonceGenerator24ByteType? = nil,
|
||||
standardUserDefaults: UserDefaultsType? = nil,
|
||||
date: Date? = nil
|
||||
) {
|
||||
_api = api
|
||||
_storage = storage
|
||||
_sodium = sodium
|
||||
_aeadXChaCha20Poly1305Ietf = aeadXChaCha20Poly1305Ietf
|
||||
_sign = sign
|
||||
_genericHash = genericHash
|
||||
_ed25519 = ed25519
|
||||
_nonceGenerator16 = nonceGenerator16
|
||||
_nonceGenerator24 = nonceGenerator24
|
||||
_standardUserDefaults = standardUserDefaults
|
||||
_date = date
|
||||
}
|
||||
|
||||
// MARK: - Convenience
|
||||
|
||||
public func with(
|
||||
api: OnionRequestAPIType.Type? = nil,
|
||||
storage: SessionMessagingKitStorageProtocol? = nil,
|
||||
sodium: SodiumType? = nil,
|
||||
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
|
||||
sign: SignType? = nil,
|
||||
genericHash: GenericHashType? = nil,
|
||||
ed25519: Ed25519Type.Type? = nil,
|
||||
nonceGenerator16: NonceGenerator16ByteType? = nil,
|
||||
nonceGenerator24: NonceGenerator24ByteType? = nil,
|
||||
standardUserDefaults: UserDefaultsType? = nil,
|
||||
date: Date? = nil
|
||||
) -> Dependencies {
|
||||
return Dependencies(
|
||||
api: (api ?? self._api),
|
||||
storage: (storage ?? self._storage),
|
||||
sodium: (sodium ?? self._sodium),
|
||||
aeadXChaCha20Poly1305Ietf: (aeadXChaCha20Poly1305Ietf ?? self._aeadXChaCha20Poly1305Ietf),
|
||||
sign: (sign ?? self._sign),
|
||||
genericHash: (genericHash ?? self._genericHash),
|
||||
ed25519: (ed25519 ?? self._ed25519),
|
||||
nonceGenerator16: (nonceGenerator16 ?? self._nonceGenerator16),
|
||||
nonceGenerator24: (nonceGenerator24 ?? self._nonceGenerator24),
|
||||
standardUserDefaults: (standardUserDefaults ?? self._standardUserDefaults),
|
||||
date: (date ?? self._date)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Convenience
|
||||
|
||||
fileprivate func getValueSettingIfNull<T>(_ maybeValue: inout T?, _ valueGenerator: () -> T) -> T {
|
||||
guard let value: T = maybeValue else {
|
||||
let value: T = valueGenerator()
|
||||
maybeValue = value
|
||||
return value
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class RoomPollInfoSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a RoomPollInfo") {
|
||||
context("when initializing with a room") {
|
||||
it("copies all the relevant values across") {
|
||||
let room: OpenGroupAPI.Room = OpenGroupAPI.Room(
|
||||
token: "testToken",
|
||||
name: "testName",
|
||||
description: nil,
|
||||
infoUpdates: 123,
|
||||
messageSequence: 0,
|
||||
created: 0,
|
||||
activeUsers: 234,
|
||||
activeUsersCutoff: 0,
|
||||
imageId: nil,
|
||||
pinnedMessages: nil,
|
||||
admin: true,
|
||||
globalAdmin: true,
|
||||
admins: [],
|
||||
hiddenAdmins: nil,
|
||||
moderator: true,
|
||||
globalModerator: true,
|
||||
moderators: [],
|
||||
hiddenModerators: nil,
|
||||
read: true,
|
||||
defaultRead: true,
|
||||
defaultAccessible: true,
|
||||
write: true,
|
||||
defaultWrite: true,
|
||||
upload: true,
|
||||
defaultUpload: true
|
||||
)
|
||||
let roomPollInfo: OpenGroupAPI.RoomPollInfo = OpenGroupAPI.RoomPollInfo(room: room)
|
||||
|
||||
expect(roomPollInfo.token).to(equal(room.token))
|
||||
expect(roomPollInfo.activeUsers).to(equal(room.activeUsers))
|
||||
expect(roomPollInfo.admin).to(equal(room.admin))
|
||||
expect(roomPollInfo.globalAdmin).to(equal(room.globalAdmin))
|
||||
expect(roomPollInfo.moderator).to(equal(room.moderator))
|
||||
expect(roomPollInfo.globalModerator).to(equal(room.globalModerator))
|
||||
expect(roomPollInfo.read).to(equal(room.read))
|
||||
expect(roomPollInfo.defaultRead).to(equal(room.defaultRead))
|
||||
expect(roomPollInfo.defaultAccessible).to(equal(room.defaultAccessible))
|
||||
expect(roomPollInfo.write).to(equal(room.write))
|
||||
expect(roomPollInfo.defaultWrite).to(equal(room.defaultWrite))
|
||||
expect(roomPollInfo.upload).to(equal(room.upload))
|
||||
expect(roomPollInfo.defaultUpload).to(equal(room.defaultUpload))
|
||||
expect(roomPollInfo.details).to(equal(room))
|
||||
}
|
||||
}
|
||||
|
||||
context("when decoding") {
|
||||
it("defaults admin and moderator values to false if omitted") {
|
||||
let roomPollInfoJson: String = """
|
||||
{
|
||||
"token": "testToken",
|
||||
"active_users": 0,
|
||||
|
||||
"read": true,
|
||||
"default_read": true,
|
||||
"default_accessible": true,
|
||||
"write": true,
|
||||
"default_write": true,
|
||||
"upload": true,
|
||||
"default_upload": true,
|
||||
|
||||
"details": null
|
||||
}
|
||||
"""
|
||||
let roomData: Data = roomPollInfoJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.RoomPollInfo = try! JSONDecoder().decode(OpenGroupAPI.RoomPollInfo.self, from: roomData)
|
||||
|
||||
expect(result.admin).to(beFalse())
|
||||
expect(result.globalAdmin).to(beFalse())
|
||||
expect(result.moderator).to(beFalse())
|
||||
expect(result.globalModerator).to(beFalse())
|
||||
}
|
||||
|
||||
it("sets the admin and moderator values when provided") {
|
||||
let roomPollInfoJson: String = """
|
||||
{
|
||||
"token": "testToken",
|
||||
"active_users": 0,
|
||||
|
||||
"admin": true,
|
||||
"global_admin": true,
|
||||
|
||||
"moderator": true,
|
||||
"global_moderator": true,
|
||||
|
||||
"read": true,
|
||||
"default_read": true,
|
||||
"default_accessible": true,
|
||||
"write": true,
|
||||
"default_write": true,
|
||||
"upload": true,
|
||||
"default_upload": true,
|
||||
|
||||
"details": null
|
||||
}
|
||||
"""
|
||||
let roomData: Data = roomPollInfoJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.RoomPollInfo = try! JSONDecoder().decode(OpenGroupAPI.RoomPollInfo.self, from: roomData)
|
||||
|
||||
expect(result.admin).to(beTrue())
|
||||
expect(result.globalAdmin).to(beTrue())
|
||||
expect(result.moderator).to(beTrue())
|
||||
expect(result.globalModerator).to(beTrue())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class RoomSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a Room") {
|
||||
context("when decoding") {
|
||||
it("defaults admin and moderator values to false if omitted") {
|
||||
let roomJson: String = """
|
||||
{
|
||||
"token": "testToken",
|
||||
"name": "testName",
|
||||
"description": "testDescription",
|
||||
"info_updates": 0,
|
||||
"message_sequence": 0,
|
||||
"created": 1,
|
||||
|
||||
"active_users": 0,
|
||||
"active_users_cutoff": 0,
|
||||
"image_id": 0,
|
||||
"pinned_messages": [],
|
||||
|
||||
"admins": [],
|
||||
"hidden_admins": [],
|
||||
|
||||
"moderators": [],
|
||||
"hidden_moderators": [],
|
||||
|
||||
"read": true,
|
||||
"default_read": true,
|
||||
"default_accessible": true,
|
||||
"write": true,
|
||||
"default_write": true,
|
||||
"upload": true,
|
||||
"default_upload": true
|
||||
}
|
||||
"""
|
||||
let roomData: Data = roomJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.Room = try! JSONDecoder().decode(OpenGroupAPI.Room.self, from: roomData)
|
||||
|
||||
expect(result.admin).to(beFalse())
|
||||
expect(result.globalAdmin).to(beFalse())
|
||||
expect(result.moderator).to(beFalse())
|
||||
expect(result.globalModerator).to(beFalse())
|
||||
}
|
||||
|
||||
it("sets the admin and moderator values when provided") {
|
||||
let roomJson: String = """
|
||||
{
|
||||
"token": "testToken",
|
||||
"name": "testName",
|
||||
"description": "testDescription",
|
||||
"info_updates": 0,
|
||||
"message_sequence": 0,
|
||||
"created": 1,
|
||||
|
||||
"active_users": 0,
|
||||
"active_users_cutoff": 0,
|
||||
"image_id": 0,
|
||||
"pinned_messages": [],
|
||||
|
||||
"admin": true,
|
||||
"global_admin": true,
|
||||
"admins": [],
|
||||
"hidden_admins": [],
|
||||
|
||||
"moderator": true,
|
||||
"global_moderator": true,
|
||||
"moderators": [],
|
||||
"hidden_moderators": [],
|
||||
|
||||
"read": true,
|
||||
"default_read": true,
|
||||
"default_accessible": true,
|
||||
"write": true,
|
||||
"default_write": true,
|
||||
"upload": true,
|
||||
"default_upload": true
|
||||
}
|
||||
"""
|
||||
let roomData: Data = roomJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.Room = try! JSONDecoder().decode(OpenGroupAPI.Room.self, from: roomData)
|
||||
|
||||
expect(result.admin).to(beTrue())
|
||||
expect(result.globalAdmin).to(beTrue())
|
||||
expect(result.moderator).to(beTrue())
|
||||
expect(result.globalModerator).to(beTrue())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,253 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SOGSMessageSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a SOGSMessage") {
|
||||
var messageJson: String!
|
||||
var messageData: Data!
|
||||
var decoder: JSONDecoder!
|
||||
var testSign: TestSign!
|
||||
var dependencies: Dependencies!
|
||||
|
||||
beforeEach {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"session_id": "05\(TestConstants.publicKey)",
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "VGVzdERhdGE=",
|
||||
"signature": "VGVzdERhdGE="
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
testSign = TestSign()
|
||||
dependencies = Dependencies(
|
||||
sign: testSign,
|
||||
ed25519: TestEd25519.self
|
||||
)
|
||||
decoder = JSONDecoder()
|
||||
decoder.userInfo = [ Dependencies.userInfoKey: dependencies as Any ]
|
||||
}
|
||||
|
||||
context("when decoding") {
|
||||
it("defaults the whisper values to false") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"posted": 234,
|
||||
"seqno": 345
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.Message? = try? decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
|
||||
expect(result).toNot(beNil())
|
||||
expect(result?.whisper).to(beFalse())
|
||||
expect(result?.whisperMods).to(beFalse())
|
||||
}
|
||||
|
||||
context("and there is no content") {
|
||||
it("does not need a sender") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
let result: OpenGroupAPI.Message? = try? decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
|
||||
expect(result).toNot(beNil())
|
||||
expect(result?.sender).to(beNil())
|
||||
expect(result?.base64EncodedData).to(beNil())
|
||||
expect(result?.base64EncodedSignature).to(beNil())
|
||||
}
|
||||
}
|
||||
|
||||
context("and there is content") {
|
||||
it("errors if there is no sender") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "VGVzdERhdGE=",
|
||||
"signature": "VGVzdERhdGE="
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
|
||||
it("errors if the data is not a base64 encoded string") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"session_id": "05\(TestConstants.publicKey)",
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "Test!!!",
|
||||
"signature": "VGVzdERhdGE="
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
|
||||
it("errors if the signature is not a base64 encoded string") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"session_id": "05\(TestConstants.publicKey)",
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "VGVzdERhdGE=",
|
||||
"signature": "Test!!!"
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
|
||||
it("errors if the dependencies are not provided to the JSONDecoder") {
|
||||
decoder = JSONDecoder()
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
|
||||
it("errors if the session_id value is not valid") {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"session_id": "TestId",
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "VGVzdERhdGE=",
|
||||
"signature": "VGVzdERhdGE="
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
|
||||
|
||||
context("that is blinded") {
|
||||
beforeEach {
|
||||
messageJson = """
|
||||
{
|
||||
"id": 123,
|
||||
"session_id": "15\(TestConstants.publicKey)",
|
||||
"posted": 234,
|
||||
"seqno": 345,
|
||||
"whisper": false,
|
||||
"whisper_mods": false,
|
||||
|
||||
"data": "VGVzdERhdGE=",
|
||||
"signature": "VGVzdERhdGE="
|
||||
}
|
||||
"""
|
||||
messageData = messageJson.data(using: .utf8)!
|
||||
}
|
||||
|
||||
it("succeeds if it succeeds verification") {
|
||||
testSign.mockData[.verify] = true
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.toNot(beNil())
|
||||
}
|
||||
|
||||
it("throws if it fails verification") {
|
||||
testSign.mockData[.verify] = false
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
}
|
||||
|
||||
context("that is unblinded") {
|
||||
it("succeeds if it succeeds verification") {
|
||||
TestEd25519.mockData[
|
||||
.verifySignature(
|
||||
signature: Data(base64Encoded: "VGVzdERhdGE=")!,
|
||||
publicKey: Data(hex: TestConstants.publicKey),
|
||||
data: Data(base64Encoded: "VGVzdERhdGE=")!
|
||||
)
|
||||
] = true
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.toNot(beNil())
|
||||
}
|
||||
|
||||
it("throws if it fails verification") {
|
||||
TestEd25519.mockData[
|
||||
.verifySignature(
|
||||
signature: Data(base64Encoded: "VGVzdERhdGE=")!,
|
||||
publicKey: Data(hex: TestConstants.publicKey),
|
||||
data: Data(base64Encoded: "VGVzdERhdGE=")!
|
||||
)
|
||||
] = false
|
||||
|
||||
expect {
|
||||
try decoder.decode(OpenGroupAPI.Message.self, from: messageData)
|
||||
}
|
||||
.to(throwError(HTTP.Error.parsingFailed))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SendDirectMessageRequestSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a SendDirectMessageRequest") {
|
||||
context("when encoding") {
|
||||
it("encodes the data as a base64 string") {
|
||||
let request: OpenGroupAPI.SendDirectMessageRequest = OpenGroupAPI.SendDirectMessageRequest(
|
||||
message: "TestData".data(using: .utf8)!
|
||||
)
|
||||
let requestData: Data = try! JSONEncoder().encode(request)
|
||||
let requestDataString: String = String(data: requestData, encoding: .utf8)!
|
||||
|
||||
expect(requestDataString).toNot(contain("TestData"))
|
||||
expect(requestDataString).to(contain("VGVzdERhdGE="))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SendMessageRequestSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a SendMessageRequest") {
|
||||
context("when initializing") {
|
||||
it("defaults the optional values to nil") {
|
||||
let request: OpenGroupAPI.SendMessageRequest = OpenGroupAPI.SendMessageRequest(
|
||||
data: "TestData".data(using: .utf8)!,
|
||||
signature: "TestSignature".data(using: .utf8)!
|
||||
)
|
||||
|
||||
expect(request.whisperTo).to(beNil())
|
||||
expect(request.whisperMods).to(beNil())
|
||||
expect(request.fileIds).to(beNil())
|
||||
}
|
||||
}
|
||||
|
||||
context("when encoding") {
|
||||
it("encodes the data as a base64 string") {
|
||||
let request: OpenGroupAPI.SendMessageRequest = OpenGroupAPI.SendMessageRequest(
|
||||
data: "TestData".data(using: .utf8)!,
|
||||
signature: "TestSignature".data(using: .utf8)!,
|
||||
whisperTo: nil,
|
||||
whisperMods: nil,
|
||||
fileIds: nil
|
||||
)
|
||||
let requestData: Data = try! JSONEncoder().encode(request)
|
||||
let requestDataString: String = String(data: requestData, encoding: .utf8)!
|
||||
|
||||
expect(requestDataString).toNot(contain("TestData"))
|
||||
expect(requestDataString).to(contain("VGVzdERhdGE="))
|
||||
}
|
||||
|
||||
it("encodes the signature as a base64 string") {
|
||||
let request: OpenGroupAPI.SendMessageRequest = OpenGroupAPI.SendMessageRequest(
|
||||
data: "TestData".data(using: .utf8)!,
|
||||
signature: "TestSignature".data(using: .utf8)!,
|
||||
whisperTo: nil,
|
||||
whisperMods: nil,
|
||||
fileIds: nil
|
||||
)
|
||||
let requestData: Data = try! JSONEncoder().encode(request)
|
||||
let requestDataString: String = String(data: requestData, encoding: .utf8)!
|
||||
|
||||
expect(requestDataString).toNot(contain("TestSignature"))
|
||||
expect(requestDataString).to(contain("VGVzdFNpZ25hdHVyZQ=="))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class UpdateMessageRequestSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a UpdateMessageRequest") {
|
||||
context("when encoding") {
|
||||
it("encodes the data as a base64 string") {
|
||||
let request: OpenGroupAPI.UpdateMessageRequest = OpenGroupAPI.UpdateMessageRequest(
|
||||
data: "TestData".data(using: .utf8)!,
|
||||
signature: "TestSignature".data(using: .utf8)!,
|
||||
fileIds: nil
|
||||
)
|
||||
let requestData: Data = try! JSONEncoder().encode(request)
|
||||
let requestDataString: String = String(data: requestData, encoding: .utf8)!
|
||||
|
||||
expect(requestDataString).toNot(contain("TestData"))
|
||||
expect(requestDataString).to(contain("VGVzdERhdGE="))
|
||||
}
|
||||
|
||||
it("encodes the signature as a base64 string") {
|
||||
let request: OpenGroupAPI.UpdateMessageRequest = OpenGroupAPI.UpdateMessageRequest(
|
||||
data: "TestData".data(using: .utf8)!,
|
||||
signature: "TestSignature".data(using: .utf8)!,
|
||||
fileIds: nil
|
||||
)
|
||||
let requestData: Data = try! JSONEncoder().encode(request)
|
||||
let requestDataString: String = String(data: requestData, encoding: .utf8)!
|
||||
|
||||
expect(requestDataString).toNot(contain("TestSignature"))
|
||||
expect(requestDataString).to(contain("VGVzdFNpZ25hdHVyZQ=="))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,26 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class NonceGeneratorSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a NonceGenerator16Byte") {
|
||||
it("has the correct number of bytes") {
|
||||
expect(OpenGroupAPI.NonceGenerator16Byte().NonceBytes).to(equal(16))
|
||||
}
|
||||
}
|
||||
|
||||
describe("a NonceGenerator24Byte") {
|
||||
it("has the correct number of bytes") {
|
||||
expect(OpenGroupAPI.NonceGenerator24Byte().NonceBytes).to(equal(24))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class PersonalizationSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a Personalization") {
|
||||
it("generates bytes correctly") {
|
||||
expect(OpenGroupAPI.Personalization.sharedKeys.bytes)
|
||||
.to(equal([115, 111, 103, 115, 46, 115, 104, 97, 114, 101, 100, 95, 107, 101, 121, 115]))
|
||||
expect(OpenGroupAPI.Personalization.authHeader.bytes)
|
||||
.to(equal([115, 111, 103, 115, 46, 97, 117, 116, 104, 95, 104, 101, 97, 100, 101, 114]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SOGSEndpointSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a SOGSEndpoint") {
|
||||
it("generates the path value correctly") {
|
||||
// Utility
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.onion.path).to(equal("oxen/v4/lsrpc"))
|
||||
expect(OpenGroupAPI.Endpoint.batch.path).to(equal("batch"))
|
||||
expect(OpenGroupAPI.Endpoint.sequence.path).to(equal("sequence"))
|
||||
expect(OpenGroupAPI.Endpoint.capabilities.path).to(equal("capabilities"))
|
||||
|
||||
// Rooms
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.rooms.path).to(equal("rooms"))
|
||||
expect(OpenGroupAPI.Endpoint.room("test").path).to(equal("room/test"))
|
||||
expect(OpenGroupAPI.Endpoint.roomPollInfo("test", 123).path).to(equal("room/test/pollInfo/123"))
|
||||
|
||||
// Messages
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.roomMessage("test").path).to(equal("room/test/message"))
|
||||
expect(OpenGroupAPI.Endpoint.roomMessageIndividual("test", id: 123).path).to(equal("room/test/message/123"))
|
||||
expect(OpenGroupAPI.Endpoint.roomMessagesRecent("test").path).to(equal("room/test/messages/recent"))
|
||||
expect(OpenGroupAPI.Endpoint.roomMessagesBefore("test", id: 123).path).to(equal("room/test/messages/before/123"))
|
||||
expect(OpenGroupAPI.Endpoint.roomMessagesSince("test", seqNo: 123).path)
|
||||
.to(equal("room/test/messages/since/123"))
|
||||
|
||||
// Pinning
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.roomPinMessage("test", id: 123).path).to(equal("room/test/pin/123"))
|
||||
expect(OpenGroupAPI.Endpoint.roomUnpinMessage("test", id: 123).path).to(equal("room/test/unpin/123"))
|
||||
expect(OpenGroupAPI.Endpoint.roomUnpinAll("test").path).to(equal("room/test/unpin/all"))
|
||||
|
||||
// Files
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.roomFile("test").path).to(equal("room/test/file"))
|
||||
expect(OpenGroupAPI.Endpoint.roomFileIndividual("test", 123).path).to(equal("room/test/file/123"))
|
||||
|
||||
// Inbox/Outbox (Message Requests)
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.inbox.path).to(equal("inbox"))
|
||||
expect(OpenGroupAPI.Endpoint.inboxSince(id: 123).path).to(equal("inbox/since/123"))
|
||||
expect(OpenGroupAPI.Endpoint.inboxFor(sessionId: "test").path).to(equal("inbox/test"))
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.outbox.path).to(equal("outbox"))
|
||||
expect(OpenGroupAPI.Endpoint.outboxSince(id: 123).path).to(equal("outbox/since/123"))
|
||||
|
||||
// Users
|
||||
|
||||
expect(OpenGroupAPI.Endpoint.userBan("test").path).to(equal("user/test/ban"))
|
||||
expect(OpenGroupAPI.Endpoint.userUnban("test").path).to(equal("user/test/unban"))
|
||||
expect(OpenGroupAPI.Endpoint.userModerator("test").path).to(equal("user/test/moderator"))
|
||||
expect(OpenGroupAPI.Endpoint.userDeleteMessages("test").path).to(equal("user/test/deleteMessages"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SOGSErrorSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("a SOGSError") {
|
||||
it("generates the error description correctly") {
|
||||
expect(OpenGroupAPI.Error.decryptionFailed.errorDescription)
|
||||
.to(equal("Couldn't decrypt response."))
|
||||
expect(OpenGroupAPI.Error.signingFailed.errorDescription)
|
||||
.to(equal("Couldn't sign message."))
|
||||
expect(OpenGroupAPI.Error.noPublicKey.errorDescription)
|
||||
.to(equal("Couldn't find server public key."))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
@testable import SessionMessagingKit
|
||||
|
||||
class SodiumProtocolsSpec: QuickSpec {
|
||||
// MARK: - Spec
|
||||
|
||||
override func spec() {
|
||||
describe("an AeadXChaCha20Poly1305IetfType") {
|
||||
let testValue: [UInt8] = [1, 2, 3]
|
||||
|
||||
it("provides the default values in it's extensions") {
|
||||
let testAead: TestAeadXChaCha20Poly1305Ietf = TestAeadXChaCha20Poly1305Ietf()
|
||||
testAead.mockData[.encrypt] = testValue
|
||||
testAead.mockData[.decrypt] = testValue
|
||||
|
||||
expect(testAead.encrypt(message: [], secretKey: [], nonce: [])).to(equal(testValue))
|
||||
expect(testAead.encrypt(message: [], secretKey: [], nonce: [], additionalData: nil)).to(equal(testValue))
|
||||
expect(testAead.decrypt(authenticatedCipherText: [], secretKey: [], nonce: [])).to(equal(testValue))
|
||||
expect(testAead.decrypt(authenticatedCipherText: [], secretKey: [], nonce: [], additionalData: nil))
|
||||
.to(equal(testValue))
|
||||
}
|
||||
}
|
||||
|
||||
describe("a GenericHashType") {
|
||||
let testValue: [UInt8] = [1, 2, 3]
|
||||
|
||||
it("provides the default values in it's extensions") {
|
||||
let testGenericHash: TestGenericHash = TestGenericHash()
|
||||
testGenericHash.mockData[.hash] = testValue
|
||||
testGenericHash.mockData[.hashSaltPersonal] = testValue
|
||||
|
||||
expect(testGenericHash.hash(message: [])).to(equal(testValue))
|
||||
expect(testGenericHash.hash(message: [], key: nil)).to(equal(testValue))
|
||||
expect(testGenericHash.hashSaltPersonal(message: [], outputLength: 0, salt: [], personal: []))
|
||||
.to(equal(testValue))
|
||||
expect(testGenericHash.hashSaltPersonal(message: [], outputLength: 0, key: nil, salt: [], personal: []))
|
||||
.to(equal(testValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue