Commit e2dcd4e8 authored by David Fifield's avatar David Fifield
Browse files

Begin test program with tests for escape.

The exact escaping of of escape is unspecified; it says only that it
will encode so as to remove certain byte values. The idea is that you
shouldn't be able to inject a line by sending '\n', or truncate one with
'\0'. So the tests don't look for specific output values, only that they
don't contain any forbidden bytes.
parent e0fedd18
Loading
Loading
Loading
Loading

src/pt/pt_test.go

0 → 100644
+40 −0
Original line number Diff line number Diff line
package pt

import "testing"

func stringIsSafe(s string) bool {
	for _, c := range []byte(s) {
		if c == '\x00' || c == '\n' || c > 127 {
			return false
		}
	}
	return true
}

func TestEscape(t *testing.T) {
	tests := [...]string {
		"",
		"abc",
		"a\nb",
		"a\\b",
		"ab\\",
		"ab\\\n",
		"ab\n\\",
	}

	check := func (input string) {
		output := escape(input)
		if !stringIsSafe(output) {
			t.Errorf("escape(%q) → %q", input, output)
		}
	}
	for _, input := range tests {
		check(input)
	}
	for b := 0; b < 256; b++ {
		// check one-byte string with each byte value 0–255
		check(string([]byte{byte(b)}))
		// check UTF-8 encoding of each character 0–255
		check(string(b))
	}
}