utils_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2015, Cong Ding. All rights reserved.
  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. //
  15. // Author: Cong Ding <[email protected]>
  16. package stun
  17. import (
  18. "testing"
  19. )
  20. func TestPadding(t *testing.T) {
  21. b := []byte{1, 2}
  22. expected := []byte{1, 2, 0, 0}
  23. result := padding(b)
  24. if len(result) != len(expected) {
  25. t.Errorf("Padding error: result size wrong.\n")
  26. }
  27. for i := range expected {
  28. if expected[i] != result[i] {
  29. t.Errorf("Padding error: data wrong in bit %d.\n", i)
  30. }
  31. }
  32. }
  33. func TestAlign(t *testing.T) {
  34. d := make(map[uint16]uint16)
  35. d[1] = 4
  36. d[4] = 4
  37. d[5] = 8
  38. d[6] = 8
  39. d[7] = 8
  40. d[8] = 8
  41. d[65528] = 65528
  42. d[65529] = 65532
  43. d[65531] = 65532
  44. d[65532] = 65532
  45. for k, v := range d {
  46. if align(k) != v {
  47. t.Errorf("Align error: expected %d, get %d", align(k), v)
  48. }
  49. }
  50. }
  51. func TestIsLocalAddress(t *testing.T) {
  52. if !isLocalAddress(":1234", "127.0.0.1:8888") {
  53. t.Errorf("isLocal error")
  54. }
  55. if !isLocalAddress("192.168.0.1:1234", "192.168.0.1:8888") {
  56. t.Errorf("isLocal error")
  57. }
  58. if !isLocalAddress("8.8.8.8:1234", "8.8.8.8:8888") {
  59. t.Errorf("isLocal error")
  60. }
  61. if isLocalAddress(":1234", "8.8.8.8:8888") {
  62. t.Errorf("isLocal error")
  63. }
  64. }