summaryrefslogtreecommitdiff
path: root/spec/support/shared/functional/file_resource.rb
blob: 44048598c7ee07dc8a8689e0974eca5f524ebad3 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#
# Author:: Seth Chisamore (<schisamo@opscode.com>)
# Copyright:: Copyright (c) 2011 Opscode, 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.
#

shared_context "deploying with move" do
  before do
    Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH
    Chef::Config[:file_atomic_update] = true
  end
end

shared_context "deploying with copy" do
  before do
    Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH
    Chef::Config[:file_atomic_update] = false
  end
end

shared_context "deploying via tmpdir" do
  before do
    Chef::Config[:file_staging_uses_destdir] = false
    Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH
  end
end

shared_context "deploying via destdir" do
  before do
    Chef::Config[:file_staging_uses_destdir] = true
    Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH
  end
end


shared_examples_for "a file with the wrong content" do
  before do
    # Assert starting state is as expected
    File.should exist(path)
    # Kinda weird, in this case @expected_checksum is the cksum of the file
    # with incorrect content.
    sha256_checksum(path).should == @expected_checksum
  end

  include_context "diff disabled"

  context "when running action :create" do
    context "with backups enabled" do
      before do
        resource.run_action(:create)
      end

      it "overwrites the file with the updated content when the :create action is run" do
        File.stat(path).mtime.should > @expected_mtime
        sha256_checksum(path).should_not == @expected_checksum
      end

      it "backs up the existing file" do
        Dir.glob(backup_glob).size.should equal(1)
      end

      it "is marked as updated by last action" do
        resource.should be_updated_by_last_action
      end

      it "should restore the security contexts on selinux", :selinux_only do
        selinux_security_context_restored?(path).should be_true
      end
    end

    context "with backups disabled" do
      before do
        resource.backup(0)
        resource.run_action(:create)
      end

      it "should not attempt to backup the existing file if :backup == 0" do
        Dir.glob(backup_glob).size.should equal(0)
      end

      it "should restore the security contexts on selinux", :selinux_only do
        selinux_security_context_restored?(path).should be_true
      end
    end
  end

  describe "when running action :create_if_missing" do
    before do
      resource.run_action(:create_if_missing)
    end

    it "doesn't overwrite the file when the :create_if_missing action is run" do
      File.stat(path).mtime.should == @expected_mtime
      sha256_checksum(path).should == @expected_checksum
    end

    it "is not marked as updated" do
      resource.should_not be_updated_by_last_action
    end

    it "should restore the security contexts on selinux", :selinux_only do
      selinux_security_context_restored?(path).should be_true
    end
  end

  describe "when running action :delete" do
    before do
      resource.run_action(:delete)
    end

    it "deletes the file" do
      File.should_not exist(path)
    end

    it "is marked as updated by last action" do
      resource.should be_updated_by_last_action
    end
  end
end

shared_examples_for "a file with the correct content" do
  before do
    # Assert starting state is as expected
    File.should exist(path)
    sha256_checksum(path).should == @expected_checksum
  end

  include_context "diff disabled"

  describe "when running action :create" do
    before do
      resource.run_action(:create)
    end
    it "does not overwrite the original when the :create action is run" do
      sha256_checksum(path).should == @expected_checksum
    end

    it "does not update the mtime of the file when the :create action is run" do
      File.stat(path).mtime.should == @expected_mtime
    end

    it "is not marked as updated by last action" do
      resource.should_not be_updated_by_last_action
    end

    it "should restore the security contexts on selinux", :selinux_only do
      selinux_security_context_restored?(path).should be_true
    end
  end

  describe "when running action :create_if_missing" do
    before do
      resource.run_action(:create_if_missing)
    end

    it "doesn't overwrite the file when the :create_if_missing action is run" do
      sha256_checksum(path).should == @expected_checksum
    end

    it "is not marked as updated by last action" do
      resource.should_not be_updated_by_last_action
    end

    it "should restore the security contexts on selinux", :selinux_only do
      selinux_security_context_restored?(path).should be_true
    end
  end

  describe "when running action :delete" do
    before do
      resource.run_action(:delete)
    end

    it "deletes the file when the :delete action is run" do
      File.should_not exist(path)
    end

    it "is marked as updated by last action" do
      resource.should be_updated_by_last_action
    end
  end
