go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/ipsec/models_test.go (about)

     1  // Copyright (c) 2018 Cisco and/or its affiliates.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at:
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vpp_ipsec_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	ipsec "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipsec"
    21  )
    22  
    23  /*func TestIPSecSPDKey(t *testing.T) {
    24  	tests := []struct {
    25  		name        string
    26  		spdIndex    string
    27  		expectedKey string
    28  	}{
    29  		{
    30  			name:        "valid SPD index",
    31  			spdIndex:    "1",
    32  			expectedKey: "vpp/config/v2/ipsec/spd/1",
    33  		},
    34  		{
    35  			name:        "empty SPD index",
    36  			spdIndex:    "",
    37  			expectedKey: "vpp/config/v2/ipsec/spd/<invalid>",
    38  		},
    39  		{
    40  			name:        "invalid SPD index",
    41  			spdIndex:    "spd1",
    42  			expectedKey: "vpp/config/v2/ipsec/spd/<invalid>",
    43  		},
    44  	}
    45  	for _, test := range tests {
    46  		t.Run(test.name, func(t *testing.T) {
    47  			key := ipsec.SPDKey(test.spdIndex)
    48  			if key != test.expectedKey {
    49  				t.Errorf("failed for: spdName=%s\n"+
    50  					"expected key:\n\t%q\ngot key:\n\t%q",
    51  					test.name, test.expectedKey, key)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestParseIPSecSPDNameFromKey(t *testing.T) {
    58  	tests := []struct {
    59  		name             string
    60  		key              string
    61  		expectedSPDIndex string
    62  		expectedIsSPDKey bool
    63  	}{
    64  		{
    65  			name:             "valid SPD index",
    66  			key:              "vpp/config/v2/ipsec/spd/1",
    67  			expectedSPDIndex: "1",
    68  			expectedIsSPDKey: true,
    69  		},
    70  		{
    71  			name:             "empty SPD index",
    72  			key:              "vpp/config/v2/ipsec/spd/<invalid>",
    73  			expectedSPDIndex: "<invalid>",
    74  			expectedIsSPDKey: true,
    75  		},
    76  		{
    77  			name:             "invalid SPD index",
    78  			key:              "vpp/config/v2/ipsec/spd/spd1",
    79  			expectedSPDIndex: "",
    80  			expectedIsSPDKey: true,
    81  		},
    82  		{
    83  			name:             "not SPD key",
    84  			key:              "vpp/config/v2/ipsec/sa/spd1",
    85  			expectedSPDIndex: "",
    86  			expectedIsSPDKey: false,
    87  		},
    88  	}
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			spdName, isSPDKey := models.Model(&ipsec.SecurityPolicyDatabase{}).ParseKey(test.key)
    92  			if isSPDKey != test.expectedIsSPDKey {
    93  				t.Errorf("expected isSPDKey: %v\tgot: %v", test.expectedIsSPDKey, isSPDKey)
    94  			}
    95  			if spdName != test.expectedSPDIndex {
    96  				t.Errorf("expected spdName: %s\tgot: %s", test.expectedSPDIndex, spdName)
    97  			}
    98  		})
    99  	}
   100  }*/
   101  
   102  func TestSPDInterfaceKey(t *testing.T) {
   103  	tests := []struct {
   104  		name        string
   105  		spdIndex    uint32
   106  		ifName      string
   107  		expectedKey string
   108  	}{
   109  		{
   110  			name:        "valid SPD index & iface name",
   111  			spdIndex:    1,
   112  			ifName:      "if1",
   113  			expectedKey: "vpp/spd/1/interface/if1",
   114  		},
   115  		{
   116  			name:        "empty SPD & valid interface",
   117  			ifName:      "if1",
   118  			expectedKey: "vpp/spd/0/interface/if1",
   119  		},
   120  		{
   121  			name:        "invalid interface",
   122  			spdIndex:    1,
   123  			ifName:      "",
   124  			expectedKey: "vpp/spd/1/interface/<invalid>",
   125  		},
   126  		{
   127  			name:        "Gbe interface",
   128  			spdIndex:    1,
   129  			ifName:      "GigabitEthernet0/a/0",
   130  			expectedKey: "vpp/spd/1/interface/GigabitEthernet0/a/0",
   131  		},
   132  	}
   133  	for _, test := range tests {
   134  		t.Run(test.name, func(t *testing.T) {
   135  			key := ipsec.SPDInterfaceKey(test.spdIndex, test.ifName)
   136  			if key != test.expectedKey {
   137  				t.Errorf("failed for: spdIdx=%d idName=%s\n"+
   138  					"expected key:\n\t%q\ngot key:\n\t%q",
   139  					test.spdIndex, test.ifName, test.expectedKey, key)
   140  			}
   141  		})
   142  	}
   143  }
   144  
   145  func TestParseSPDInterfaceKey(t *testing.T) {
   146  	tests := []struct {
   147  		name                 string
   148  		key                  string
   149  		expectedSPDIndex     string
   150  		expectedIfName       string
   151  		expectedIsSAIfaceKey bool
   152  	}{
   153  		{
   154  			name:                 "valid SPD & iface name",
   155  			key:                  "vpp/spd/1/interface/if1",
   156  			expectedSPDIndex:     "1",
   157  			expectedIfName:       "if1",
   158  			expectedIsSAIfaceKey: true,
   159  		},
   160  		{
   161  			name:                 "invalid SPD but valid interface",
   162  			key:                  "vpp/spd/<invalid>/interface/if1",
   163  			expectedSPDIndex:     "<invalid>",
   164  			expectedIfName:       "if1",
   165  			expectedIsSAIfaceKey: true,
   166  		},
   167  		{
   168  			name:                 "valid SPD but invalid interface",
   169  			key:                  "vpp/spd/1/interface/<invalid>",
   170  			expectedSPDIndex:     "1",
   171  			expectedIfName:       "<invalid>",
   172  			expectedIsSAIfaceKey: true,
   173  		},
   174  		{
   175  			name:                 "Gbe interface",
   176  			key:                  "vpp/spd/1/interface/GigabitEthernet0/8/0",
   177  			expectedSPDIndex:     "1",
   178  			expectedIfName:       "GigabitEthernet0/8/0",
   179  			expectedIsSAIfaceKey: true,
   180  		},
   181  		{
   182  			name:                 "not SPD-interface key",
   183  			key:                  "vpp/config/v2/ipsec/spd/1",
   184  			expectedSPDIndex:     "",
   185  			expectedIfName:       "",
   186  			expectedIsSAIfaceKey: false,
   187  		},
   188  	}
   189  	for _, test := range tests {
   190  		t.Run(test.name, func(t *testing.T) {
   191  			spdIdx, ifName, isSPDIfaceKey := ipsec.ParseSPDInterfaceKey(test.key)
   192  			if isSPDIfaceKey != test.expectedIsSAIfaceKey {
   193  				t.Errorf("expected isSPDIfaceKey: %v\tgot: %v", test.expectedIsSAIfaceKey, isSPDIfaceKey)
   194  			}
   195  			if spdIdx != test.expectedSPDIndex {
   196  				t.Errorf("expected spdIdx: %s\tgot: %s", test.expectedSPDIndex, spdIdx)
   197  			}
   198  			if ifName != test.expectedIfName {
   199  				t.Errorf("expected ifName: %s\tgot: %s", test.expectedIfName, ifName)
   200  			}
   201  		})
   202  	}
   203  }
   204  
   205  /*func TestIPSecSAKey(t *testing.T) {
   206  	tests := []struct {
   207  		name        string
   208  		saIndex     string
   209  		expectedKey string
   210  	}{
   211  		{
   212  			name:        "valid SA index",
   213  			saIndex:     "1",
   214  			expectedKey: "vpp/config/v2/ipsec/sa/1",
   215  		},
   216  		{
   217  			name:        "empty SA index",
   218  			saIndex:     "",
   219  			expectedKey: "vpp/config/v2/ipsec/sa/<invalid>",
   220  		},
   221  		{
   222  			name:        "invalid SA index",
   223  			saIndex:     "sa1",
   224  			expectedKey: "vpp/config/v2/ipsec/sa/<invalid>",
   225  		},
   226  	}
   227  	for _, test := range tests {
   228  		t.Run(test.name, func(t *testing.T) {
   229  			key := ipsec.SAKey(test.saIndex)
   230  			if key != test.expectedKey {
   231  				t.Errorf("failed for: saName=%s\n"+
   232  					"expected key:\n\t%q\ngot key:\n\t%q",
   233  					test.name, test.expectedKey, key)
   234  			}
   235  		})
   236  	}
   237  }
   238  
   239  func TestParseIPSecSANameFromKey(t *testing.T) {
   240  	tests := []struct {
   241  		name            string
   242  		key             string
   243  		expectedSAIndex string
   244  		expectedIsSAKey bool
   245  	}{
   246  		{
   247  			name:            "valid SA index",
   248  			key:             "vpp/config/v2/ipsec/sa/1",
   249  			expectedSAIndex: "1",
   250  			expectedIsSAKey: true,
   251  		},
   252  		{
   253  			name:            "empty SA index",
   254  			key:             "vpp/config/v2/ipsec/sa/<invalid>",
   255  			expectedSAIndex: "<invalid>",
   256  			expectedIsSAKey: true,
   257  		},
   258  		{
   259  			name:            "invalid SPD index",
   260  			key:             "vpp/config/v2/ipsec/sa/sa1",
   261  			expectedSAIndex: "",
   262  			expectedIsSAKey: true,
   263  		},
   264  		{
   265  			name:            "not SA key",
   266  			key:             "vpp/config/v2/ipsec/tunnel/sa1",
   267  			expectedSAIndex: "",
   268  			expectedIsSAKey: false,
   269  		},
   270  	}
   271  	for _, test := range tests {
   272  		t.Run(test.name, func(t *testing.T) {
   273  			saName, isSAKey := models.Model(&ipsec.SecurityAssociation{}).ParseKey(test.key)
   274  			if isSAKey != test.expectedIsSAKey {
   275  				t.Errorf("expected isSAKey: %v\tgot: %v", test.expectedIsSAKey, isSAKey)
   276  			}
   277  			if saName != test.expectedSAIndex {
   278  				t.Errorf("expected saName: %s\tgot: %s", test.expectedSAIndex, saName)
   279  			}
   280  		})
   281  	}
   282  }*/