blob: ec051c13878bb92ab2f96dcf3f02e6419b88c0e0 (
plain)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
  | 
describe Hbc::Audit, :cask do
  def include_msg?(messages, msg)
    if msg.is_a?(Regexp)
      Array(messages).any? { |m| m =~ msg }
    else
      Array(messages).include?(msg)
    end
  end
  matcher :pass do
    match do |audit|
      !audit.errors? && !audit.warnings?
    end
  end
  matcher :fail do
    match(&:errors?)
  end
  matcher :warn do
    match do |audit|
      audit.warnings? && !audit.errors?
    end
  end
  matcher :fail_with do |error_msg|
    match do |audit|
      include_msg?(audit.errors, error_msg)
    end
  end
  matcher :warn_with do |warning_msg|
    match do |audit|
      include_msg?(audit.warnings, warning_msg)
    end
  end
  let(:cask) { instance_double(Hbc::Cask) }
  let(:download) { false }
  let(:check_token_conflicts) { false }
  let(:fake_system_command) { class_double(Hbc::SystemCommand) }
  let(:audit) {
    Hbc::Audit.new(cask, download:              download,
                         check_token_conflicts: check_token_conflicts,
                         command:               fake_system_command)
  }
  describe "#result" do
    subject { audit.result }
    context "when there are errors" do
      before do
        audit.add_error "bad"
      end
      it { is_expected.to match(/failed/) }
    end
    context "when there are warnings" do
      before do
        audit.add_warning "eh"
      end
      it { is_expected.to match(/warning/) }
    end
    context "when there are errors and warnings" do
      before do
        audit.add_error "bad"
        audit.add_warning "eh"
      end
      it { is_expected.to match(/failed/) }
    end
    context "when there are no errors or warnings" do
      it { is_expected.to match(/passed/) }
    end
  end
  describe "#run!" do
    let(:cask) { Hbc::CaskLoader.load(cask_token) }
    subject { audit.run! }
    describe "required stanzas" do
      %w[version sha256 url name homepage].each do |stanza|
        context "when missing #{stanza}" do
          let(:cask_token) { "missing-#{stanza}" }
          it { is_expected.to fail_with(/#{stanza} stanza is required/) }
        end
      end
    end
    describe "preflight stanza checks" do
      let(:error_msg) { "only a single preflight stanza is allowed" }
      context "when the cask has no preflight stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one preflight stanza" do
        let(:cask_token) { "with-preflight" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple preflight stanzas" do
        let(:cask_token) { "with-preflight-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "uninstall_postflight stanza checks" do
      let(:error_msg) { "only a single postflight stanza is allowed" }
      context "when the cask has no postflight stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one postflight stanza" do
        let(:cask_token) { "with-postflight" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple postflight stanzas" do
        let(:cask_token) { "with-postflight-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "uninstall stanza checks" do
      let(:error_msg) { "only a single uninstall stanza is allowed" }
      context "when the cask has no uninstall stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one uninstall stanza" do
        let(:cask_token) { "with-uninstall-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple uninstall stanzas" do
        let(:cask_token) { "with-uninstall-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "uninstall_preflight stanza checks" do
      let(:error_msg) { "only a single uninstall_preflight stanza is allowed" }
      context "when the cask has no uninstall_preflight stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one uninstall_preflight stanza" do
        let(:cask_token) { "with-uninstall-preflight" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple uninstall_preflight stanzas" do
        let(:cask_token) { "with-uninstall-preflight-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "uninstall_postflight stanza checks" do
      let(:error_msg) { "only a single uninstall_postflight stanza is allowed" }
      context "when the cask has no uninstall_postflight stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one uninstall_postflight stanza" do
        let(:cask_token) { "with-uninstall-postflight" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple uninstall_postflight stanzas" do
        let(:cask_token) { "with-uninstall-postflight-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "zap stanza checks" do
      let(:error_msg) { "only a single zap stanza is allowed" }
      context "when the cask has no zap stanza" do
        let(:cask_token) { "with-uninstall-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has only one zap stanza" do
        let(:cask_token) { "with-zap-rmdir" }
        it { should_not warn_with(error_msg) }
      end
      context "when the cask has multiple zap stanzas" do
        let(:cask_token) { "with-zap-multi" }
        it { is_expected.to warn_with(error_msg) }
      end
    end
    describe "version checks" do
      let(:error_msg) { "you should use version :latest instead of version 'latest'" }
      context "when version is 'latest'" do
        let(:cask_token) { "version-latest-string" }
        it { is_expected.to fail_with(error_msg) }
      end
      context "when version is :latest" do
        let(:cask_token) { "version-latest-with-checksum" }
        it { should_not fail_with(error_msg) }
      end
    end
    describe "sha256 checks" do
      context "when version is :latest and sha256 is not :no_check" do
        let(:cask_token) { "version-latest-with-checksum" }
        it { is_expected.to fail_with("you should use sha256 :no_check when version is :latest") }
      end
      context "when sha256 is not a legal SHA-256 digest" do
        let(:cask_token) { "invalid-sha256" }
        it { is_expected.to fail_with("sha256 string must be of 64 hexadecimal characters") }
      end
      context "when sha256 is sha256 for empty string" do
        let(:cask_token) { "sha256-for-empty-string" }
        it { is_expected.to fail_with(/cannot use the sha256 for an empty string/) }
      end
    end
    describe "appcast checks" do
      context "when appcast has no sha256" do
        let(:cask_token) { "appcast-missing-checkpoint" }
        it { is_expected.to fail_with(/checkpoint sha256 is required for appcast/) }
      end
      context "when appcast checkpoint is not a string of 64 hexadecimal characters" do
        let(:cask_token) { "appcast-invalid-checkpoint" }
        it { is_expected.to fail_with(/string must be of 64 hexadecimal characters/) }
      end
      context "when appcast checkpoint is sha256 for empty string" do
        let(:cask_token) { "appcast-checkpoint-sha256-for-empty-string" }
        it { is_expected.to fail_with(/cannot use the sha256 for an empty string/) }
      end
      context "when appcast checkpoint is valid sha256" do
        let(:cask_token) { "appcast-valid-checkpoint" }
        it { should_not fail_with(/appcast :checkpoint/) }
      end
      context "when verifying appcast HTTP code" do
        let(:cask_token) { "appcast-valid-checkpoint" }
        let(:download) { instance_double(Hbc::Download) }
        let(:wrong_code_msg) { /unexpected HTTP response code/ }
        let(:curl_error_msg) { /error retrieving appcast/ }
        let(:fake_curl_result) { instance_double(Hbc::SystemCommand::Result) }
        before do
          allow(audit).to receive(:check_appcast_checkpoint_accuracy)
          allow(fake_system_command).to receive(:run).and_return(fake_curl_result)
          allow(fake_curl_result).to receive(:success?).and_return(success)
        end
        context "when curl succeeds" do
          let(:success) { true }
          before do
            allow(fake_curl_result).to receive(:stdout).and_return(stdout)
          end
          context "when HTTP code is 200" do
            let(:stdout) { "200" }
            it { should_not warn_with(wrong_code_msg) }
          end
          context "when HTTP code is not 200" do
            let(:stdout) { "404" }
            it { is_expected.to warn_with(wrong_code_msg) }
          end
        end
        context "when curl fails" do
          let(:success) { false }
          before do
            allow(fake_curl_result).to receive(:stderr).and_return("Some curl error")
          end
          it { is_expected.to warn_with(curl_error_msg) }
        end
      end
      context "when verifying appcast checkpoint" do
        let(:cask_token) { "appcast-valid-checkpoint" }
        let(:download) { instance_double(Hbc::Download) }
        let(:mismatch_msg) { /appcast checkpoint mismatch/ }
        let(:curl_error_msg) { /error retrieving appcast/ }
        let(:fake_curl_result) { instance_double(Hbc::SystemCommand::Result) }
        let(:expected_checkpoint) { "d5b2dfbef7ea28c25f7a77cd7fa14d013d82b626db1d82e00e25822464ba19e2" }
        before do
          allow(audit).to receive(:check_appcast_http_code)
          allow(Hbc::SystemCommand).to receive(:run).and_return(fake_curl_result)
          allow(fake_curl_result).to receive(:success?).and_return(success)
        end
        context "when appcast download succeeds" do
          let(:success) { true }
          let(:appcast_text) { instance_double(::String) }
          before do
            allow(fake_curl_result).to receive(:stdout).and_return(appcast_text)
            allow(appcast_text).to receive(:gsub).and_return(appcast_text)
            allow(appcast_text).to receive(:end_with?).with("\n").and_return(true)
            allow(Digest::SHA2).to receive(:hexdigest).and_return(actual_checkpoint)
          end
          context "when appcast checkpoint is out of date" do
            let(:actual_checkpoint) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" }
            it { is_expected.to warn_with(mismatch_msg) }
            it { should_not warn_with(curl_error_msg) }
          end
          context "when appcast checkpoint is up to date" do
            let(:actual_checkpoint) { expected_checkpoint }
            it { should_not warn_with(mismatch_msg) }
            it { should_not warn_with(curl_error_msg) }
          end
        end
        context "when appcast download fails" do
          let(:success) { false }
          before do
            allow(fake_curl_result).to receive(:stderr).and_return("Some curl error")
          end
          it { is_expected.to warn_with(curl_error_msg) }
        end
      end
    end
    describe "preferred download URL formats" do
      let(:warning_msg) { /URL format incorrect/ }
      context "with incorrect SourceForge URL format" do
        let(:cask_token) { "sourceforge-incorrect-url-format" }
        it { is_expected.to warn_with(warning_msg) }
      end
      context "with correct SourceForge URL format" do
        let(:cask_token) { "sourceforge-correct-url-format" }
        it { should_not warn_with(warning_msg) }
      end
      context "with correct SourceForge URL format for version :latest" do
        let(:cask_token) { "sourceforge-version-latest-correct-url-format" }
        it { should_not warn_with(warning_msg) }
      end
      context "with incorrect OSDN URL format" do
        let(:cask_token) { "osdn-incorrect-url-format" }
        it { is_expected.to warn_with(warning_msg) }
      end
      context "with correct OSDN URL format" do
        let(:cask_token) { "osdn-correct-url-format" }
        it { should_not warn_with(warning_msg) }
      end
    end
    describe "generic artifact checks" do
      context "with relative target" do
        let(:cask_token) { "generic-artifact-relative-target" }
        it { is_expected.to fail_with(/target must be absolute path for Generic Artifact/) }
      end
      context "with absolute target" do
        let(:cask_token) { "generic-artifact-absolute-target" }
        it { should_not fail_with(/target required for Generic Artifact/) }
      end
    end
    describe "url checks" do
      context "given a block" do
        let(:cask_token) { "booby-trap" }
        context "when loading the cask" do
          it "does not evaluate the block" do
            expect { cask }.not_to raise_error
          end
        end
        context "when doing the audit" do
          it "evaluates the block" do
            expect(subject).to fail_with(/Boom/)
          end
        end
      end
    end
    describe "token conflicts" do
      let(:cask_token) { "with-binary" }
      let(:check_token_conflicts) { true }
      before do
        expect(audit).to receive(:core_formula_names).and_return(formula_names)
      end
      context "when cask token conflicts with a core formula" do
        let(:formula_names) { %w[with-binary other-formula] }
        it { is_expected.to warn_with(/possible duplicate/) }
      end
      context "when cask token does not conflict with a core formula" do
        let(:formula_names) { %w[other-formula] }
        it { should_not warn_with(/possible duplicate/) }
      end
    end
    describe "audit of downloads" do
      let(:cask_token) { "with-binary" }
      let(:cask) { Hbc::CaskLoader.load(cask_token) }
      let(:download) { instance_double(Hbc::Download) }
      let(:verify) { class_double(Hbc::Verify).as_stubbed_const }
      let(:error_msg) { "Download Failed" }
      context "when download and verification succeed" do
        before do
          expect(download).to receive(:perform)
          expect(verify).to receive(:all)
        end
        it { should_not fail_with(/#{error_msg}/) }
      end
      context "when download fails" do
        before do
          expect(download).to receive(:perform).and_raise(StandardError.new(error_msg))
        end
        it { is_expected.to fail_with(/#{error_msg}/) }
      end
      context "when verification fails" do
        before do
          expect(download).to receive(:perform)
          expect(verify).to receive(:all).and_raise(StandardError.new(error_msg))
        end
        it { is_expected.to fail_with(/#{error_msg}/) }
      end
    end
    context "when an exception is raised" do
      let(:cask) { instance_double(Hbc::Cask) }
      before do
        expect(cask).to receive(:version).and_raise(StandardError.new)
      end
      it { is_expected.to fail_with(/exception while auditing/) }
    end
  end
end
  |