65 lines
2.4 KiB
Swift
65 lines
2.4 KiB
Swift
import XCTest
|
|
@testable import MacMonitor
|
|
|
|
final class MMStorageTests: XCTestCase {
|
|
|
|
func testStatFSBufferParsing() {
|
|
var stat = statfs()
|
|
stat.f_bsize = 4096
|
|
stat.f_blocks = 125_000_000 // 500 GB
|
|
stat.f_bfree = 50_000_000 // 200 GB
|
|
stat.f_bavail = 45_000_000
|
|
stat.f_flags = UInt32(MNT_LOCAL)
|
|
|
|
let mountPath = "/Volumes/External"
|
|
let devicePath = "/dev/disk2s1"
|
|
let fsType = "apfs"
|
|
|
|
_ = mountPath.withCString { mntPtr in
|
|
strncpy(&stat.f_mntonname.0, mntPtr, Int(MNAMELEN))
|
|
}
|
|
_ = devicePath.withCString { devPtr in
|
|
strncpy(&stat.f_mntfromname.0, devPtr, Int(MNAMELEN))
|
|
}
|
|
_ = fsType.withCString { fsPtr in
|
|
strncpy(&stat.f_fstypename.0, fsPtr, Int(MFSTYPENAMELEN))
|
|
}
|
|
|
|
withUnsafePointer(to: stat) { ptr in
|
|
let volumes = MMStorageTelemetryProvider.parseStatFSBuffer(ptr, count: 1)
|
|
XCTAssertEqual(volumes.count, 1)
|
|
|
|
let vol = volumes.first!
|
|
XCTAssertEqual(vol["volumeName"] as? String, "External")
|
|
XCTAssertEqual(vol["mountPoint"] as? String, "/Volumes/External")
|
|
XCTAssertEqual(vol["devicePath"] as? String, "/dev/disk2s1")
|
|
XCTAssertEqual(vol["fsType"] as? String, "apfs")
|
|
|
|
let total = vol["totalBytes"] as? UInt64 ?? 0
|
|
let free = vol["freeBytes"] as? UInt64 ?? 0
|
|
let used = vol["usedBytes"] as? UInt64 ?? 0
|
|
let pct = vol["usedPercent"] as? Double ?? 0
|
|
|
|
XCTAssertEqual(total, 512_000_000_000)
|
|
XCTAssertEqual(free, 204_800_000_000)
|
|
XCTAssertEqual(used, 307_200_000_000)
|
|
XCTAssertEqual(pct, 60.0, accuracy: 0.1)
|
|
}
|
|
}
|
|
|
|
func testLiveStorageProvider() throws {
|
|
let provider = MMStorageTelemetryProvider()
|
|
XCTAssertTrue(provider.isAvailable)
|
|
XCTAssertEqual(provider.domain, .storage)
|
|
|
|
let sample = try provider.sampleTelemetry()
|
|
let volumes = sample["volumes"] as? [[String: Any]]
|
|
XCTAssertNotNil(volumes)
|
|
XCTAssertGreaterThan(volumes?.count ?? 0, 0)
|
|
|
|
let firstVol = volumes?.first
|
|
XCTAssertNotNil(firstVol?["mountPoint"])
|
|
XCTAssertGreaterThan(firstVol?["totalBytes"] as? UInt64 ?? 0, 0)
|
|
}
|
|
}
|