...

Source file src/github.com/coreos/go-systemd/v22/dbus/dbus_test.go

Documentation: github.com/coreos/go-systemd/v22/dbus

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dbus
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestNeedsEscape(t *testing.T) {
    22  	// Anything not 0-9a-zA-Z should always be escaped
    23  	for want, vals := range map[bool][]byte{
    24  		false: []byte{'a', 'b', 'z', 'A', 'Q', '1', '4', '9'},
    25  		true:  []byte{'#', '%', '$', '!', '.', '_', '-', '%', '\\'},
    26  	} {
    27  		for i := 1; i < 10; i++ {
    28  			for _, b := range vals {
    29  				got := needsEscape(i, b)
    30  				if got != want {
    31  					t.Errorf("needsEscape(%d, %c) returned %t, want %t", i, b, got, want)
    32  				}
    33  			}
    34  		}
    35  	}
    36  
    37  	// 0-9 in position 0 should be escaped
    38  	for want, vals := range map[bool][]byte{
    39  		false: []byte{'A', 'a', 'e', 'x', 'Q', 'Z'},
    40  		true:  []byte{'0', '4', '5', '9'},
    41  	} {
    42  		for _, b := range vals {
    43  			got := needsEscape(0, b)
    44  			if got != want {
    45  				t.Errorf("needsEscape(0, %c) returned %t, want %t", b, got, want)
    46  			}
    47  		}
    48  	}
    49  
    50  }
    51  
    52  func TestPathBusEscape(t *testing.T) {
    53  	for in, want := range map[string]string{
    54  		"":                   "_",
    55  		"foo.service":        "foo_2eservice",
    56  		"foobar":             "foobar",
    57  		"woof@woof.service":  "woof_40woof_2eservice",
    58  		"0123456":            "_30123456",
    59  		"account_db.service": "account_5fdb_2eservice",
    60  		"got-dashes":         "got_2ddashes",
    61  	} {
    62  		got := PathBusEscape(in)
    63  		if got != want {
    64  			t.Errorf("bad result for PathBusEscape(%s): got %q, want %q", in, got, want)
    65  		}
    66  	}
    67  
    68  }
    69  
    70  func TestPathBusUnescape(t *testing.T) {
    71  	for in, want := range map[string]string{
    72  		"_":                      "",
    73  		"foo_2eservice":          "foo.service",
    74  		"foobar":                 "foobar",
    75  		"woof_40woof_2eservice":  "woof@woof.service",
    76  		"_30123456":              "0123456",
    77  		"account_5fdb_2eservice": "account_db.service",
    78  		"got_2ddashes":           "got-dashes",
    79  		"foobar_":                "foobar_",
    80  		"foobar_2":               "foobar_2",
    81  	} {
    82  		got := pathBusUnescape(in)
    83  		if got != want {
    84  			t.Errorf("bad result for pathBusUnescape(%s): got %q, want %q", in, got, want)
    85  		}
    86  	}
    87  }
    88  
    89  // TestNew ensures that New() works without errors.
    90  func TestNew(t *testing.T) {
    91  	_, err := New()
    92  
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  }
    97  

View as plain text