-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpk_test.gleam
More file actions
300 lines (277 loc) · 9.58 KB
/
Copy pathmpk_test.gleam
File metadata and controls
300 lines (277 loc) · 9.58 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//// Tests mpk behavior for mxpak.
////
import gleam/list
import gleam/option
import gleam/string
import gleeunit/should
import mxpak/error
import mxpak/mpk/metadata
import mxpak/mpk/xml
import mxpak/mpk/zip
import mxpak/widget
import simplifile
/// Verifies zip extract invalid binary behavior.
pub fn zip_extract_invalid_binary_test() -> Nil {
zip.extract(<<"not a zip":utf8>>)
|> should.be_error
|> error.message
|> string.is_empty
|> should.be_false
Nil
}
/// Verifies zip extract file from invalid behavior.
pub fn zip_extract_file_from_invalid_test() -> Nil {
zip.extract_file(<<"bad":utf8>>, "foo.txt")
|> should.be_error
Nil
}
/// Verifies OTP sanitizes parent-directory entries to in-root names.
pub fn zip_extract_normalizes_parent_directory_entries_test() -> Nil {
let entries = zip.extract(parent_directory_zip_binary()) |> should.be_ok
entries
|> list.first
|> should.equal(
Ok(zip.ZipEntry(name: "evil.txt", content: <<"escaped":utf8>>)),
)
Nil
}
/// Verifies backslash archive entries are rejected before extraction.
pub fn zip_extract_rejects_backslash_entries_test() -> Nil {
let error_value = zip.extract(backslash_zip_binary()) |> should.be_error
error_value
|> error.message
|> string.contains("..\\evil.txt")
|> should.be_true
Nil
}
/// Verifies nested archive entries remain usable.
pub fn zip_extract_allows_nested_entries_test() -> Nil {
let entries = zip.extract(valid_zip_binary()) |> should.be_ok
entries
|> list.first
|> should.equal(
Ok(
zip.ZipEntry(name: "com/example/Widget.mjs", content: <<
"export default 1\n":utf8,
>>),
),
)
Nil
}
/// Verifies truncated archives fail without crashing.
pub fn zip_extract_truncated_archive_test() -> Nil {
zip.extract(truncated_zip_binary())
|> should.be_error
Nil
}
// A stored ZIP archive containing one ../evil.txt entry.
pub fn parse_widget_name_test() -> Nil {
let xml = "<widget><name>DataGrid</name></widget>"
xml.parse_widget_name(xml)
|> should.equal(Ok("DataGrid"))
Nil
}
/// Verifies parse widget name missing behavior.
pub fn parse_widget_name_missing_test() -> Nil {
xml.parse_widget_name("<widget></widget>")
|> should.be_error
Nil
}
/// Verifies extract widget file paths behavior.
pub fn extract_widget_file_paths_test() -> Nil {
let package_xml =
"<package><clientModule><widgetFile path=\"com/example/Widget.mjs\"/><widgetFile path=\"com/example/Other.mjs\"/></clientModule></package>"
let paths =
xml.extract_widget_file_paths(package_xml)
|> should.be_ok
{ paths == ["com/example/Widget.mjs", "com/example/Other.mjs"] }
|> should.be_true
Nil
}
/// Verifies extract widget file paths empty behavior.
pub fn extract_widget_file_paths_empty_test() -> Nil {
xml.extract_widget_file_paths("<package></package>")
|> should.be_ok
|> should.equal([])
Nil
}
/// Verifies parse properties behavior.
pub fn parse_properties_test() -> Nil {
let widget_xml =
"<widget><property key=\"dataSource\" required=\"true\"/><property key=\"label\" required=\"false\">desc</property></widget>"
let props =
xml.parse_properties(widget_xml)
|> should.be_ok
{
case props {
[first, second] -> {
first.key == "dataSource"
&& first.required == True
&& second.key == "label"
&& second.required == False
}
_ -> False
}
}
|> should.be_true
Nil
}
/// Verifies parse properties no required behavior.
pub fn parse_properties_no_required_test() -> Nil {
let widget_xml = "<widget><property key=\"value\"/></widget>"
let props =
xml.parse_properties(widget_xml)
|> should.be_ok
list.length(props)
|> should.equal(1)
let prop =
list.first(props)
|> should.be_ok
{ prop.key == "value" }
|> should.be_true
prop.required
|> should.be_false
Nil
}
/// Verifies to safe identifier behavior.
pub fn to_safe_identifier_test() -> Nil {
{ xml.to_safe_identifier("Progress Bar") == "ProgressBar" }
|> should.be_true
{ xml.to_safe_identifier("Data-Grid") == "DataGrid" }
|> should.be_true
{ xml.to_safe_identifier("Normal") == "Normal" }
|> should.be_true
Nil
}
/// Verifies to module file name behavior.
pub fn to_module_file_name_test() -> Nil {
{ xml.to_module_file_name("Progress Bar") == "progress_bar" }
|> should.be_true
{ xml.to_module_file_name("DataGrid") == "data_grid" }
|> should.be_true
Nil
}
/// Verifies to snake case behavior.
pub fn to_snake_case_test() -> Nil {
{ xml.to_snake_case("camelCase") == "camel_case" }
|> should.be_true
{ xml.to_snake_case("HTTPClient") == "http_client" }
|> should.be_true
{ xml.to_snake_case("simpleTest") == "simple_test" }
|> should.be_true
Nil
}
/// Verifies to gleam var behavior.
pub fn to_gleam_var_test() -> Nil {
{ xml.to_gleam_var("dataSource") == "data_source" }
|> should.be_true
{ xml.to_gleam_var("type") == "type_" }
|> should.be_true
{ xml.to_gleam_var("import") == "import_" }
|> should.be_true
{ xml.to_gleam_var("normalName") == "normal_name" }
|> should.be_true
Nil
}
/// Verifies metadata write read roundtrip behavior.
pub fn metadata_write_read_roundtrip_test() -> Nil {
let dir = "build/test_tmp/meta_rt"
simplifile.create_directory_all(dir)
|> should.be_ok
let path = dir <> "/meta.toml"
metadata.write(path, "2.0.0", option.Some(42), widget.Pluggable)
|> should.be_ok
let config =
metadata.read(path)
|> should.be_ok
{ config.version == "2.0.0" }
|> should.be_true
config.id
|> should.equal(option.Some(42))
config.kind
|> should.equal(widget.Pluggable)
simplifile.delete(dir)
|> should.be_ok
Nil
}
/// Verifies metadata classic roundtrip behavior.
pub fn metadata_classic_roundtrip_test() -> Nil {
let dir = "build/test_tmp/meta_rt2"
simplifile.create_directory_all(dir)
|> should.be_ok
let path = dir <> "/meta.toml"
metadata.write(path, "1.0.0", option.None, widget.Classic)
|> should.be_ok
let config =
metadata.read(path)
|> should.be_ok
{ config.version == "1.0.0" }
|> should.be_true
config.id
|> should.be_none
config.kind
|> should.equal(widget.Classic)
simplifile.delete(dir)
|> should.be_ok
Nil
}
fn parent_directory_zip_binary() -> BitArray {
<<
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23,
0x5d, 0x5b, 0x0c, 0xf8, 0x92, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e, 0x2f, 0x65, 0x76, 0x69, 0x6c, 0x2e, 0x74,
0x78, 0x74, 0x65, 0x73, 0x63, 0x61, 0x70, 0x65, 0x64, 0x50, 0x4b, 0x01, 0x02,
0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23, 0x5d, 0x5b,
0x0c, 0xf8, 0x92, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00,
0x00, 0x00, 0x00, 0x2e, 0x2e, 0x2f, 0x65, 0x76, 0x69, 0x6c, 0x2e, 0x74, 0x78,
0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x39, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
>>
}
// A stored ZIP archive containing one ..\evil.txt entry.
fn backslash_zip_binary() -> BitArray {
<<
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x4d, 0x23,
0x5d, 0x5b, 0x0c, 0xf8, 0x92, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e, 0x5c, 0x65, 0x76, 0x69, 0x6c, 0x2e, 0x74,
0x78, 0x74, 0x65, 0x73, 0x63, 0x61, 0x70, 0x65, 0x64, 0x50, 0x4b, 0x01, 0x02,
0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x4d, 0x23, 0x5d, 0x5b,
0x0c, 0xf8, 0x92, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00,
0x00, 0x00, 0x00, 0x2e, 0x2e, 0x5c, 0x65, 0x76, 0x69, 0x6c, 0x2e, 0x74, 0x78,
0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x39, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
>>
}
// A stored ZIP archive containing one nested widget entry.
fn valid_zip_binary() -> BitArray {
<<
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23,
0x5d, 0xbc, 0x6c, 0xb4, 0x4d, 0x11, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
0x16, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6d, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2f, 0x57, 0x69, 0x64, 0x67, 0x65, 0x74, 0x2e, 0x6d, 0x6a, 0x73,
0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c,
0x74, 0x20, 0x31, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00,
0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23, 0x5d, 0xbc, 0x6c, 0xb4, 0x4d, 0x11, 0x00,
0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x63, 0x6f,
0x6d, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x57, 0x69, 0x64,
0x67, 0x65, 0x74, 0x2e, 0x6d, 0x6a, 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x44, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00,
0x00, 0x00, 0x00,
>>
}
fn truncated_zip_binary() -> BitArray {
<<
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23,
0x5d, 0xbc, 0x6c, 0xb4, 0x4d, 0x11, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
0x16, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6d, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2f, 0x57, 0x69, 0x64, 0x67, 0x65, 0x74, 0x2e, 0x6d, 0x6a, 0x73,
0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c,
0x74, 0x20, 0x31, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00,
0x00, 0x00, 0x00, 0xf4, 0x4c, 0x23, 0x5d, 0xbc, 0x6c, 0xb4, 0x4d, 0x11, 0x00,
0x00, 0x00,
>>
}
/// Verifies parse widget name behavior.