commit e2dcd4e884cd95ba471454cb77922d7322773b2b
Author: David Fifield <david(a)bamsoftware.com>
Date: Sat Oct 26 15:20:44 2013 -0700
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.
---
src/pt/pt_test.go | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/src/pt/pt_test.go b/src/pt/pt_test.go
new file mode 100644
index 0000000..baece70
--- /dev/null
+++ b/src/pt/pt_test.go
@@ -0,0 +1,40 @@
+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))
+ }
+}