|
| 1 | +# -*- coding:binary -*- |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +require 'rex/ole' |
| 5 | + |
| 6 | +describe Rex::OLE::DIFAT do |
| 7 | + |
| 8 | + let(:storage) do |
| 9 | + Rex::OLE::Storage.new |
| 10 | + end |
| 11 | + |
| 12 | + subject(:difat) do |
| 13 | + described_class.new(storage) |
| 14 | + end |
| 15 | + |
| 16 | + describe ".new" do |
| 17 | + it "returns a Rex::OLE::DIFAT instance" do |
| 18 | + expect(described_class.new(storage)).to be_a(Rex::OLE::DIFAT) |
| 19 | + end |
| 20 | + |
| 21 | + it "initializes @stg" do |
| 22 | + expect(difat.instance_variable_get(:@stg)).to eq(storage) |
| 23 | + end |
| 24 | + |
| 25 | + it "initializes @entries" do |
| 26 | + expect(difat.instance_variable_get(:@entries)).to be_an(Array) |
| 27 | + end |
| 28 | + |
| 29 | + it "initializes @entries as empty array" do |
| 30 | + expect(difat.instance_variable_get(:@entries)).to be_empty |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe "#[]=" do |
| 35 | + context "when the entry doesn't exist" do |
| 36 | + it "sets an element in the @entries array" do |
| 37 | + difat[0] = 1 |
| 38 | + expect(difat.instance_variable_get(:@entries)[0]).to eq(1) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context "when the entry exists" do |
| 43 | + it "replaces the element in the @entries array" do |
| 44 | + difat[0] = 1 |
| 45 | + difat[0] = 2 |
| 46 | + expect(difat.instance_variable_get(:@entries)[0]).to eq(2) |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe "#[]" do |
| 52 | + context "when the entry doesn't exist" do |
| 53 | + it "returns nil" do |
| 54 | + expect(difat[3]).to eq(nil) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "when the entry exists" do |
| 59 | + it "returns the entry value" do |
| 60 | + difat[3] = 31 |
| 61 | + expect(difat[3]).to eq(31) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + |
| 67 | + describe "#+" do |
| 68 | + context "when @entries is empty" do |
| 69 | + it "sets the @entries values" do |
| 70 | + difat + [1, 2] |
| 71 | + expect(difat.instance_variable_get(:@entries)).to eq([1, 2]) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context "when @entries isn't empty" do |
| 76 | + it "concatenates the array to @entries" do |
| 77 | + difat[2] = 0 |
| 78 | + difat + [1, 2] |
| 79 | + expect(difat.instance_variable_get(:@entries)).to eq([nil, nil, 0, 1, 2]) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe "#<<" do |
| 85 | + it "concatenates the element to the @entries array" do |
| 86 | + difat[0] = 1 |
| 87 | + difat << 3 |
| 88 | + expect(difat.instance_variable_get(:@entries)).to eq([1, 3]) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe "#length" do |
| 93 | + context "when @entries is empty" do |
| 94 | + it "returns 0" do |
| 95 | + expect(difat.length).to eq(0) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context "when @entries isn't empty" do |
| 100 | + it "returns the @entries length" do |
| 101 | + difat[0] = 1 |
| 102 | + difat[1] = 2 |
| 103 | + expect(difat.length).to eq(2) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "#slice!" do |
| 109 | + context "when @entries is empty" do |
| 110 | + it "returns empty array" do |
| 111 | + expect(difat.slice!(0, 1)).to eq([]) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + context "when start is out of range" do |
| 116 | + it "returns nil" do |
| 117 | + difat[0] = 1 |
| 118 | + expect(difat.slice!(10, 1)).to eq(nil) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context "when stop is 0" do |
| 123 | + it "returns empty array" do |
| 124 | + difat[0] = 1 |
| 125 | + expect(difat.slice!(0, 0)).to eq([]) |
| 126 | + end |
| 127 | + |
| 128 | + it "doesn't delete nothing" do |
| 129 | + difat[0] = 1 |
| 130 | + difat.slice!(0, 0) |
| 131 | + expect(difat[0]).to eq(1) |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + context "when @entries is long enough" do |
| 136 | + it "returns the deleted elements" do |
| 137 | + difat + [1, 2] |
| 138 | + expect(difat.slice!(0, 1)).to eq([1]) |
| 139 | + end |
| 140 | + |
| 141 | + it "deletes the elements in the range" do |
| 142 | + difat + [1, 2] |
| 143 | + difat.slice!(0, 1) |
| 144 | + expect(difat.instance_variable_get(:@entries)).to eq([2]) |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe "#reset" do |
| 150 | + it "resets the @entries array" do |
| 151 | + difat[0] = 1 |
| 152 | + difat.reset |
| 153 | + expect(difat.length).to eq(0) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + describe "#each" do |
| 158 | + it "calls the block for every @entries element" do |
| 159 | + difat + [1, 2, 3] |
| 160 | + res = 0 |
| 161 | + difat.each { |elem| res += elem} |
| 162 | + expect(res).to eq(1 + 2 + 3) |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + describe "#to_s" do |
| 167 | + it "returns an String" do |
| 168 | + expect(difat.to_s).to be_an(String) |
| 169 | + end |
| 170 | + |
| 171 | + it "starts with {" do |
| 172 | + expect(difat.to_s).to start_with('{') |
| 173 | + end |
| 174 | + |
| 175 | + it "ends with }" do |
| 176 | + expect(difat.to_s).to end_with('}') |
| 177 | + end |
| 178 | + |
| 179 | + it "contains @entries values" do |
| 180 | + difat + [Rex::OLE::SECT_FAT, 1, 2, 3, Rex::OLE::SECT_DIF, Rex::OLE::SECT_FREE, Rex::OLE::SECT_END] |
| 181 | + expect(difat.to_s).to match(/FAT, 0x1, 0x2, 0x3, DIF, FREE, END/) |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + describe "#read" do |
| 186 | + |
| 187 | + end |
| 188 | + |
| 189 | + describe "#write" do |
| 190 | + |
| 191 | + end |
| 192 | +end |
0 commit comments