github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/net/nss_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package net
     8  
     9  import (
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  const ubuntuTrustyAvahi = `# /etc/nsswitch.conf
    16  #
    17  # Example configuration of GNU Name Service Switch functionality.
    18  # If you have the libc-doc-reference' and nfo' packages installed, try:
    19  # nfo libc "Name Service Switch"' for information about this file.
    20  
    21  passwd:         compat
    22  group:          compat
    23  shadow:         compat
    24  
    25  hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4
    26  networks:       files
    27  
    28  protocols:      db files
    29  services:       db files
    30  ethers:         db files
    31  rpc:            db files
    32  
    33  netgroup:       nis
    34  `
    35  
    36  func TestParseNSSConf(t *testing.T) {
    37  	tests := []struct {
    38  		name string
    39  		in   string
    40  		want *nssConf
    41  	}{
    42  		{
    43  			name: "no_newline",
    44  			in:   "foo: a b",
    45  			want: &nssConf{
    46  				sources: map[string][]nssSource{
    47  					"foo": {{source: "a"}, {source: "b"}},
    48  				},
    49  			},
    50  		},
    51  		{
    52  			name: "newline",
    53  			in:   "foo: a b\n",
    54  			want: &nssConf{
    55  				sources: map[string][]nssSource{
    56  					"foo": {{source: "a"}, {source: "b"}},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "whitespace",
    62  			in:   "   foo:a    b    \n",
    63  			want: &nssConf{
    64  				sources: map[string][]nssSource{
    65  					"foo": {{source: "a"}, {source: "b"}},
    66  				},
    67  			},
    68  		},
    69  		{
    70  			name: "comment1",
    71  			in:   "   foo:a    b#c\n",
    72  			want: &nssConf{
    73  				sources: map[string][]nssSource{
    74  					"foo": {{source: "a"}, {source: "b"}},
    75  				},
    76  			},
    77  		},
    78  		{
    79  			name: "comment2",
    80  			in:   "   foo:a    b #c \n",
    81  			want: &nssConf{
    82  				sources: map[string][]nssSource{
    83  					"foo": {{source: "a"}, {source: "b"}},
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "crit",
    89  			in:   "   foo:a    b [!a=b    X=Y ] c#d \n",
    90  			want: &nssConf{
    91  				sources: map[string][]nssSource{
    92  					"foo": {
    93  						{source: "a"},
    94  						{
    95  							source: "b",
    96  							criteria: []nssCriterion{
    97  								{
    98  									negate: true,
    99  									status: "a",
   100  									action: "b",
   101  								},
   102  								{
   103  									status: "x",
   104  									action: "y",
   105  								},
   106  							},
   107  						},
   108  						{source: "c"},
   109  					},
   110  				},
   111  			},
   112  		},
   113  
   114  		// Ubuntu Trusty w/ avahi-daemon, libavahi-* etc installed.
   115  		{
   116  			name: "ubuntu_trusty_avahi",
   117  			in:   ubuntuTrustyAvahi,
   118  			want: &nssConf{
   119  				sources: map[string][]nssSource{
   120  					"passwd": {{source: "compat"}},
   121  					"group":  {{source: "compat"}},
   122  					"shadow": {{source: "compat"}},
   123  					"hosts": {
   124  						{source: "files"},
   125  						{
   126  							source: "mdns4_minimal",
   127  							criteria: []nssCriterion{
   128  								{
   129  									negate: false,
   130  									status: "notfound",
   131  									action: "return",
   132  								},
   133  							},
   134  						},
   135  						{source: "dns"},
   136  						{source: "mdns4"},
   137  					},
   138  					"networks": {{source: "files"}},
   139  					"protocols": {
   140  						{source: "db"},
   141  						{source: "files"},
   142  					},
   143  					"services": {
   144  						{source: "db"},
   145  						{source: "files"},
   146  					},
   147  					"ethers": {
   148  						{source: "db"},
   149  						{source: "files"},
   150  					},
   151  					"rpc": {
   152  						{source: "db"},
   153  						{source: "files"},
   154  					},
   155  					"netgroup": {
   156  						{source: "nis"},
   157  					},
   158  				},
   159  			},
   160  		},
   161  	}
   162  
   163  	for _, tt := range tests {
   164  		gotConf := parseNSSConf(strings.NewReader(tt.in))
   165  		if !reflect.DeepEqual(gotConf, tt.want) {
   166  			t.Errorf("%s: mismatch\n got %#v\nwant %#v", tt.name, gotConf, tt.want)
   167  		}
   168  	}
   169  }