Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #398 from mozilla/bug_fix_results_hash
Browse files Browse the repository at this point in the history
Add unit-tests for potential results bug
  • Loading branch information
Jonathan Claudius authored Jul 3, 2017
2 parents 5bbb115 + 7c264f8 commit e4a7c03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
8 changes: 2 additions & 6 deletions lib/ssh_scan/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,8 @@ def set_end_time
end

def scan_duration
if start_time.nil?
raise "Cannot calculate scan duration without start_time set"
end

if end_time.nil?
raise "Cannot calculate scan duration without end_time set"
if start_time.nil? || end_time.nil?
return nil
end

end_time - start_time
Expand Down
31 changes: 26 additions & 5 deletions spec/ssh_scan/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
expect(result.languages_server_to_client).to eql([])
expect(result.start_time).to be_nil
expect(result.end_time).to be_nil
expect{ result.scan_duration }.to raise_error(
RuntimeError,
"Cannot calculate scan duration without start_time set"
)
expect(result.scan_duration).to be_nil
end

context "when setting IP" do
Expand All @@ -44,6 +41,7 @@

result.ip = "192.168.1.1"
expect(result.ip).to eql("192.168.1.1")
expect(result.to_hash).to be_kind_of(Hash)
end

it "should prevent setting result.ip to invalid values" do
Expand Down Expand Up @@ -73,6 +71,7 @@

result.port = 31337
expect(result.port).to eql(31337)
expect(result.to_hash).to be_kind_of(Hash)
end

it "should prevent setting result.port to invalid values" do
Expand Down Expand Up @@ -106,6 +105,7 @@
result.banner = banner
expect(result.banner).to be_kind_of(SSHScan::Banner)
expect(result.banner.to_s).to eql("This is my SSH Banner")
expect(result.to_hash).to be_kind_of(Hash)
end

it "should prevent setting result.banner to invalid values" do
Expand Down Expand Up @@ -138,6 +138,7 @@

result.banner = SSHScan::Banner.new("SSH-2.0-server")
expect(result.ssh_version).to eql(2.0)
expect(result.to_hash).to be_kind_of(Hash)
end
end

Expand All @@ -148,6 +149,7 @@

result.hostname = "bananas.example.com"
expect(result.hostname).to eql("bananas.example.com")
expect(result.to_hash).to be_kind_of(Hash)
end
end

Expand All @@ -158,7 +160,7 @@
result.set_end_time

result_hash = result.to_hash
expect(result_hash).to be_kind_of(Hash)
expect(result.to_hash).to be_kind_of(Hash)
end

it "should translate the result into a valid JSON string" do
Expand Down Expand Up @@ -189,6 +191,7 @@
expect(result.compliant?).to eql(compliance[:compliant])
expect(result.compliance_recommendations).to eql(compliance[:recommendations])
expect(result.compliance_references).to eql(compliance[:references])
expect(result.to_hash).to be_kind_of(Hash)
end
end

Expand All @@ -212,4 +215,22 @@
end
end

context "when dealing with errors" do
it "should append errors " do
compliance = {
:policy => "Test Policy",
:compliant => true,
:recommendations => ["do this", "do that"],
:references => ["https://reference.example.com"],
}
result = SSHScan::Result.new()
result.set_compliance = compliance
result.error = "This is an error"

expect(result.to_hash).to be_kind_of(Hash)
expect(result.to_hash["error"]).to eql("This is an error")
end
end


end

0 comments on commit e4a7c03

Please sign in to comment.