netutils_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutils2
  15. import (
  16. "os"
  17. "reflect"
  18. "testing"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  21. )
  22. func TestNetBytes2Mask(t *testing.T) {
  23. type args struct {
  24. mask []byte
  25. }
  26. tests := []struct {
  27. name string
  28. args args
  29. want string
  30. }{
  31. {
  32. "test 255.255.255.255",
  33. args{[]byte{255, 255, 255, 255}},
  34. "255.255.255.255",
  35. },
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. if got := NetBytes2Mask(tt.args.mask); got != tt.want {
  40. t.Errorf("NetBytes2Mask() = %v, want %v", got, tt.want)
  41. }
  42. })
  43. }
  44. }
  45. func TestFormatMac(t *testing.T) {
  46. type args struct {
  47. macStr string
  48. }
  49. tests := []struct {
  50. name string
  51. args args
  52. want string
  53. }{
  54. {
  55. name: "test-format-mac-1",
  56. args: args{"FFFFFFFFFFFF"},
  57. want: "ff:ff:ff:ff:ff:ff",
  58. },
  59. {
  60. name: "test-format-mac-2",
  61. args: args{"FFFFFFFFFF"},
  62. want: "",
  63. },
  64. {
  65. name: "test-format-mac-3",
  66. args: args{"FFDDEECCBBAA"},
  67. want: "ff:dd:ee:cc:bb:aa",
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. if got := FormatMac(tt.args.macStr); got != tt.want {
  73. t.Errorf("FormatMac() = %v, want %v", got, tt.want)
  74. }
  75. })
  76. }
  77. }
  78. func TestNewNetInterface(t *testing.T) {
  79. n := NewNetInterface("eth0")
  80. t.Logf("NetInterface: %s %s %s %s", n.name, n.Addr, n.Mask.String(), n.mac)
  81. addrs := n.GetAddresses()
  82. t.Logf("addrs: %s", jsonutils.Marshal(addrs).String())
  83. slaves := n.GetSlaveAddresses()
  84. t.Logf("slaves: %s", jsonutils.Marshal(slaves).String())
  85. routes := n.GetRouteSpecs()
  86. t.Logf("routes: %s", jsonutils.Marshal(routes).String())
  87. for i := range routes {
  88. t.Logf("route to %s", routes[i].Dst.String())
  89. }
  90. }
  91. func TestMyDefault(t *testing.T) {
  92. myip, err := MyIP()
  93. if err != nil {
  94. // Skip if it's no route to host
  95. t.Fatalf("MyIP: %v", err)
  96. }
  97. if myip != "" {
  98. srcIp, ifname, err := DefaultSrcIpDev()
  99. if err != nil {
  100. t.Fatalf("default srcip dev: %v", err)
  101. }
  102. if srcIp.String() != myip {
  103. t.Errorf("myip: %s, srcip: %s", myip, srcIp.String())
  104. }
  105. if ifname == "" {
  106. t.Errorf("empty ifname")
  107. }
  108. }
  109. }
  110. func TestGetMainNicFromDeployApi(t *testing.T) {
  111. nics1 := []*types.SServerNic{
  112. {
  113. Ip: "10.168.222.19",
  114. Gateway: "10.168.222.1",
  115. },
  116. {
  117. Ip: "114.114.114.114",
  118. Gateway: "114.114.114.1",
  119. },
  120. }
  121. nics2 := []*types.SServerNic{
  122. {
  123. Ip: "10.168.222.19",
  124. },
  125. {
  126. Ip: "114.114.114.114",
  127. Gateway: "114.114.114.1",
  128. },
  129. }
  130. nics3 := []*types.SServerNic{
  131. {
  132. Ip: "10.168.222.19",
  133. Gateway: "10.168.222.1",
  134. },
  135. {
  136. Ip: "114.114.114.114",
  137. },
  138. }
  139. nics4 := []*types.SServerNic{
  140. {
  141. Ip: "10.168.222.19",
  142. },
  143. {
  144. Ip: "114.114.114.114",
  145. },
  146. }
  147. cases := []struct {
  148. nics []*types.SServerNic
  149. want *types.SServerNic
  150. }{
  151. {
  152. nics1,
  153. nics1[1],
  154. },
  155. {
  156. nics2,
  157. nics2[1],
  158. },
  159. {
  160. nics3,
  161. nics3[0],
  162. },
  163. {
  164. nics4,
  165. nics4[1],
  166. },
  167. }
  168. for _, c := range cases {
  169. got, err := GetMainNicFromDeployApi(c.nics)
  170. if err != nil {
  171. t.Errorf("error %s", err)
  172. } else if got != c.want {
  173. t.Errorf("error: got %v want %v", got, c.want)
  174. }
  175. }
  176. }
  177. func TestGetRouteSpecs(t *testing.T) {
  178. dirs, _ := os.ReadDir("/sys/class/net")
  179. for _, file := range dirs {
  180. t.Logf("dir: %s", file.Name())
  181. n := NewNetInterface(file.Name())
  182. routes := n.GetRouteSpecs()
  183. for i := range routes {
  184. t.Logf("route %s via %s", routes[i].Dst.String(), routes[i].Gw.String())
  185. }
  186. }
  187. }
  188. func TestSplitV46Addr(t *testing.T) {
  189. cases := []struct {
  190. in string
  191. want4 []string
  192. want6 []string
  193. }{
  194. {
  195. in: "192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.10",
  196. want4: []string{"192.168.1.1", "192.168.1.10", "192.168.1.2", "192.168.1.3"},
  197. want6: []string{},
  198. },
  199. {
  200. in: "192.168.1.1,192.168.1.2,fe80::1,192.168.1.10",
  201. want4: []string{"192.168.1.1", "192.168.1.10", "192.168.1.2"},
  202. want6: []string{"fe80::1"},
  203. },
  204. }
  205. for _, c := range cases {
  206. got4, got6 := SplitV46Addr(c.in)
  207. if !reflect.DeepEqual(got4, c.want4) {
  208. t.Errorf("SplitV46Addr(%s) = %v, want %v", c.in, got4, c.want4)
  209. }
  210. if !reflect.DeepEqual(got6, c.want6) {
  211. t.Errorf("SplitV46Addr(%s) = %v, want %v", c.in, got6, c.want6)
  212. }
  213. }
  214. }