net_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. func TestNetCmd(t *testing.T) {
  8. app := NewApp()
  9. output := new(bytes.Buffer)
  10. app.Writer = output
  11. err := app.Run([]string{"ovpm", "net"})
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. if !strings.Contains(output.String(), "list, l") {
  16. t.Fatal("subcommand missing 'list, l'")
  17. }
  18. if !strings.Contains(output.String(), "types, t") {
  19. t.Fatal("subcommand missing 'types, t'")
  20. }
  21. if !strings.Contains(output.String(), "def, d") {
  22. t.Fatal("subcommand missing 'undef, u'")
  23. }
  24. if !strings.Contains(output.String(), "assoc, a") {
  25. t.Fatal("subcommand missing 'assoc, a'")
  26. }
  27. if !strings.Contains(output.String(), "dissoc, di") {
  28. t.Fatal("subcommand missing 'dissoc, di'")
  29. }
  30. }
  31. func TestNetDefineCmd(t *testing.T) {
  32. app := NewApp()
  33. output := new(bytes.Buffer)
  34. app.Writer = output
  35. var err error
  36. // Empty call
  37. err = app.Run([]string{"ovpm", "net", "def"})
  38. if err == nil {
  39. t.Fatal("error is expected about missing fields, but we didn't got error")
  40. }
  41. // Missing type
  42. err = app.Run([]string{"ovpm", "net", "def", "--cidr", "192.168.1.1/24"})
  43. if err == nil {
  44. t.Fatal("error is expected about missing network type, but we didn't got error")
  45. }
  46. // Missing name
  47. err = app.Run([]string{"ovpm", "net", "def", "--type", "SERVERNET", "--cidr", "192.168.1.1/24"})
  48. if err == nil {
  49. t.Fatal("error is expected about missing network name, but we didn't got error")
  50. }
  51. // Incorrect type
  52. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNUT", "--cidr", "192.168.1.1/24"})
  53. if err == nil {
  54. t.Fatal("error is expected about incorrect server type, but we didn't got error")
  55. }
  56. // Incorrect use of via
  57. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1/24", "--via", "8.8.8.8"})
  58. if err == nil {
  59. t.Fatal("error is expected about incorrect use of via, but we didn't got error")
  60. }
  61. // Incorrect cidr format
  62. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1"})
  63. if err == nil {
  64. t.Fatal("error is expected about incorrect cidr format, but we didn't got error")
  65. }
  66. // Ensure ROUTE type use without --via
  67. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "ROUTE", "--cidr", "192.168.1.1/24"})
  68. if err != nil && !strings.Contains(err.Error(), "grpc") {
  69. t.Fatalf("error is not expected: %v", err)
  70. }
  71. // Incorrect use of via
  72. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1/24", "--via", "8.8.8.8/24"})
  73. if err == nil {
  74. t.Fatal("error is expected about incorrect via format, but we didn't got error")
  75. }
  76. // Ensure network name alphanumeric and dot, dash, underscore chars are allowed
  77. err = app.Run([]string{"ovpm", "net", "def", "--name", "asd.asd-d5sa_fasA32", "--type", "ROUTE", "--cidr", "192.168.1.1/24"})
  78. if err != nil && !strings.Contains(err.Error(), "grpc") {
  79. t.Fatalf("error is not expected: %v", err)
  80. }
  81. }
  82. func TestNetUnDefineCmd(t *testing.T) {
  83. app := NewApp()
  84. output := new(bytes.Buffer)
  85. app.Writer = output
  86. var err error
  87. // Empty call
  88. err = app.Run([]string{"ovpm", "net", "undef"})
  89. if err == nil {
  90. t.Fatal("error is expected about missing fields, but we didn't got error")
  91. }
  92. }
  93. func TestAssocCmd(t *testing.T) {
  94. app := NewApp()
  95. output := new(bytes.Buffer)
  96. app.Writer = output
  97. var err error
  98. // Empty call
  99. err = app.Run([]string{"ovpm", "net", "assoc"})
  100. if err == nil {
  101. t.Fatal("error is expected about missing fields, but we didn't got error")
  102. }
  103. // Missing network name
  104. err = app.Run([]string{"ovpm", "net", "def", "--user", "asd"})
  105. if err == nil {
  106. t.Fatal("error is expected about missing network name, but we didn't got error")
  107. }
  108. // Missing username
  109. err = app.Run([]string{"ovpm", "net", "def", "--network", "asddsa"})
  110. if err == nil {
  111. t.Fatal("error is expected about missing username, but we didn't got error")
  112. }
  113. }