...
1 package dbus
2
3 import (
4 "testing"
5 )
6
7 var escapeTestCases = []struct {
8 in, out string
9 }{
10 {in: "", out: ""},
11 {in: "ABCDabcdZYXzyx01289", out: "ABCDabcdZYXzyx01289"},
12 {in: `_-/\*`, out: `_-/\*`},
13 {in: `=+:~!`, out: `%3d%2b%3a%7e%21`},
14 {in: `space here`, out: `space%20here`},
15 {in: `Привет`, out: `%d0%9f%d1%80%d0%b8%d0%b2%d0%b5%d1%82`},
16 {in: `ჰეი`, out: `%e1%83%b0%e1%83%94%e1%83%98`},
17 {in: `你好`, out: `%e4%bd%a0%e5%a5%bd`},
18 {in: `こんにちは`, out: `%e3%81%93%e3%82%93%e3%81%ab%e3%81%a1%e3%81%af`},
19 }
20
21
22 var escapeBenchmarkCases = []struct {
23 in, out string
24 }{
25 {in: "/run/user/1000/bus", out: "/run/user/1000/bus"},
26 {in: "/path/with/a/single space/bus", out: "/path/with/a/single%20space/bus"},
27 }
28
29 func TestEscapeBusAddressValue(t *testing.T) {
30 for _, tc := range escapeTestCases {
31 out := EscapeBusAddressValue(tc.in)
32 if out != tc.out {
33 t.Errorf("input: %q; want %q, got %q", tc.in, tc.out, out)
34 }
35 in, err := UnescapeBusAddressValue(out)
36 if err != nil {
37 t.Errorf("unescape error: %v", err)
38 } else if in != tc.in {
39 t.Errorf("unescape: want %q, got %q", tc.in, in)
40 }
41 }
42 }
43
44 func BenchmarkEscapeBusAddressValue(b *testing.B) {
45 var out string
46
47 for i := 0; i < b.N; i++ {
48 for _, tc := range escapeBenchmarkCases {
49 out = EscapeBusAddressValue(tc.in)
50 }
51 }
52 b.Log("out:", out)
53 }
54
View as plain text