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 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。