Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Jan 16, 2023
1 parent 199f192 commit bcefe2c
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions UnitTests/Sources/LoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

@testable import ElementX
@testable import MatrixRustSDK
import XCTest

class LoggingTests: XCTestCase {
Expand Down Expand Up @@ -172,4 +173,163 @@ class LoggingTests: XCTestCase {

XCTAssertTrue(logFile.lastPathComponent.contains(subLogName))
}

func testRoomSummaryContentIsRedacted() throws {
// Given a room summary that contains sensitive information
let roomName = "Private Conversation"
let lastMessage = "Secret information"
let roomSummary = RoomSummaryDetails(id: "myroomid",
name: roomName,
isDirect: true,
avatarURLString: nil,
lastMessage: AttributedString(lastMessage),
lastMessageTimestamp: .now,
unreadNotificationCount: 0)

// When logging that value
XCTAssert(MXLogger.logFiles.isEmpty)
let configuration = MXLogConfiguration()
configuration.logLevel = .info
configuration.redirectLogsToFiles = true
MXLog.configure(configuration)
MXLog.info(roomSummary)

// Then the log file should not include the sensitive information
guard let logFile = MXLogger.logFiles.first else {
XCTFail(Constants.genericFailure)
return
}

let content = try String(contentsOf: logFile)
XCTAssertTrue(content.contains(roomSummary.id))
XCTAssertFalse(content.contains(roomName))
XCTAssertFalse(content.contains(lastMessage))
}

// swiftlint:disable function_body_length
func testTimelineContentIsRedacted() throws {
// Given timeline items that contain text
let textAttributedString = "TextAttributed"
let textMessage = TextRoomTimelineItem(id: "mytextmessage", text: "TextString",
attributedComponents: [.init(attributedString: AttributedString(textAttributedString),
isBlockquote: false)],
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false, senderId: "sender")
let noticeAttributedString = "NoticeAttributed"
let noticeMessage = NoticeRoomTimelineItem(id: "mynoticemessage", text: "NoticeString",
attributedComponents: [.init(attributedString: AttributedString(noticeAttributedString),
isBlockquote: false)],
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false, senderId: "sender")
let emoteAttributedString = "EmoteAttributed"
let emoteMessage = EmoteRoomTimelineItem(id: "myemotemessage", text: "EmoteString",
attributedComponents: [.init(attributedString: AttributedString(emoteAttributedString),
isBlockquote: false)],
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false, senderId: "sender")
let imageMessage = ImageRoomTimelineItem(id: "myimagemessage", text: "ImageString",
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false,
senderId: "sender", source: nil)
let videoMessage = VideoRoomTimelineItem(id: "myvideomessage", text: "VideoString",
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false,
senderId: "sender", duration: 0, source: nil, thumbnailSource: nil)
let fileMessage = FileRoomTimelineItem(id: "myfilemessage", text: "FileString",
timestamp: "", groupState: .single, isOutgoing: false, isEditable: false,
senderId: "sender", source: nil, thumbnailSource: nil)

// When logging that value
XCTAssert(MXLogger.logFiles.isEmpty)
let configuration = MXLogConfiguration()
configuration.logLevel = .info
configuration.redirectLogsToFiles = true
MXLog.configure(configuration)
MXLog.info(textMessage)
MXLog.info(noticeMessage)
MXLog.info(emoteMessage)
MXLog.info(imageMessage)
MXLog.info(videoMessage)
MXLog.info(fileMessage)

// Then the log file should not include the text content
guard let logFile = MXLogger.logFiles.first else {
XCTFail(Constants.genericFailure)
return
}

let content = try String(contentsOf: logFile)
XCTAssertTrue(content.contains(textMessage.id))
XCTAssertFalse(content.contains(textMessage.text))
XCTAssertFalse(content.contains(textAttributedString))

XCTAssertTrue(content.contains(noticeMessage.id))
XCTAssertFalse(content.contains(noticeMessage.text))
XCTAssertFalse(content.contains(noticeAttributedString))

XCTAssertTrue(content.contains(emoteMessage.id))
XCTAssertFalse(content.contains(emoteMessage.text))
XCTAssertFalse(content.contains(emoteAttributedString))

XCTAssertTrue(content.contains(imageMessage.id))
XCTAssertFalse(content.contains(imageMessage.text))

XCTAssertTrue(content.contains(videoMessage.id))
XCTAssertFalse(content.contains(videoMessage.text))

XCTAssertTrue(content.contains(fileMessage.id))
XCTAssertFalse(content.contains(fileMessage.text))
}
// swiftlint:enable function_body_length

func testRustMessageContentIsRedacted() throws {
// Given message content that contain text
let textString = "TextString"
let textMessage = TextMessageContent(body: "",
formatted: FormattedBody(format: .html, body: "<b>\(textString)</b>"))
let noticeString = "NoticeString"
let noticeMessage = NoticeMessageContent(body: noticeString,
formatted: FormattedBody(format: .html, body: "<b>\(noticeString)</b>"))
let emoteString = "EmoteString"
let emoteMessage = EmoteMessageContent(body: emoteString,
formatted: FormattedBody(format: .html, body: "<b>\(emoteString)</b>"))

let pointer = Unmanaged.passRetained(NSURL(fileURLWithPath: "/tmp/file")).toOpaque()
let imageMessage = ImageMessageContent(body: "ImageString", source: MediaSource(unsafeFromRawPointer: pointer), info: nil)
let videoMessage = VideoMessageContent(body: "VideoString", source: MediaSource(unsafeFromRawPointer: pointer), info: nil)
let fileMessage = FileMessageContent(body: "FileString", source: MediaSource(unsafeFromRawPointer: pointer), info: nil)

// When logging that value
XCTAssert(MXLogger.logFiles.isEmpty)
let configuration = MXLogConfiguration()
configuration.logLevel = .info
configuration.redirectLogsToFiles = true
MXLog.configure(configuration)
MXLog.info(textMessage)
MXLog.info(noticeMessage)
MXLog.info(emoteMessage)
MXLog.info(imageMessage)
MXLog.info(videoMessage)
MXLog.info(fileMessage)

// Then the log file should not include the text content
guard let logFile = MXLogger.logFiles.first else {
XCTFail(Constants.genericFailure)
return
}

let content = try String(contentsOf: logFile)
XCTAssertTrue(content.contains(String(describing: TextMessageContent.self)))
XCTAssertFalse(content.contains(textString))

XCTAssertTrue(content.contains(String(describing: NoticeMessageContent.self)))
XCTAssertFalse(content.contains(noticeString))

XCTAssertTrue(content.contains(String(describing: EmoteMessageContent.self)))
XCTAssertFalse(content.contains(emoteString))

XCTAssertTrue(content.contains(String(describing: ImageMessageContent.self)))
XCTAssertFalse(content.contains(imageMessage.body))

XCTAssertTrue(content.contains(String(describing: VideoMessageContent.self)))
XCTAssertFalse(content.contains(videoMessage.body))

XCTAssertTrue(content.contains(String(describing: FileMessageContent.self)))
XCTAssertFalse(content.contains(fileMessage.body))
}
}

0 comments on commit bcefe2c

Please sign in to comment.