nss_test.go 3.4 KB

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