douxian1939 2017-03-21 07:58
浏览 56
已采纳

net.IPNet的IP部分不满足reflect.DeepEqual,但等于

I'm experiencing unexpected behavior in a unit test which uses reflect.DeepEqual to test for the equality of two parsed structures. The structures contain a Slice of net.IPNet structs. The tests fail because reflect.DeepEqual returns false.

While tracking this issue down I found the following unexpected behavior of DeepEqual. The most fascinating thing for me is that it works with IPv6 addresses and using the war parsed IP, not the masked one in the net.IPNet struct.

Can anyone explain to me:

  • Why are those IPv4 addresses not DeepEqual, despite their byte-representation seems to be?
  • Why are they DeepEqual for IPv6 addresses?
  • How to construct an net.IP instance that match the one generated by ParseCIDR with respect to DeepEqual?

Example program, runable with go run:

package main

import (
    "fmt"
    "net"
    "reflect"
)

func main() {
    aip, a, _ := net.ParseCIDR("135.104.0.0/32")
    //aip, a, _ := net.ParseCIDR("abcd:2345::/65")

    bip := net.IPv4(135, 104, 0, 0)
    //bip := net.ParseIP("abcd:2345::")


    // IPa: 135.104.0.0 3133352e3130342e302e30
    fmt.Printf("IPa: %s %x
", a.IP, a.IP)

    // IPb: 135.104.0.0 3133352e3130342e302e30
    fmt.Printf("IPb: %s %x
", bip, bip)

    fmt.Println("eq?:", a.IP.Equal(bip)) // true

    // I'd expect this to be true
    fmt.Println("deep eq?:", reflect.DeepEqual(a.IP, bip)) // false

    fmt.Println("deep eq w/o mask?:", reflect.DeepEqual(aip, bip)) // true
}
  • 写回答

1条回答 默认 最新

  • doumen1883 2017-03-21 08:09
    关注

    They aren't deep equal because they don't meet the definition of deep equality.

    By looking at the internal representation of each value, as I did on this playground, we can see that while they are both IPv4 address, bip is preceded by a number of extra bytes (presumably the IPv6 representation... why, I'm not sure).

    IPa: 135.104.0.0 3133352e3130342e302e30 net.IP net.IP{0x87, 0x68, 0x0, 0x0}
    IPb: 135.104.0.0 3133352e3130342e302e30 net.IP net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x87, 0x68, 0x0, 0x0}
    

    Further, using DeepEqual to compare something as basic as an IP address is a Very Bad Idea™, for the simple reason that IPs are not deep structures. They are (for equality purposes) simple opaque values. So just compare the opaque values directly. This will mean outputting something from your various storage structs, but output them in a common format and compare that. Comparing straight IPs in their byte representation (int32) will generally be the most efficient. If you need to compare the network address, netmask, etc, do it the same way. Alternately, compare a string representation, if that's easier for your tools.

    But at the core, an IP address is just a 32-bit integer (or 128-bit integer in the case of IPv6), and the other attributes are other integers. Checking for Deep Equality on integers makes no sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)