PageRenderTime 1513ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/server/server_test.go

https://github.com/nugget/netskel
Go | 246 lines | 198 code | 44 blank | 4 comment | 5 complexity | 9a2164548729d83fec0f6977c10d25f1 MD5 | raw file
Possible License(s): MIT, 0BSD, BSD-3-Clause
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "regexp"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func init() {
  12. Send = fakeSend
  13. Sendln = fakeSendln
  14. }
  15. func fakeSend(format string, a ...interface{}) (int, error) {
  16. stdoutBuffer += fmt.Sprintf(format, a...)
  17. return 0, nil
  18. }
  19. func fakeSendln(a ...interface{}) (int, error) {
  20. stdoutBuffer += fmt.Sprintln(a...)
  21. return 0, nil
  22. }
  23. var (
  24. stdoutBuffer string
  25. DATAFILE string
  26. )
  27. func clearStdout() {
  28. stdoutBuffer = ""
  29. }
  30. func TestHarness(t *testing.T) {
  31. assert.Equal(t, 1, 1, "Math stopped working.")
  32. }
  33. var parseTests = []struct {
  34. in string
  35. out session
  36. }{
  37. {
  38. "netskeldb 6ec558e1-5f06-4083-9070-206819b53916 luser host.example.com",
  39. session{
  40. Command: "netskeldb",
  41. UUID: "6ec558e1-5f06-4083-9070-206819b53916",
  42. Username: "luser",
  43. Hostname: "host.example.com"},
  44. },
  45. {
  46. "addkey luser host.example.com",
  47. session{
  48. Command: "addkey",
  49. UUID: "nouuid",
  50. Username: "luser",
  51. Hostname: "host.example.com"},
  52. },
  53. {
  54. "sendfile db/testfile 6ec558e1-5f06-4083-9070-206819b53916 luser host.example.com",
  55. session{
  56. Command: "sendfile",
  57. UUID: "6ec558e1-5f06-4083-9070-206819b53916",
  58. Username: "luser",
  59. Hostname: "host.example.com"},
  60. },
  61. {
  62. "sendbase64 db/testfile 6ec558e1-5f06-4083-9070-206819b53916 luser host.example.com",
  63. session{
  64. Command: "sendbase64",
  65. UUID: "6ec558e1-5f06-4083-9070-206819b53916",
  66. Username: "luser",
  67. Hostname: "host.example.com"},
  68. },
  69. {
  70. "unrecognized command string will fail to parse",
  71. session{
  72. Command: "unrecognized",
  73. UUID: "nouuid",
  74. Username: "user",
  75. Hostname: "unknown"},
  76. },
  77. {
  78. "netskeldb this-is-a-malformed-uuid luser host.example.com",
  79. session{
  80. Command: "netskeldb",
  81. UUID: "nouuid",
  82. Username: "luser",
  83. Hostname: "host.example.com"},
  84. },
  85. }
  86. func TestParsing(t *testing.T) {
  87. for _, tt := range parseTests {
  88. t.Run(tt.in, func(t *testing.T) {
  89. s := newSession()
  90. nsCommand := strings.Split(tt.in, " ")
  91. s.Parse(nsCommand)
  92. // RemoteAddr may have been legitimately populated if you are
  93. // running go test on a remote host via ssh. We remove it here
  94. // so that we know what to compare for with the assertions.
  95. s.RemoteAddr = ""
  96. assert.Equal(t, tt.out, s, "The session structure was not Parse()d correctly.")
  97. })
  98. }
  99. }
  100. func TestNetskelDB(t *testing.T) {
  101. clearStdout()
  102. s := newSession()
  103. s.NetskelDB()
  104. assert.Contains(t, stdoutBuffer, "server.go", "Netskeldb was not generated correctly")
  105. assert.Contains(t, stdoutBuffer, "bin/", "Netskeldb was not generated correctly")
  106. }
  107. func TestListDirParent(t *testing.T) {
  108. // This will hit the ".git" special handling and directory handling code
  109. clearStdout()
  110. err := listDir("..")
  111. assert.Nil(t, err)
  112. assert.Contains(t, stdoutBuffer, "700", "I expected at least one directory")
  113. assert.NotContains(t, stdoutBuffer, ".git/", "The git directory should be ignored")
  114. }
  115. func TestListDirNotFound(t *testing.T) {
  116. clearStdout()
  117. err := listDir("/this/directory/does/not/exist")
  118. assert.True(t, os.IsNotExist(err))
  119. }
  120. func TestHeartbeat(t *testing.T) {
  121. clearStdout()
  122. s := newSession()
  123. s.UUID = "6ec558e1-5f06-4083-9070-206819b53916"
  124. s.Hostname = "host.example.org"
  125. s.Username = "luser"
  126. s.Heartbeat()
  127. assert.Equal(t, s.Hostname, clientGet(s.UUID, "hostname"))
  128. assert.Equal(t, s.Username, clientGet(s.UUID, "username"))
  129. }
  130. func TestSendRaw(t *testing.T) {
  131. clearStdout()
  132. s := newSession()
  133. err := s.SendRaw(DATAFILE)
  134. assert.Nil(t, err, "SendRaw exited with an error")
  135. assert.Equal(t, "Hello, world!\n", stdoutBuffer, "File was not sent correctly")
  136. }
  137. func TestSendRawNotFound(t *testing.T) {
  138. clearStdout()
  139. s := newSession()
  140. err := s.SendRaw("/this/file/does/not/exist")
  141. assert.True(t, os.IsNotExist(err), "SendRaw somehow sent a non-existent file.")
  142. }
  143. func TestSendHexdump(t *testing.T) {
  144. clearStdout()
  145. s := newSession()
  146. err := s.SendHexdump(DATAFILE)
  147. assert.Nil(t, err, "SendHexdump exited with an error")
  148. assert.Equal(t, "48656c6c6f2c20776f726c64210a\n", stdoutBuffer, "File was not sent correctly")
  149. }
  150. func TestSendHexdumpNotFound(t *testing.T) {
  151. clearStdout()
  152. s := newSession()
  153. err := s.SendHexdump("/this/file/does/not/exist")
  154. assert.True(t, os.IsNotExist(err), "SendHexdump somehow sent a non-existent file.")
  155. }
  156. func TestSendBase64(t *testing.T) {
  157. clearStdout()
  158. s := newSession()
  159. err := s.SendBase64(DATAFILE)
  160. assert.Nil(t, err, "SendBase64 exited with an error")
  161. assert.Equal(t, "SGVsbG8sIHdvcmxkIQo=\n", stdoutBuffer, "File was not sent correctly.")
  162. }
  163. func TestSendBase64NotFound(t *testing.T) {
  164. clearStdout()
  165. s := newSession()
  166. err := s.SendBase64("/this/file/does/not/exist")
  167. assert.True(t, os.IsNotExist(err), "SendBase64 somehow sent a non-existent file.")
  168. }
  169. func TestAddKey(t *testing.T) {
  170. clearStdout()
  171. s := newSession()
  172. s.Hostname = "host.example.org"
  173. s.Username = "luser"
  174. err := s.AddKey()
  175. if err != nil {
  176. t.Log(err)
  177. t.Error("AddKey error")
  178. }
  179. keyfile, err := ioutil.ReadFile(AUTHKEYSFILE)
  180. if err != nil {
  181. t.Error("Unable to read AUTHKEYSFILE")
  182. }
  183. r := regexp.MustCompile(`restrict ssh-rsa ([^ ]+) ([^ ]+) ([^ ]+) `)
  184. matches := r.FindStringSubmatch(string(keyfile))
  185. s.UUID = matches[3]
  186. assert.Contains(t, stdoutBuffer, "Netskel private key generated", "key not transmitted properly")
  187. assert.Equal(t, s.Hostname, matches[2])
  188. assert.Equal(t, s.Hostname, clientGet(s.UUID, "hostname"))
  189. assert.Equal(t, s.Hostname, clientGet(s.UUID, "originalHostname"))
  190. }
  191. func TestMain(m *testing.M) {
  192. CLIENTDB = "testing.db"
  193. DATAFILE = "sample.dat"
  194. AUTHKEYSFILE = "testing_keys"
  195. ioutil.WriteFile(DATAFILE, []byte("Hello, world!\n"), 0644)
  196. ioutil.WriteFile(AUTHKEYSFILE, []byte{}, 0644)
  197. code := m.Run()
  198. os.Remove(CLIENTDB)
  199. os.Remove(DATAFILE)
  200. os.Remove(AUTHKEYSFILE)
  201. os.Exit(code)
  202. }