Skip to content

Commit a79ffa3

Browse files
committed
Fix some errors on malformed license load
1 parent c88755f commit a79ffa3

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

lib/code0/license/boundary.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def add_boundary(data, license_name)
2020

2121
def remove_boundary(data)
2222
after_boundary = data.split(BOUNDARY_START).last
23-
after_boundary.split(BOUNDARY_END).first
23+
after_boundary&.split(BOUNDARY_END)&.first
2424
end
2525

2626
private

lib/code0/license/encryptor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def encrypt(data)
4242

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

4647
json_data = Base64.decode64(data.chomp)
4748

spec/code0/license_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Code0::License do
4+
described_class.encryption_key = OpenSSL::PKey::RSA.generate(2048)
5+
46
let(:license_data) { default_license_data }
57
let(:license) { described_class.new(license_data) }
68
let(:default_license_data) do
@@ -31,6 +33,49 @@
3133
)
3234
end
3335

36+
describe ".load" do
37+
subject(:load) { described_class.load(data) }
38+
39+
shared_examples "raises decryption error" do |example_name|
40+
it(example_name) { expect { load }.to raise_error(Code0::License::Encryptor::DecryptionError) }
41+
end
42+
43+
context "when data is nil" do
44+
let(:data) { nil }
45+
46+
it { expect { load }.to raise_error(Code0::License::ValidationError) }
47+
end
48+
49+
it_behaves_like "raises decryption error", "when data is an empty string" do
50+
let(:data) { "" }
51+
end
52+
53+
it_behaves_like "raises decryption error", "when data is a start boundary" do
54+
let(:data) do
55+
<<~DATA
56+
--------BEGIN CODE0 LICENSE--------
57+
DATA
58+
end
59+
end
60+
61+
it_behaves_like "raises decryption error", "when data is an end boundary" do
62+
let(:data) do
63+
<<~DATA
64+
---------END CODE0 LICENSE---------
65+
DATA
66+
end
67+
end
68+
69+
it_behaves_like "raises decryption error", "when data is a boundary" do
70+
let(:data) do
71+
<<~DATA
72+
--------BEGIN CODE0 LICENSE--------
73+
---------END CODE0 LICENSE---------
74+
DATA
75+
end
76+
end
77+
end
78+
3479
describe "#valid?" do
3580
subject { license.valid? }
3681

0 commit comments

Comments
 (0)