Skip to content

Commit

Permalink
Fix the zero value of ICECredentialType
Browse files Browse the repository at this point in the history
Ensure that the zero value of ICECredentialType is password.
  • Loading branch information
jech authored and Sean-Der committed May 11, 2022
1 parent 37a145a commit b457994
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
20 changes: 8 additions & 12 deletions icecredentialtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ICECredentialType int
const (
// ICECredentialTypePassword describes username and password based
// credentials as described in https://tools.ietf.org/html/rfc5389.
ICECredentialTypePassword ICECredentialType = iota + 1
ICECredentialTypePassword ICECredentialType = iota

// ICECredentialTypeOauth describes token based credential as described
// in https://tools.ietf.org/html/rfc7635.
Expand All @@ -25,21 +25,19 @@ const (
iceCredentialTypeOauthStr = "oauth"
)

func newICECredentialType(raw string) ICECredentialType {
func newICECredentialType(raw string) (ICECredentialType, error) {
switch raw {
case iceCredentialTypePasswordStr:
return ICECredentialTypePassword
return ICECredentialTypePassword, nil
case iceCredentialTypeOauthStr:
return ICECredentialTypeOauth
return ICECredentialTypeOauth, nil
default:
return ICECredentialType(Unknown)
return ICECredentialTypePassword, errInvalidICECredentialTypeString
}
}

func (t ICECredentialType) String() string {
switch t {
case Unknown:
return ""
case ICECredentialTypePassword:
return iceCredentialTypePasswordStr
case ICECredentialTypeOauth:
Expand All @@ -52,15 +50,13 @@ func (t ICECredentialType) String() string {
// UnmarshalJSON parses the JSON-encoded data and stores the result
func (t *ICECredentialType) UnmarshalJSON(b []byte) error {
var val string
var tmp ICECredentialType
if err := json.Unmarshal(b, &val); err != nil {
return err
}

tmp = newICECredentialType(val)

if (tmp == ICECredentialType(Unknown)) && (val != "") {
return fmt.Errorf("%w: (%s)", errInvalidICECredentialTypeString, val)
tmp, err := newICECredentialType(val)
if err != nil {
return fmt.Errorf("%w: (%s)", err, val)
}

*t = tmp
Expand Down
10 changes: 6 additions & 4 deletions icecredentialtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ func TestNewICECredentialType(t *testing.T) {
}

for i, testCase := range testCases {
tpe, err := newICECredentialType(testCase.credentialTypeString)
assert.NoError(t, err)
assert.Equal(t,
testCase.expectedCredentialType,
newICECredentialType(testCase.credentialTypeString),
testCase.expectedCredentialType, tpe,
"testCase: %d %v", i, testCase,
)
}
Expand Down Expand Up @@ -53,9 +54,10 @@ func TestICECredentialType_new(t *testing.T) {
}

for i, testCase := range testCases {
tpe, err := newICECredentialType(testCase.expectedString)
assert.NoError(t, err)
assert.Equal(t,
newICECredentialType(testCase.expectedString),
testCase.credentialType,
tpe, testCase.credentialType,
"testCase: %d %v", i, testCase,
)
}
Expand Down
16 changes: 8 additions & 8 deletions iceserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,19 @@ func (s *ICEServer) iceserverUnmarshalFields(m map[string]interface{}) error {
}
if val, ok := m["credentialType"]; ok {
ct, ok := val.(string)
if !ok || (newICECredentialType(ct) == ICECredentialType(Unknown)) {
if !ok {
return errInvalidICEServer
}
s.CredentialType = newICECredentialType(ct)
tpe, err := newICECredentialType(ct)
if err != nil {
return err
}
s.CredentialType = tpe
} else {
s.CredentialType = ICECredentialType(Unknown)
s.CredentialType = ICECredentialTypePassword
}
if val, ok := m["credential"]; ok {
switch s.CredentialType {
case ICECredentialType(Unknown):
s.Credential = val
case ICECredentialTypePassword:
s.Credential = val
case ICECredentialTypeOauth:
Expand Down Expand Up @@ -172,8 +174,6 @@ func (s ICEServer) MarshalJSON() ([]byte, error) {
if s.Credential != nil {
m["credential"] = s.Credential
}
if s.CredentialType != ICECredentialType(Unknown) {
m["credentialType"] = s.CredentialType
}
m["credentialType"] = s.CredentialType
return json.Marshal(m)
}
9 changes: 9 additions & 0 deletions iceserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ func TestICEServer_validate(t *testing.T) {
}
})
}

func TestICEServerZeroValue(t *testing.T) {
server := ICEServer{
URLs: []string{"turn:galene.org:1195"},
Username: "galene",
Credential: "secret",
}
assert.Equal(t, server.CredentialType, ICECredentialTypePassword)
}
6 changes: 5 additions & 1 deletion peerconnection_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,16 @@ func valueToICECredential(iceCredentialValue js.Value) interface{} {
}

func valueToICEServer(iceServerValue js.Value) ICEServer {
tpe, err := newICECredentialType(valueToStringOrZero(iceServerValue.Get("credentialType")))
if err != nil {
tpe = ICECredentialTypePassword
}
s := ICEServer{
URLs: valueToStrings(iceServerValue.Get("urls")), // required
Username: valueToStringOrZero(iceServerValue.Get("username")),
// Note: Credential and CredentialType are not currently supported.
Credential: valueToICECredential(iceServerValue.Get("credential")),
CredentialType: newICECredentialType(valueToStringOrZero(iceServerValue.Get("credentialType"))),
CredentialType: tpe,
}

// default to password
Expand Down

0 comments on commit b457994

Please sign in to comment.