Roll versions after J-Relase (master branch)
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typevalidation_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package securityapi
22
23 import (
24         "testing"
25
26         "github.com/stretchr/testify/assert"
27         "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
28 )
29
30 func TestValidateClientIdNotPresent(t *testing.T) {
31         accessTokenUnderTest := AccessTokenReq{}
32         valid, err := accessTokenUnderTest.Validate()
33
34         assert.Equal(t, false, valid)
35         assert.Equal(t, AccessTokenErrErrorInvalidRequest, err.Error)
36         assert.Equal(t, "Invalid request", *err.ErrorDescription)
37 }
38
39 func TestValidateGrantType(t *testing.T) {
40         accessTokenUnderTest := AccessTokenReq{
41                 ClientId:  "clientId",
42                 GrantType: AccessTokenReqGrantType(""),
43         }
44         valid, err := accessTokenUnderTest.Validate()
45
46         assert.Equal(t, false, valid)
47         assert.Equal(t, AccessTokenErrErrorInvalidGrant, err.Error)
48         assert.Equal(t, "Invalid value for grant_type", *err.ErrorDescription)
49
50         accessTokenUnderTest.GrantType = AccessTokenReqGrantType("client_credentials")
51         valid, err = accessTokenUnderTest.Validate()
52         assert.Equal(t, true, valid)
53 }
54
55 func TestValidateScopeNotValid(t *testing.T) {
56         scope := "scope#aefId:path"
57         accessTokenUnderTest := AccessTokenReq{
58                 ClientId:  "clientId",
59                 GrantType: ("client_credentials"),
60                 Scope:     &scope,
61         }
62         valid, err := accessTokenUnderTest.Validate()
63
64         assert.Equal(t, false, valid)
65         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
66         assert.Equal(t, "Scope should start with 3gpp", *err.ErrorDescription)
67
68         scope = "3gpp#aefId:path"
69         accessTokenUnderTest.Scope = &scope
70         valid, err = accessTokenUnderTest.Validate()
71         assert.Equal(t, true, valid)
72 }
73
74 func TestValidateScopeMalformed(t *testing.T) {
75         scope := "3gpp"
76         accessTokenUnderTest := AccessTokenReq{
77                 ClientId:  "clientId",
78                 GrantType: ("client_credentials"),
79                 Scope:     &scope,
80         }
81         valid, err := accessTokenUnderTest.Validate()
82
83         assert.Equal(t, false, valid)
84         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
85         assert.Equal(t, "Malformed scope", *err.ErrorDescription)
86
87         scope = "3gpp#aefId"
88         accessTokenUnderTest.Scope = &scope
89         valid, err = accessTokenUnderTest.Validate()
90         assert.Equal(t, false, valid)
91         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
92         assert.Equal(t, "Malformed scope", *err.ErrorDescription)
93
94         scope = "3gpp#aefId:path"
95         accessTokenUnderTest.Scope = &scope
96         valid, err = accessTokenUnderTest.Validate()
97         assert.Equal(t, true, valid)
98 }
99
100 func TestValidateServiceSecurity(t *testing.T) {
101         serviceSecurityUnderTest := ServiceSecurity{}
102
103         err := serviceSecurityUnderTest.Validate()
104         assert.NotNil(t, err)
105         assert.Contains(t, err.Error(), "missing")
106         assert.Contains(t, err.Error(), "notificationDestination")
107
108         serviceSecurityUnderTest.NotificationDestination = "invalid dest"
109         err = serviceSecurityUnderTest.Validate()
110         if assert.Error(t, err) {
111                 assert.Contains(t, err.Error(), "invalid")
112                 assert.Contains(t, err.Error(), "notificationDestination")
113         }
114
115         serviceSecurityUnderTest.NotificationDestination = "http://golang.cafe/"
116         err = serviceSecurityUnderTest.Validate()
117         assert.NotNil(t, err)
118         assert.Contains(t, err.Error(), "missing")
119         assert.Contains(t, err.Error(), "SecurityInfo")
120
121         serviceSecurityUnderTest.SecurityInfo = []SecurityInformation{
122                 {
123                         PrefSecurityMethods: []publishserviceapi.SecurityMethod{
124                                 publishserviceapi.SecurityMethodOAUTH,
125                         },
126                 },
127         }
128         err = serviceSecurityUnderTest.Validate()
129         assert.Nil(t, err)
130 }
131
132 func TestValidatePrefSecurityMethodsNotPresent(t *testing.T) {
133         securityInfoUnderTest := SecurityInformation{}
134         err := securityInfoUnderTest.Validate()
135
136         assert.NotNil(t, err)
137         assert.Contains(t, err.Error(), "missing")
138         assert.Contains(t, err.Error(), "PrefSecurityMethods")
139
140         securityInfoUnderTest.PrefSecurityMethods = []publishserviceapi.SecurityMethod{
141                 publishserviceapi.SecurityMethodOAUTH,
142         }
143         err = securityInfoUnderTest.Validate()
144         assert.Nil(t, err)
145 }