File: path_helper_spec.rb

package info (click to toggle)
ruby-chef-config 16.12.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 376 kB
  • sloc: ruby: 3,077; makefile: 4
file content (372 lines) | stat: -rw-r--r-- 14,227 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#
# Author:: Bryan McLellan <btm@loftninjas.org>
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "chef-config/path_helper"
require "spec_helper"

RSpec.describe ChefConfig::PathHelper do

  let(:path_helper) { described_class }

  context "common functionality" do
    context "join" do
      it "joins starting with '' resolve to absolute paths" do
        expect(path_helper.join("", "a", "b")).to eq("#{path_helper.path_separator}a#{path_helper.path_separator}b")
      end

      it "joins ending with '' add a / to the end" do
        expect(path_helper.join("a", "b", "")).to eq("a#{path_helper.path_separator}b#{path_helper.path_separator}")
      end
    end

    context "dirname" do
      it "dirname('abc') is '.'" do
        expect(path_helper.dirname("abc")).to eq(".")
      end
      it "dirname('/') is '/'" do
        expect(path_helper.dirname(path_helper.path_separator)).to eq(path_helper.path_separator)
      end
      it "dirname('a/b/c') is 'a/b'" do
        expect(path_helper.dirname(path_helper.join("a", "b", "c"))).to eq(path_helper.join("a", "b"))
      end
      it "dirname('a/b/c/') is 'a/b'" do
        expect(path_helper.dirname(path_helper.join("a", "b", "c", ""))).to eq(path_helper.join("a", "b"))
      end
      it "dirname('/a/b/c') is '/a/b'" do
        expect(path_helper.dirname(path_helper.join("", "a", "b", "c"))).to eq(path_helper.join("", "a", "b"))
      end
    end
  end

  context "forcing windows/non-windows" do
    context "forcing windows" do
      it "path_separator is \\" do
        expect(path_helper.path_separator(windows: true)).to eq('\\')
      end

      context "platform-specific #join behavior" do
        it "joins components on Windows when some end with unix separators" do
          expected = "C:\\foo\\bar\\baz"
          expect(path_helper.join('C:\\foo/', "bar", "baz", windows: true)).to eq(expected)
        end

        it "joins components when some end with separators" do
          expected = "C:\\foo\\bar\\baz"
          expect(path_helper.join('C:\\foo\\', "bar", "baz", windows: true)).to eq(expected)
        end

        it "joins components when some end and start with separators" do
          expected = "C:\\foo\\bar\\baz"
          expect(path_helper.join('C:\\foo\\', "bar/", "/baz", windows: true)).to eq(expected)
        end

        it "joins components that don't end in separators" do
          expected = "C:\\foo\\bar\\baz"
          expect(path_helper.join('C:\\foo', "bar", "baz", windows: true)).to eq(expected)
        end
      end

      it "cleanpath changes slashes into backslashes and leaves backslashes alone" do
        expect(path_helper.cleanpath('/a/b\\c/d/', windows: true)).to eq('\\a\\b\\c\\d')
      end

      it "cleanpath does not remove leading double backslash" do
        expect(path_helper.cleanpath('\\\\a/b\\c/d/', windows: true)).to eq('\\\\a\\b\\c\\d')
      end
    end

    context "forcing unix" do
      it "path_separator is /" do
        expect(path_helper.path_separator(windows: false)).to eq("/")
      end

      it "cleanpath removes extra slashes alone" do
        expect(path_helper.cleanpath("/a///b/c/d/", windows: false)).to eq("/a/b/c/d")
      end

      context "platform-specific #join behavior" do
        it "joins components when some end with separators" do
          expected = "/foo/bar/baz"
          expect(path_helper.join("/foo/", "bar", "baz", windows: false)).to eq(expected)
        end

        it "joins components when some end and start with separators" do
          expected = "/foo/bar/baz"
          expect(path_helper.join("/foo/", "bar/", "/baz", windows: false)).to eq(expected)
        end

        it "joins components that don't end in separators" do
          expected = "/foo/bar/baz"
          expect(path_helper.join("/foo", "bar", "baz", windows: false)).to eq(expected)
        end
      end

      it "cleanpath changes backslashes into slashes and leaves slashes alone" do
        expect(path_helper.cleanpath('/a/b\\c/d/', windows: false)).to eq("/a/b/c/d")
      end

      it "cleanpath does not remove leading double backslash" do
        expect(path_helper.cleanpath('\\\\a/b\\c/d/', windows: false)).to eq("//a/b/c/d")
      end
    end
  end

  context "on windows", :windows_only do

    before(:each) do
      allow(ChefUtils).to receive(:windows?).and_return(true)
    end

    it "path_separator is \\" do
      expect(path_helper.path_separator).to eq('\\')
    end

    context "platform-specific #join behavior" do
      it "joins components on Windows when some end with unix separators" do
        expected = "C:\\foo\\bar\\baz"
        expect(path_helper.join('C:\\foo/', "bar", "baz")).to eq(expected)
      end

      it "joins components when some end with separators" do
        expected = "C:\\foo\\bar\\baz"
        expect(path_helper.join('C:\\foo\\', "bar", "baz")).to eq(expected)
      end

      it "joins components when some end and start with separators" do
        expected = "C:\\foo\\bar\\baz"
        expect(path_helper.join('C:\\foo\\', "bar/", "/baz")).to eq(expected)
      end

      it "joins components that don't end in separators" do
        expected = "C:\\foo\\bar\\baz"
        expect(path_helper.join('C:\\foo', "bar", "baz")).to eq(expected)
      end
    end

    it "cleanpath changes slashes into backslashes and leaves backslashes alone" do
      expect(path_helper.cleanpath('/a/b\\c/d/')).to eq('\\a\\b\\c\\d')
    end

    it "cleanpath does not remove leading double backslash" do
      expect(path_helper.cleanpath('\\\\a/b\\c/d/')).to eq('\\\\a\\b\\c\\d')
    end
  end

  context "on unix", :unix_only do
    before(:each) do
      allow(ChefUtils).to receive(:windows?).and_return(false)
    end

    it "path_separator is /" do
      expect(path_helper.path_separator).to eq("/")
    end

    it "cleanpath removes extra slashes alone" do
      expect(path_helper.cleanpath("/a///b/c/d/")).to eq("/a/b/c/d")
    end

    context "platform-specific #join behavior" do
      it "joins components when some end with separators" do
        expected = path_helper.cleanpath("/foo/bar/baz")
        expect(path_helper.join("/foo/", "bar", "baz")).to eq(expected)
      end

      it "joins components when some end and start with separators" do
        expected = path_helper.cleanpath("/foo/bar/baz")
        expect(path_helper.join("/foo/", "bar/", "/baz")).to eq(expected)
      end

      it "joins components that don't end in separators" do
        expected = path_helper.cleanpath("/foo/bar/baz")
        expect(path_helper.join("/foo", "bar", "baz")).to eq(expected)
      end
    end

    it "cleanpath changes backslashes into slashes and leaves slashes alone" do
      expect(path_helper.cleanpath('/a/b\\c/d/', windows: false)).to eq("/a/b/c/d")
    end

    # NOTE: this seems a bit weird to me, but this is just the way Pathname#cleanpath works
    it "cleanpath does not remove leading double backslash" do
      expect(path_helper.cleanpath('\\\\a/b\\c/d/')).to eq("//a/b/c/d")
    end
  end

  context "validate_path" do
    context "on windows" do
      before(:each) do
        # pass by default
        allow(ChefUtils).to receive(:windows?).and_return(true)
        allow(path_helper).to receive(:printable?).and_return(true)
        allow(path_helper).to receive(:windows_max_length_exceeded?).and_return(false)
      end

      it "returns the path if the path passes the tests" do
        expect(path_helper.validate_path("C:\\ThisIsRigged")).to eql("C:\\ThisIsRigged")
      end

      it "does not raise an error if everything looks great" do
        expect { path_helper.validate_path("C:\\cool path\\dude.exe") }.not_to raise_error
      end

      it "raises an error if the path has invalid characters" do
        allow(path_helper).to receive(:printable?).and_return(false)
        expect { path_helper.validate_path("Newline!\n") }.to raise_error(ChefConfig::InvalidPath)
      end

      it "Adds the \\\\?\\ prefix if the path exceeds MAX_LENGTH and does not have it" do
        long_path = "C:\\" + "a" * 250 + "\\" + "b" * 250
        prefixed_long_path = "\\\\?\\" + long_path
        allow(path_helper).to receive(:windows_max_length_exceeded?).and_return(true)
        expect(path_helper.validate_path(long_path)).to eql(prefixed_long_path)
      end
    end
  end

  context "windows_max_length_exceeded?" do
    it "returns true if the path is too long (259 + NUL) for the API" do
      expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 6)).to be_truthy
    end

    it "returns false if the path is not too long (259 + NUL) for the standard API" do
      expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 5)).to be_falsey
    end

    it "returns false if the path is over 259 characters but uses the \\\\?\\ prefix" do
      expect(path_helper.windows_max_length_exceeded?("\\\\?\\C:\\" + "a" * 250 + "\\" + "b" * 250)).to be_falsey
    end
  end

  context "printable?" do
    it "returns true if the string contains no non-printable characters" do
      expect(path_helper.printable?("C:\\Program Files (x86)\\Microsoft Office\\Files.lst")).to be_truthy
    end

    it "returns true when given 'abc' in unicode" do
      expect(path_helper.printable?("\u0061\u0062\u0063")).to be_truthy
    end

    it "returns true when given japanese unicode" do
      expect(path_helper.printable?("\uff86\uff87\uff88")).to be_truthy
    end

    it "returns false if the string contains a non-printable character" do
      expect(path_helper.printable?("\my files\work\notes.txt")).to be_falsey
    end

    # This isn't necessarily a requirement, but here to be explicit about functionality.
    it "returns false if the string contains a newline or tab" do
      expect(path_helper.printable?("\tThere's no way,\n\t *no* way,\n\t that you came from my loins.\n")).to be_falsey
    end
  end

  context "canonical_path" do
    context "on windows", :windows_only do
      it "returns an absolute path with backslashes instead of slashes" do
        expect(path_helper.canonical_path("\\\\?\\C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
      end

      it "adds the \\\\?\\ prefix if it is missing" do
        expect(path_helper.canonical_path("C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
      end

      it "returns a lowercase path" do
        expect(path_helper.canonical_path("\\\\?\\C:\\CASE\\INSENSITIVE")).to eq("\\\\?\\c:\\case\\insensitive")
      end
    end

    context "not on windows", :unix_only do
      it "returns a canonical path" do
        expect(path_helper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default")).to eq("/etc/apache.d/sites-available/default")
      end
    end
  end

  context "paths_eql?" do
    it "returns true if the paths are the same" do
      allow(path_helper).to receive(:canonical_path).with("bandit", windows: ChefUtils.windows?).and_return("c:/bandit/bandit")
      allow(path_helper).to receive(:canonical_path).with("../bandit/bandit", windows: ChefUtils.windows?).and_return("c:/bandit/bandit")
      expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_truthy
    end

    it "returns false if the paths are different" do
      allow(path_helper).to receive(:canonical_path).with("bandit", windows: ChefUtils.windows?).and_return("c:/Bo/Bandit")
      allow(path_helper).to receive(:canonical_path).with("../bandit/bandit", windows: ChefUtils.windows?).and_return("c:/bandit/bandit")
      expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_falsey
    end
  end

  context "escape_glob" do
    it "escapes characters reserved by glob" do
      path = "C:\\this\\*path\\[needs]\\escaping?"
      escaped_path = "C:\\\\this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
      expect(path_helper.escape_glob(path, windows: true)).to eq(escaped_path)
    end

    context "when given more than one argument" do
      it "joins, cleanpaths, and escapes characters reserved by glob" do
        args = ["this/*path", "[needs]", "escaping?"]
        escaped_path = if ChefUtils.windows?
                         "this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
                       else
                         "this/\\*path/\\[needs\\]/escaping\\?"
                       end
        expect(path_helper.escape_glob(*args)).to eq(escaped_path)
      end
    end
  end

  context "escape_glob_dir" do
    it "escapes characters reserved by glob without using backslashes for path separators" do
      path = "C:/this/*path/[needs]/escaping?"
      escaped_path = "C:/this/\\*path/\\[needs\\]/escaping\\?"
      expect(path_helper.escape_glob_dir(path)).to eq(escaped_path)
    end

    context "when given more than one argument" do
      it "joins, cleanpaths, and escapes characters reserved by glob" do
        args = ["this/*path", "[needs]", "escaping?"]
        escaped_path = "this/\\*path/\\[needs\\]/escaping\\?"
        expect(path_helper).to receive(:join).with(*args).and_call_original
        expect(path_helper.escape_glob_dir(*args)).to eq(escaped_path)
      end
    end
  end

  context "all_homes" do
    before do
      stub_const("ENV", env)
      allow(ChefUtils).to receive(:windows?).and_return(is_windows)
    end

    context "on windows" do
      let (:is_windows) { true }
    end

    context "on unix" do
      let (:is_windows) { false }

      context "when HOME is not set" do
        let (:env) { {} }
        it "returns an empty array" do
          expect(path_helper.all_homes).to eq([])
        end
      end
    end
  end
end