@@ -3,6 +3,7 @@ package validate
33import (
44 "os"
55 "path/filepath"
6+ "strings"
67 "testing"
78
89 "github.com/opencontainers/runc/libcontainer/configs"
@@ -877,3 +878,168 @@ func TestValidateIOPriority(t *testing.T) {
877878 }
878879 }
879880}
881+
882+ func TestValidateNetDevices (t * testing.T ) {
883+ testCases := []struct {
884+ name string
885+ isErr bool
886+ config * configs.Config
887+ }{
888+ {
889+ name : "network device with configured network namespace" ,
890+ config : & configs.Config {
891+ Namespaces : configs .Namespaces (
892+ []configs.Namespace {
893+ {
894+ Type : configs .NEWNET ,
895+ Path : "/var/run/netns/blue" ,
896+ },
897+ },
898+ ),
899+ NetDevices : map [string ]* configs.LinuxNetDevice {
900+ "eth0" : {},
901+ },
902+ },
903+ },
904+ {
905+ name : "network device rename" ,
906+ config : & configs.Config {
907+ Namespaces : configs .Namespaces (
908+ []configs.Namespace {
909+ {
910+ Type : configs .NEWNET ,
911+ Path : "/var/run/netns/blue" ,
912+ },
913+ },
914+ ),
915+ NetDevices : map [string ]* configs.LinuxNetDevice {
916+ "eth0" : {
917+ Name : "c0" ,
918+ },
919+ },
920+ },
921+ },
922+ {
923+ name : "network device network namespace created by runc" ,
924+ config : & configs.Config {
925+ Namespaces : configs .Namespaces (
926+ []configs.Namespace {
927+ {
928+ Type : configs .NEWNET ,
929+ },
930+ },
931+ ),
932+ NetDevices : map [string ]* configs.LinuxNetDevice {
933+ "eth0" : {},
934+ },
935+ },
936+ },
937+ {
938+ name : "network device network namespace empty" ,
939+ isErr : true ,
940+ config : & configs.Config {
941+ Namespaces : configs .Namespaces (
942+ []configs.Namespace {},
943+ ),
944+ NetDevices : map [string ]* configs.LinuxNetDevice {
945+ "eth0" : {},
946+ },
947+ },
948+ },
949+ {
950+ name : "network device rootless EUID" ,
951+ isErr : true ,
952+ config : & configs.Config {
953+ Namespaces : configs .Namespaces (
954+ []configs.Namespace {
955+ {
956+ Type : configs .NEWNET ,
957+ Path : "/var/run/netns/blue" ,
958+ },
959+ },
960+ ),
961+ RootlessEUID : true ,
962+ NetDevices : map [string ]* configs.LinuxNetDevice {
963+ "eth0" : {},
964+ },
965+ },
966+ },
967+ {
968+ name : "network device rootless" ,
969+ isErr : true ,
970+ config : & configs.Config {
971+ Namespaces : configs .Namespaces (
972+ []configs.Namespace {
973+ {
974+ Type : configs .NEWNET ,
975+ Path : "/var/run/netns/blue" ,
976+ },
977+ },
978+ ),
979+ RootlessCgroups : true ,
980+ NetDevices : map [string ]* configs.LinuxNetDevice {
981+ "eth0" : {},
982+ },
983+ },
984+ },
985+ {
986+ name : "network device bad name" ,
987+ isErr : true ,
988+ config : & configs.Config {
989+ Namespaces : configs .Namespaces (
990+ []configs.Namespace {
991+ {
992+ Type : configs .NEWNET ,
993+ Path : "/var/run/netns/blue" ,
994+ },
995+ },
996+ ),
997+ NetDevices : map [string ]* configs.LinuxNetDevice {
998+ "eth0" : {
999+ Name : "eth0/" ,
1000+ },
1001+ },
1002+ },
1003+ },
1004+ }
1005+
1006+ for _ , tc := range testCases {
1007+ t .Run (tc .name , func (t * testing.T ) {
1008+ config := tc .config
1009+ config .Rootfs = "/var"
1010+
1011+ err := Validate (config )
1012+ if tc .isErr && err == nil {
1013+ t .Error ("expected error, got nil" )
1014+ }
1015+
1016+ if ! tc .isErr && err != nil {
1017+ t .Error (err )
1018+ }
1019+ })
1020+ }
1021+ }
1022+
1023+ func TestDevValidName (t * testing.T ) {
1024+ testCases := []struct {
1025+ name string
1026+ valid bool
1027+ }{
1028+ {name : "" , valid : false },
1029+ {name : "a" , valid : true },
1030+ {name : strings .Repeat ("a" , unix .IFNAMSIZ ), valid : true },
1031+ {name : strings .Repeat ("a" , unix .IFNAMSIZ + 1 ), valid : false },
1032+ {name : "." , valid : false },
1033+ {name : ".." , valid : false },
1034+ {name : "dev/null" , valid : false },
1035+ {name : "valid:name" , valid : false },
1036+ {name : "valid name" , valid : false },
1037+ }
1038+ for _ , tc := range testCases {
1039+ t .Run (tc .name , func (t * testing.T ) {
1040+ if devValidName (tc .name ) != tc .valid {
1041+ t .Fatalf ("name %q, expected valid: %v" , tc .name , tc .valid )
1042+ }
1043+ })
1044+ }
1045+ }
0 commit comments