Skip to content

Commit

Permalink
Added status attribute to PngCheck::EmptyPngError, PngCheck::CorruptP…
Browse files Browse the repository at this point in the history
…ngError
  • Loading branch information
maxirmx committed Oct 9, 2022
1 parent feb9863 commit cd727aa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
2 changes: 2 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Where:
* `valid` is `true` if the buffer is correct image
* otherwise an exception is raised. Possible exception types are `PngCheck::EmptyPngError`, `PngCheck::CorruptPngError`

Both exception classes have additional status attribute that is set to one of STATUX_XXX codes described above. We believe that
exceptions with STATUS_WARNING can be ignored in the majority of applications.

==== Pre-validation with libpng-ruby

Expand Down
13 changes: 9 additions & 4 deletions lib/pngcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ module PngCheck
EMPTY_IMAGE = "Image is empty"

class CorruptPngError < StandardError
def initialize(msg, status = STATUS_MAJOR_ERROR)
def initialize(msg, sts = STATUS_MAJOR_ERROR)
super(msg)
@status = sts
end

def status
@status
end
end

class EmptyPngError < StandardError
def initialize(msg = EMPTY_IMAGE, status = STATUS_CRITICAL_ERROR)
super(msg)
class EmptyPngError < CorruptPngError
def initialize(msg = EMPTY_IMAGE)
super(msg, STATUS_CRITICAL_ERROR)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pngcheck/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PngCheck
VERSION = "0.2.8"
VERSION = "0.3.0"
end
94 changes: 59 additions & 35 deletions spec/pngcheck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# puts "--> info: #{info}\n"
end

it "analyzes MacOS screenshot" do
it "sets STATUS_WARNING for MacOS screenshot" do
status, info = PngCheck.analyze_file("spec/examples/macos-screenshot.png")
expect(status).to eql PngCheck::STATUS_WARNING
expect(info).to include "illegal (unless recently approved) unknown, public chunk"
expect(info).to include "illegal (unless recently approved) unknown"
# puts "--> info: #{info}\n"
end

Expand All @@ -45,21 +45,24 @@
expect(PngCheck.check_file("spec/examples/correct2.png")).to eql true
end
it "raises an exception on corrupt file check" do
expect do
PngCheck.check_file("spec/examples/corrupt.png")
end.to raise_error(PngCheck::CorruptPngError)
PngCheck.check_file("spec/examples/corrupt.png")
expect(false).to be true
rescue PngCheck::CorruptPngError => e
expect(e.status).to eql PngCheck::STATUS_MAJOR_ERROR
end

it "raises an exception on empty file check" do
expect do
PngCheck.check_file("spec/examples/empty.png")
end.to raise_error(PngCheck::EmptyPngError)
PngCheck.check_file("spec/examples/empty.png")
expect(false).to be true
rescue PngCheck::EmptyPngError => e
expect(e.status).to eql PngCheck::STATUS_CRITICAL_ERROR
end

it "raises an exception on missing file check" do
expect do
PngCheck.check_file("spec/examples/nofile.png")
end.to raise_error(PngCheck::CorruptPngError)
PngCheck.check_file("spec/examples/nofile.png")
expect(false).to be true
rescue PngCheck::CorruptPngError => e
expect(e.status).to eql PngCheck::STATUS_CRITICAL_ERROR
end

it "analyzes correct buffer" do
Expand Down Expand Up @@ -100,40 +103,61 @@
expect(PngCheck.check_buffer(encoded)).to eql true
end

it "sets STATUS_WARNING for a buffer generated from MacOS screenshot" do
encoded = File.binread("spec/examples/macos-screenshot.png")
status, info = PngCheck.analyze_buffer(encoded)
expect(status).to eql PngCheck::STATUS_WARNING
expect(info).to include "illegal (unless recently approved) unknown"
end

it "raises an exception on corrupt buffer check" do
encoded = File.binread("spec/examples/corrupt.png")
expect do
PngCheck.check_buffer(encoded)
end.to raise_error(PngCheck::CorruptPngError)
PngCheck.check_buffer(encoded)
expect(false).to be true
rescue PngCheck::CorruptPngError => e
expect(e.status).to eql PngCheck::STATUS_MAJOR_ERROR
end

it "raises an exception on trash buffer check" do
encoded = "[this is just a string]"
expect do
PngCheck.check_buffer(encoded)
end.to raise_error(PngCheck::CorruptPngError)
PngCheck.check_buffer(encoded)
expect(false).to be true
rescue PngCheck::CorruptPngError => e
expect(e.status).to eql PngCheck::STATUS_CRITICAL_ERROR
end

it "raises an exception on empty buffer check" do
encoded = ""
expect do
PngCheck.check_buffer(encoded)
end.to raise_error(PngCheck::EmptyPngError)
end

require "png"

it "can be used with libpng-ruby" do
encoded = File.binread("spec/examples/correct.png")
begin
expect(PngCheck.check_buffer(encoded)).to eql true
dec = PNG::Decoder.new
raw = dec << encoded
rescue PngCheck::CorruptPngError => e
puts "Exception #{e.message}"
PngCheck.check_buffer(encoded)
expect(false).to be true
rescue PngCheck::EmptyPngError => e
expect(e.status).to eql PngCheck::STATUS_CRITICAL_ERROR
end

it "raises an exception on macos screenshot with status equal to STATUS_WARNING" do
encoded = File.binread("spec/examples/macos-screenshot.png")
PngCheck.check_buffer(encoded)
expect(false).to be true
rescue PngCheck::CorruptPngError => e
expect(e.status).to eql PngCheck::STATUS_WARNING
end

begin
require "png"
it "can be used with libpng-ruby" do
encoded = File.binread("spec/examples/correct.png")
begin
expect(PngCheck.check_buffer(encoded)).to eql true
dec = PNG::Decoder.new
raw = dec << encoded
rescue PngCheck::CorruptPngError => e
puts "Exception #{e.message}"
end
f = "aaaaaaaaa"
expect(raw.unpack(f)).to start_with
"\xF4\xD1\r\xF4\xD1\r\xF4\xD1\r\xF4".unpack(f)
end
f = "aaaaaaaaa"
expect(raw.unpack(f)).to start_with
"\xF4\xD1\r\xF4\xD1\r\xF4\xD1\r\xF4".unpack(f)
rescue LoadError => e
puts "Failed to load libpng-ruby, skipping compatibility test"
end
end

0 comments on commit cd727aa

Please sign in to comment.