Add the FrozenStrings magic comment
[puppet-modules/puppetlabs-apt.git] / spec / unit / puppet / provider / apt_key_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 describe Puppet::Type.type(:apt_key).provider(:apt_key) do
6   describe 'instances' do
7     it 'has an instance method' do
8       expect(described_class).to respond_to :instances
9     end
10   end
11
12   describe 'prefetch' do
13     it 'has a prefetch method' do
14       expect(described_class).to respond_to :prefetch
15     end
16   end
17
18   context 'self.instances no key' do
19     before :each do
20       # Unable to remove `master` from below terminology as it relies on outside code
21       allow(described_class).to receive(:apt_key).with(
22         ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
23       ).and_return('uid:-::::1284991450::07BEBE04F4AE4A8E885A761325717D8509D9C1DC::Ubuntu Extras Archive Automatic Signing Key <ftpmaster@ubuntu.com>::::::::::0:')
24     end
25     it 'returns no resources' do
26       expect(described_class.instances.size).to eq(0)
27     end
28   end
29
30   context 'self.instances multiple keys' do
31     before :each do
32       command_output = <<-OUTPUT
33 Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.DU0GdRxjmE --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyring /etc/apt/trusted.gpg.d/puppetlabs-pc1-keyring.gpg --no-tty --list-keys --with-colons --fingerprint --fixed-list-mode
34 tru:t:1:1549900774:0:3:1:5
35 pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:
36 fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:
37 uid:-::::1095016255::B84AE656F4F5A826C273A458512EF8E282754CE1::Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>:
38 sub:-:2048:16:251BEFF479164387:1095016263::::::e:
39 pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
40 fpr:::::::::C5986B4F1257FFA86632CBA746181433FBB75451:
41 OUTPUT
42       allow(described_class).to receive(:apt_key).with(
43         ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
44       ).and_return(command_output)
45     end
46     it 'returns 2 resources' do
47       expect(described_class.instances.size).to eq(2)
48       expect(described_class.instances[0].name).to eq('630239CC130E1A7FD81A27B140976EAF437D05B5')
49       expect(described_class.instances[0].id).to eq('40976EAF437D05B5')
50       expect(described_class.instances[1].name).to eq('C5986B4F1257FFA86632CBA746181433FBB75451')
51       expect(described_class.instances[1].id).to eq('46181433FBB75451')
52     end
53   end
54
55   context 'create apt_key resource' do
56     it 'apt_key with content set and source nil' do
57       expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
58                                                          '--keyserver',
59                                                          :"keyserver.ubuntu.com",
60                                                          '--recv-keys',
61                                                          'C105B9DE'])
62       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
63                                            id: 'C105B9DE',
64                                            ensure: 'present')
65
66       provider = described_class.new(resource)
67       expect(provider).not_to be_exist
68       provider.create
69       expect(provider).to be_exist
70     end
71
72     it 'apt_key content and source nil, options set' do
73       expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
74                                                          '--keyserver',
75                                                          :"keyserver.ubuntu.com",
76                                                          '--keyserver-options',
77                                                          'jimno',
78                                                          '--recv-keys',
79                                                          'C105B9DE'])
80       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
81                                            id: 'C105B9DE',
82                                            options: 'jimno',
83                                            ensure: 'present')
84
85       provider = described_class.new(resource)
86       expect(provider).not_to be_exist
87       provider.create
88       expect(provider).to be_exist
89     end
90
91     it 'apt_key with content set' do
92       expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
93       resource = Puppet::Type::Apt_key.new(name: 'gsd',
94                                            id: 'C105B9DE',
95                                            content: 'asad',
96                                            ensure: 'present')
97
98       provider = described_class.new(resource)
99       expect(provider).not_to be_exist
100       expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
101       provider.create
102       expect(provider).to be_exist
103     end
104
105     it 'apt_key with source set' do
106       expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
107       resource = Puppet::Type::Apt_key.new(name: 'gsd',
108                                            id: 'C105B9DE',
109                                            source: 'ftp://bla/herpderp.gpg',
110                                            ensure: 'present')
111
112       provider = described_class.new(resource)
113       expect(provider).not_to be_exist
114       expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
115       provider.create
116       expect(provider).to be_exist
117     end
118
119     it 'apt_key with source and weak ssl verify set' do
120       expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
121       resource = Puppet::Type::Apt_key.new(name: 'gsd',
122                                            id: 'C105B9DE',
123                                            source: 'https://bla/herpderp.gpg',
124                                            ensure: 'present',
125                                            weak_ssl: true)
126
127       provider = described_class.new(resource)
128       expect(provider).not_to be_exist
129       expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
130       provider.create
131       expect(provider).to be_exist
132     end
133
134     describe 'different valid id keys' do
135       hash_of_keys = {
136         '32bit key id' => 'EF8D349F',
137         '64bit key id' => '7F438280EF8D349F',
138         '160bit key fingerprint' => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
139         '32bit key id lowercase' =>   'EF8D349F'.downcase,
140         '64bit key id lowercase' =>   '7F438280EF8D349F'.downcase,
141         '160bit key fingerprint lowercase' => '6F6B15509CF8E59E6E469F327F438280EF8D349F'.downcase,
142         '32bit key id 0x formatted' =>   '0xEF8D349F',
143         '64bit key id 0x formatted' =>   '0x7F438280EF8D349F',
144         '160bit key fingerprint 0x formatted' => '0x6F6B15509CF8E59E6E469F327F438280EF8D349F',
145       }
146       hash_of_keys.each do |key_type, value|
147         it "#{key_type} #{value} is valid" do
148           expect(described_class).to receive(:apt_key).with(array_including('adv', '--no-tty',
149                                                                             '--keyserver',
150                                                                             :"keyserver.ubuntu.com",
151                                                                             '--recv-keys'))
152           resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
153                                                id: value,
154                                                ensure: 'present')
155
156           provider = described_class.new(resource)
157           expect(provider).not_to be_exist
158           provider.create
159           expect(provider).to be_exist
160         end
161       end
162     end
163
164     it 'apt_key with invalid key length' do
165       expect {
166         Puppet::Type::Apt_key.new(name: 'source and content nil',
167                                   id: '1',
168                                   ensure: 'present')
169       }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
170     end
171   end
172
173   context 'key_line_hash function' do
174     it 'matches rsa' do
175       expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
176         key_expiry: nil,
177         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
178         key_long: '40976EAF437D05B5',
179         key_short: '437D05B5',
180         key_size: '1024',
181         key_type: :rsa,
182       )
183     end
184
185     it 'matches dsa' do
186       expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
187         key_expiry: nil,
188         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
189         key_long: '40976EAF437D05B5',
190         key_short: '437D05B5',
191         key_size: '1024',
192         key_type: :dsa,
193       )
194     end
195
196     it 'matches ecc' do
197       expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
198         key_expiry: nil,
199         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
200         key_long: '40976EAF437D05B5',
201         key_short: '437D05B5',
202         key_size: '1024',
203         key_type: :ecc,
204       )
205     end
206
207     it 'matches ecdsa' do
208       expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
209         key_expiry: nil,
210         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
211         key_long: '40976EAF437D05B5',
212         key_short: '437D05B5',
213         key_size: '1024',
214         key_type: :ecdsa,
215       )
216     end
217   end
218 end