Debian Codegraph @

npm-normalize-package-bin/test/object.js

raw
1 const normalize = require('../')
2 const t = require('tap')
3
4 t.test('benign object', async t => {
5 // just clean up the ./ in the targets and remove anything weird
6 const pkg = { name: 'hello',
7 version: 'world',
8 bin: {
9 y: './x/y',
10 z: './y/z',
11 a: './a',
12 } }
13 const expect = { name: 'hello',
14 version: 'world',
15 bin: {
16 y: 'x/y',
17 z: 'y/z',
18 a: 'a',
19 } }
20 t.strictSame(normalize(pkg), expect)
21 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
22 })
23
24 t.test('empty and non-string targets', async t => {
25 // just clean up the ./ in the targets and remove anything weird
26 const pkg = { name: 'hello',
27 version: 'world',
28 bin: {
29 z: './././',
30 y: '',
31 './x': 'x.js',
32 re: /asdf/,
33 foo: { bar: 'baz' },
34 false: false,
35 null: null,
36 array: [1, 2, 3],
37 func: function () {},
38 } }
39 const expect = { name: 'hello',
40 version: 'world',
41 bin: {
42 x: 'x.js',
43 } }
44 t.strictSame(normalize(pkg), expect)
45 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
46 })
47
48 t.test('slashy object', async t => {
49 const pkg = { name: 'hello',
50 version: 'world',
51 bin: {
52 '/path/foo': '/etc/passwd',
53 bar: '/etc/passwd',
54 '/etc/glorb/baz': '/etc/passwd',
55 '/etc/passwd:/bin/usr/exec': '/etc/passwd',
56 } }
57 const expect = {
58 name: 'hello',
59 version: 'world',
60 bin: {
61 foo: 'etc/passwd',
62 bar: 'etc/passwd',
63 baz: 'etc/passwd',
64 exec: 'etc/passwd',
65 },
66 }
67 t.strictSame(normalize(pkg), expect)
68 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
69 })
70
71 t.test('dotty object', async t => {
72 const pkg = {
73 name: 'hello',
74 version: 'world',
75 bin: {
76 nodots: '../../../../etc/passwd',
77 '../../../../../../dots': '../../../../etc/passwd',
78 '.././../\\./..//C:\\./': 'this is removed',
79 '.././../\\./..//C:\\/': 'super safe programming language',
80 '.././../\\./..//C:\\x\\y\\z/': 'xyz',
81 } }
82 const expect = { name: 'hello',
83 version: 'world',
84 bin: {
85 nodots: 'etc/passwd',
86 dots: 'etc/passwd',
87 C: 'super safe programming language',
88 z: 'xyz',
89 } }
90 t.strictSame(normalize(pkg), expect)
91 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
92 })
93
94 t.test('weird object', async t => {
95 const pkg = { name: 'hello', version: 'world', bin: /asdf/ }
96 const expect = { name: 'hello', version: 'world' }
97 t.strictSame(normalize(pkg), expect)
98 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
99 })
100
101 t.test('oddball keys', async t => {
102 const pkg = {
103 bin: {
104 '~': 'target',
105 '£': 'target',
106 ζ: 'target',
107 ぎ: 'target',
108 操: 'target',
109 '🎱': 'target',
110 '💎': 'target',
111 '💸': 'target',
112 '🦉': 'target',
113 'сheck-dom': 'target',
114 Ωpm: 'target',
115 ζλ: 'target',
116 мга: 'target',
117 пше: 'target',
118 тзч: 'target',
119 тзь: 'target',
120 нфкт: 'target',
121 ссср: 'target',
122 君の名は: 'target',
123 },
124 }
125
126 const expect = {
127 bin: {
128 '~': 'target',
129 '£': 'target',
130 ζ: 'target',
131 ぎ: 'target',
132 操: 'target',
133 '🎱': 'target',
134 '💎': 'target',
135 '💸': 'target',
136 '🦉': 'target',
137 'сheck-dom': 'target',
138 Ωpm: 'target',
139 ζλ: 'target',
140 мга: 'target',
141 пше: 'target',
142 тзч: 'target',
143 тзь: 'target',
144 нфкт: 'target',
145 ссср: 'target',
146 君の名は: 'target',
147 },
148 }
149
150 t.strictSame(normalize(pkg), expect)
151 t.strictSame(normalize(normalize(pkg)), expect, 'double sanitize ok')
152 })