Skip to content

Commit

Permalink
Fix pervasive unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorgs committed Jan 20, 2025
1 parent 147f7e2 commit 50e659b
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 101 deletions.
7 changes: 7 additions & 0 deletions test/data/lszcrypt/cca-and-ep11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CARD.DOMAIN TYPE MODE STATUS REQUESTS
----------------------------------------------
01 CEX5C CCA-Coproc online 1
01.0001 CEX5C CCA-Coproc online 1
01.0002 CEX5C CCA-Coproc offline 0
02 CEX7C EP11-Coproc online 6
02.0001 CEX7C EP11-Coproc online 4
6 changes: 6 additions & 0 deletions test/data/mkvps/cca-invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AES NEW: empty 0x0000000000000000
AES CUR: invalid 0x0000000000000000
AES OLD: invalid 0x0000000000000000
APKA NEW: empty 0x0000000000000000
APKA CUR: invalid 0x0000000000000000
APKA OLD: invalid 0x0000000000000000
9 changes: 9 additions & 0 deletions test/data/mkvps/cca-valid1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AES NEW: empty 0x0000000000000000
AES CUR: valid 0xd2344556789008
AES OLD: invalid 0x0000000000000000
APKA NEW: empty 0x0000000000000000
APKA CUR: invalid 0x0000000000000000
APKA OLD: invalid 0x0000000000000000
ASYM NEW: empty 0x00000000000000000000000000000000
ASYM CUR: invalid 0x00000000000000000000000000000000
ASYM OLD: invalid 0x00000000000000000000000000000000
2 changes: 2 additions & 0 deletions test/data/mkvps/ep11-invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WK CUR: invalid -
WK NEW: empty -
2 changes: 2 additions & 0 deletions test/data/mkvps/ep11-valid1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WK CUR: valid 0xbcd32323232325dbc44312012034455933aaacccbbbb32999abcd123bcdf1098
WK NEW: empty -
7 changes: 7 additions & 0 deletions test/support/storage_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ def crypttab_entry(*values)
def stub_product_features(features)
Yast::ProductFeatures.Import(features)
end

# Double for a APQN (used in pervasive encryption)
def apqn_mock(name, master_key, ep11: false)
instance_double(
Y2Storage::EncryptionProcesses::Apqn, name: name, master_key_pattern: master_key, ep11?: ep11
)
end
end
# rubocop:enable all
end
Expand Down
18 changes: 11 additions & 7 deletions test/y2partitioner/actions/controllers/encryption_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,19 +642,23 @@
shared_context "apqns" do
before do
allow(Y2Storage::EncryptionProcesses::Apqn).to receive(:all).and_return([apqn1, apqn2, apqn3])
allow(apqn1).to receive(:master_key_pattern).and_return "0x123445"
allow(apqn2).to receive(:master_key_pattern).and_return "0x654478"
end

let(:apqn1) { Y2Storage::EncryptionProcesses::Apqn.new("01.0001", "", "", "online") }
let(:apqn1) { Y2Storage::EncryptionProcesses::Apqn.new("01.0001", "", "CCA-Coproc", "online") }

let(:apqn2) { Y2Storage::EncryptionProcesses::Apqn.new("01.0002", "", "", "online") }
let(:apqn2) { Y2Storage::EncryptionProcesses::Apqn.new("01.0002", "", "CCA-Coproc", "online") }

let(:apqn3) { Y2Storage::EncryptionProcesses::Apqn.new("01.0003", "", "", "offline") }
let(:apqn3) { Y2Storage::EncryptionProcesses::Apqn.new("01.0003", "", "CCA-Coproc", "offline") }

let(:apqn3) { Y2Storage::EncryptionProcesses::Apqn.new("02.0001", "", "EP11-Coproc", "online") }
end

describe "#online_apqns" do
include_context "apqns"

it "returns all online APQNs" do
it "returns all online APQNs with a valid key" do
expect(subject.online_apqns).to contain_exactly(apqn1, apqn2)
end
end
Expand Down Expand Up @@ -696,13 +700,13 @@
let(:key) { instance_double(Y2Storage::EncryptionProcesses::SecureKey, remove: nil) }

it "returns nil" do
expect(subject.test_secure_key_generation).to be_nil
expect(subject.test_secure_key_generation([], "")).to be_nil
end

it "removes the temporary key" do
expect(key).to receive(:remove)

