Testing the Port Scanning Functionality
Implement the port scanning functionality.
We'll cover the following...
We'll cover the following...
Creating the scanHosts_test.go file
We create a file scanHosts_test.go for the tests. We define the package package scan_test so it only tests the exposed API, as we did with the hosts tests:
package scan_test
Then, we add the import section.
For these tests, we’ll use:
- The
netpackage to create a local TCP server. - The package
strconvto convert strings to integer numbers. - The
testingpackage for the testing function. - The
scanpackage that we’re testing.
import ("net""strconv""testing""pScan/scan")
The import list
Adding the TestStateString() function
Now, we add our first test function TestStateString() to test the String() method of the state type. We want to ensure it returns open or closed:
func TestStateString(t *testing.T) {ps := scan.PortState{}if ps.Open.String() != "closed" {t.Errorf("Expected %q, got %q instead\n", "closed", ps.Open.String())}ps.Open = trueif ps.Open.String() != "open" {t.Errorf("Expected %q, got %q instead\n", "open", ps.Open.String())}}
Adding the function TestStateString()
For this test, we’re defining an instance of the type scan.PortState. By default,
the ...