File: test_helper.rb

package info (click to toggle)
ruby-gpgme 2.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,920 kB
  • sloc: ruby: 3,129; ansic: 2,559; sh: 7; makefile: 5
file content (110 lines) | stat: -rw-r--r-- 2,038 bytes parent folder | download | duplicates (3)
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
# -*- encoding: utf-8 -*-

# include compiled gpgme_n.bundle
tmp_dir = File.join(File.dirname(__FILE__), '..', 'tmp')
$:.unshift(tmp_dir) if File.directory?(tmp_dir)

# this interfers otherwise with our tests
ENV.delete('GPG_AGENT_INFO')

require 'rubygems'
require 'minitest/autorun'
require 'minitest/spec'
require 'minitest/pride'
require 'mocha'
require 'gpgme'

require File.dirname(__FILE__) + "/support/resources"


def import_keys
  KEYS.each do |key|
    import_key(key)
  end
end

def import_key(key, only = :all)
  GPGME::Key.import(key[:public]) unless only == :secret
  GPGME::Key.import(key[:secret]) unless only == :public
end

def remove_keys
  KEYS.each do |key|
    remove_key(key)
  end
end

def remove_all_keys
  GPGME::Key.find(:public).each do |k|
    k.delete!(true)
  end
  GPGME::Key.find(:secret).each do |k|
    k.delete!(true)
  end
end

def remove_key(key)
  GPGME::Key.find(:public, key[:sha]).each do |k|
    k.delete!(true)
  end
  GPGME::Key.find(:secret, key[:sha]).each do |k|
    k.delete!(true)
  end
end

def with_key(key, only = :all, &block)
  import_key key, only

  begin
    yield
  ensure
    remove_key key
  end
end

def without_key(key, &block)
  remove_key key

  begin
    yield
  ensure
    import_key key
  end
end

DIRS = []

at_exit do
  DIRS.each do |dir|
    FileUtils.remove_entry dir
  end
  DIRS.clear
end

def ensure_keys(proto)
  return false unless GPGME::Engine.check_version proto

  case proto
  when GPGME::PROTOCOL_OpenPGP
    # We use a different home directory for the keys to not disturb current
    # installation
    require 'tmpdir'
    require 'pathname'

    if DIRS.empty?
      dir = Dir.mktmpdir
      GPGME::Engine.home_dir = dir
      DIRS.push(dir)
      pinentry = Pathname.new(__FILE__).dirname + 'pinentry'
      gpg_agent_conf = Pathname.new(dir) + 'gpg-agent.conf'
      gpg_agent_conf.open('w+') {|io|
        io.write("pinentry-program #{pinentry}\n")
      }
      remove_all_keys
      import_keys
    end
    true
  else
    return false
  end
end