Skip to content

Commit

Permalink
Add parameter verification for api creation network
Browse files Browse the repository at this point in the history
Signed-off-by: zhangguanzhang <zhangguanzhang@qq.com>
  • Loading branch information
zhangguanzhang authored and mheon committed Aug 17, 2020
1 parent 0286cfe commit ea66108
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ func networkIntersect(n1, n2 *net.IPNet) bool {
// ValidateUserNetworkIsAvailable returns via an error if a network is available
// to be used
func ValidateUserNetworkIsAvailable(config *config.Config, userNet *net.IPNet) error {
if len(userNet.IP) == 0 || len(userNet.Mask) == 0 {
return errors.Errorf("network %s's ip or mask cannot be empty", userNet.String())
}

ones, bit := userNet.Mask.Size()
if ones == 0 || bit == 0 {
return errors.Errorf("network %s's mask is invalid", userNet.String())
}

networks, err := GetNetworksFromFilesystem(config)
if err != nil {
return err
Expand Down
28 changes: 27 additions & 1 deletion test/apiv2/35-networks.at
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
# network-related tests
#

t GET /networks/non-existing-network 404
t GET networks/non-existing-network 404 \
.cause='network not found'

if root; then
t POST libpod/networks/create?name=network1 '' 200 \
.Filename~.*/network1\\.conflist

# --data '{"Subnet":{"IP":"10.10.254.0","Mask":[255,255,255,0]}}'
t POST libpod/networks/create?name=network2 '"Subnet":{"IP":"10.10.254.0","Mask":[255,255,255,0]}' 200 \
.Filename~.*/network2\\.conflist

# test for empty mask
t POST libpod/networks/create '"Subnet":{"IP":"10.10.1.0","Mask":[]}' 500 \
.cause~'.*cannot be empty'
# test for invalid mask
t POST libpod/networks/create '"Subnet":{"IP":"10.10.1.0","Mask":[0,255,255,0]}' 500 \
.cause~'.*mask is invalid'

# clean the network
t DELETE libpod/networks/network1 200 \
.[0].Name~network1 \
.[0].Err=null
t DELETE libpod/networks/network2 200 \
.[0].Name~network2 \
.[0].Err=null

fi

# vim: filetype=sh

0 comments on commit ea66108

Please sign in to comment.