From 75fe6a0e272a915e525138f06368e97e45b9158a Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Wed, 24 Jan 2024 12:02:31 +0530 Subject: [PATCH] Unit tests added for iaf_file (#6933) Signed-off-by: Nik08 --- test/fixtures/test-inspec-profile-0.1.0.iaf | Bin 0 -> 1271 bytes test/unit/iaf_file_test.rb | 88 ++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 test/fixtures/test-inspec-profile-0.1.0.iaf create mode 100644 test/unit/iaf_file_test.rb diff --git a/test/fixtures/test-inspec-profile-0.1.0.iaf b/test/fixtures/test-inspec-profile-0.1.0.iaf new file mode 100644 index 0000000000000000000000000000000000000000..6b7f380ab2c6bb8ec2cc95466059d6d8f912f81e GIT binary patch literal 1271 zcmb`DZB$YR0EW?xuZAxf$|SB6rhvK`2r0LBEy5VF}0 z5CB03z;^&#!v76RMdA~|w_o+fSR0S-l#{O;c`(kxxlFMW2Q=jx3d2a@;~=UScY#+! z#55aMuJ9OGpfiz~c%6h&Xzo~(#73d_FoB62K@LMOu2VC#@aNGugE?zjA@g4H^=qxXq&A$pN!g2`hyb)i|b7Ks6AB zM}ws8aFj(5%M-eZI`TN#j4OK*Tm};=N?>^5|;PqZCBN7KS+t0oa7#H0*enBRXi6Bp-WX7P|br3kR<@t@uJjP z#H@6~WkI*(?YF7|PgM(7lE<(pGvceahEyg!3KwSw-GMV182I zgyz1BzUs{G{;YM2L;5eFk8rhK@yw8Vuw=8dca*b%(pWV%m5t4KB2QGNmzpAu>!Lz` zVqvASg&*;@AHkn8=QYJGIX`<*^}dN^-Q-heQks^rX@j0)3hm<;%O|U|=299neomUV z`Z+uJ-c!9WD1sAVJig{~hY^R_{yT1MJh$gsepgEx>x7@RnES9r26*mo34WF7YPN4l z?jBrPLzma#dk6ady0pZ*XCWO-dPB-9_r5>Fo{1lhTz`M;Qs<5R)8US|$D^f{S0cRO zfcU1M&DsBjEapJ&tD#w|)^^?7vUT~+z%xH&PfezX{EJ3xoryOW8k*H%XuOWMF+YxZ zq+-zucF)&k^`BI60z~tV6OaHvuj07)WD0o?tJS0m9gP@ZzMRlxL^8W)MwvV+1{m7x%1+D6LwnBAoGv$6ht$c6>N}VXO;_4}u>IWR{$}yNgxXr#P%k-L}VEbM%kl zV+E~?&Nm+*aZU%1#9Yy|xV|K%W%o8`7gwMUR|nQIdZ3Oc%a0z) zvJ%fe7#D<|_-wVkny~pctGjGcX6ZZUO-z69k+*t(ev4<*WXFxuZ{j!UZawk(0RD4- VBX08{5WHT15Sm(?NeCkl{suHY6IK8K literal 0 HcmV?d00001 diff --git a/test/unit/iaf_file_test.rb b/test/unit/iaf_file_test.rb new file mode 100644 index 000000000..87393a7ae --- /dev/null +++ b/test/unit/iaf_file_test.rb @@ -0,0 +1,88 @@ +require "helper" +require "inspec/iaf_file" + +describe "Inspec::IafFile" do + + # Using progress official public key for testing + let(:valid_verification_key_name) { "progress-2022-05-04" } + + after do + # delete local copy of public key which is created due to testing + File.delete("#{Inspec.config_dir}/keys/#{valid_verification_key_name}.pem.pub") if File.exist?("#{Inspec.config_dir}/keys/#{valid_verification_key_name}.pem.pub") + end + + describe "Finding validation key for iaf file" do + before do + stub_request(:get, "https://raw.githubusercontent.com/inspec/inspec/main/etc/keys/garbage-keyname.pem.pub") + .with( + headers: { + "Accept" => "*/*", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "User-Agent" => "Ruby", + } + ) + .to_return(status: 404, body: "", headers: {}) + end + + it "returns keypath when key is found" do + path = Inspec::IafFile.find_validation_key(valid_verification_key_name) + _(path).must_equal "#{Inspec.src_root}/etc/keys/#{valid_verification_key_name}.pem.pub" + end + + it "returns error when key not found" do + _ { Inspec::IafFile.find_validation_key("garbage-keyname") }.must_raise(Inspec::Exceptions::ProfileValidationKeyNotFound) + end + + it "returns error when key not passed" do + _ { Inspec::IafFile.find_validation_key }.must_raise(ArgumentError) + end + end + + describe "Finding signing key for iaf file" do + it "returns error when key not found" do + _ { Inspec::IafFile.find_signing_key("garbage-keyname") }.must_raise(Inspec::Exceptions::ProfileSigningKeyNotFound) + end + end + + describe "Fetch validation key from github" do + before do + stub_request(:get, "https://raw.githubusercontent.com/inspec/inspec/main/etc/keys/garbage-keyname.pem.pub") + .with( + headers: { + "Accept" => "*/*", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "User-Agent" => "Ruby", + } + ) + .to_return(status: 404, body: "", headers: {}) + + stub_request(:get, "https://raw.githubusercontent.com/inspec/inspec/main/etc/keys/progress-2022-05-04.pem.pub") + .with( + headers: { + "Accept" => "*/*", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "User-Agent" => "Ruby", + } + ) + .to_return(status: 200, body: "", headers: {}) + end + + it "returns true when key found" do + result = Inspec::IafFile.fetch_validation_key_from_github(valid_verification_key_name) + _(result).must_equal true + end + + it "returns false when key not found" do + result = Inspec::IafFile.fetch_validation_key_from_github("garbage-keyname") + _(result).must_equal false + end + + end + + describe "Testing validity of an iaf file" do + it "validates successfully" do + iaf_file = Inspec::IafFile.new("test/fixtures/test-inspec-profile-0.1.0.iaf") + _(iaf_file.valid?).must_equal true + end + end +end \ No newline at end of file