Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some errors on malformed license load #1

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading