...

Source file src/net/nss_test.go

Documentation: net

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

View as plain text