scan_common_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package scan
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. var TestingScanner = &Scanner{
  7. Description: "Tests common scan functions",
  8. scan: func(addr, hostname string) (Grade, Output, error) {
  9. switch addr {
  10. case "bad.example.com:443":
  11. return Bad, "bad.com", nil
  12. case "Warning.example.com:443":
  13. return Warning, "Warning.com", nil
  14. case "good.example.com:443":
  15. return Good, "good.com", nil
  16. case "skipped.example.com:443/0":
  17. return Skipped, "skipped", nil
  18. default:
  19. return Grade(-1), "invalid", fmt.Errorf("scan: invalid grade")
  20. }
  21. },
  22. }
  23. var TestingFamily = &Family{
  24. Description: "Tests the scan_common",
  25. Scanners: map[string]*Scanner{
  26. "TestingScanner": TestingScanner,
  27. },
  28. }
  29. func TestCommon(t *testing.T) {
  30. if TestingFamily.Scanners["TestingScanner"] != TestingScanner {
  31. t.FailNow()
  32. }
  33. var grade Grade
  34. var output Output
  35. var err error
  36. grade, output, err = TestingScanner.Scan("bad.example.com:443", "bad.example.com")
  37. if grade != Bad || output.(string) != "bad.com" || err != nil {
  38. t.FailNow()
  39. }
  40. grade, output, err = TestingScanner.Scan("Warning.example.com:443", "Warning.example.com")
  41. if grade != Warning || output.(string) != "Warning.com" || err != nil {
  42. t.FailNow()
  43. }
  44. grade, output, err = TestingScanner.Scan("good.example.com:443", "good.example.com")
  45. if grade != Good || output.(string) != "good.com" || err != nil {
  46. t.FailNow()
  47. }
  48. grade, output, err = TestingScanner.Scan("skipped.example.com:443/0", "")
  49. if grade != Skipped || output.(string) != "skipped" || err != nil {
  50. t.FailNow()
  51. }
  52. _, _, err = TestingScanner.Scan("invalid", "invalid")
  53. if err == nil {
  54. t.FailNow()
  55. }
  56. }