Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CapacitorSet committed Aug 9, 2018
1 parent de627bd commit 9723166
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ install:
- sudo apt-get update -qq
- sudo apt-get install -y software-properties-common python-software-properties lxc-dev
- go get -u -v gopkg.in/alecthomas/gometalinter.v2
- go get -u -v github.com/antchfx/xmlquery # Used in tests
- go get -u -v github.com/honeytrap/honeytrap
- gometalinter.v2 --install

Expand Down
44 changes: 44 additions & 0 deletions tests/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Honeytrap
* Copyright (C) 2016-2017 DutchSec (https://dutchsec.com/)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* version 3 along with this program in the file "LICENSE". If not, see
* <http://www.gnu.org/licenses/agpl-3.0.txt>.
*
* See https://honeytrap.io/ for more details. All requests should be sent to
* licensing@honeytrap.io
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* Honeytrap" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by Honeytrap" and retain the original copyright notice.
*/

package honeytrap_test

import (
"os"
"testing"
)

// Nothing special, just check that we can run Honeytrap
func TestDummy(t *testing.T) {
tmpConf, p := runWithConfig("", "--version")
defer os.Remove(tmpConf)
mustWait(p)
}
88 changes: 88 additions & 0 deletions tests/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Honeytrap
* Copyright (C) 2016-2017 DutchSec (https://dutchsec.com/)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* version 3 along with this program in the file "LICENSE". If not, see
* <http://www.gnu.org/licenses/agpl-3.0.txt>.
*
* See https://honeytrap.io/ for more details. All requests should be sent to
* licensing@honeytrap.io
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* Honeytrap" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by Honeytrap" and retain the original copyright notice.
*/

package cli_test

import (
"strings"
"testing"
)

func TestHelp(t *testing.T) {
out := run(t, "--help")
// The output should contain an explanation of a few flags.
// --help and --version are future-proof.
shouldContain := []string{"--help", "--version"}
for _, s := range shouldContain {
if !strings.Contains(out, s) {
t.Errorf("Help text doesn't contain '%s'", s)
}
}
}

func TestListServices(t *testing.T) {
out := runWithConfig(t, "", "--list-services")
/* The output should contain a list of services; http and telnet are some
* basic ones.
*/
shouldContain := []string{"http", "telnet"}
for _, s := range shouldContain {
if !strings.Contains(out, s) {
t.Errorf("Services list doesn't contain '%s'", s)
}
}
}

func TestListChannels(t *testing.T) {
out := runWithConfig(t, "", "--list-channels")
/* The output should contain a list of channels; console and file are some
* basic ones.
*/
shouldContain := []string{"console", "file"}
for _, s := range shouldContain {
if !strings.Contains(out, s) {
t.Errorf("Channels list doesn't contain '%s'", s)
}
}
}

func TestListListeners(t *testing.T) {
out := runWithConfig(t, "", "--list-listeners")
/* The output should contain a list of listeners; socket and agent are some
* basic ones.
*/
shouldContain := []string{"socket", "agent"}
for _, s := range shouldContain {
if !strings.Contains(out, s) {
t.Errorf("Listeners list doesn't contain '%s'", s)
}
}
}
52 changes: 52 additions & 0 deletions tests/cli/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Honeytrap
* Copyright (C) 2016-2017 DutchSec (https://dutchsec.com/)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* version 3 along with this program in the file "LICENSE". If not, see
* <http://www.gnu.org/licenses/agpl-3.0.txt>.
*
* See https://honeytrap.io/ for more details. All requests should be sent to
* licensing@honeytrap.io
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* Honeytrap" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by Honeytrap" and retain the original copyright notice.
*/

package cli_test

import "testing"

func TestInvalidConfig(t *testing.T) {
hasErrored := false
handler := func(error) { hasErrored = true }
runWithConfigAndErrHandler(t, "This is invalid TOML", handler)
if !hasErrored {
t.Error("Didn't error out on an invalid config")
}
}

func TestValidConfig(t *testing.T) {
hasErrored := false
handler := func(error) { hasErrored = true }
runWithConfigAndErrHandler(t, "# Empty config file", handler)
if hasErrored {
t.Error("Error out on a valid (empty) config")
}
}
78 changes: 78 additions & 0 deletions tests/cli/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Honeytrap
* Copyright (C) 2016-2017 DutchSec (https://dutchsec.com/)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* version 3 along with this program in the file "LICENSE". If not, see
* <http://www.gnu.org/licenses/agpl-3.0.txt>.
*
* See https://honeytrap.io/ for more details. All requests should be sent to
* licensing@honeytrap.io
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* Honeytrap" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by Honeytrap" and retain the original copyright notice.
*/

// This file contains test utilities, no actual tests.

package cli_test

import (
"os"
"os/exec"
"path"
"testing"

"github.com/satori/go.uuid"
)

func run(t *testing.T, flags ...string) string {
return runWithErrHandler(func(e error) {
t.Fatal(e)
}, flags...)
}

func runWithConfig(t *testing.T, config string, flags ...string) string {
return runWithConfigAndErrHandler(t, config, func(e error) { t.Fatal(e) }, flags...)
}

func runWithErrHandler(errHandler func(error), flags ...string) string {
cmd := exec.Command("/honeytrap/honeytrap", flags...)
out, err := cmd.Output()
if err != nil {
errHandler(err)
}
return string(out)
}

func runWithConfigAndErrHandler(t *testing.T, config string, errHandler func(error), flags ...string) string {
tmpPath := path.Join(os.TempDir(), "honeytrap-testfile-"+uuid.NewV4().String())
tmp, err := os.Create(tmpPath)
if err != nil {
t.Fatal(err)
}
_, err = tmp.WriteString(config)
if err != nil {
t.Fatal(err)
}
flags = append(flags, "-c", tmpPath)
ret := runWithErrHandler(errHandler, flags...)
os.Remove(tmpPath)
return ret
}
61 changes: 61 additions & 0 deletions tests/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Honeytrap
* Copyright (C) 2016-2017 DutchSec (https://dutchsec.com/)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* version 3 along with this program in the file "LICENSE". If not, see
* <http://www.gnu.org/licenses/agpl-3.0.txt>.
*
* See https://honeytrap.io/ for more details. All requests should be sent to
* licensing@honeytrap.io
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* Honeytrap" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by Honeytrap" and retain the original copyright notice.
*/

package honeytrap_test

import (
"net/http"
"os"
"testing"
)

// Test that we can create a HTTP service and connect to it
func TestHTTP(t *testing.T) {
tmpConf, p := runWithConfig(serviceWithPort("http", "tcp/8000"))
defer os.Remove(tmpConf)
defer p.Kill()
httpClient := http.Client{}
_, err := httpClient.Get("http://127.0.0.1:8000/")
if err != nil {
t.Error(err)
}
}

// Test that nmap recognizes the HTTP service
func TestNmapHTTP(t *testing.T) {
tmpConf, p := runWithConfig(serviceWithPort("http", "tcp/8000"))
defer os.Remove(tmpConf)
defer p.Kill()
product := nmapIdentify(t, "8000")
if product != "Apache httpd" {
t.Errorf("Expected 'Apache httpd' identification, found '%s'", product)
}
}
Loading

0 comments on commit 9723166

Please sign in to comment.