File: extra_field_ut_test.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (98 lines) | stat: -rw-r--r-- 2,585 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
# frozen_string_literal: true

require_relative 'test_helper'

class ZipExtraFieldUTTest < Minitest::Test
  PARSE_TESTS = [
    ["UT\x05\x00\x01PS>A", 0b001, true, true, false],
    ["UT\x05\x00\x02PS>A", 0b010, false, true, true],
    ["UT\x05\x00\x04PS>A", 0b100, true, false, true],
    ["UT\x09\x00\x03PS>APS>A", 0b011, false, true, false],
    ["UT\x09\x00\x05PS>APS>A", 0b101, true, false, false],
    ["UT\x09\x00\x06PS>APS>A", 0b110, false, false, true],
    ["UT\x13\x00\x07PS>APS>APS>A", 0b111, false, false, false]
  ].freeze

  def test_parse
    PARSE_TESTS.each do |bin, flags, a, c, m|
      ut = ::Zip::ExtraField::UniversalTime.new(bin)
      assert_equal(flags, ut.flag)
      assert(ut.atime.nil? == a)
      assert(ut.ctime.nil? == c)
      assert(ut.mtime.nil? == m)
    end
  end

  def test_parse_size_zero
    ut = ::Zip::ExtraField::UniversalTime.new("UT\x00")
    assert_equal(0b000, ut.flag)
    assert_nil(ut.atime)
    assert_nil(ut.ctime)
    assert_nil(ut.mtime)
  end

  def test_parse_size_nil
    ut = ::Zip::ExtraField::UniversalTime.new('UT')
    assert_equal(0b000, ut.flag)
    assert_nil(ut.atime)
    assert_nil(ut.ctime)
    assert_nil(ut.mtime)
  end

  def test_parse_nil
    ut = ::Zip::ExtraField::UniversalTime.new
    assert_equal(0b000, ut.flag)
    assert_nil(ut.atime)
    assert_nil(ut.ctime)
    assert_nil(ut.mtime)
  end

  def test_set_clear_times
    time = ::Zip::DOSTime.now
    ut = ::Zip::ExtraField::UniversalTime.new
    assert_equal(0b000, ut.flag)

    ut.mtime = time
    assert_equal(0b001, ut.flag)
    assert_equal(time, ut.mtime)

    ut.ctime = time
    assert_equal(0b101, ut.flag)
    assert_equal(time, ut.ctime)

    ut.atime = time
    assert_equal(0b111, ut.flag)
    assert_equal(time, ut.atime)

    ut.ctime = nil
    assert_equal(0b011, ut.flag)
    assert_nil ut.ctime

    ut.mtime = nil
    assert_equal(0b010, ut.flag)
    assert_nil ut.mtime

    ut.atime = nil
    assert_equal(0b000, ut.flag)
    assert_nil ut.atime
  end

  def test_pack
    time = ::Zip::DOSTime.at('PS>A'.unpack1('l<'))
    ut = ::Zip::ExtraField::UniversalTime.new
    assert_equal("\x00", ut.pack_for_local)
    assert_equal("\x00", ut.pack_for_c_dir)

    ut.mtime = time
    assert_equal("\x01PS>A", ut.pack_for_local)
    assert_equal("\x01PS>A", ut.pack_for_c_dir)

    ut.atime = time
    assert_equal("\x03PS>APS>A", ut.pack_for_local)
    assert_equal("\x03PS>A", ut.pack_for_c_dir)

    ut.ctime = time
    assert_equal("\x07PS>APS>APS>A", ut.pack_for_local)
    assert_equal("\x07PS>A", ut.pack_for_c_dir)
  end
end