Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feedback needed, WIP] Add tests #412

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ addons:
install:
- sudo add-apt-repository -y ppa:ubuntu-lxc/stable
- sudo apt-get update -qq
- sudo apt-get install -y software-properties-common python-software-properties lxc-dev
- sudo apt-get install -y software-properties-common python-software-properties lxc-dev nmap
- 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

script:
- go build ./...
# Go 1.9 doesn't support `go test -coverprofile` with multiple packages, so run basic tests instead
- if [[ -z "$(go version | grep '1\.9')" ]]; then go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...; else go test -v ./...; fi
- go build ./...
- go build -tags "lxc" ./...
- GOARCH=arm go build ./...
- gometalinter.v2 --fast --deadline 10m --errors --vendor --sort=linter ./... --exclude='blank import' --exclude='should have comment' --exclude='should be of the form "' --exclude='can be annoying to use' # Report warnings, but do not fail if there are any
Expand Down
14 changes: 5 additions & 9 deletions services/redis/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
*/
package redis

import (
"fmt"
)

type cmd func(*redisService, []interface{}) (string, bool)

var mapCmds = map[string]cmd{
Expand All @@ -58,7 +54,7 @@ var mapInfoCmds = map[string]infoSection{
func (s *redisService) infoCmd(args []interface{}) (string, bool) {
switch len(args) {
case 0:
return fmt.Sprintf(lenMsg(), len(s.infoSectionsMsg()), s.infoSectionsMsg()), false
return bulkString(s.infoSectionsMsg(), true), false
case 1:
_word := args[0].(redisDatum)
word, success := _word.ToString()
Expand All @@ -67,15 +63,15 @@ func (s *redisService) infoCmd(args []interface{}) (string, bool) {
}
fn, ok := mapInfoCmds[word]
if ok {
return fmt.Sprintf(lenMsg(), len(fn(s)), fn(s)), false
return bulkString(fn(s), true), false
}
if word == "default" {
return fmt.Sprintf(lenMsg(), len(s.infoSectionsMsg()), s.infoSectionsMsg()), false
return bulkString(s.infoSectionsMsg(), true), false
}
if word == "all" {
return fmt.Sprintf(lenMsg(), len(s.allSectionsMsg()), s.allSectionsMsg()), false
return bulkString(s.allSectionsMsg(), true), false
}
return fmt.Sprintf(lenMsg(), len(lineBreakMsg()), lineBreakMsg()), false
return bulkString("", false), false
default:
return errorMsg("syntax"), false
}
Expand Down
18 changes: 9 additions & 9 deletions services/redis/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package redis

import (
"fmt"
"strings"
)

func (s *redisService) sectionsMsg() string {
Expand Down Expand Up @@ -73,7 +74,7 @@ config_file:

func (s *redisService) infoClientsMsg() string {
return `# Clients
connected_clients:0
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
Expand Down Expand Up @@ -206,14 +207,6 @@ func (s *redisService) infoKeyspaceMsg() string {
`
}

func lenMsg() string {
return "$%d\r\n%s\r\n"
}

func lineBreakMsg() string {
return ""
}

func errorMsg(errType string) string {
switch errType {
case "syntax":
Expand All @@ -222,3 +215,10 @@ func errorMsg(errType string) string {
return "-ERR unknown command '%s'\r\n"
}
}

func bulkString(text string, convertToCRLF bool) string {
if convertToCRLF {
text = strings.Replace(text, "\n", "\r\n", -1)
}
return fmt.Sprintf("$%d\r\n%s\r\n", len(text), text)
}
34 changes: 21 additions & 13 deletions services/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"github.com/honeytrap/honeytrap/event"
"github.com/honeytrap/honeytrap/pushers"
"github.com/honeytrap/honeytrap/services"
logging "github.com/op/go-logging"
"github.com/op/go-logging"
)

var log = logging.MustGetLogger("services/redis")
Expand Down Expand Up @@ -96,7 +96,14 @@ func (d *redisDatum) ToString() (value string, success bool) {
}

func parseRedisData(scanner *bufio.Scanner) (redisDatum, error) {
scanner.Scan()
success := scanner.Scan()
if !success {
err := scanner.Err()
if err == nil {
err = fmt.Errorf("eof")
}
return redisDatum{}, err
}
cmd := scanner.Text()
if len(cmd) == 0 {
return redisDatum{}, nil
Expand Down Expand Up @@ -136,33 +143,34 @@ func parseRedisData(scanner *bufio.Scanner) (redisDatum, error) {
}

func (s *redisService) Handle(ctx context.Context, conn net.Conn) error {

defer conn.Close()

scanner := bufio.NewScanner(conn)

for {
datum, err := parseRedisData(scanner)

if err != nil {
log.Error(err.Error())
continue
if err.Error() != "eof" {
log.Error(err.Error())
}
break
}

// Dirty hack to ignore "empty" packets (\r\n with no Redis content)
if datum.DataType == 0x00 {
continue
}
// Redis commands are sent as an array of strings, so expect that
if datum.DataType != 0x2a {
log.Error("Expected array, got data type %q", datum.DataType)
continue
break
}
items := datum.Content.([]interface{})
firstItem := items[0].(redisDatum)
command, success := firstItem.ToString()
if !success {
log.Error("Expected a command string, got something else (type=%q)", firstItem.DataType)
continue
break
}
answer, closeConn := s.REDISHandler(command, items[1:])

Expand All @@ -177,11 +185,11 @@ func (s *redisService) Handle(ctx context.Context, conn net.Conn) error {

if closeConn {
break
} else {
_, err := conn.Write([]byte(answer))
if err != nil {
log.Error("error writing response: %s", err.Error())
}
}
_, err = conn.Write([]byte(answer))
if err != nil {
log.Error("error writing response: %s", err.Error())
break
}
}

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")
}
}
Loading