Welcome to mirror list, hosted at ThFree Co, Russian Federation.

recursive_merge_folders_spec.rb « import_export « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b9590da421ebac82aa33e7fea7d7d1312869801 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::RecursiveMergeFolders do
  describe '.merge' do
    it 'merges folder and ignores symlinks and files that share hard links' do
      Dir.mktmpdir do |tmpdir|
        source = "#{tmpdir}/source"
        FileUtils.mkdir_p("#{source}/folder/folder")
        FileUtils.touch("#{source}/file1.txt")
        FileUtils.touch("#{source}/file_that_shares_hard_links.txt")
        FileUtils.touch("#{source}/folder/file2.txt")
        FileUtils.touch("#{source}/folder/folder/file3.txt")
        FileUtils.ln_s("#{source}/file1.txt", "#{source}/symlink-file1.txt")
        FileUtils.ln_s("#{source}/folder", "#{source}/symlink-folder")
        FileUtils.link("#{source}/file_that_shares_hard_links.txt", "#{source}/hard_link.txt")

        target = "#{tmpdir}/target"
        FileUtils.mkdir_p("#{target}/folder/folder")
        FileUtils.mkdir_p("#{target}/folderA")
        FileUtils.touch("#{target}/fileA.txt")

        described_class.merge(source, target)

        expect(Dir.children("#{tmpdir}/target")).to match_array(%w[folder file1.txt folderA fileA.txt])
        expect(Dir.children("#{tmpdir}/target/folder")).to match_array(%w[folder file2.txt])
        expect(Dir.children("#{tmpdir}/target/folder/folder")).to match_array(%w[file3.txt])
      end
    end

    it 'raises an error for invalid source path' do
      Dir.mktmpdir do |tmpdir|
        expect do
          described_class.merge("#{tmpdir}/../", tmpdir)
        end.to raise_error(Gitlab::PathTraversal::PathTraversalAttackError)
      end
    end

    it 'raises an error for source path outside temp dir' do
      Dir.mktmpdir do |tmpdir|
        expect do
          described_class.merge('/', tmpdir )
        end.to raise_error(StandardError, 'path / is not allowed')
      end
    end

    it 'raises an error for invalid target path' do
      Dir.mktmpdir do |tmpdir|
        expect do
          described_class.merge(tmpdir, "#{tmpdir}/../")
        end.to raise_error(Gitlab::PathTraversal::PathTraversalAttackError)
      end
    end
  end
end