end

shared_examples_for "a file resource" do
  describe "when deploying with :move" do

    include_context "deploying with move"

    describe "when deploying via tmpdir" do

      include_context "deploying via tmpdir"

      it_behaves_like "a configured file resource"
    end

    describe "when deploying via destdir" do

      include_context "deploying via destdir"

      it_behaves_like "a configured file resource"
    end
  end

  describe "when deploying with :copy" do

    include_context "deploying with copy"

    describe "when deploying via tmpdir" do

      include_context "deploying via tmpdir"

      it_behaves_like "a configured file resource"
    end

    describe "when deploying via destdir" do

      include_context "deploying via destdir"

      it_behaves_like "a configured file resource"
    end
  end

  describe "when running under why run" do

    before do
      Chef::Config[:why_run] = true
    end

    after do
      Chef::Config[:why_run] = false
    end

    context "and the resource has a path with a missing intermediate directory" do
      # CHEF-3978

      let(:path) do
        File.join(test_file_dir, "intermediate_dir", make_tmpname(file_base))
      end

      it "successfully doesn't create the file" do
        resource.run_action(:create) # should not raise
        File.should_not exist(path)
      end
    end

  end

  describe "when setting atomic_update" do
    it "booleans should work" do
      lambda {resource.atomic_update(true)}.should_not raise_error
      lambda {resource.atomic_update(false)}.should_not raise_error
    end

    it "anything else should raise an error" do
      lambda {resource.atomic_update(:copy)}.should raise_error(ArgumentError)
      lambda {resource.atomic_update(:move)}.should raise_error(ArgumentError)
      lambda {resource.atomic_update(958)}.should raise_error(ArgumentError)
    end
  end

end

shared_examples_for "file resource not pointing to a real file" do
  def symlink?(file_path)
    if windows?
      Chef::ReservedNames::Win32::File.symlink?(file_path)
    else
      File.symlink?(file_path)
    end
  end

  def real_file?(file_path)
    !symlink?(file_path) && File.file?(file_path)
  end

  describe "when force_unlink is set to true" do
    it ":create unlinks the target" do
      real_file?(path).should be_false
      resource.force_unlink(true)
      resource.run_action(:create)
      real_file?(path).should be_true
      binread(path).should == expected_content
      resource.should be_updated_by_last_action
    end
  end

  describe "when force_unlink is set to false" do
    it ":create raises an error" do
      lambda {resource.run_action(:create) }.should raise_error(Chef::Exceptions::FileTypeMismatch)
    end
  end

  describe "when force_unlink is not set (default)" do
    it ":create raises an error" do
      lambda {resource.run_action(:create) }.should raise_error(Chef::Exceptions::FileTypeMismatch)
    end
  end
end

