Skip to content

Commit

Permalink
Merge pull request #26 from K-S-A/master
Browse files Browse the repository at this point in the history
Update Utils.zunpad and add specs
  • Loading branch information
se3000 authored Jun 11, 2018
2 parents a99c101 + 95a4a3f commit cd59755
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/eth/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def zpad(x, l)
end

def zunpad(x)
x.sub /\A\x00+/, ''
x.sub(/\A\x00+/, '')
end

def zpad_int(n, l=32)
Expand Down
27 changes: 27 additions & 0 deletions spec/eth/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,31 @@
end
end

describe ".zunpad" do
subject { Eth::Utils.zunpad(address) }

context "with single leading null byte" do
let(:address) { "\0\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r" }

it "returns address without leading null byte" do
expect(subject).to eq("\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r")
end
end

context "with multiple leading null bytes" do
let(:address) { "\0\0\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r" }

it "returns address without leading null bytes" do
expect(subject).to eq("\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r")
end
end

context "without leading null byte" do
let(:address) { "\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r" }

it "returns unchanged address" do
expect(subject).to eq("\xc8\x1b\x94\x934 \"\x1az\xc0\x04\xa9\x02B\xd8\xb1\xd3\xe5\x07\r")
end
end
end
end

0 comments on commit cd59755

Please sign in to comment.