@@ -230,3 +230,78 @@ func TestNamedMapOpts(t *testing.T) {
230230 t .Errorf ("expected map-size to be in the values, got %v" , tmpMap )
231231 }
232232}
233+
234+ func TestValidateMACAddress (t * testing.T ) {
235+ if _ , err := ValidateMACAddress (`92:d0:c6:0a:29:33` ); err != nil {
236+ t .Fatalf ("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s" , err )
237+ }
238+
239+ if _ , err := ValidateMACAddress (`92:d0:c6:0a:33` ); err == nil {
240+ t .Fatalf ("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC" )
241+ }
242+
243+ if _ , err := ValidateMACAddress (`random invalid string` ); err == nil {
244+ t .Fatalf ("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC" )
245+ }
246+ }
247+
248+ func TestValidateLink (t * testing.T ) {
249+ valid := []string {
250+ "name" ,
251+ "dcdfbe62ecd0:alias" ,
252+ "7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da" ,
253+ "angry_torvalds:linus" ,
254+ }
255+ invalid := map [string ]string {
256+ "" : "empty string specified for links" ,
257+ "too:much:of:it" : "bad format for links: too:much:of:it" ,
258+ }
259+
260+ for _ , link := range valid {
261+ if _ , err := ValidateLink (link ); err != nil {
262+ t .Fatalf ("ValidateLink(`%q`) should succeed: error %q" , link , err )
263+ }
264+ }
265+
266+ for link , expectedError := range invalid {
267+ if _ , err := ValidateLink (link ); err == nil {
268+ t .Fatalf ("ValidateLink(`%q`) should have failed validation" , link )
269+ } else {
270+ if ! strings .Contains (err .Error (), expectedError ) {
271+ t .Fatalf ("ValidateLink(`%q`) error should contain %q" , link , expectedError )
272+ }
273+ }
274+ }
275+ }
276+
277+ func TestParseLink (t * testing.T ) {
278+ name , alias , err := ParseLink ("name:alias" )
279+ if err != nil {
280+ t .Fatalf ("Expected not to error out on a valid name:alias format but got: %v" , err )
281+ }
282+ if name != "name" {
283+ t .Fatalf ("Link name should have been name, got %s instead" , name )
284+ }
285+ if alias != "alias" {
286+ t .Fatalf ("Link alias should have been alias, got %s instead" , alias )
287+ }
288+ // short format definition
289+ name , alias , err = ParseLink ("name" )
290+ if err != nil {
291+ t .Fatalf ("Expected not to error out on a valid name only format but got: %v" , err )
292+ }
293+ if name != "name" {
294+ t .Fatalf ("Link name should have been name, got %s instead" , name )
295+ }
296+ if alias != "name" {
297+ t .Fatalf ("Link alias should have been name, got %s instead" , alias )
298+ }
299+ // empty string link definition is not allowed
300+ if _ , _ , err := ParseLink ("" ); err == nil || ! strings .Contains (err .Error (), "empty string specified for links" ) {
301+ t .Fatalf ("Expected error 'empty string specified for links' but got: %v" , err )
302+ }
303+ // more than two colons are not allowed
304+ if _ , _ , err := ParseLink ("link:alias:wrong" ); err == nil || ! strings .Contains (err .Error (), "bad format for links: link:alias:wrong" ) {
305+ t .Fatalf ("Expected error 'bad format for links: link:alias:wrong' but got: %v" , err )
306+ }
307+ }
0 commit comments