kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //Package adaptercore provides the utility for olt devices, flows and statistics |
| 18 | package adaptercore |
| 19 | |
| 20 | import ( |
| 21 | "math" |
| 22 | "reflect" |
| 23 | "testing" |
| 24 | |
| 25 | fu "github.com/opencord/voltha-go/rw_core/utils" |
| 26 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/go/voltha" |
| 28 | ) |
| 29 | |
| 30 | func TestMkUniPortNum(t *testing.T) { |
| 31 | type args struct { |
| 32 | intfID uint32 |
| 33 | onuID uint32 |
| 34 | uniID uint32 |
| 35 | } |
| 36 | tests := []struct { |
| 37 | name string |
| 38 | args args |
| 39 | want uint32 |
| 40 | }{ |
| 41 | // TODO: Add test cases. |
| 42 | {"MkUniPortNum-1", args{1, 1, 1}, ((1 * 2048) + (1 * 16) + 1)}, |
| 43 | {"MkUniPortNum-2", args{4, 5, 6}, ((4 * 2048) + (5 * 16) + 6)}, |
| 44 | // Negative test cases to cover the log.warn |
| 45 | {"MkUniPortNum-3", args{4, 130, 6}, ((4 * 2048) + (130 * 16) + 6)}, |
| 46 | } |
| 47 | for _, tt := range tests { |
| 48 | t.Run(tt.name, func(t *testing.T) { |
| 49 | if got := MkUniPortNum(tt.args.intfID, tt.args.onuID, tt.args.uniID); got != tt.want { |
| 50 | t.Errorf("MkUniPortNum() = %v, want %v", got, tt.want) |
| 51 | } else { |
| 52 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestOnuIDFromPortNum(t *testing.T) { |
| 59 | type args struct { |
| 60 | portNum uint32 |
| 61 | } |
| 62 | tests := []struct { |
| 63 | name string |
| 64 | args args |
| 65 | want uint32 |
| 66 | }{ |
| 67 | // TODO: Add test cases. |
| 68 | {"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 16) & 127)}, |
| 69 | {"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 16) & 127)}, |
| 70 | } |
| 71 | for _, tt := range tests { |
| 72 | t.Run(tt.name, func(t *testing.T) { |
| 73 | if got := OnuIDFromPortNum(tt.args.portNum); got != tt.want { |
| 74 | t.Errorf("OnuIDFromPortNum() = %v, want %v", got, tt.want) |
| 75 | } else { |
| 76 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestIntfIDFromUniPortNum(t *testing.T) { |
| 83 | type args struct { |
| 84 | portNum uint32 |
| 85 | } |
| 86 | tests := []struct { |
| 87 | name string |
| 88 | args args |
| 89 | want uint32 |
| 90 | }{ |
| 91 | // TODO: Add test cases. |
| 92 | {"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 2048) & 15)}, |
| 93 | // Negative Testcase |
| 94 | {"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 2048) & 15)}, |
| 95 | } |
| 96 | for _, tt := range tests { |
| 97 | t.Run(tt.name, func(t *testing.T) { |
| 98 | if got := IntfIDFromUniPortNum(tt.args.portNum); got != tt.want { |
| 99 | t.Errorf("IntfIDFromUniPortNum() = %v, want %v", got, tt.want) |
| 100 | } else { |
| 101 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestUniIDFromPortNum(t *testing.T) { |
| 108 | type args struct { |
| 109 | portNum uint32 |
| 110 | } |
| 111 | tests := []struct { |
| 112 | name string |
| 113 | args args |
| 114 | want uint32 |
| 115 | }{ |
| 116 | |
| 117 | // TODO: Add test cases. |
| 118 | {"UniIDFromPortNum-1", args{portNum: 8096}, (8096 & 15)}, |
| 119 | {"UniIDFromPortNum-2", args{portNum: 1024}, (1024 & 15)}, |
| 120 | } |
| 121 | for _, tt := range tests { |
| 122 | t.Run(tt.name, func(t *testing.T) { |
| 123 | if got := UniIDFromPortNum(tt.args.portNum); got != tt.want { |
| 124 | t.Errorf("UniIDFromPortNum() = %v, want %v", got, tt.want) |
| 125 | } else { |
| 126 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 127 | } |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestIntfIDToPortNo(t *testing.T) { |
| 133 | type args struct { |
| 134 | intfID uint32 |
| 135 | intfType voltha.Port_PortType |
| 136 | } |
| 137 | tests := []struct { |
| 138 | name string |
| 139 | args args |
| 140 | want uint32 |
| 141 | }{ |
| 142 | // TODO: Add test cases. |
| 143 | {"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 16)) + 120)}, |
| 144 | {"IntfIDToPortNo-2", args{intfID: 1024, intfType: voltha.Port_ETHERNET_UNI}, 0}, |
| 145 | {"IntfIDToPortNo-3", args{intfID: 456, intfType: voltha.Port_PON_OLT}, (uint32(2*math.Pow(2, 28)) + 456)}, |
| 146 | {"IntfIDToPortNo-4", args{intfID: 28, intfType: voltha.Port_PON_ONU}, 0}, |
| 147 | {"IntfIDToPortNo-5", args{intfID: 45, intfType: voltha.Port_UNKNOWN}, 0}, |
| 148 | {"IntfIDToPortNo-6", args{intfID: 45, intfType: voltha.Port_VENET_OLT}, 0}, |
| 149 | {"IntfIDToPortNo-7", args{intfID: 45, intfType: voltha.Port_VENET_ONU}, 0}, |
| 150 | } |
| 151 | for _, tt := range tests { |
| 152 | t.Run(tt.name, func(t *testing.T) { |
| 153 | if got := IntfIDToPortNo(tt.args.intfID, tt.args.intfType); got != tt.want { |
| 154 | t.Errorf("IntfIDToPortNo() = %v, want %v", got, tt.want) |
| 155 | } else { |
| 156 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func TestIntfIDFromNniPortNum(t *testing.T) { |
| 163 | type args struct { |
| 164 | portNum uint32 |
| 165 | } |
| 166 | |
| 167 | tests := []struct { |
| 168 | name string |
| 169 | args args |
| 170 | want uint32 |
| 171 | }{ |
| 172 | // TODO: Add test cases. |
| 173 | {"IntfIDFromNniPortNum-1", args{portNum: 8081}, 8081}, |
| 174 | {"IntfIDFromNniPortNum-2", args{portNum: 9090}, 9090}, |
| 175 | {"IntfIDFromNniPortNum-3", args{portNum: 0}, 0}, |
| 176 | {"IntfIDFromNniPortNum-3", args{portNum: 65535}, 65535}, |
| 177 | } |
| 178 | for _, tt := range tests { |
| 179 | t.Run(tt.name, func(t *testing.T) { |
| 180 | if got := IntfIDFromNniPortNum(tt.args.portNum); got != tt.want { |
| 181 | t.Errorf("IntfIDFromNniPortNum() = %v, want %v", got, tt.want) |
| 182 | } else { |
| 183 | t.Logf("Expected %v , Actual %v \n", tt.want, got) |
| 184 | } |
| 185 | }) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestIntfIDToPortTypeName(t *testing.T) { |
| 190 | type args struct { |
| 191 | intfID uint32 |
| 192 | } |
| 193 | var input uint32 |
| 194 | input = uint32(2*math.Pow(2, 28)) | 3 |
| 195 | tests := []struct { |
| 196 | name string |
| 197 | args args |
| 198 | want voltha.Port_PortType |
| 199 | }{ |
| 200 | // TODO: Add test cases. |
| 201 | {"IntfIDToPortTypeName-1", args{intfID: 65536}, voltha.Port_ETHERNET_NNI}, |
| 202 | {"IntfIDToPortTypeName-2", args{intfID: 1000}, voltha.Port_ETHERNET_UNI}, |
| 203 | {"IntfIDToPortTypeName-2", args{intfID: input}, voltha.Port_PON_OLT}, |
| 204 | } |
| 205 | for _, tt := range tests { |
| 206 | t.Run(tt.name, func(t *testing.T) { |
| 207 | if got := IntfIDToPortTypeName(tt.args.intfID); !reflect.DeepEqual(got, tt.want) { |
| 208 | t.Errorf("IntfIDToPortTypeName() = %v, want %v", got, tt.want) |
| 209 | } |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestExtractAccessFromFlow(t *testing.T) { |
| 215 | type args struct { |
| 216 | inPort uint32 |
| 217 | outPort uint32 |
| 218 | } |
| 219 | tests := []struct { |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 220 | name string |
| 221 | args args |
| 222 | port uint32 |
| 223 | IntfID uint32 |
| 224 | onuID uint32 |
| 225 | uniID uint32 |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 226 | }{ |
| 227 | // TODO: Add test cases. |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 228 | {"ExtractAccessFromFlow-1", args{inPort: 100, outPort: 65536}, 100, 0, 6, 4}, |
| 229 | {"ExtractAccessFromFlow-1", args{inPort: 65536, outPort: 10}, 10, 0, 0, 10}, |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 230 | } |
| 231 | for _, tt := range tests { |
| 232 | t.Run(tt.name, func(t *testing.T) { |
| 233 | got, got1, got2, got3 := ExtractAccessFromFlow(tt.args.inPort, tt.args.outPort) |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 234 | if got != tt.port { |
| 235 | t.Errorf("ExtractAccessFromFlow() got = %v, want %v", got, tt.port) |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 236 | } |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 237 | if got1 != tt.IntfID { |
| 238 | t.Errorf("ExtractAccessFromFlow() got1 = %v, want %v", got1, tt.IntfID) |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 239 | } |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 240 | if got2 != tt.onuID { |
| 241 | t.Errorf("ExtractAccessFromFlow() got2 = %v, want %v", got2, tt.onuID) |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 242 | } |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 243 | if got3 != tt.uniID { |
| 244 | t.Errorf("ExtractAccessFromFlow() got3 = %v, want %v", got3, tt.uniID) |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 245 | } |
| 246 | }) |
| 247 | } |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 248 | //t.Error() |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | func TestIsUpstream(t *testing.T) { |
| 252 | type args struct { |
| 253 | outPort uint32 |
| 254 | } |
| 255 | tests := []struct { |
| 256 | name string |
| 257 | args args |
| 258 | want bool |
| 259 | }{ |
| 260 | // TODO: Add test cases. |
| 261 | {"TestIsUpstream-1", args{outPort: 65533}, true}, |
| 262 | {"TestIsUpstream-2", args{outPort: 65536}, true}, |
| 263 | {"TestIsUpstream-3", args{outPort: 65537}, true}, |
| 264 | {"TestIsUpstream-4", args{outPort: 65538}, true}, |
| 265 | {"TestIsUpstream-5", args{outPort: 65539}, true}, |
| 266 | {"TestIsUpstream-6", args{outPort: 1000}, false}, |
| 267 | } |
| 268 | for _, tt := range tests { |
| 269 | t.Run(tt.name, func(t *testing.T) { |
| 270 | if got := IsUpstream(tt.args.outPort); got != tt.want { |
| 271 | t.Errorf("IsUpstream() = %v, want %v", got, tt.want) |
| 272 | } |
| 273 | }) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestIsControllerBoundFlow(t *testing.T) { |
| 278 | type args struct { |
| 279 | outPort uint32 |
| 280 | } |
| 281 | tests := []struct { |
| 282 | name string |
| 283 | args args |
| 284 | want bool |
| 285 | }{ |
| 286 | // TODO: Add test cases. |
| 287 | {"IsControllerBoundFlow-1", args{outPort: 65533}, true}, |
| 288 | {"IsControllerBoundFlow-2", args{outPort: 65536}, false}, |
| 289 | {"IsControllerBoundFlow-3", args{outPort: 65537}, false}, |
| 290 | {"IsControllerBoundFlow-4", args{outPort: 65538}, false}, |
| 291 | {"IsControllerBoundFlow-5", args{outPort: 65539}, false}, |
| 292 | {"IsControllerBoundFlow-6", args{outPort: 1000}, false}, |
| 293 | } |
| 294 | for _, tt := range tests { |
| 295 | t.Run(tt.name, func(t *testing.T) { |
| 296 | if got := IsControllerBoundFlow(tt.args.outPort); got != tt.want { |
| 297 | t.Errorf("IsControllerBoundFlow() = %v, want %v", got, tt.want) |
| 298 | } |
| 299 | }) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | func TestFlowExtractInfo(t *testing.T) { |
| 304 | fa := &fu.FlowArgs{ |
| 305 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 306 | fu.InPort(2), |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 307 | fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2)), |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 308 | fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 309 | fu.EthType(2048), |
| 310 | }, |
| 311 | |
| 312 | Actions: []*ofp.OfpAction{ |
| 313 | fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))), |
| 314 | fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)), |
| 315 | fu.Output(1), |
| 316 | }, |
| 317 | } |
| 318 | ofpstats := fu.MkFlowStat(fa) |
| 319 | type args struct { |
| 320 | flow *ofp.OfpFlowStats |
| 321 | flowDirection string |
| 322 | } |
| 323 | tests := []struct { |
| 324 | name string |
| 325 | args args |
| 326 | want uint32 |
| 327 | want1 uint32 |
| 328 | want2 uint32 |
| 329 | want3 uint32 |
| 330 | want4 uint32 |
| 331 | want5 uint32 |
| 332 | wantErr bool |
| 333 | }{ |
| 334 | // TODO: Add test cases. |
| 335 | {"FlowExtractInfo-1", args{flow: ofpstats, flowDirection: "upstream"}, 2, 0, 0, 2, 0, 0, false}, |
| 336 | |
| 337 | // Negative Testcases |
kdarapu | 820d54b | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 338 | {"FlowExtractInfo-2", args{flow: ofpstats, flowDirection: "downstream"}, 1, 0, 0, 1, 2, 2048, false}, |
kdarapu | 4cf5cc7 | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 339 | {"FlowExtractInfo-3", args{flow: nil, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true}, |
| 340 | {"FlowExtractInfo-4", args{flow: &ofp.OfpFlowStats{}, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true}, |
| 341 | } |
| 342 | for _, tt := range tests { |
| 343 | t.Run(tt.name, func(t *testing.T) { |
| 344 | got, got1, got2, got3, got4, got5, err := FlowExtractInfo(tt.args.flow, tt.args.flowDirection) |
| 345 | if (err != nil) != tt.wantErr { |
| 346 | t.Errorf("FlowExtractInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 347 | return |
| 348 | } |
| 349 | if got != tt.want { |
| 350 | t.Errorf("FlowExtractInfo() got = %v, want %v", got, tt.want) |
| 351 | return |
| 352 | } |
| 353 | if got1 != tt.want1 { |
| 354 | t.Errorf("FlowExtractInfo() got1 = %v, want %v", got1, tt.want1) |
| 355 | return |
| 356 | } |
| 357 | if got2 != tt.want2 { |
| 358 | t.Errorf("FlowExtractInfo() got2 = %v, want %v", got2, tt.want2) |
| 359 | return |
| 360 | } |
| 361 | if got3 != tt.want3 { |
| 362 | t.Errorf("FlowExtractInfo() got3 = %v, want %v", got3, tt.want3) |
| 363 | return |
| 364 | } |
| 365 | if got4 != tt.want4 { |
| 366 | t.Errorf("FlowExtractInfo() got4 = %v, want %v", got4, tt.want4) |
| 367 | return |
| 368 | } |
| 369 | if got5 != tt.want5 { |
| 370 | t.Errorf("FlowExtractInfo() got5 = %v, want %v", got5, tt.want5) |
| 371 | return |
| 372 | } |
| 373 | }) |
| 374 | } |
| 375 | } |