1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
package types_test
import (
"encoding/json"
"net"
"github.com/mimuret/golang-iij-dpf/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("types.net", func() {
Context("ParseIPNet", func() {
var (
err error
ipnet *types.IPNet
)
When("failed to parse net.IPNet", func() {
BeforeEach(func() {
_, err = types.ParseIPNet("//")
})
It("returns err", func() {
Expect(err).To(HaveOccurred())
})
})
When("parse successfull", func() {
BeforeEach(func() {
ipnet, err = types.ParseIPNet("192.168.0.0/24")
})
It("returns count value", func() {
Expect(err).To(Succeed())
Expect(ipnet).To(Equal(&types.IPNet{
net.IPNet{
IP: net.ParseIP("192.168.0.0").To4(),
Mask: net.IPv4Mask(255, 255, 255, 0),
},
}))
})
})
})
Context("IPNet", func() {
var (
err error
bs []byte
ipnet *types.IPNet
)
BeforeEach(func() {
ipnet = &types.IPNet{}
})
Context("MarshalJSON", func() {
When("valid value", func() {
BeforeEach(func() {
ipnet, err = types.ParseIPNet("192.168.0.0/24")
Expect(err).To(Succeed())
bs, err = json.Marshal(ipnet)
Expect(err).To(Succeed())
})
It("returns count value", func() {
Expect(string(bs)).To(Equal(`"192.168.0.0/24"`))
})
})
When("valid empty", func() {
BeforeEach(func() {
bs, err = json.Marshal(ipnet)
Expect(err).To(Succeed())
})
It("returns count value", func() {
Expect(string(bs)).To(Equal(`"\u003cnil\u003e"`))
})
})
})
Context("UnmarshalJSON", func() {
When("valid format", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"192.168.0.0/24"`), ipnet)
Expect(err).To(Succeed())
})
It("returns count value", func() {
Expect(ipnet).To(Equal(&types.IPNet{
net.IPNet{
IP: net.ParseIP("192.168.0.0").To4(),
Mask: net.IPv4Mask(255, 255, 255, 0),
},
}))
})
})
When("input value is struct", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`{"name": "hoge"}`), ipnet)
})
It("returns err", func() {
Expect(err).To(HaveOccurred())
})
})
When("invalid format", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"192.168.0.0/24/24"`), ipnet)
})
It("returns err", func() {
Expect(err).To(HaveOccurred())
})
})
When("\u003cnil\u003e", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"\u003cnil\u003e"`), ipnet)
})
It("returns err", func() {
Expect(err).To(Succeed())
Expect(*ipnet).To(Equal(types.IPNet{}))
})
})
})
Context("DeepCopy", func() {
BeforeEach(func() {
ipnet, err = types.ParseIPNet("2001:db8::1/64")
Expect(err).To(Succeed())
})
It("returns copy object", func() {
copy := ipnet.DeepCopy()
Expect(copy).To(Equal(ipnet))
Expect(copy).ShouldNot(BeIdenticalTo(ipnet))
Expect(copy.IPNet.IP).ShouldNot(BeIdenticalTo(ipnet.IPNet.IP))
Expect(copy.IPNet.Mask).ShouldNot(BeIdenticalTo(ipnet.IPNet.Mask))
})
})
})
})
|