summaryrefslogtreecommitdiff
path: root/spec/unit/win32/link_spec.rb
blob: 04fc0457034a777c8b1b41af103bfe4195230697 (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
#
# Copyright:: Copyright 2012-2017, 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 "spec_helper"
if ChefUtils.windows?
  require "chef/win32/api/file"
  require "chef/win32/file"
  require "chef/win32/version"
end

describe Chef::ReservedNames::Win32::File, :windows_only do
  context "#symlink" do
    let(:with_privilege) { Chef::ReservedNames::Win32::API::File::SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE }
    let(:without_privilege) { 0x0 }

    context "an invalid parameter is passed" do
      it "will throw an exception if an invalid parameter is passed" do
        allow(File).to receive(:directory?).and_return(false)
        allow(Chef::ReservedNames::Win32::File).to receive(:encode_path) { |a| a }
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:windows_10?).and_return(true)
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:build_number).and_return(1)
        allow(Chef::ReservedNames::Win32::File).to receive(:CreateSymbolicLinkW).and_return(nil)

        expect { Chef::ReservedNames::Win32::File.symlink("a", "b") }.to raise_error Chef::Exceptions::Win32APIError
      end
    end

    context "a valid parameter is passed" do
      before(:each) do
        allow(File).to receive(:directory?).and_return(false)
        allow(Chef::ReservedNames::Win32::File).to receive(:encode_path) { |a| a }
        allow(Chef::ReservedNames::Win32::File).to receive(:CreateSymbolicLinkW).with(any_args) { "don't //actually// do this" }
      end

      it "will not pass the unprivileged symlink flag if the node is not Windows 10" do
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:windows_10?).and_return(false)

        expect(Chef::ReservedNames::Win32::File).to receive(:CreateSymbolicLinkW).with("b", "a", without_privilege)
        described_class.symlink("a", "b")
      end

      it "will not pass the unprivileged symlink flag if the node is not at least Windows 10 Creators Update" do
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:windows_10?).and_return(true)
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:build_number).and_return(1)

        expect(Chef::ReservedNames::Win32::File).to receive(:CreateSymbolicLinkW).with("b", "a", without_privilege)
        described_class.symlink("a", "b")
      end

      it "will pass the unprivileged symlink flag if the node is Windows 10 Creators Update or higher" do
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:windows_10?).and_return(true)
        allow_any_instance_of(Chef::ReservedNames::Win32::Version).to receive(:build_number).and_return(15063)

        expect(Chef::ReservedNames::Win32::File).to receive(:CreateSymbolicLinkW).with("b", "a", with_privilege)
        described_class.symlink("a", "b")
      end
    end
  end
end