Skip to content

Commit

Permalink
add tests for hmacs
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 3, 2025
1 parent c786d57 commit 8417076
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 32 deletions.
31 changes: 30 additions & 1 deletion Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ConversationTests: XCTestCase {
XCTAssertEqual(convoCountAllowed, 1)
XCTAssertEqual(convoCountDenied, 1)
}

func testCanSyncAllConversationsFiltered() async throws {
let fixtures = try await fixtures()

Expand Down Expand Up @@ -189,4 +189,33 @@ class ConversationTests: XCTestCase {

await fulfillment(of: [expectation1], timeout: 3)
}

func testReturnsAllHMACKeys() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let opts = ClientOptions(
api: ClientOptions.Api(env: .local, isSecure: false),
dbEncryptionKey: key)
let fixtures = try await fixtures()
var conversations: [Conversation] = []
for _ in 0..<5 {
let account = try PrivateKey.generate()
let client = try await Client.create(
account: account, options: opts)
do {
let newConversation = try await fixtures.alixClient
.conversations
.newConversation(
with: client.address
)
conversations.append(newConversation)
} catch {
print("Error creating conversation: \(error)")
}
}
let hmacKeys = try await fixtures.alixClient.conversations.getHmacKeys()
let topics = hmacKeys.hmacKeys.keys
conversations.forEach { conversation in
XCTAssertTrue(topics.contains(conversation.topic))
}
}
}
107 changes: 76 additions & 31 deletions Tests/XMTPTests/HistorySyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
class HistorySyncTests: XCTestCase {
func testSyncConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let alixClient = try await Client.create(
Expand All @@ -25,13 +25,12 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db"
)
)

let group = try await alixClient.conversations.newGroup(
with: [fixtures.bo.walletAddress])
try await group.updateConsentState(state: .denied)
XCTAssertEqual(try group.consentState(), .denied)



let alixClient2 = try await Client.create(
account: alix,
options: .init(
Expand All @@ -40,19 +39,21 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient2.inboxState(refreshFromNetwork: true)
XCTAssertEqual(state.installations.count, 2)

try await alixClient2.preferences.syncConsent()
try await alixClient.conversations.syncAllConversations()
sleep(2)
try await alixClient2.conversations.syncAllConversations()
sleep(2)

if let dm2 = try await alixClient2.findConversation(conversationId: group.id) {

if let dm2 = try await alixClient2.findConversation(
conversationId: group.id)
{
XCTAssertEqual(try dm2.consentState(), .denied)

try await alixClient2.preferences.setConsentState(
entries: [
ConsentRecord(
Expand All @@ -69,10 +70,10 @@ class HistorySyncTests: XCTestCase {
XCTAssertEqual(try dm2.consentState(), .allowed)
}
}

func testSyncMessages() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let alixClient = try await Client.create(
Expand All @@ -83,13 +84,13 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db"
)
)

let group = try await alixClient.conversations.newGroup(
with: [fixtures.bo.walletAddress])
try await group.send(content: "hi")
let messageCount = try await group.messages().count
XCTAssertEqual(messageCount, 2)

let alixClient2 = try await Client.create(
account: alix,
options: .init(
Expand All @@ -98,28 +99,29 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient2.inboxState(refreshFromNetwork: true)
XCTAssertEqual(state.installations.count, 2)

try await alixClient.conversations.syncAllConversations()
sleep(2)
try await alixClient2.conversations.syncAllConversations()
sleep(2)

if let dm2 = try await alixClient2.findConversation(conversationId: group.id) {

if let dm2 = try await alixClient2.findConversation(
conversationId: group.id)
{
let messageCount = try await group.messages().count
XCTAssertEqual(messageCount, 2)
}
}



func testStreamConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()

let alixClient = try await Client.create(
account: alix,
options: .init(
Expand All @@ -128,9 +130,11 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db"
)
)

let alixGroup = try await alixClient.conversations.newGroup(with: [fixtures.bo.walletAddress])


let alixGroup = try await alixClient.conversations.newGroup(with: [
fixtures.bo.walletAddress
])

let alixClient2 = try await Client.create(
account: alix,
options: .init(
Expand All @@ -139,33 +143,74 @@ class HistorySyncTests: XCTestCase {
dbDirectory: "xmtp_db2"
)
)

try await alixGroup.send(content: "Hello")
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()
let alixGroup2 = try alixClient2.findGroup(groupId: alixGroup.id)!

var consentList = [ConsentRecord]()
let expectation = XCTestExpectation(description: "Stream Consent")
expectation.expectedFulfillmentCount = 3

Task(priority: .userInitiated) {
for try await entry in await alixClient.preferences.streamConsent() {
for try await entry in await alixClient.preferences.streamConsent()
{
consentList.append(entry)
expectation.fulfill()
}
}
sleep(1)
try await alixGroup2.updateConsentState(state: .denied)
let dm = try await alixClient2.conversations.newConversation(with: fixtures.caro.walletAddress)
let dm = try await alixClient2.conversations.newConversation(
with: fixtures.caro.walletAddress)
try await dm.updateConsentState(state: .denied)

sleep(5)
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()

await fulfillment(of: [expectation], timeout: 3)
print(consentList)
XCTAssertEqual(try alixGroup.consentState(), .denied)
}

func testStreamPrivatePreferences() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let expectation = XCTestExpectation(description: "Stream Preferences")
expectation.expectedFulfillmentCount = 1

Task(priority: .userInitiated) {
for try await _ in await alixClient.preferences.streamPreferenceUpdates()
{
expectation.fulfill()
}
}

sleep(2)

let alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

try await alixClient2.conversations.syncAllConversations()
try await alixClient.conversations.syncAllConversations()

await fulfillment(of: [expectation], timeout: 3)
}
}

0 comments on commit 8417076

Please sign in to comment.