subject.test_secure_key_generation
subject.test_secure_key_generation([], "")
end
end

Expand All @@ -714,7 +718,7 @@
let(:error) { Cheetah::ExecutionFailed.new("", "", "", "", "error") }

it "returns the error message" do
expect(subject.test_secure_key_generation).to eq("error")
expect(subject.test_secure_key_generation([], "")).to eq("error")
end
end
end
Expand Down
32 changes: 10 additions & 22 deletions test/y2partitioner/widgets/apqn_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,17 @@
require "y2partitioner/widgets/apqn_selector"

describe Y2Partitioner::Widgets::ApqnSelector do
subject { described_class.new(controller) }
subject { described_class.new(apqns_by_key, initial_keys, initial_apqns) }

let(:controller) do
instance_double(Y2Partitioner::Actions::Controllers::Encryption, online_apqns: apqns)
end
let(:all_apqns) { [apqn1, apqn2, apqn3, apqn4] }
let(:apqns_by_key) { all_apqns.group_by(&:master_key_pattern) }
let(:initial_keys) { apqn1.master_key_pattern }
let(:initial_apqns) { [apqn1] }

let(:apqns) { [apqn1, apqn2] }
let(:apqn1) { apqn_mock("01.0001", "0x123") }
let(:apqn2) { apqn_mock("01.0002", "0x456") }
let(:apqn3) { apqn_mock("02.0001", "0x123") }
let(:apqn4) { apqn_mock("03.0001", "0xabcdefg", ep11: true) }

let(:apqn1) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0001") }

let(:apqn2) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0002") }

include_examples "CWM::MultiSelectionBox"

describe "#store" do
before do
allow(subject).to receive(:value).and_return(apqns)
end

it "saves selected APQNs in the controller" do
expect(controller).to receive(:apqns=).with(apqns)

subject.store
end
end
include_examples "CWM::CustomWidget"
end
109 changes: 53 additions & 56 deletions test/y2partitioner/widgets/encrypt_method_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,51 @@

before do
allow(Yast2::Popup).to receive(:show)
allow(controller).to receive(:online_apqns).and_return(apqns)
allow(Y2Partitioner::Widgets::PervasiveKeySelector).to receive(:new).and_return(master_key_widget)
end

include_examples "CWM::CustomWidget"

describe "#contents" do
before do
allow(controller).to receive(:online_apqns).and_return(apqns)
end

let(:apqns) { [] }
let(:master_key_widget) do
instance_double(
Y2Partitioner::Widgets::PervasiveKeySelector, widget_id: "mkw", value: selected_master_key
)
end

let(:apqn1) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0001") }
let(:apqns) { [apqn1, apqn2, apqn3, apqn4] }
let(:apqn1) { apqn_mock("01.0001", "0x123") }
let(:apqn2) { apqn_mock("01.0002", "0x456") }
let(:apqn3) { apqn_mock("02.0001", "0x123") }
let(:apqn4) { apqn_mock("03.0001", "0xabcdefg", ep11: true) }
let(:selected_master_key) { apqn4.master_key_pattern }

let(:apqn2) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0002") }
include_examples "CWM::CustomWidget"

describe "#contents" do
it "displays the encryption password widget" do
widget = subject.contents.nested_find { |i| i.is_a?(Y2Partitioner::Widgets::EncryptPassword) }

expect(widget).to_not be_nil
end

context "when there are more than one online APQNs" do
let(:apqns) { [apqn1, apqn2] }
context "and some APQN is already selected" do
before { controller.apqns = [apqn1] }

it "displays the APQN selector widget" do
widget = subject.contents.nested_find { |i| i.is_a?(Y2Partitioner::Widgets::ApqnSelector) }
it "displays the APQN selector widget" do
widget = subject.contents.nested_find { |i| i.is_a?(Y2Partitioner::Widgets::ApqnSelector) }

expect(widget).to_not be_nil
expect(widget).to_not be_nil
end
end

context "and no APQNs has been selected yet" do
before { controller.apqns = [] }

it "displays the APQN selector widget" do
widget = subject.contents.nested_find { |i| i.is_a?(Y2Partitioner::Widgets::ApqnSelector) }

expect(widget).to_not be_nil
end
end
end

Expand All @@ -179,26 +196,32 @@
end
end

describe "#validate" do
describe "#handle" do
before do
allow(Y2Partitioner::Widgets::ApqnSelector).to receive(:new).and_return(apqn_widget)
allow(Y2Partitioner::Widgets::PervasiveKey).to receive(:new).and_return(full_key_widget)
end

