From 1eb2e4dd72c82b89e5d72b0a9b1e0f7caf0da6c3 Mon Sep 17 00:00:00 2001 From: DevMhrn Date: Mon, 8 Jun 2026 17:37:47 +0530 Subject: [PATCH] feat(unikernels): add unit tests for IsIPInSubnet function Introduce a new test file for LinuxNet subnet validation, covering various scenarios including same subnet, different subnet, and invalid masks. Enhance the IsIPInSubnet function to handle edge cases by returning false for invalid inputs. Signed-off-by: DevMhrn --- pkg/unikontainers/unikernels/linux.go | 3 + .../unikernels/linux_network_test.go | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 pkg/unikontainers/unikernels/linux_network_test.go diff --git a/pkg/unikontainers/unikernels/linux.go b/pkg/unikontainers/unikernels/linux.go index d6bb80952..6de6ad79a 100644 --- a/pkg/unikontainers/unikernels/linux.go +++ b/pkg/unikontainers/unikernels/linux.go @@ -60,6 +60,9 @@ func IsIPInSubnet(ln LinuxNet) bool { ip := net.ParseIP(ln.Address) gw := net.ParseIP(ln.Gateway) mask := net.IPMask(net.ParseIP(ln.Mask).To4()) + if ip == nil || gw == nil || mask == nil { + return false + } subnet := gw.Mask(mask) return ip.Mask(mask).Equal(subnet) diff --git a/pkg/unikontainers/unikernels/linux_network_test.go b/pkg/unikontainers/unikernels/linux_network_test.go new file mode 100644 index 000000000..79612e7da --- /dev/null +++ b/pkg/unikontainers/unikernels/linux_network_test.go @@ -0,0 +1,76 @@ +// Copyright (c) 2023-2026, Nubificus LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package unikernels + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIsIPInSubnet(t *testing.T) { + tests := []struct { + name string + net LinuxNet + want bool + }{ + { + name: "same subnet", + net: LinuxNet{Address: "10.0.0.5", Gateway: "10.0.0.1", Mask: "255.255.255.0"}, + want: true, + }, + { + name: "different subnet", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: "255.255.255.0"}, + want: false, + }, + { + name: "empty mask", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: ""}, + want: false, + }, + { + name: "garbage mask", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: "abc"}, + want: false, + }, + { + name: "truncated mask", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: "255.255.255"}, + want: false, + }, + { + name: "IPv6 mask", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: "ffff:ffff::"}, + want: false, + }, + { + name: "CIDR-style mask", + net: LinuxNet{Address: "10.244.0.5", Gateway: "169.254.1.1", Mask: "/24"}, + want: false, + }, + { + name: "all empty", + net: LinuxNet{}, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, IsIPInSubnet(tt.net)) + }) + } +}