File: windows_test.rb

package info (click to toggle)
ruby-train 3.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: ruby: 10,002; sh: 17; makefile: 8
file content (99 lines) | stat: -rw-r--r-- 3,515 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
require "helper"
require "train/transports/mock"
require "train/file/local/windows"

describe "file common" do
  let(:cls) { Train::File::Local::Windows }
  let(:backend) do
    backend = Train::Transports::Mock.new.connection
    backend.mock_os({ family: "windows" })
    backend
  end

  it "check escaping of invalid chars in path" do
    wf = cls.new(nil, nil)
    _(wf.sanitize_filename("c:/test") ).must_equal "c:/test"
    _(wf.sanitize_filename("c:/test directory") ).must_equal "c:/test directory"
    %w{ < > " * ?}.each do |char|
      _(wf.sanitize_filename("c:/test#{char}directory") ).must_equal "c:/testdirectory"
    end
  end

  it "returns file version" do
    out = rand.to_s
    backend.mock_command('[System.Diagnostics.FileVersionInfo]::GetVersionInfo("path").FileVersion', out)
    _(cls.new(backend, "path").file_version).must_equal out
  end

  it "returns product version" do
    out = rand.to_s
    backend.mock_command('[System.Diagnostics.FileVersionInfo]::GetVersionInfo("path").FileVersion', out)
    _(cls.new(backend, "path").file_version).must_equal out
  end

  it "returns owner of file" do
    out = rand.to_s
    backend.mock_command('Get-Acl "path" | select -expand Owner', out)
    _(cls.new(backend, "path").owner).must_equal out
  end

  describe "#md5sum" do
    let(:md5_checksum) { "4ce0c733cdcf1d2f78532bbd9ce3441d" }
    let(:filepath) { 'C:\Windows\explorer.exe' }

    let(:ruby_md5_mock) do
      checksum_mock = mock
      checksum_mock.expects(:update).returns("")
      checksum_mock.expects(:hexdigest).returns(md5_checksum)
      checksum_mock
    end

    it "defaults to a Ruby based checksum if other methods fail" do
      backend.mock_command("CertUtil -hashfile #{filepath} MD5", "", "", 1)
      Digest::MD5.expects(:new).returns(ruby_md5_mock)
      _(cls.new(backend, "/tmp/testfile").md5sum).must_equal md5_checksum
    end

    it "calculates the correct md5sum on the `windows` platform family" do
      output = <<-EOC
        MD5 hash of file C:\\Windows\\explorer.exe:\r
        4c e0 c7 33 cd cf 1d 2f 78 53 2b bd 9c e3 44 1d\r
        CertUtil: -hashfile command completed successfully.\r
      EOC

      backend.mock_command("CertUtil -hashfile #{filepath} MD5", output)
      _(cls.new(backend, filepath).md5sum).must_equal md5_checksum
    end
  end

  describe "#sha256sum" do
    let(:sha256_checksum) do
      "85270240a5fd51934f0627c92b2282749d071fdc9ac351b81039ced5b10f798b"
    end
    let(:filepath) { 'C:\Windows\explorer.exe' }

    let(:ruby_sha256_mock) do
      checksum_mock = mock
      checksum_mock.expects(:update).returns("")
      checksum_mock.expects(:hexdigest).returns(sha256_checksum)
      checksum_mock
    end

    it "defaults to a Ruby based checksum if other methods fail" do
      backend.mock_command('CertUtil -hashfile #{filepath} SHA256', "", "", 1)
      Digest::SHA256.expects(:new).returns(ruby_sha256_mock)
      _(cls.new(backend, "/tmp/testfile").sha256sum).must_equal sha256_checksum
    end

    it "calculates the correct sha256sum on the `windows` platform family" do
      output = <<-EOC
        SHA256 hash of file C:\\Windows\\explorer.exe:\r
        85 27 02 40 a5 fd 51 93 4f 06 27 c9 2b 22 82 74 9d 07 1f dc 9a c3 51 b8 10 39 ce d5 b1 0f 79 8b\r
        CertUtil: -hashfile command completed successfully.\r
      EOC

      backend.mock_command("CertUtil -hashfile #{filepath} SHA256", output)
      _(cls.new(backend, filepath).sha256sum).must_equal sha256_checksum
    end
  end
end