File: stringz_test.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (127 lines) | stat: -rwxr-xr-x 2,886 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
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

describe BinData::Stringz, "when empty" do
  let(:obj) { BinData::Stringz.new }

  it "initial state" do
    _(obj.value).must_equal ""
    _(obj.num_bytes).must_equal 1
    _(obj.to_binary_s).must_equal_binary "\0"
  end
end

describe BinData::Stringz, "with value set" do
  let(:obj) { BinData::Stringz.new("abcd") }

  it "initial state" do
    _(obj.value).must_equal "abcd"
    _(obj.num_bytes).must_equal 5
    _(obj.to_binary_s).must_equal_binary "abcd\0"
  end
end

describe BinData::Stringz, "when reading" do
  let(:obj) { BinData::Stringz.new }

  it "stops at the first zero byte" do
    io = StringIO.new("abcd\0xyz\0")
    obj.read(io)
    _(io.pos).must_equal 5
    _(obj).must_equal "abcd"
  end

  it "handles a zero length string" do
    io = StringIO.new("\0abcd")
    obj.read(io)
    _(io.pos).must_equal 1
    _(obj).must_equal ""
  end

  it "fails if no zero byte is found" do
    _ {obj.read("abcd") }.must_raise EOFError
  end
end

describe BinData::Stringz, "when setting the value" do
  let(:obj) { BinData::Stringz.new }

  it "includes the zero byte in num_bytes total" do
    obj.assign("abcd")
    _(obj.num_bytes).must_equal 5
  end

  it "accepts empty strings" do
    obj.assign("")
    _(obj).must_equal ""
  end

  it "accepts strings that aren't zero terminated" do
    obj.assign("abcd")
    _(obj).must_equal "abcd"
  end

  it "accepts strings that are zero terminated" do
    obj.assign("abcd\0")
    _(obj).must_equal "abcd"
  end

  it "accepts up to the first zero byte" do
    obj.assign("abcd\0xyz\0")
    _(obj).must_equal "abcd"
  end
end

describe BinData::Stringz, "with max_length" do
  let(:obj) { BinData::Stringz.new(max_length: 5) }

  it "reads less than max_length" do
    io = StringIO.new("abc\0xyz")
    obj.read(io)
    _(obj).must_equal "abc"
  end

  it "reads exactly max_length" do
    io = StringIO.new("abcd\0xyz")
    obj.read(io)
    _(obj).must_equal "abcd"
  end

  it "reads no more than max_length" do
    io = StringIO.new("abcdefg\0xyz")
    obj.read(io)
    _(io.pos).must_equal 5
    _(obj).must_equal "abcd"
  end

  it "accepts values less than max_length" do
    obj.assign("abc")
    _(obj).must_equal "abc"
  end

  it "accepts values exactly max_length" do
    obj.assign("abcd")
    _(obj).must_equal "abcd"
  end

  it "trims values greater than max_length" do
    obj.assign("abcdefg")
    _(obj).must_equal "abcd"
  end

  it "writes values greater than max_length" do
    obj.assign("abcdefg")
    _(obj.to_binary_s).must_equal_binary "abcd\0"
  end

  it "writes values less than max_length" do
    obj.assign("abc")
    _(obj.to_binary_s).must_equal_binary "abc\0"
  end

  it "writes values exactly max_length" do
    obj.assign("abcd")
    _(obj.to_binary_s).must_equal_binary "abcd\0"
  end
end