| 1 | package env |
| 2 | |
| 3 | import ( |
| 4 | "math/rand" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/sylvinus/agent-vm/internal/bashref" |
| 9 | ) |
| 10 | |
| 11 | var corpus = []string{ |
| 12 | "", |
| 13 | "A=1\n", |
| 14 | "A=1", |
| 15 | "export A=1\nB=2\n", |
| 16 | " export A='x y'\n", |
| 17 | "A='multi\nline\nB=inside'\nB=2\n", |
| 18 | "A='open\nB=2\n", |
| 19 | "A=\"plain\"\nB=\"$HOME\"\n", |
| 20 | "A=x # comment\nB=y#notcomment\n", |
| 21 | "A=x;rm -rf /\n", |
| 22 | "A=$(id)\n", |
| 23 | "A=$HOME\n", |
| 24 | "B=1\nA=x$y\n", |
| 25 | "A=~/x\n", |
| 26 | "# just a comment\n\n \nA=1\r\nB='crlf'\r\n", |
| 27 | "A=1\nA=2\n", |
| 28 | "A='it'\"'\"'s'\n", |
| 29 | "not an assignment\nA=1\n", |
| 30 | "A=x\\\ny\n", |
| 31 | "A= 1\n", |
| 32 | "A=\tB\n", |
| 33 | "1A=x\n", |
| 34 | "A=x y\n", |
| 35 | "A='x' # c\n", |
| 36 | "A=$'esc\\'aped'\nB=after\n", |
| 37 | "A=\"dq \\\" esc\"\nB=after\n", |
| 38 | "A='a'\"b\"c\n", |
| 39 | "export\tA=1\nexport B\n", |
| 40 | "A=x # ' not a quote\nB=1\n", |
| 41 | "A='x # not a comment'\nB=1\n", |
| 42 | "A=café\nB='naïve'\n", |
| 43 | "A=\x92\xff\n", |
| 44 | } |
| 45 | |
| 46 | func bashRead(t *testing.T, content, key string) (string, int) { |
| 47 | r := bashref.Script(t, "", nil, `printf '%s' "$1" | _agent_vm_env_read - "$2"`, content, key) |
| 48 | return r.Stdout, r.Code |
| 49 | } |
| 50 | |
| 51 | func TestReadBash(t *testing.T) { |
| 52 | for _, c := range corpus { |
| 53 | for _, k := range []string{"A", "B", "C"} { |
| 54 | v, st := Lookup(c, k) |
| 55 | bv, bst := bashRead(t, c, k) |
| 56 | if v != bv || st != bst { |
| 57 | t.Errorf("Read(%q, %s) = %q %d, bash %q %d", c, k, v, st, bv, bst) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestNamesDropBash(t *testing.T) { |
| 64 | for _, c := range corpus { |
| 65 | r := bashref.Script(t, "", nil, `printf '%s' "$1" | _agent_vm_env_lines list -`, c) |
| 66 | if got := strings.Join(Names(c), "\n"); got != strings.TrimSuffix(r.Stdout, "\n") { |
| 67 | t.Errorf("Names(%q) = %q, bash %q", c, got, r.Stdout) |
| 68 | } |
| 69 | for _, k := range []string{"A", "B"} { |
| 70 | r := bashref.Script(t, "", nil, `printf '%s' "$1" | _agent_vm_env_lines drop - "$2"`, c, k) |
| 71 | if got := Drop(c, k); got != r.Stdout { |
| 72 | t.Errorf("Drop(%q, %s) = %q, bash %q", c, k, got, r.Stdout) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | func TestReadBashRandom(t *testing.T) { |
| 80 | if testing.Short() { |
| 81 | t.Skip() |
| 82 | } |
| 83 | alphabet := []string{"A=", "B=", "export ", "'", "\"", "$", "\\", "#", " ", "\t", "\n", "x", ";", "\r", "~", "$'", "="} |
| 84 | rnd := rand.New(rand.NewSource(1)) |
| 85 | for n := 0; n < 300; n++ { |
| 86 | var b strings.Builder |
| 87 | for i := rnd.Intn(14); i >= 0; i-- { |
| 88 | b.WriteString(alphabet[rnd.Intn(len(alphabet))]) |
| 89 | } |
| 90 | c := b.String() |
| 91 | v, st := Lookup(c, "A") |
| 92 | bv, bst := bashRead(t, c, "A") |
| 93 | if v != bv || st != bst { |
| 94 | t.Errorf("Read(%q, A) = %q %d, bash %q %d", c, v, st, bv, bst) |
| 95 | } |
| 96 | r := bashref.Script(t, "", nil, `printf '%s' "$1" | _agent_vm_env_lines drop - A`, c) |
| 97 | if got := Drop(c, "A"); got != r.Stdout { |
| 98 | t.Errorf("Drop(%q, A) = %q, bash %q", c, got, r.Stdout) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestSQEscapeBash(t *testing.T) { |
| 104 | for _, s := range []string{"", "plain", "O'Brien", "''", "a'b'c", "new\nline"} { |
| 105 | r := bashref.Run(t, "", nil, "_agent_vm_sq_escape", s) |
| 106 | if SQEscape(s) != r.Stdout { |
| 107 | t.Errorf("SQEscape(%q) = %q, bash %q", s, SQEscape(s), r.Stdout) |
| 108 | } |
| 109 | } |
| 110 | } |