Skip to content

Commit

Permalink
Fix some errors on malformed license load
Browse files Browse the repository at this point in the history
  • Loading branch information
Taucher2003 committed May 9, 2024
1 parent c88755f commit a79ffa3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/code0/license/boundary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add_boundary(data, license_name)

def remove_boundary(data)
after_boundary = data.split(BOUNDARY_START).last
after_boundary.split(BOUNDARY_END).first
after_boundary&.split(BOUNDARY_END)&.first
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/code0/license/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def encrypt(data)

def decrypt(data)
raise KeyError, "Provided key is not a public key." unless key.public?
raise DecryptionError, "Provided data is nil" if data.nil?

json_data = Base64.decode64(data.chomp)

Expand Down
45 changes: 45 additions & 0 deletions spec/code0/license_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Code0::License do
described_class.encryption_key = OpenSSL::PKey::RSA.generate(2048)

let(:license_data) { default_license_data }
let(:license) { described_class.new(license_data) }
let(:default_license_data) do
Expand Down Expand Up @@ -31,6 +33,49 @@
)
end

describe ".load" do
subject(:load) { described_class.load(data) }

shared_examples "raises decryption error" do |example_name|
it(example_name) { expect { load }.to raise_error(Code0::License::Encryptor::DecryptionError) }
end

context "when data is nil" do
let(:data) { nil }

it { expect { load }.to raise_error(Code0::License::ValidationError) }
end

it_behaves_like "raises decryption error", "when data is an empty string" do
let(:data) { "" }
end

it_behaves_like "raises decryption error", "when data is a start boundary" do
let(:data) do
<<~DATA
--------BEGIN CODE0 LICENSE--------
DATA
end
end

it_behaves_like "raises decryption error", "when data is an end boundary" do
let(:data) do
<<~DATA
---------END CODE0 LICENSE---------
DATA
end
end

it_behaves_like "raises decryption error", "when data is a boundary" do
let(:data) do
<<~DATA
--------BEGIN CODE0 LICENSE--------
---------END CODE0 LICENSE---------
DATA
end
end
end

describe "#valid?" do
subject { license.valid? }

Expand Down

0 comments on commit a79ffa3

Please sign in to comment.