shared_examples_for "a configured file resource" do

  include_context "diff disabled"

  before do
    Chef::Log.level = :info
  end

   # note the stripping of the drive letter from the tmpdir on windows
  let(:backup_glob) { File.join(CHEF_SPEC_BACKUP_PATH, test_file_dir.sub(/^([A-Za-z]:)/, ""), "#{file_base}*") }

  # Most tests update the resource, but a few do not. We need to test that the
  # resource is marked updated or not correctly, but the test contexts are
  # composed between correct/incorrect content and correct/incorrect
  # permissions. We override this "let" definition in the context where content
  # and permissions are correct.
  let(:expect_updated?) { true }

  include Chef::Mixin::ShellOut

  def selinux_security_context_restored?(path)
    @restorecon_path = which("restorecon") if @restorecon_path.nil?
    restorecon_test_command = "#{@restorecon_path} -n -v #{path}"
    cmdresult = shell_out(restorecon_test_command)
    # restorecon will print the required changes to stdout if any is
    # needed
    cmdresult.stdout.empty?
  end

  def binread(file)
    content = File.open(file, "rb") do |f|
      f.read
    end
    content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding)
    content
  end

  context "when the target file is a symlink", :not_supported_on_win2k3 do
    let(:symlink_target) {
      File.join(CHEF_SPEC_DATA, "file-test-target")
    }


    describe "when configured not to manage symlink's target" do
      before(:each) do
        # configure not to manage symlink source
        resource.manage_symlink_source(false)

        # create symlinks for test context
        FileUtils.touch(symlink_target)

        if windows?
          Chef::ReservedNames::Win32::File.symlink(symlink_target, path)
        else
          File.symlink(symlink_target, path)
        end
      end

      after(:each) do
        FileUtils.rm_rf(symlink_target)
        FileUtils.rm_rf(path)
      end

      describe "when symlink target has correct content" do
        before(:each) do
          File.open(symlink_target, "wb") { |f| f.print expected_content }
        end

        it_behaves_like "file resource not pointing to a real file"
      end

      describe "when symlink target has the wrong content" do
        before(:each) do
          File.open(symlink_target, "wb") { |f| f.print "This is so wrong!!!" }
        end

        after(:each) do
          # symlink should never be followed
          binread(symlink_target).should == "This is so wrong!!!"
        end

        it_behaves_like "file resource not pointing to a real file"
      end
    end

    # Unix-only for now. Windows behavior may differ because of how ACL
    # management handles symlinks. Since symlinks are rare on Windows and this
    # feature primarily exists to support the case where a well-known file
    # (e.g., resolv.conf) has been converted to a symlink, we're okay with the
    # discrepancy.
    context "when configured to manage the symlink source", :unix_only do

      before do
        resource.manage_symlink_source(true)
      end

      context "but the symlink is part of a loop" do
        let(:link1_path) { File.join(CHEF_SPEC_DATA, "points-to-link2") }
        let(:link2_path) { File.join(CHEF_SPEC_DATA, "points-to-link1") }

        before do
          # point resource at link1:
          resource.path(link1_path)
          # create symlinks for test context
          File.symlink(link1_path, link2_path)
          File.symlink(link2_path, link1_path)
        end

        after(:each) do
          FileUtils.rm_rf(link1_path)
          FileUtils.rm_rf(link2_path)
        end

        it "raises an InvalidSymlink error" do
          lambda { resource.run_action(:create) }.should raise_error(Chef::Exceptions::InvalidSymlink)
        end

        it "issues a warning/assumption in whyrun mode" do
          begin
            Chef::Config[:why_run] = true
            resource.run_action(:create) # should not raise
          ensure
            Chef::Config[:why_run] = false
          end
        end
      end

      context "but the symlink points to a nonexistent file" do
        let(:link_path) { File.join(CHEF_SPEC_DATA, "points-to-nothing") }
        let(:not_existent_source) { File.join(CHEF_SPEC_DATA, "i-am-not-here") }

        before do
          resource.path(link_path)
          # create symlinks for test context
          File.symlink(not_existent_source, link_path)
          FileUtils.rm_rf(not_existent_source)
        end

        after(:each) do
          FileUtils.rm_rf(link_path)
        end
        it "raises an InvalidSymlink error" do
          lambda { resource.run_action(:create) }.should raise_error(Chef::Exceptions::InvalidSymlink)
        end

        it "issues a warning/assumption in whyrun mode" do
          begin
            Chef::Config[:why_run] = true
            resource.run_action(:create) # should not raise
          ensure
            Chef::Config[:why_run] = false
          end
        end
      end

      context "but the symlink is points to a non-file fs entry" do
        let(:link_path) { File.join(CHEF_SPEC_DATA, "points-to-dir") }
        let(:not_a_file_path) { File.join(CHEF_SPEC_DATA, "dir-at-end-of-symlink") }

        before do
          # point resource at link1:
          resource.path(link_path)
          # create symlinks for test context
          File.symlink(not_a_file_path, link_path)
          Dir.mkdir(not_a_file_path)
        end

        after(:each) do
          FileUtils.rm_rf(link_path)
          FileUtils.rm_rf(not_a_file_path)
        end

        it "raises an InvalidSymlink error" do
          lambda { resource.run_action(:create) }.should raise_error(Chef::Exceptions::FileTypeMismatch)
        end

        it "issues a warning/assumption in whyrun mode" do
          begin
            Chef::Config[:why_run] = true
            resource.run_action(:create) # should not raise
          ensure
            Chef::Config[:why_run] = false
          end
        end
      end

      context "when the symlink source is a real file" do

        let(:wrong_content) { "this is the wrong content" }
        let(:link_path) { File.join(CHEF_SPEC_DATA, "points-to-real-file") }

        before do
          # point resource at link:
          resource.path(link_path)
          # create symlinks for test context
          File.symlink(path, link_path)
        end

        after(:each) do
          # shared examples should not change our test setup of a file resource
          # pointing at a symlink:
          resource.path.should == link_path
          FileUtils.rm_rf(link_path)
        end

        context "and the permissions are incorrect" do
          before do
            # Create source (real) file
            File.open(path, "wb") { |f| f.write(expected_content) }
          end


          include_context "setup broken permissions"

          include_examples "a securable resource with existing target"

          it "does not replace the symlink with a real file" do
            resource.run_action(:create)
            File.should be_symlink(link_path)
          end

        end

        context "and the content is incorrect" do
          before do
            # Create source (real) file
            File.open(path, "wb") { |f| f.write(wrong_content) }
          end

          it "updates the source file content" do
            pending
          end

          it "marks the resource as updated" do
            resource.run_action(:create)
            resource.should be_updated_by_last_action
          end

          it "does not replace the symlink with a real file" do
            resource.run_action(:create)
            File.should be_symlink(link_path)
          end
        end

        context "and the content and permissions are correct" do
          let(:expect_updated?) { false }

          before do
            # Create source (real) file
            File.open(path, "wb") { |f| f.write(expected_content) }
          end
          include_context "setup correct permissions"

          include_examples "a securable resource with existing target"

        end

      end

      context "when the symlink points to a symlink which points to a real file" do

        let(:wrong_content) { "this is the wrong content" }
        let(:link_to_file_path) { File.join(CHEF_SPEC_DATA, "points-to-real-file") }
        let(:link_to_link_path) { File.join(CHEF_SPEC_DATA, "points-to-next-link") }

        before do
          # point resource at link:
          resource.path(link_to_link_path)
          # create symlinks for test context
          File.symlink(path, link_to_file_path)
          File.symlink(link_to_file_path, link_to_link_path)

          # Create source (real) file
          File.open(path, "wb") { |f| f.write(wrong_content) }
        end

        include_context "setup broken permissions"

        include_examples "a securable resource with existing target"

        after(:each) do
          # shared examples should not change our test setup of a file resource
          # pointing at a symlink:
          resource.path.should == link_to_link_path
          FileUtils.rm_rf(link_to_file_path)
          FileUtils.rm_rf(link_to_link_path)
        end

        it "does not replace the symlink with a real file" do
          resource.run_action(:create)
          File.should be_symlink(link_to_link_path)
          File.should be_symlink(link_to_file_path)
        end

      end
    end
  end

  context "when the target file is a directory" do
    before(:each) do
      FileUtils.mkdir_p(path)
    end

    after(:each) do
      FileUtils.rm_rf(path)
    end

    it_behaves_like "file resource not pointing to a real file"
  end

  context "when the target file is a blockdev",:unix_only, :requires_root, :not_supported_on_solaris do
    include Chef::Mixin::ShellOut
    let(:path) do
      File.join(CHEF_SPEC_DATA, "testdev")
    end

    before(:each) do
      result = shell_out("mknod #{path} b 1 2")
      result.stderr.empty?
    end

    after(:each) do
      FileUtils.rm_rf(path)
    end

    it_behaves_like "file resource not pointing to a real file"
  end

  context "when the target file is a chardev",:unix_only, :requires_root, :not_supported_on_solaris do
    include Chef::Mixin::ShellOut
    let(:path) do
      File.join(CHEF_SPEC_DATA, "testdev")
    end

    before(:each) do
      result = shell_out("mknod #{path} c 1 2")
      result.stderr.empty?
    end

    after(:each) do
      FileUtils.rm_rf(path)
    end

    it_behaves_like "file resource not pointing to a real file"
  end

  context "when the target file is a pipe",:unix_only do
    include Chef::Mixin::ShellOut
    let(:path) do
      File.join(CHEF_SPEC_DATA, "testpipe")
    end

    before(:each) do
      result = shell_out("mkfifo #{path}")
      result.stderr.empty?
    end

    after(:each) do
      FileUtils.rm_rf(path)
    end

    it_behaves_like "file resource not pointing to a real file"
  end

  context "when the target file is a socket",:unix_only do
    require 'socket'

    # It turns out that the path to a socket can have at most ~104
    # bytes. Therefore we are creating our sockets in tmpdir so that
    # they have a shorter path.
    let(:test_socket_dir) { File.join(Dir.tmpdir, "sockets") }

    before do
      FileUtils::mkdir_p(test_socket_dir)
    end

    after do
      FileUtils::rm_rf(test_socket_dir)
    end

    let(:path) do
      File.join(test_socket_dir, "testsocket")
    end

    before(:each) do
      path.bytesize.should <= 104
      UNIXServer.new(path)
    end

    after(:each) do
      FileUtils.rm_rf(path)
    end

    it_behaves_like "file resource not pointing to a real file"
  end

  # Regression test for http://tickets.opscode.com/browse/CHEF-4082
  context "when notification is configured" do
    describe "when path is specified with normal seperator" do
      before do
        @notified_resource = Chef::Resource.new("punk", resource.run_context)
        resource.notifies(:run, @notified_resource, :immediately)
        resource.run_action(:create)
      end

      it "should notify the other resources correctly" do
        resource.should be_updated_by_last_action
        resource.run_context.immediate_notifications(resource).length.should == 1
      end
    end

    describe "when path is specified with windows seperator", :windows_only do
      let(:path) {
        File.join(test_file_dir, make_tmpname(file_base)).gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)
      }

      before do
        @notified_resource = Chef::Resource.new("punk", resource.run_context)
        resource.notifies(:run, @notified_resource, :immediately)
        resource.run_action(:create)
      end

      it "should notify the other resources correctly" do
        resource.should be_updated_by_last_action
        resource.run_context.immediate_notifications(resource).length.should == 1
      end
    end
  end

  context "when the target file does not exist" do
    before do
      # Assert starting state is expected
      File.should_not exist(path)
    end

    describe "when running action :create" do
      before do
        resource.run_action(:create)
      end

      it "creates the file when the :create action is run" do
        File.should exist(path)
      end

      it "creates the file with the correct content when the :create action is run" do
        binread(path).should == expected_content
      end

      it "is marked as updated by last action" do
        resource.should be_updated_by_last_action
      end

      it "should restore the security contexts on selinux", :selinux_only do
        selinux_security_context_restored?(path).should be_true
      end
    end

    describe "when running action :create_if_missing" do
      before do
        resource.run_action(:create_if_missing)
      end

      it "creates the file with the correct content" do
        binread(path).should == expected_content
      end

      it "is marked as updated by last action" do
        resource.should be_updated_by_last_action
      end

      it "should restore the security contexts on selinux", :selinux_only do
        selinux_security_context_restored?(path).should be_true
      end
    end

    describe "when running action :delete" do
      before do
        resource.run_action(:delete)
      end

      it "deletes the file when the :delete action is run" do
        File.should_not exist(path)
      end

      it "is not marked updated by last action" do
        resource.should_not be_updated_by_last_action
      end
    end
  end

  # Set up the context for security tests
  def allowed_acl(sid, expected_perms)
    [ ACE.access_allowed(sid, expected_perms[:specific]) ]
  end

  def denied_acl(sid, expected_perms)
    [ ACE.access_denied(sid, expected_perms[:specific]) ]
  end

  def parent_inheritable_acls
    dummy_file_path = File.join(test_file_dir, "dummy_file")
    FileUtils.touch(dummy_file_path)
    dummy_desc = get_security_descriptor(dummy_file_path)
    FileUtils.rm_rf(dummy_file_path)
    dummy_desc
  end

  it_behaves_like "a securable resource without existing target"

  context "when the target file has the wrong content" do
    before(:each) do
      File.open(path, "wb") { |f| f.print "This is so wrong!!!" }
      now = Time.now.to_i
      File.utime(now - 9000, now - 9000, path)

      @expected_mtime = File.stat(path).mtime
      @expected_checksum = sha256_checksum(path)
    end

    describe "and the target file has the correct permissions" do
      include_context "setup correct permissions"

      it_behaves_like "a file with the wrong content"

      it_behaves_like "a securable resource with existing target"
    end

    context "and the target file has incorrect permissions" do
      include_context "setup broken permissions"

      it_behaves_like "a file with the wrong content"

      it_behaves_like "a securable resource with existing target"
    end
  end

  context "when the target file has the correct content" do
    before(:each) do
      File.open(path, "wb") { |f| f.print expected_content }
      now = Time.now.to_i
      File.utime(now - 9000, now - 9000, path)

      @expected_mtime = File.stat(path).mtime
      @expected_checksum = sha256_checksum(path)
    end

    describe "and the target file has the correct permissions" do

      # When permissions and content are correct, chef should do nothing and
      # the resource should not be marked updated.
      let(:expect_updated?) { false }

      include_context "setup correct permissions"

      it_behaves_like "a file with the correct content"

      it_behaves_like "a securable resource with existing target"
    end

    context "and the target file has incorrect permissions" do
      include_context "setup broken permissions"

      it_behaves_like "a file with the correct content"

      it_behaves_like "a securable resource with existing target"
    end
  end

  # Regression test for http://tickets.opscode.com/browse/CHEF-4419
  context "when the path starts with '/' and target file exists", :windows_only do
    let(:path) do
      File.join(test_file_dir[2..test_file_dir.length], make_tmpname(file_base))
    end

    before do
      File.open(path, "wb") { |f| f.print expected_content }
      now = Time.now.to_i
      File.utime(now - 9000, now - 9000, path)

      @expected_mtime = File.stat(path).mtime
      @expected_checksum = sha256_checksum(path)
    end

    describe ":create action should run without any updates" do
      before do
        # Assert starting state is as expected
        File.should exist(path)
        sha256_checksum(path).should == @expected_checksum
        resource.run_action(:create)
      end

      it "does not overwrite the original when the :create action is run" do
        sha256_checksum(path).should == @expected_checksum
      end

      it "does not update the mtime of the file when the :create action is run" do
        File.stat(path).mtime.should == @expected_mtime
      end

      it "is not marked as updated by last action" do
        resource.should_not be_updated_by_last_action
      end
    end
  end

end

shared_context Chef::Resource::File  do
  if windows?
    require 'chef/win32/file'
  end

  # We create the files in a different directory than tmp to exercise
  # different file deployment strategies more completely.
  let(:test_file_dir) do
    if windows?
      File.join(ENV['systemdrive'], "test-dir")
    else
      File.join(CHEF_SPEC_DATA, "test-dir")
    end
  end

  let(:path) do
    File.join(test_file_dir, make_tmpname(file_base))
  end

  before do
    FileUtils::mkdir_p(test_file_dir)
  end

  after(:each) do
    FileUtils.rm_r(path) if File.exists?(path)
    FileUtils.rm_r(CHEF_SPEC_BACKUP_PATH) if File.exists?(CHEF_SPEC_BACKUP_PATH)
  end

  after do
    FileUtils::rm_rf(test_file_dir)
  end
end