File: etc_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (465 lines) | stat: -rw-r--r-- 17,394 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
require 'puppet'
require 'spec_helper'
require 'puppet_spec/character_encoding'

# The Ruby::Etc module is largely non-functional on Windows - many methods
# simply return nil regardless of input, the Etc::Group struct is not defined,
# and Etc::Passwd is missing fields
# We want to test that:
# - We correctly set external encoding values IF they're valid UTF-8 bytes
# - We do not modify non-UTF-8 values if they're NOT valid UTF-8 bytes

describe Puppet::Etc, :if => !Puppet.features.microsoft_windows? do
  # http://www.fileformat.info/info/unicode/char/5e0c/index.htm
  # 希 Han Character 'rare; hope, expect, strive for'
  # In EUC_KR: \xfd \xf1 - 253 241
  # In UTF-8: \u5e0c - \xe5 \xb8 \x8c - 229 184 140
  let(:euc_kr) { [253, 241].pack('C*').force_encoding(Encoding::EUC_KR) } # valid_encoding? == true
  let(:euc_kr_as_binary) { [253, 241].pack('C*') } # valid_encoding? == true
  let(:euc_kr_as_utf_8) { [253, 241].pack('C*').force_encoding(Encoding::UTF_8) } # valid_encoding? == false

  # characters representing different UTF-8 widths
  # 1-byte A
  # 2-byte ۿ - http://www.fileformat.info/info/unicode/char/06ff/index.htm - 0xDB 0xBF / 219 191
  # 3-byte ᚠ - http://www.fileformat.info/info/unicode/char/16A0/index.htm - 0xE1 0x9A 0xA0 / 225 154 160
  # 4-byte 𠜎 - http://www.fileformat.info/info/unicode/char/2070E/index.htm - 0xF0 0xA0 0x9C 0x8E / 240 160 156 142
  let(:mixed_utf_8) { "A\u06FF\u16A0\u{2070E}".force_encoding(Encoding::UTF_8) } # Aۿᚠ𠜎
  let(:mixed_utf_8_as_binary) { "A\u06FF\u16A0\u{2070E}".force_encoding(Encoding::BINARY) }
  let(:mixed_utf_8_as_euc_kr) { "A\u06FF\u16A0\u{2070E}".force_encoding(Encoding::EUC_KR) }

  # An uninteresting value that ruby might return in an Etc struct.
  let(:root) { 'root' }

  # Set up example Etc Group structs with values representative of what we would
  # get back in these encodings

  let(:utf_8_group_struct) do
    group = Etc::Group.new
    # In a UTF-8 environment, these values will come back as UTF-8, even if
    # they're not valid UTF-8. We do not modify anything about either the
    # valid or invalid UTF-8 strings.

    # Group member contains a mix of valid and invalid UTF-8-labeled strings
    group.mem = [mixed_utf_8, root.dup.force_encoding(Encoding::UTF_8), euc_kr_as_utf_8]
    # group name contains same EUC_KR bytes labeled as UTF-8
    group.name = euc_kr_as_utf_8
    # group passwd field is valid UTF-8
    group.passwd = mixed_utf_8
    group.gid = 12345
    group
  end

  let(:euc_kr_group_struct) do
    # In an EUC_KR environment, values will come back as EUC_KR, even if they're
    # not valid in that encoding. For values that are valid in UTF-8 we expect
    # their external encoding to be set to UTF-8 by Puppet::Etc. For values that
    # are invalid in UTF-8, we expect the string to be kept intact, unmodified,
    # as we can't transcode it.
    group = Etc::Group.new
    group.mem = [euc_kr, root.dup.force_encoding(Encoding::EUC_KR), mixed_utf_8_as_euc_kr]
    group.name = euc_kr
    group.passwd = mixed_utf_8_as_euc_kr
    group.gid = 12345
    group
  end

  let(:ascii_group_struct) do
    # In a POSIX environment, any strings containing only values under
    # code-point 128 will be returned as ASCII, whereas anything above that
    # point will be returned as BINARY. In either case we override the encoding
    # to UTF-8 if that would be valid.
    group = Etc::Group.new
    group.mem = [euc_kr_as_binary, root.dup.force_encoding(Encoding::ASCII), mixed_utf_8_as_binary]
    group.name = euc_kr_as_binary
    group.passwd = mixed_utf_8_as_binary
    group.gid = 12345
    group
  end

  let(:utf_8_user_struct) do
    user = Etc::Passwd.new
    # user name contains same EUC_KR bytes labeled as UTF-8
    user.name = euc_kr_as_utf_8
    # group passwd field is valid UTF-8
    user.passwd = mixed_utf_8
    user.uid = 12345
    user
  end

  let(:euc_kr_user_struct) do
    user = Etc::Passwd.new
    user.name = euc_kr
    user.passwd = mixed_utf_8_as_euc_kr
    user.uid = 12345
    user
  end

  let(:ascii_user_struct) do
    user = Etc::Passwd.new
    user.name = euc_kr_as_binary
    user.passwd = mixed_utf_8_as_binary
    user.uid = 12345
    user
  end

  shared_examples "methods that return an overridden group struct from Etc" do |param|
    it "should return a new Struct object with corresponding canonical_ members" do
      group = Etc::Group.new
      expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(group)
      puppet_group = Puppet::Etc.send(subject, *param)

      expect(puppet_group.members).to include(*group.members)
      expect(puppet_group.members).to include(*group.members.map { |mem| "canonical_#{mem}".to_sym })

      # Confirm we haven't just added the new members to the original struct object, ie this is really a new struct
      expect(group.members.any? { |elem| elem.match(/^canonical_/) }).to be_falsey
    end

    context "when Encoding.default_external is UTF-8" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(utf_8_group_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::UTF_8) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should leave the valid UTF-8 values in arrays unmodified" do
        expect(overridden.mem[0]).to eq(mixed_utf_8)
        expect(overridden.mem[1]).to eq(root)
      end

      it "should replace invalid characters with replacement characters in invalid UTF-8 values in arrays" do
        expect(overridden.mem[2]).to eq("\uFFFD\uFFFD")
      end

      it "should keep an unmodified version of the invalid UTF-8 values in arrays in the corresponding canonical_ member" do
        expect(overridden.canonical_mem[2]).to eq(euc_kr_as_utf_8)
      end

      it "should leave the valid UTF-8 values unmodified" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should replace invalid characters with '?' characters in invalid UTF-8 values" do
        expect(overridden.name).to eq("\uFFFD\uFFFD")
      end

      it "should keep an unmodified version of the invalid UTF-8 values in the corresponding canonical_ member" do
        expect(overridden.canonical_name).to eq(euc_kr_as_utf_8)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        utf_8_group_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end

    context "when Encoding.default_external is EUC_KR (i.e., neither UTF-8 nor POSIX)" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(euc_kr_group_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::EUC_KR) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should override EUC_KR-labeled values in arrays to UTF-8 if that would result in valid UTF-8" do
        expect(overridden.mem[2]).to eq(mixed_utf_8)
        expect(overridden.mem[1]).to eq(root)
      end

      it "should leave valid EUC_KR-labeled values that would not be valid UTF-8 in arrays unmodified" do
        expect(overridden.mem[0]).to eq(euc_kr)
      end

      it "should override EUC_KR-labeled values to UTF-8 if that would result in valid UTF-8" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should leave valid EUC_KR-labeled values that would not be valid UTF-8 unmodified" do
        expect(overridden.name).to eq(euc_kr)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        euc_kr_group_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end

    context "when Encoding.default_external is POSIX (ASCII-7bit)" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(ascii_group_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::ASCII) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should not modify binary values in arrays that would be invalid UTF-8" do
        expect(overridden.mem[0]).to eq(euc_kr_as_binary)
      end

      it "should set the encoding to UTF-8 on binary values in arrays that would be valid UTF-8" do
        expect(overridden.mem[1]).to eq(root.dup.force_encoding(Encoding::UTF_8))
        expect(overridden.mem[2]).to eq(mixed_utf_8)
      end

      it "should not modify binary values that would be invalid UTF-8" do
        expect(overridden.name).to eq(euc_kr_as_binary)
      end

      it "should set the encoding to UTF-8 on binary values that would be valid UTF-8" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        ascii_group_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end
  end

  shared_examples "methods that return an overridden user struct from Etc" do |param|
    it "should return a new Struct object with corresponding canonical_ members" do
      user = Etc::Passwd.new
      expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(user)
      puppet_user = Puppet::Etc.send(subject, *param)

      expect(puppet_user.members).to include(*user.members)
      expect(puppet_user.members).to include(*user.members.map { |mem| "canonical_#{mem}".to_sym })
      # Confirm we haven't just added the new members to the original struct object, ie this is really a new struct
      expect(user.members.any? { |elem| elem.match(/^canonical_/)}).to be_falsey
    end

    context "when Encoding.default_external is UTF-8" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(utf_8_user_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::UTF_8) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should leave the valid UTF-8 values unmodified" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should replace invalid characters with unicode replacement characters in invalid UTF-8 values" do
        expect(overridden.name).to eq("\uFFFD\uFFFD")
      end

      it "should keep an unmodified version of the invalid UTF-8 values in the corresponding canonical_ member" do
        expect(overridden.canonical_name).to eq(euc_kr_as_utf_8)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        utf_8_user_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end

    context "when Encoding.default_external is EUC_KR (i.e., neither UTF-8 nor POSIX)" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(euc_kr_user_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::EUC_KR) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should override valid UTF-8 EUC_KR-labeled values to UTF-8" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should leave invalid EUC_KR-labeled values unmodified" do
        expect(overridden.name).to eq(euc_kr)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        euc_kr_user_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end

    context "when Encoding.default_external is POSIX (ASCII-7bit)" do
      before do
        expect(Etc).to receive(subject).with(param.nil? ? no_args : param).and_return(ascii_user_struct)
      end

      let(:overridden) {
        PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::ASCII) do
          Puppet::Etc.send(subject, *param)
        end
      }

      it "should not modify binary values that would be invalid UTF-8" do
        expect(overridden.name).to eq(euc_kr_as_binary)
      end

      it "should set the encoding to UTF-8 on binary values that would be valid UTF-8" do
        expect(overridden.passwd).to eq(mixed_utf_8)
      end

      it "should copy all values to the new struct object" do
        # Confirm we've actually copied all the values to the canonical_members
        ascii_user_struct.each_pair do |member, value|
          expect(overridden["canonical_#{member}"]).to eq(value)

          # Confirm we've reassigned all non-string and array values
          if !value.is_a?(String) && !value.is_a?(Array)
            expect(overridden[member]).to eq(value)
            expect(overridden[member].object_id).to eq(value.object_id)
          end
        end
      end
    end
  end

  describe :getgrent do
    it_should_behave_like "methods that return an overridden group struct from Etc"
  end

  describe :getgrnam do
    it_should_behave_like "methods that return an overridden group struct from Etc", 'foo'

    it "should call Etc.getgrnam with the supplied group name" do
      expect(Etc).to receive(:getgrnam).with('foo')
      Puppet::Etc.getgrnam('foo')
    end
  end

  describe :getgrgid do
    it_should_behave_like "methods that return an overridden group struct from Etc", 0

    it "should call Etc.getgrgid with supplied group id" do
      expect(Etc).to receive(:getgrgid).with(0)
      Puppet::Etc.getgrgid(0)
    end
  end

  describe :getpwent do
    it_should_behave_like "methods that return an overridden user struct from Etc"
  end

  describe :getpwnam do
    it_should_behave_like "methods that return an overridden user struct from Etc", 'foo'

    it "should call Etc.getpwnam with that username" do
      expect(Etc).to receive(:getpwnam).with('foo')
      Puppet::Etc.getpwnam('foo')
    end
  end

  describe :getpwuid do
    it_should_behave_like "methods that return an overridden user struct from Etc", 2

    it "should call Etc.getpwuid with the id" do
      expect(Etc).to receive(:getpwuid).with(2)
      Puppet::Etc.getpwuid(2)
    end
  end

  describe :group do
    it 'should return the next group struct if a block is not provided' do
      expect(Puppet::Etc).to receive(:getgrent).and_return(ascii_group_struct)

      expect(Puppet::Etc.group).to eql(ascii_group_struct)
    end

    it 'should iterate over the available groups if a block is provided' do
      expected_groups = [
        utf_8_group_struct,
        euc_kr_group_struct,
        ascii_group_struct
      ]
      allow(Puppet::Etc).to receive(:getgrent).and_return(*(expected_groups + [nil]))

      expect(Puppet::Etc).to receive(:setgrent)
      expect(Puppet::Etc).to receive(:endgrent)

      actual_groups = []
      Puppet::Etc.group { |group| actual_groups << group }

      expect(actual_groups).to eql(expected_groups)
    end
  end

  describe "endgrent" do
    it "should call Etc.getgrent" do
      expect(Etc).to receive(:getgrent)
      Puppet::Etc.getgrent
    end
  end

  describe "setgrent" do
    it "should call Etc.setgrent" do
      expect(Etc).to receive(:setgrent)
      Puppet::Etc.setgrent
    end
  end

  describe "endpwent" do
    it "should call Etc.endpwent" do
      expect(Etc).to receive(:endpwent)
      Puppet::Etc.endpwent
    end
  end

  describe "setpwent" do
    it "should call Etc.setpwent" do
      expect(Etc).to receive(:setpwent)
      Puppet::Etc.setpwent
    end
  end
end