|
| 1 | +//go:build linux |
| 2 | +// +build linux |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGetFields(t *testing.T) { |
| 12 | + var tt = []struct { |
| 13 | + input string |
| 14 | + number int |
| 15 | + expected [11]string |
| 16 | + }{ |
| 17 | + // Empty lines |
| 18 | + { |
| 19 | + input: "", |
| 20 | + number: 0, |
| 21 | + }, |
| 22 | + { |
| 23 | + input: " ", |
| 24 | + number: 0, |
| 25 | + }, |
| 26 | + { |
| 27 | + input: " ", |
| 28 | + number: 0, |
| 29 | + }, |
| 30 | + { |
| 31 | + input: " ", |
| 32 | + number: 0, |
| 33 | + }, |
| 34 | + |
| 35 | + // Comments |
| 36 | + { |
| 37 | + input: "#", |
| 38 | + number: 0, |
| 39 | + }, |
| 40 | + { |
| 41 | + input: "# ", |
| 42 | + number: 0, |
| 43 | + }, |
| 44 | + { |
| 45 | + input: "# ", |
| 46 | + number: 0, |
| 47 | + }, |
| 48 | + { |
| 49 | + input: "# I'm a lazy dog", |
| 50 | + number: 0, |
| 51 | + }, |
| 52 | + |
| 53 | + // Bad fields |
| 54 | + { |
| 55 | + input: "1 2", |
| 56 | + number: 2, |
| 57 | + expected: [11]string{"1", "2"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + input: "1 2", |
| 61 | + number: 2, |
| 62 | + expected: [11]string{"1", "2"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + input: "1 2 3", |
| 66 | + number: 3, |
| 67 | + expected: [11]string{"1", "2", "3"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + input: "1 2 3 4", |
| 71 | + number: 4, |
| 72 | + expected: [11]string{"1", "2", "3", "4"}, |
| 73 | + }, |
| 74 | + |
| 75 | + // No optional separator or no options |
| 76 | + { |
| 77 | + input: "1 2 3 4 5 6 7 NotASeparator 9 10 11", |
| 78 | + number: 6, |
| 79 | + expected: [11]string{"1", "2", "3", "4", "5", "6", "7 NotASeparator 9 10 11"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + input: "1 2 3 4 5 6 7 8 9 10 11", |
| 83 | + number: 6, |
| 84 | + expected: [11]string{"1", "2", "3", "4", "5", "6", "7 8 9 10 11"}, |
| 85 | + }, |
| 86 | + { |
| 87 | + input: "1 2 3 4 5 6 - 9 10 11", |
| 88 | + number: 11, |
| 89 | + expected: [11]string{"1", "2", "3", "4", "5", "6", "", "-", "9", "10", "11"}, |
| 90 | + }, |
| 91 | + |
| 92 | + // Normal mount table line |
| 93 | + { |
| 94 | + input: "22 27 0:21 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw", |
| 95 | + number: 11, |
| 96 | + expected: [11]string{"22", "27", "0:21", "/", "/proc", "rw,nosuid,nodev,noexec,relatime", "shared:5", "-", "proc", "proc", "rw"}, |
| 97 | + }, |
| 98 | + { |
| 99 | + input: "31 23 0:27 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw,nsdelegate,memory_recursiveprot", |
| 100 | + number: 11, |
| 101 | + expected: [11]string{"31", "23", "0:27", "/", "/sys/fs/cgroup", "rw,nosuid,nodev,noexec,relatime", "shared:9", "-", "cgroup2", "cgroup2", "rw,nsdelegate,memory_recursiveprot"}, |
| 102 | + }, |
| 103 | + { |
| 104 | + input: "40 27 0:33 / /tmp rw,nosuid,nodev shared:18 - tmpfs tmpfs", |
| 105 | + number: 10, |
| 106 | + expected: [11]string{"40", "27", "0:33", "/", "/tmp", "rw,nosuid,nodev", "shared:18", "-", "tmpfs", "tmpfs"}, |
| 107 | + }, |
| 108 | + { |
| 109 | + input: "40 27 0:33 / /tmp rw,nosuid,nodev shared:18 shared:22 - tmpfs tmpfs", |
| 110 | + number: 10, |
| 111 | + expected: [11]string{"40", "27", "0:33", "/", "/tmp", "rw,nosuid,nodev", "shared:18 shared:22", "-", "tmpfs", "tmpfs"}, |
| 112 | + }, |
| 113 | + { |
| 114 | + input: "50 27 0:33 / /tmp rw,nosuid,nodev - tmpfs tmpfs", |
| 115 | + number: 10, |
| 116 | + expected: [11]string{"50", "27", "0:33", "/", "/tmp", "rw,nosuid,nodev", "", "-", "tmpfs", "tmpfs"}, |
| 117 | + }, |
| 118 | + |
| 119 | + // Exceptional mount table lines |
| 120 | + { |
| 121 | + input: "328 27 0:73 / /mnt/a rw,relatime shared:206 - tmpfs - rw,inode64", |
| 122 | + number: 11, |
| 123 | + expected: [11]string{"328", "27", "0:73", "/", "/mnt/a", "rw,relatime", "shared:206", "-", "tmpfs", "-", "rw,inode64"}, |
| 124 | + }, |
| 125 | + { |
| 126 | + input: "330 27 0:73 / /mnt/a rw,relatime shared:206 - tmpfs 👾 rw,inode64", |
| 127 | + number: 11, |
| 128 | + expected: [11]string{"330", "27", "0:73", "/", "/mnt/a", "rw,relatime", "shared:206", "-", "tmpfs", "👾", "rw,inode64"}, |
| 129 | + }, |
| 130 | + { |
| 131 | + input: "335 27 0:73 / /mnt/👾 rw,relatime shared:206 - tmpfs 👾 rw,inode64", |
| 132 | + number: 11, |
| 133 | + expected: [11]string{"335", "27", "0:73", "/", "/mnt/👾", "rw,relatime", "shared:206", "-", "tmpfs", "👾", "rw,inode64"}, |
| 134 | + }, |
| 135 | + { |
| 136 | + input: "509 27 0:78 / /mnt/- rw,relatime shared:223 - tmpfs 👾 rw,inode64", |
| 137 | + number: 11, |
| 138 | + expected: [11]string{"509", "27", "0:78", "/", "/mnt/-", "rw,relatime", "shared:223", "-", "tmpfs", "👾", "rw,inode64"}, |
| 139 | + }, |
| 140 | + { |
| 141 | + input: "362 27 0:76 / /mnt/a\\040b rw,relatime shared:215 - tmpfs 👾 rw,inode64", |
| 142 | + number: 11, |
| 143 | + expected: [11]string{"362", "27", "0:76", "/", "/mnt/a b", "rw,relatime", "shared:215", "-", "tmpfs", "👾", "rw,inode64"}, |
| 144 | + }, |
| 145 | + { |
| 146 | + input: "1 2 3:3 / /mnt/\\011 rw shared:7 - tmpfs - rw,inode64", |
| 147 | + number: 11, |
| 148 | + expected: [11]string{"1", "2", "3:3", "/", "/mnt/\t", "rw", "shared:7", "-", "tmpfs", "-", "rw,inode64"}, |
| 149 | + }, |
| 150 | + { |
| 151 | + input: "11 2 3:3 / /mnt/a\\012b rw shared:7 - tmpfs - rw,inode64", |
| 152 | + number: 11, |
| 153 | + expected: [11]string{"11", "2", "3:3", "/", "/mnt/a\nb", "rw", "shared:7", "-", "tmpfs", "-", "rw,inode64"}, |
| 154 | + }, |
| 155 | + { |
| 156 | + input: "111 2 3:3 / /mnt/a\\134b rw shared:7 - tmpfs - rw,inode64", |
| 157 | + number: 11, |
| 158 | + expected: [11]string{"111", "2", "3:3", "/", "/mnt/a\\b", "rw", "shared:7", "-", "tmpfs", "-", "rw,inode64"}, |
| 159 | + }, |
| 160 | + { |
| 161 | + input: "1111 2 3:3 / /mnt/a\\042b rw shared:7 - tmpfs - rw,inode64", |
| 162 | + number: 11, |
| 163 | + expected: [11]string{"1111", "2", "3:3", "/", "/mnt/a\"b", "rw", "shared:7", "-", "tmpfs", "-", "rw,inode64"}, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, tc := range tt { |
| 168 | + nb, actual := parseMountInfoLine(tc.input) |
| 169 | + if nb != tc.number || !reflect.DeepEqual(actual, tc.expected) { |
| 170 | + t.Errorf("\nparseMountInfoLine(%q) == \n(%d) %q, \nexpected (%d) %q", tc.input, nb, actual, tc.number, tc.expected) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments