Simple hosts file management in Go (golang).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
4.0 KiB

  1. package goodhosts
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestHostsHas(t *testing.T) {
  7. hosts := new(Hosts)
  8. hosts.FileLines = []HostsLine{
  9. NewHostsLine("127.0.0.1 yadda"),
  10. NewHostsLine("10.0.0.7 nada"),
  11. }
  12. // We should find this entry.
  13. if !hosts.Has("10.0.0.7", "nada", false) {
  14. t.Error("Couldn't find entry in hosts file.")
  15. }
  16. // We shouldn't find this entry
  17. if hosts.Has("10.0.0.7", "shuda", false) {
  18. t.Error("Found entry that isn't in hosts file.")
  19. }
  20. }
  21. func TestHostsHasDoesntFindMissingEntry(t *testing.T) {
  22. hosts := new(Hosts)
  23. hosts.FileLines = []HostsLine{
  24. NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}
  25. if hosts.Has("10.0.0.7", "brada", false) {
  26. t.Error("Found missing entry.")
  27. }
  28. }
  29. func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
  30. hosts := new(Hosts)
  31. hosts.FileLines = []HostsLine{
  32. NewHostsLine("127.0.0.1 yadda"),
  33. NewHostsLine("10.0.0.7 nada"),
  34. }
  35. hosts.Add("10.0.0.7", "", "brada")
  36. hosts.Add("127.0.0.1", "", "yadda")
  37. checkLines := append(hosts.FileLines, hosts.SectionLines...)
  38. expectedLines := []HostsLine{
  39. NewHostsLine("127.0.0.1 yadda"),
  40. NewHostsLine("10.0.0.7 nada"),
  41. NewHostsLine("10.0.0.7 brada"),
  42. }
  43. if !reflect.DeepEqual(checkLines, expectedLines) {
  44. t.Error("Add entry failed to append entry.")
  45. }
  46. }
  47. func TestHostsAddWithComment(t *testing.T) {
  48. hosts := new(Hosts)
  49. hosts.FileLines = []HostsLine{
  50. NewHostsLine("127.0.0.1 yadda"),
  51. NewHostsLine("10.0.0.7 nada"),
  52. }
  53. hosts.Add("10.0.0.7", "Test Comment", "brada", "yadda")
  54. checkLines := append(hosts.FileLines, hosts.SectionLines...)
  55. expectedLines := []HostsLine{
  56. NewHostsLine("127.0.0.1 yadda"),
  57. NewHostsLine("10.0.0.7 nada"),
  58. NewHostsLine("10.0.0.7 brada #Test Comment"),
  59. NewHostsLine("10.0.0.7 yadda #Test Comment"),
  60. }
  61. if !reflect.DeepEqual(checkLines, expectedLines) {
  62. t.Error("Add entry failed to append entry.")
  63. }
  64. }
  65. func TestHostsAddWhenIpDoesntExist(t *testing.T) {
  66. hosts := new(Hosts)
  67. hosts.FileLines = []HostsLine{
  68. NewHostsLine("127.0.0.1 yadda"),
  69. }
  70. hosts.Add("10.0.0.7", "", "brada", "yadda")
  71. checkLines := append(hosts.FileLines, hosts.SectionLines...)
  72. expectedLines := []HostsLine{
  73. NewHostsLine("127.0.0.1 yadda"),
  74. NewHostsLine("10.0.0.7 brada"),
  75. NewHostsLine("10.0.0.7 yadda"),
  76. }
  77. if !reflect.DeepEqual(checkLines, expectedLines) {
  78. t.Error("Add entry failed to append entry.")
  79. }
  80. }
  81. func TestHostsRemoveWhenLastHostIpCombo(t *testing.T) {
  82. hosts := new(Hosts)
  83. hosts.FileLines = []HostsLine{
  84. NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}
  85. hosts.Remove("10.0.0.7", "nada")
  86. expectedLines := []HostsLine{NewHostsLine("127.0.0.1 yadda")}
  87. if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
  88. t.Error("Remove entry failed to remove entry.")
  89. }
  90. }
  91. func TestHostsRemoveWhenIpHasOtherHosts(t *testing.T) {
  92. hosts := new(Hosts)
  93. hosts.FileLines = []HostsLine{
  94. NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada brada")}
  95. hosts.Remove("10.0.0.7", "nada")
  96. expectedLines := []HostsLine{
  97. NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada")}
  98. if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
  99. t.Error("Remove entry failed to remove entry.")
  100. }
  101. }
  102. func TestHostsRemoveMultipleEntries(t *testing.T) {
  103. hosts := new(Hosts)
  104. hosts.FileLines = []HostsLine{
  105. NewHostsLine("127.0.0.1 yadda nadda prada")}
  106. hosts.Remove("127.0.0.1", "yadda", "prada")
  107. if hosts.FileLines[0].Raw != "127.0.0.1 nadda" {
  108. t.Error("Failed to remove multiple entries.")
  109. }
  110. }
  111. func TestHostsRemoveLineWithComments(t *testing.T) {
  112. hosts := new(Hosts)
  113. hosts.FileLines = []HostsLine{
  114. NewHostsLine("127.0.0.1 yadda"),
  115. NewHostsLine("10.0.0.7 brada"),
  116. }
  117. nadaLine := NewHostsLine("10.0.0.7 nada")
  118. nadaLine.Comment = "Test comment"
  119. hosts.FileLines = append(hosts.FileLines, nadaLine)
  120. hosts.Remove("10.0.0.7", "nada")
  121. expectedLines := []HostsLine{
  122. NewHostsLine("127.0.0.1 yadda"),
  123. NewHostsLine("10.0.0.7 brada"),
  124. }
  125. if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
  126. t.Error("Remove entry failed to remove entry.")
  127. }
  128. }