allow(controller).to receive(:secure_key)
let(:full_key_widget) do
instance_double(Y2Partitioner::Widgets::PervasiveKey)
end

allow(controller).to receive(:online_apqns).and_return(apqns)
it "refreshes all internal widgets if the master key changes" do
expect(full_key_widget).to receive(:refresh)

subject.handle({ "ID" => master_key_widget.widget_id })
end
end

describe "#validate" do
before do
allow(Y2Partitioner::Widgets::ApqnSelector).to receive(:new).and_return(apqn_widget)
allow(controller).to receive(:secure_key)
allow(controller).to receive(:test_secure_key_generation).and_return(generation_test_error)
end

let(:apqn_widget) { instance_double(Y2Partitioner::Widgets::ApqnSelector, value: selected_apqns) }

let(:apqns) { [apqn1, apqn2] }

let(:selected_apqns) { [apqn1, apqn2] }

let(:apqn1) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0001") }

let(:apqn2) { instance_double(Y2Storage::EncryptionProcesses::Apqn, name: "01.0002") }
let(:selected_apqns) { [apqn4] }
let(:selected_master_key) { apqn4.master_key_pattern }

context "and the secure key cannot be generated" do
let(:generation_test_error) { "error" }
Expand All @@ -207,37 +230,11 @@
expect(subject.validate).to eq(false)
end

context "when there are more than one selected APQNs" do
let(:selected_apqns) { [apqn1, apqn2] }

it "shows an specific error" do
expect(Yast2::Popup).to receive(:show)
.with(/all selected APQNs are configured/, headline: :error, details: "error")

subject.validate
end
end

context "when there is only one selected APQN" do
let(:selected_apqns) { [apqn1] }
it "shows an specific error" do
expect(Yast2::Popup).to receive(:show)
.with(/secure key cannot be generated/, headline: :error, details: "error")

it "shows an specific error" do
expect(Yast2::Popup).to receive(:show)
.with(/the selected APQN is configured/, headline: :error, details: "error")

subject.validate
end
end

context "when there is no selected APQN" do
let(:selected_apqns) { [] }

it "shows an specific error" do
expect(Yast2::Popup).to receive(:show)
.with(/all available APQNs are configured/, headline: :error, details: "error")

subject.validate
end
subject.validate
end
end

Expand Down
49 changes: 49 additions & 0 deletions test/y2partitioner/widgets/pervasive_key_selector_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env rspec

# Copyright (c) [2025] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"
require "cwm/rspec"
require "y2partitioner/widgets/pervasive_key_selector"

describe Y2Partitioner::Widgets::PervasiveKeySelector do
subject(:widget) { described_class.new(apqns_by_key, initial_key) }

let(:all_apqns) { [apqn1, apqn2, apqn3, apqn4, apqn5, apqn6] }
let(:apqns_by_key) { all_apqns.group_by(&:master_key_pattern) }
let(:initial_key) { apqn1.master_key_pattern }

let(:apqn1) { apqn_mock("01.0001", "0x123") }
let(:apqn2) { apqn_mock("01.0002", "0x456") }
let(:apqn3) { apqn_mock("02.0001", "0x123") }
let(:apqn4) { apqn_mock("03.0001", "0xabcdefgabcde", ep11: true) }
let(:apqn5) { apqn_mock("03.0002", "0x7654321abcde", ep11: true) }
let(:apqn6) { apqn_mock("03.0003", "0x7654321abcde", ep11: true) }

include_examples "CWM::ComboBox"

describe "#init" do
it "sets the current key value" do
expect(widget).to receive(:value=).with(initial_key)

widget.init
end
end
end
37 changes: 37 additions & 0 deletions test/y2partitioner/widgets/pervasive_key_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env rspec

# Copyright (c) [2025] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"
require "cwm/rspec"
require "y2partitioner/widgets/pervasive_key"

describe Y2Partitioner::Widgets::PervasiveKey do
subject { described_class.new(initial_key) }
let(:initial_key) { "0x123456" }

include_examples "CWM::AbstractWidget"

context "when the key is longer than 20 characters" do
let(:initial_key) { "0x123456789012345678901234567890" }

include_examples "CWM::AbstractWidget"
end
end
Loading

0 comments on commit 50e659b

Please sign in to comment.