sonar

Scan files at memory bandwidth speed.
Log | Files | Refs

scan_test.odin (3050B)


      1 package scan
      2 
      3 import "core:testing"
      4 
      5 @(test)
      6 test_choose_prefers_the_mft_when_it_can_be_read :: proc(t: ^testing.T) {
      7 	ntfs := Target {
      8 		fs     = .Ntfs,
      9 		volume = `\\.\C:`,
     10 	}
     11 	testing.expect_value(t, choose(ntfs, readable = true).engine, Engine.Mft)
     12 
     13 	// A volume that would not open leaves the reader unavailable rather than merely
     14 	// slower, so the fallback runs and the caller is told what would pay.
     15 	fallback := choose(ntfs, readable = false)
     16 	testing.expect_value(t, fallback.engine, Engine.Walk)
     17 	testing.expect(t, fallback.permission_would_help)
     18 }
     19 
     20 @(test)
     21 test_choose_walks_what_it_cannot_read_directly :: proc(t: ^testing.T) {
     22 	// Elevation buys nothing on these, so it must not be suggested. A mount point is
     23 	// what makes a target walkable, and `resolve` sets one for everything that has a
     24 	// directory tree behind it.
     25 	for fs in ([]Filesystem{.Refs, .Exfat, .Fat32, .Network, .Other}) {
     26 		c := choose(Target{fs = fs, volume = `\\.\D:`, mount = `D:\`}, readable = true)
     27 		testing.expect_value(t, c.engine, Engine.Walk)
     28 		testing.expect(t, !c.permission_would_help)
     29 	}
     30 }
     31 
     32 @(test)
     33 test_choose_reads_an_image_without_elevation :: proc(t: ^testing.T) {
     34 	// An image is a file like any other: opening it is the whole of the permission
     35 	// needed, so asking for more would send a reader that cannot work.
     36 	image := Target {
     37 		fs     = .Ntfs,
     38 		volume = "disk.img",
     39 		image  = true,
     40 	}
     41 	testing.expect_value(t, choose(image, readable = true).engine, Engine.Mft)
     42 }
     43 
     44 @(test)
     45 test_choose_gives_up_on_a_volume_it_cannot_name :: proc(t: ^testing.T) {
     46 	// A raw device or an image whose filesystem went unrecognised has no tree to
     47 	// walk, so offering the walker would report an empty volume as an answer.
     48 	for v in ([]Target{{volume = "/dev/sda1"}, {volume = "disk.img", image = true}}) {
     49 		testing.expect_value(t, choose(v, readable = true).engine, Engine.None)
     50 	}
     51 }
     52 
     53 @(test)
     54 test_choose_gives_up_on_a_target_with_no_volume :: proc(t: ^testing.T) {
     55 	testing.expect_value(t, choose(Target{}, readable = true).engine, Engine.None)
     56 }
     57 
     58 @(test)
     59 test_resolve_finds_the_volume_behind_a_path :: proc(t: ^testing.T) {
     60 	when ODIN_OS != .Windows {
     61 		return
     62 	}
     63 	// The system drive is the one target every Windows machine has, and reading its
     64 	// name needs no privilege.
     65 	root, _, err := resolve("C:")
     66 	testing.expect_value(t, err, Error.None)
     67 	defer target_destroy(&root)
     68 	testing.expect_value(t, root.mount, `C:\`)
     69 	testing.expect_value(t, root.volume, `\\.\C:`)
     70 	testing.expect_value(t, root.root, "")
     71 
     72 	// A path below the mount point keeps the same volume and records the subtree.
     73 	sub, _, sub_err := resolve(`C:\Windows`)
     74 	testing.expect_value(t, sub_err, Error.None)
     75 	defer target_destroy(&sub)
     76 	testing.expect_value(t, sub.volume, root.volume)
     77 	testing.expect_value(t, sub.root, `\Windows`)
     78 	testing.expect_value(t, sub.fs, root.fs)
     79 }
     80 
     81 @(test)
     82 test_resolve_rejects_what_is_not_there :: proc(t: ^testing.T) {
     83 	when ODIN_OS != .Windows {
     84 		return
     85 	}
     86 	_, _, err := resolve(`Q:\no\such\path`)
     87 	testing.expect_value(t, err, Error.Target_Not_Found)
     88 }