deno.land / std@0.224.0 / ini / parse_test.ts

نووسراو ببینە
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { IniMap, parse, type ParseOptions } from "./mod.ts";import { assert, assertEquals, assertStrictEquals, assertThrows,} from "../assert/mod.ts";
function assertValidParse( text: string, expected: unknown, options?: ParseOptions,) { assertEquals(parse(text, options), expected);}
function assertInvalidParse( text: string, // deno-lint-ignore no-explicit-any ErrorClass: new (...args: any[]) => Error, msgIncludes?: string, options?: ParseOptions,) { assertThrows( () => parse(text, options), ErrorClass, msgIncludes, );}
Deno.test({ name: "parse()", fn() { assertValidParse(`a=100`, { a: 100 }, { reviver: (_, value) => Number(value), }); assertValidParse(`a=b\n[section]\nc=d`, { a: "b", section: { c: "d" } }); },});
Deno.test({ name: "parse() with comment", fn() { assertValidParse(`#comment\na=b`, { a: "b" }); assertValidParse(`;comment\ra=b`, { a: "b" }); assertValidParse(`//comment\n\ra=b`, { a: "b" }); },});
Deno.test({ name: "parse() special character", fn() { assertValidParse(`a=👪`, { a: "👪" }); assertValidParse(`a=🦕`, { a: "🦕" }); assertValidParse( `a=\u543e\u8f29\u306f\u732b\u3067\u3042\u308b\u3002`, { a: "\u543e\u8f29\u306f\u732b\u3067\u3042\u308b\u3002" }, ); },});
Deno.test({ name: "parse() throws error with correct messages", fn() { assertInvalidParse( `:::::`, SyntaxError, "Unexpected token : in INI at line 1", ); assertInvalidParse( `[`, SyntaxError, "Unexpected end of INI section at line 1", ); assertInvalidParse( `[]`, SyntaxError, "Unexpected empty section name at line 1", ); assertInvalidParse( `=100`, SyntaxError, "Unexpected empty key name at line 1", ); },});
Deno.test({ name: "parse() handles __proto__", fn() { // The result of JSON.parse and the result of INI.parse should match const json = JSON.parse('{"__proto__": 100}'); const ini = parse("__proto__ = 100", { reviver: (key, value) => { if (key === "__proto__") return Number(value); }, }); assertEquals(ini, json); assertEquals((ini as Record<string, number>).__proto__, 100); assertEquals((ini as Record<string, string>).__proto__, json.__proto__); assertStrictEquals(Object.getPrototypeOf(ini), Object.prototype); assertStrictEquals( Object.getPrototypeOf(ini), Object.getPrototypeOf(json), ); },});
Deno.test({ name: "parse() duplicates object key", fn() { // The result of JSON.parse and the result of INI.parse should match const json = JSON.parse('{"aaa": 0, "aaa": 1}'); const ini = parse("aaa=0\naaa=1", { reviver: (_, value) => Number(value), }); assertEquals(ini, { aaa: 1 }); assertEquals(ini, json); assertEquals( IniMap.from("#comment\naaa=0\naaa=1", { deduplicate: true }) .toString(), "#comment\naaa=1", ); },});
Deno.test({ name: "parse() does not parse other than strings", fn() { assertInvalidParse( // deno-lint-ignore no-explicit-any undefined as any, SyntaxError, "Unexpected token undefined in INI at line 0", ); assertInvalidParse( // deno-lint-ignore no-explicit-any 0 as any, SyntaxError, "Unexpected token 0 in INI at line 0", ); },});
Deno.test({ name: "parse() uses Object.defineProperty when setting object property", async fn() { // Tests if the value is set using `Object.defineProperty(target, key, {value})` // instead of `target[key] = value` when parsing the object. // This makes a difference in behavior when __proto__ is set in Node.js and browsers. // Using `Object.defineProperty` avoids prototype pollution in Node.js and browsers. // reference: https://github.com/advisories/GHSA-9c47-m6qq-7p4h (CVE-2022-46175)
const testCode = ` Object.defineProperty(Object.prototype, "__proto__", { set() { throw new Error("Don't try to set the value directly to the key __proto__.") } }); import { parse } from "${import.meta.resolve("./parse.ts")}"; parse('[__proto__]\\nisAdmin = true'); `; const command = new Deno.Command(Deno.execPath(), { stdout: "inherit", stderr: "inherit", args: ["eval", "--no-lock", testCode], }); const { success } = await command.output(); assert(success); },});
std

Version Info

Tagged at
3 weeks ago