-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JSONReGetGroup for JSON unmarshalling of group scalars and elem…
…ents (#3) Signed-off-by: bytemare <[email protected]>
- Loading branch information
Showing
14 changed files
with
264 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree or at | ||
// https://spdx.org/licenses/MIT.html | ||
|
||
// Package encoding provides serde utilities. | ||
package encoding | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/bytemare/ecc" | ||
"github.com/bytemare/ecc/internal" | ||
) | ||
|
||
func jsonReGetField(key, s, catch string) (string, error) { | ||
r := fmt.Sprintf(`%q:%s`, key, catch) | ||
re := regexp.MustCompile(r) | ||
matches := re.FindStringSubmatch(s) | ||
|
||
if len(matches) != 2 { | ||
return "", internal.ErrDecodingInvalidJSONEncoding | ||
} | ||
|
||
return matches[1], nil | ||
} | ||
|
||
// JSONReGetGroup attempts to find the group JSON encoding in s. The optional key argument overrides the default key the | ||
// regex will use to look for the group. | ||
func JSONReGetGroup(s string, key ...string) (ecc.Group, error) { | ||
reKey := "group" | ||
if len(key) != 0 && key[0] != "" { | ||
reKey = key[0] | ||
} | ||
|
||
f, err := jsonReGetField(reKey, s, `(\w+)`) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
i, err := strconv.Atoi(f) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to read Group: %w", err) | ||
} | ||
|
||
if i < 0 || i > 63 { | ||
return 0, internal.ErrInvalidGroup | ||
} | ||
|
||
c := ecc.Group(i) | ||
if !c.Available() { | ||
return 0, internal.ErrInvalidGroup | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree or at | ||
// https://spdx.org/licenses/MIT.html | ||
|
||
package ecc_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bytemare/ecc" | ||
eccEncoding "github.com/bytemare/ecc/encoding" | ||
"github.com/bytemare/ecc/internal" | ||
) | ||
|
||
func replaceStringInBytes(data []byte, old, new string) []byte { | ||
s := string(data) | ||
s = strings.Replace(s, old, new, 1) | ||
|
||
return []byte(s) | ||
} | ||
|
||
type jsonTesterBaddie struct { | ||
key, value, expectedError string | ||
} | ||
|
||
func testJSONBaddie(in any, baddie jsonTesterBaddie) error { | ||
data, err := json.Marshal(in) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data = replaceStringInBytes(data, baddie.key, baddie.value) | ||
|
||
_, err = eccEncoding.JSONReGetGroup(string(data)) | ||
|
||
if len(baddie.expectedError) != 0 { // we're expecting an error | ||
if err == nil || | ||
!strings.HasPrefix(err.Error(), baddie.expectedError) { | ||
return fmt.Errorf("expected error %q, got %q", baddie.expectedError, err) | ||
} | ||
} else { | ||
if err != nil { | ||
return fmt.Errorf("unexpected error %q", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func jsonTester(badJSONErr string, in any) error { | ||
// JSON: bad json | ||
baddie := jsonTesterBaddie{ | ||
key: "\"group\"", | ||
value: "bad", | ||
expectedError: "invalid character 'b' looking for beginning of object key string", | ||
} | ||
|
||
if err := testJSONBaddie(in, baddie); err != nil { | ||
// return err | ||
} | ||
|
||
// UnmarshallJSON: bad group | ||
baddie = jsonTesterBaddie{ | ||
key: "\"group\"", | ||
value: "\"group\":2, \"oldGroup\"", | ||
expectedError: internal.ErrInvalidGroup.Error(), | ||
} | ||
|
||
if err := testJSONBaddie(in, baddie); err != nil { | ||
return err | ||
} | ||
|
||
// UnmarshallJSON: bad ciphersuite | ||
baddie = jsonTesterBaddie{ | ||
key: "\"group\"", | ||
value: "\"group\":70, \"oldGroup\"", | ||
expectedError: internal.ErrInvalidGroup.Error(), | ||
} | ||
|
||
if err := testJSONBaddie(in, baddie); err != nil { | ||
return err | ||
} | ||
|
||
// UnmarshallJSON: bad ciphersuite | ||
baddie = jsonTesterBaddie{ | ||
key: "\"group\"", | ||
value: "\"group\":-1, \"oldGroup\"", | ||
expectedError: badJSONErr, | ||
} | ||
|
||
if err := testJSONBaddie(in, baddie); err != nil { | ||
return err | ||
} | ||
|
||
// UnmarshallJSON: bad ciphersuite | ||
overflow := "9223372036854775808" // MaxInt64 + 1 | ||
baddie = jsonTesterBaddie{ | ||
key: "\"group\"", | ||
value: "\"group\":" + overflow + ", \"oldGroup\"", | ||
expectedError: "failed to read Group: strconv.Atoi: parsing \"9223372036854775808\": value out of range", | ||
} | ||
|
||
if err := testJSONBaddie(in, baddie); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestJSONReGetGroup_BadString(t *testing.T) { | ||
testAllGroups(t, func(group *testGroup) { | ||
test := struct { | ||
Group ecc.Group `json:"group"` | ||
Int int `json:"int"` | ||
}{ | ||
Group: group.group, | ||
Int: 1, | ||
} | ||
|
||
// JSON: bad json | ||
errInvalidJSON := "invalid JSON encoding" | ||
if err := jsonTester(errInvalidJSON, test); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters