// // Copyright 2022 New Vector Ltd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // @testable import ElementX @testable import MatrixRustSDK import XCTest class LoggingTests: XCTestCase { private enum Constants { static let genericFailure = "Test failed" } override func setUpWithError() throws { MXLogger.deleteLogFiles() } override func tearDownWithError() throws { // Put teardown code here. This method is called after the invocation of each test method in the class. } func testFileLogging() throws { XCTAssertTrue(MXLogger.logFiles.isEmpty) let log = UUID().uuidString MXLog.configure(logLevel: .info, redirectToFiles: true) MXLog.info(log) guard let logFile = MXLogger.logFiles.first else { XCTFail(Constants.genericFailure) return } let content = try String(contentsOf: logFile) XCTAssert(content.contains(log)) } func testFileRotationOnLaunch() throws { // Given a fresh launch with no logs. XCTAssertTrue(MXLogger.logFiles.isEmpty) // When launching the app 5 times. let launchCount = 5 for index in 0..\(textString)")) let noticeString = "NoticeString" let noticeMessage = NoticeMessageContent(body: noticeString, formatted: FormattedBody(format: .html, body: "\(noticeString)")) let emoteString = "EmoteString" let emoteMessage = EmoteMessageContent(body: emoteString, formatted: FormattedBody(format: .html, body: "\(emoteString)")) 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", filename: "FileName", source: MediaSource(unsafeFromRawPointer: pointer), info: nil) // When logging that value XCTAssert(MXLogger.logFiles.isEmpty) MXLog.configure(logLevel: .info, redirectToFiles: true) 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)) } }