-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexportfunctions_test.go
More file actions
125 lines (113 loc) · 2.95 KB
/
exportfunctions_test.go
File metadata and controls
125 lines (113 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ejson2env_test
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/Shopify/ejson2env/v2"
)
func TestExport(t *testing.T) {
t.Parallel()
cases := map[string]struct {
env map[string]string
expected string
expectedQuiet string
}{
"empty": {
env: map[string]string{},
expected: "",
},
"single key": {
env: map[string]string{
"key": "value",
},
expected: "export key=value\n",
expectedQuiet: "key=value\n",
},
"attempt command injection in key": {
env: map[string]string{
"key; touch pwned.txt": "value",
},
expected: "",
expectedQuiet: "",
},
"newline in key": {
env: map[string]string{
"touch pwned.txt;\ndummy": "value",
},
expected: "",
expectedQuiet: "",
},
"attempt command injection in value": {
env: map[string]string{
"key": "value; touch pwned.txt",
},
expected: "export key='value; touch pwned.txt'\n",
expectedQuiet: "key='value; touch pwned.txt'\n",
},
"attempt command injection via control characters": {
env: map[string]string{
"key": "\bvalue; touch pwned.txt",
},
expected: "export key='value; touch pwned.txt'\n",
expectedQuiet: "key='value; touch pwned.txt'\n",
},
"newline in value": {
env: map[string]string{
"key": "value\nnewline",
},
expected: "export key='value\nnewline'\n",
expectedQuiet: "key='value\nnewline'\n",
},
"escaped newlines in value": {
env: map[string]string{
"key": "value\\nnewline",
},
expected: "export key='value\\nnewline'\n",
expectedQuiet: "key='value\\nnewline'\n",
},
}
for label, tc := range cases {
tc := tc
t.Run(label, func(t *testing.T) {
t.Parallel()
t.Run("ExportEnv", func(t *testing.T) {
var buf bytes.Buffer
ejson2env.ExportEnv(&buf, tc.env)
t.Log(buf.String())
if buf.String() != tc.expected {
t.Errorf("expected %q, got %q", tc.expected, buf.String())
}
})
t.Run("ExportQuiet", func(t *testing.T) {
var buf bytes.Buffer
ejson2env.ExportQuiet(&buf, tc.env)
t.Log(buf.String())
if buf.String() != tc.expectedQuiet {
t.Errorf("expected %q, got %q", tc.expectedQuiet, buf.String())
}
})
})
}
}
// TestExportEnvMultipleKeys tests exporting multiple environment variables
// with map key order independence
func TestExportEnvMultipleKeys(t *testing.T) {
fmt.Println("===== RUNNING TestExportEnvMultipleKeys TEST =====")
t.Parallel()
env := map[string]string{
"key1": "value 1",
"key2": "value ' with some \" quotes and emoji 🐈",
}
var buf bytes.Buffer
ejson2env.ExportEnv(&buf, env)
output := buf.String()
t.Log(output)
// Check for each expected line individually
if !strings.Contains(output, "export key1='value 1'") {
t.Errorf("output missing 'export key1='value 1''")
}
if !strings.Contains(output, "export key2='value '\"'\"' with some \" quotes and emoji 🐈'") {
t.Errorf("output missing key2 with proper escaping")
}
}