| 1 |
package crit |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/checkpoint-restore/go-criu/v7/crit/images/pstree" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
testImgsDir = "test-imgs" |
| 15 |
) |
| 16 |
|
| 17 |
func TestNewMemoryReader(t *testing.T) { |
| 18 |
pid, err := getTestImgPID() |
| 19 |
if err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} |
| 22 |
|
| 23 |
testCases := []struct { |
| 24 |
name string |
| 25 |
dir string |
| 26 |
pid uint32 |
| 27 |
pageSize int |
| 28 |
expectedError error |
| 29 |
}{ |
| 30 |
{ |
| 31 |
name: "Page size is 0", |
| 32 |
dir: testImgsDir, |
| 33 |
pid: pid, |
| 34 |
pageSize: 0, |
| 35 |
expectedError: nil, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "Invalid page size", |
| 39 |
dir: testImgsDir, |
| 40 |
pid: pid, |
| 41 |
pageSize: 4000, |
| 42 |
expectedError: errors.New("page size should be a positive power of 2"), |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "Invalid test-imgs directory", |
| 46 |
dir: "no test directory", |
| 47 |
pid: pid, |
| 48 |
expectedError: errors.New("no such file or directory"), |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "Valid test-imgs directory, pid and page size", |
| 52 |
dir: testImgsDir, |
| 53 |
pid: pid, |
| 54 |
pageSize: 4096, |
| 55 |
expectedError: nil, |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, tc := range testCases { |
| 60 |
t.Run(tc.name, func(t *testing.T) { |
| 61 |
mr, err := NewMemoryReader(tc.dir, tc.pid, tc.pageSize) |
| 62 |
if err != nil && !strings.Contains(err.Error(), tc.expectedError.Error()) { |
| 63 |
t.Errorf("Expected error: %v, got error: %v", tc.expectedError, err) |
| 64 |
} |
| 65 |
|
| 66 |
if mr == nil && tc.expectedError == nil { |
| 67 |
t.Errorf("MemoryReader creation failed for checkpoint directory: %s and pid: %d", tc.dir, tc.pid) |
| 68 |
} |
| 69 |
}) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// TestNewMemoryReader test GetMempages method of MemoryReader. |
| 74 |
func TestGetMemPages(t *testing.T) { |
| 75 |
type testcase struct { |
| 76 |
name string |
| 77 |
mr *MemoryReader |
| 78 |
start uint64 |
| 79 |
end uint64 |
| 80 |
expectedError error |
| 81 |
} |
| 82 |
|
| 83 |
pid, err := getTestImgPID() |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
|
| 88 |
// Create a temporary empty memory pages file for testing |
| 89 |
tmpFilePath := filepath.Join(os.TempDir(), "pages-0.img") |
| 90 |
tmpFile, err := os.Create(tmpFilePath) |
| 91 |
if err != nil { |
| 92 |
t.Fatal(err) |
| 93 |
} |
| 94 |
|
| 95 |
defer func() { |
| 96 |
tmpFile.Close() |
| 97 |
if err := os.Remove(tmpFile.Name()); err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
}() |
| 101 |
|
| 102 |
mr, err := NewMemoryReader(testImgsDir, pid, sysPageSize) |
| 103 |
if err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
|
| 107 |
testCases := []testcase{ |
| 108 |
{ |
| 109 |
name: "Zero memory area size", |
| 110 |
mr: &MemoryReader{ |
| 111 |
checkpointDir: testImgsDir, |
| 112 |
pid: pid, |
| 113 |
pageSize: sysPageSize, |
| 114 |
pagesID: mr.pagesID, |
| 115 |
pagemapEntries: mr.GetPagemapEntries(), |
| 116 |
}, |
| 117 |
start: 0, |
| 118 |
end: 0, |
| 119 |
expectedError: nil, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "Valid pagemap entry 1", |
| 123 |
mr: &MemoryReader{ |
| 124 |
checkpointDir: testImgsDir, |
| 125 |
pid: pid, |
| 126 |
pageSize: sysPageSize, |
| 127 |
pagesID: mr.pagesID, |
| 128 |
pagemapEntries: mr.GetPagemapEntries(), |
| 129 |
}, |
| 130 |
start: mr.pagemapEntries[0].GetVaddr(), |
| 131 |
end: mr.pagemapEntries[0].GetVaddr() + uint64(uint32(sysPageSize)*mr.pagemapEntries[0].GetNrPages()), |
| 132 |
expectedError: nil, |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "Valid pagemap entry 2", |
| 136 |
mr: &MemoryReader{ |
| 137 |
checkpointDir: testImgsDir, |
| 138 |
pid: pid, |
| 139 |
pageSize: sysPageSize, |
| 140 |
pagesID: mr.pagesID, |
| 141 |
pagemapEntries: mr.GetPagemapEntries(), |
| 142 |
}, |
| 143 |
start: mr.pagemapEntries[1].GetVaddr(), |
| 144 |
end: mr.pagemapEntries[1].GetVaddr() + uint64(uint32(sysPageSize)*mr.pagemapEntries[1].GetNrPages()), |
| 145 |
expectedError: nil, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "Invalid pages file", |
| 149 |
mr: &MemoryReader{ |
| 150 |
checkpointDir: testImgsDir, |
| 151 |
pid: pid, |
| 152 |
pageSize: sysPageSize, |
| 153 |
pagesID: mr.pagesID + 1, |
| 154 |
pagemapEntries: mr.GetPagemapEntries(), |
| 155 |
}, |
| 156 |
start: mr.pagemapEntries[0].GetVaddr(), |
| 157 |
end: mr.pagemapEntries[0].GetVaddr() + uint64(uint32(sysPageSize)*mr.pagemapEntries[0].GetNrPages()), |
| 158 |
expectedError: errors.New("no such file or directory"), |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "Empty pages file", |
| 162 |
mr: &MemoryReader{ |
| 163 |
checkpointDir: os.TempDir(), |
| 164 |
pid: pid, |
| 165 |
pageSize: sysPageSize, |
| 166 |
pagesID: 0, |
| 167 |
pagemapEntries: mr.GetPagemapEntries(), |
| 168 |
}, |
| 169 |
start: mr.pagemapEntries[1].GetVaddr(), |
| 170 |
end: mr.pagemapEntries[1].GetVaddr() + uint64(uint32(sysPageSize)*mr.pagemapEntries[1].GetNrPages()), |
| 171 |
expectedError: errors.New("EOF"), |
| 172 |
}, |
| 173 |
} |
| 174 |
|
| 175 |
for _, tc := range testCases { |
| 176 |
t.Run(tc.name, func(t *testing.T) { |
| 177 |
buff, err := tc.mr.GetMemPages(tc.start, tc.end) |
| 178 |
if err != nil && tc.expectedError != nil { |
| 179 |
if !strings.Contains(err.Error(), tc.expectedError.Error()) { |
| 180 |
t.Errorf("Expected error: %v, got error: %v", tc.expectedError, err) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if tc.expectedError == nil && buff == nil { |
| 185 |
t.Errorf("Returned memory chunk is expected to be non-empty") |
| 186 |
} |
| 187 |
}) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func TestGetPsArgsAndEnvVars(t *testing.T) { |
| 192 |
pid, err := getTestImgPID() |
| 193 |
if err != nil { |
| 194 |
t.Fatal(err) |
| 195 |
} |
| 196 |
|
| 197 |
mr, err := NewMemoryReader(testImgsDir, pid, sysPageSize) |
| 198 |
if err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
|
| 202 |
testCases := []struct { |
| 203 |
name string |
| 204 |
expectedError error |
| 205 |
mr *MemoryReader |
| 206 |
}{ |
| 207 |
{ |
| 208 |
name: "wrong PID", |
| 209 |
expectedError: errors.New("no such file or directory"), |
| 210 |
mr: &MemoryReader{ |
| 211 |
checkpointDir: testImgsDir, |
| 212 |
pid: 0, |
| 213 |
}, |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "valid arguments and environment variables", |
| 217 |
expectedError: nil, |
| 218 |
mr: mr, |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
for _, tc := range testCases { |
| 223 |
t.Run(tc.name, func(t *testing.T) { |
| 224 |
args, err := tc.mr.GetPsArgs() |
| 225 |
if err != nil && tc.expectedError != nil { |
| 226 |
if !strings.Contains(err.Error(), tc.expectedError.Error()) { |
| 227 |
t.Errorf("Expected error: %v, got error: %v", tc.expectedError, err) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if tc.expectedError == nil && args == nil { |
| 232 |
t.Errorf("Expected non-nil arguments, got nil") |
| 233 |
} |
| 234 |
}) |
| 235 |
|
| 236 |
t.Run(tc.name, func(t *testing.T) { |
| 237 |
envVars, err := tc.mr.GetPsEnvVars() |
| 238 |
if err != nil && tc.expectedError != nil { |
| 239 |
if !strings.Contains(err.Error(), tc.expectedError.Error()) { |
| 240 |
t.Errorf("Expected error: %v, got error: %v", tc.expectedError, err) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if tc.expectedError == nil && envVars == nil { |
| 245 |
t.Errorf("Expected non-nil environment variables, got nil") |
| 246 |
} |
| 247 |
}) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestSearchPattern(t *testing.T) { |
| 252 |
pid, err := getTestImgPID() |
| 253 |
if err != nil { |
| 254 |
t.Fatal(err) |
| 255 |
} |
| 256 |
|
| 257 |
mr, err := NewMemoryReader(testImgsDir, pid, sysPageSize) |
| 258 |
if err != nil { |
| 259 |
t.Fatal(err) |
| 260 |
} |
| 261 |
|
| 262 |
testCases := []struct { |
| 263 |
name string |
| 264 |
pattern string |
| 265 |
context int |
| 266 |
escapeRegExpCharacters bool |
| 267 |
shouldMatch bool |
| 268 |
expectedError error |
| 269 |
}{ |
| 270 |
{ |
| 271 |
name: "PATH environment variable", |
| 272 |
pattern: "PATH=", |
| 273 |
shouldMatch: true, |
| 274 |
}, |
| 275 |
{ |
| 276 |
name: "PATH environment variable regex", |
| 277 |
pattern: `\bPATH=([^\s]+)\b`, |
| 278 |
shouldMatch: true, |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "PATH environment variable regex with 10 bytes context", |
| 282 |
pattern: `\bPATH=([^\s]+)\b`, |
| 283 |
context: 10, |
| 284 |
shouldMatch: true, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "PATH environment variable regex with a negative context", |
| 288 |
pattern: `\bPATH=([^\s]+)\b`, |
| 289 |
context: -1, |
| 290 |
expectedError: errors.New("context size cannot be negative"), |
| 291 |
}, |
| 292 |
{ |
| 293 |
name: "PATH environment variable regex with a large context", |
| 294 |
pattern: `\bPATH=([^\s]+)\b`, |
| 295 |
context: 100000, |
| 296 |
shouldMatch: true, |
| 297 |
}, |
| 298 |
{ |
| 299 |
name: "Non-existent pattern", |
| 300 |
pattern: "NON_EXISTENT_PATTERN", |
| 301 |
}, |
| 302 |
{ |
| 303 |
name: "PASSWORD environment variable value as regex", |
| 304 |
pattern: "123 Hello.*?", |
| 305 |
shouldMatch: true, |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "PASSWORD environment variable value with regex metacharacters to escape", |
| 309 |
pattern: `123 Hello.*?[^]@WORLD(|x)`, |
| 310 |
escapeRegExpCharacters: true, |
| 311 |
shouldMatch: true, |
| 312 |
}, |
| 313 |
} |
| 314 |
|
| 315 |
for _, tc := range testCases { |
| 316 |
t.Run(tc.name, func(t *testing.T) { |
| 317 |
matches, err := mr.SearchPattern(tc.pattern, tc.escapeRegExpCharacters, tc.context, 0) |
| 318 |
if err != nil && tc.expectedError == nil { |
| 319 |
t.Errorf("Unexpected error for pattern %s: %v", tc.pattern, err) |
| 320 |
} else if err == nil && tc.expectedError != nil { |
| 321 |
t.Errorf("Expected error for pattern %s: %v", tc.pattern, tc.expectedError) |
| 322 |
} |
| 323 |
|
| 324 |
if tc.shouldMatch && len(matches) == 0 { |
| 325 |
t.Errorf("Expected to find a match for pattern \"%s\"", tc.pattern) |
| 326 |
} else if !tc.shouldMatch && len(matches) > 0 { |
| 327 |
t.Errorf("Expected not to find any match for pattern \"%s\"", tc.pattern) |
| 328 |
} |
| 329 |
|
| 330 |
for _, match := range matches { |
| 331 |
content, err := mr.GetMemPages(match.Vaddr, match.Vaddr+uint64(match.Length)) |
| 332 |
if err != nil { |
| 333 |
t.Fatalf("Failed to get memory pages: %v", err) |
| 334 |
} |
| 335 |
|
| 336 |
buff := content.Bytes() |
| 337 |
for i := range buff { |
| 338 |
if buff[i] < 32 || buff[i] >= 127 { |
| 339 |
buff[i] = 0x3F |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if !strings.Contains(match.Match, content.String()) { |
| 344 |
t.Errorf("Expected to find %s in matched pattern %s", content.String(), match.Match) |
| 345 |
} |
| 346 |
} |
| 347 |
}) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// helper function to get the PID from the test-imgs directory |
| 352 |
func getTestImgPID() (uint32, error) { |
| 353 |
psTreeImg, err := getImg(filepath.Join(testImgsDir, "pstree.img"), &pstree.PstreeEntry{}) |
| 354 |
if err != nil { |
| 355 |
return 0, err |
| 356 |
} |
| 357 |
process := psTreeImg.Entries[0].Message.(*pstree.PstreeEntry) |
| 358 |
|
| 359 |
return process.GetPgid(), nil |
| 360 |
} |