-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate arguments for
ps
in docker top
Fix #24357 Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
- Loading branch information
1 parent
07dd69d
commit 2539332
Showing
2 changed files
with
148 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//+build !windows | ||
|
||
package daemon | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestContainerTopValidatePSArgs(t *testing.T) { | ||
tests := map[string]bool{ | ||
"ae -o uid=PID": true, | ||
"ae -o \"uid= PID\"": true, // ascii space (0x20) | ||
"ae -o \"uid= PID\"": false, // unicode space (U+2003, 0xe2 0x80 0x83) | ||
"ae o uid=PID": true, | ||
"aeo uid=PID": true, | ||
"ae -O uid=PID": true, | ||
"ae -o pid=PID2 -o uid=PID": true, | ||
"ae -o pid=PID": false, | ||
"ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this | ||
"aeo pid=PID": false, | ||
"ae": false, | ||
"": false, | ||
} | ||
for psArgs, errExpected := range tests { | ||
err := validatePSArgs(psArgs) | ||
t.Logf("tested %q, got err=%v", psArgs, err) | ||
if errExpected && err == nil { | ||
t.Fatalf("expected error, got %v (%q)", err, psArgs) | ||
} | ||
if !errExpected && err != nil { | ||
t.Fatalf("expected nil, got %v (%q)", err, psArgs) | ||
} | ||
} | ||
} | ||
|
||
func TestContainerTopParsePSOutput(t *testing.T) { | ||
tests := []struct { | ||
output []byte | ||
pids []int | ||
errExpected bool | ||
}{ | ||
{[]byte(` PID COMMAND | ||
42 foo | ||
43 bar | ||
100 baz | ||
`), []int{42, 43}, false}, | ||
{[]byte(` UID COMMAND | ||
42 foo | ||
43 bar | ||
100 baz | ||
`), []int{42, 43}, true}, | ||
// unicode space (U+2003, 0xe2 0x80 0x83) | ||
{[]byte(` PID COMMAND | ||
42 foo | ||
43 bar | ||
100 baz | ||
`), []int{42, 43}, true}, | ||
// the first space is U+2003, the second one is ascii. | ||
{[]byte(` PID COMMAND | ||
42 foo | ||
43 bar | ||
100 baz | ||
`), []int{42, 43}, true}, | ||
} | ||
|
||
for _, f := range tests { | ||
_, err := parsePSOutput(f.output, f.pids) | ||
t.Logf("tested %q, got err=%v", string(f.output), err) | ||
if f.errExpected && err == nil { | ||
t.Fatalf("expected error, got %v (%q)", err, string(f.output)) | ||
} | ||
if !f.errExpected && err != nil { | ||
t.Fatalf("expected nil, got %v (%q)", err, string(f.output)) | ||
} | ||
} | ||
} |