Source file src/net/nss_test.go

     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  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  
     7  package net
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  	"time"
    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  	t.Parallel()
    38  
    39  	tests := []struct {
    40  		name string
    41  		in   string
    42  		want *nssConf
    43  	}{
    44  		{
    45  			name: "no_newline",
    46  			in:   "foo: a b",
    47  			want: &nssConf{
    48  				sources: map[string][]nssSource{
    49  					"foo": {{source: "a"}, {source: "b"}},
    50  				},
    51  			},
    52  		},
    53  		{
    54  			name: "newline",
    55  			in:   "foo: a b\n",
    56  			want: &nssConf{
    57  				sources: map[string][]nssSource{
    58  					"foo": {{source: "a"}, {source: "b"}},
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name: "whitespace",
    64  			in:   "   foo:a    b    \n",
    65  			want: &nssConf{
    66  				sources: map[string][]nssSource{
    67  					"foo": {{source: "a"}, {source: "b"}},
    68  				},
    69  			},
    70  		},
    71  		{
    72  			name: "comment1",
    73  			in:   "   foo:a    b#c\n",
    74  			want: &nssConf{
    75  				sources: map[string][]nssSource{
    76  					"foo": {{source: "a"}, {source: "b"}},
    77  				},
    78  			},
    79  		},
    80  		{
    81  			name: "comment2",
    82  			in:   "   foo:a    b #c \n",
    83  			want: &nssConf{
    84  				sources: map[string][]nssSource{
    85  					"foo": {{source: "a"}, {source: "b"}},
    86  				},
    87  			},
    88  		},
    89  		{
    90  			name: "crit",
    91  			in:   "   foo:a    b [!a=b    X=Y ] c#d \n",
    92  			want: &nssConf{
    93  				sources: map[string][]nssSource{
    94  					"foo": {
    95  						{source: "a"},
    96  						{
    97  							source: "b",
    98  							criteria: []nssCriterion{
    99  								{
   100  									negate: true,
   101  									status: "a",
   102  									action: "b",
   103  								},
   104  								{
   105  									status: "x",
   106  									action: "y",
   107  								},
   108  							},
   109  						},
   110  						{source: "c"},
   111  					},
   112  				},
   113  			},
   114  		},
   115  
   116  		// Ubuntu Trusty w/ avahi-daemon, libavahi-* etc installed.
   117  		{
   118  			name: "ubuntu_trusty_avahi",
   119  			in:   ubuntuTrustyAvahi,
   120  			want: &nssConf{
   121  				sources: map[string][]nssSource{
   122  					"passwd": {{source: "compat"}},
   123  					"group":  {{source: "compat"}},
   124  					"shadow": {{source: "compat"}},
   125  					"hosts": {
   126  						{source: "files"},
   127  						{
   128  							source: "mdns4_minimal",
   129  							criteria: []nssCriterion{
   130  								{
   131  									negate: false,
   132  									status: "notfound",
   133  									action: "return",
   134  								},
   135  							},
   136  						},
   137  						{source: "dns"},
   138  						{source: "mdns4"},
   139  					},
   140  					"networks": {{source: "files"}},
   141  					"protocols": {
   142  						{source: "db"},
   143  						{source: "files"},
   144  					},
   145  					"services": {
   146  						{source: "db"},
   147  						{source: "files"},
   148  					},
   149  					"ethers": {
   150  						{source: "db"},
   151  						{source: "files"},
   152  					},
   153  					"rpc": {
   154  						{source: "db"},
   155  						{source: "files"},
   156  					},
   157  					"netgroup": {
   158  						{source: "nis"},
   159  					},
   160  				},
   161  			},
   162  		},
   163  	}
   164  
   165  	for _, tt := range tests {
   166  		gotConf := nssStr(t, tt.in)
   167  		gotConf.mtime = time.Time{} // ignore mtime in comparison
   168  		if !reflect.DeepEqual(gotConf, tt.want) {
   169  			t.Errorf("%s: mismatch\n got %#v\nwant %#v", tt.name, gotConf, tt.want)
   170  		}
   171  	}
   172  }
   173  

View as plain text