Skip to content

Commit

Permalink
attempt to fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Mar 6, 2024
1 parent cf6320e commit 417bbd5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 28 deletions.
9 changes: 8 additions & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all tidy generate lint vet test coverage pushdoc
.PHONY: all tidy build install lint vet test coverage

# Default "make" target to check locally that everything is ok, BEFORE pushing remotely
all: lint vet test build
Expand Down Expand Up @@ -26,6 +26,13 @@ build: tidy
# Building executable
@go build ./smc/smccli
@go build ./blockchain/chaincli
@go build ./registration

install: tidy
# Building executable
@go install ./smc/smccli
@go install ./blockchain/chaincli
@go install ./registration

coverage: tidy
# Test and generate a coverage output usable by sonarcloud
Expand Down
File renamed without changes
4 changes: 2 additions & 2 deletions server/test/admin/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func BlockchainGetDocIDs(adminPubkey kyber.Point) []registry.RegistrationID {
}

// BlockchainGetDocument polls the blockchain to get the encrypted document
func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) smc.Secret {
func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) (smc.Secret, []byte) {
encodedPk, err := pk.MarshalBinary()
if err != nil {
log.Fatal().Msgf("error: %v", err)
Expand All @@ -82,5 +82,5 @@ func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) smc.Secret
log.Error().Msgf("error decoding response: %v", err)
}

return secret
return secret, nil
}
4 changes: 2 additions & 2 deletions server/test/admin/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const registrationServer = "localhost:3001"

func RegistrationAdminGetDocument(docid registry.RegistrationID) registry.RegistrationData {
resp, err := http.Get("localhost:3000/admin/document?id=" + string(docid.ID))
resp, err := http.Get(registrationServer + "/admin/document?id=" + string(docid.ID))
if err != nil {
log.Fatal().Msgf("error: %v", err)
}
Expand All @@ -31,7 +31,7 @@ func RegistrationAdminGetDocument(docid registry.RegistrationID) registry.Regist
}

func RegistrationAdminUpdateDocument(docid registry.RegistrationID) error {
resp, err := http.Get("localhost:3000/admin/document?id=" + string(docid.ID))
resp, err := http.Get(registrationServer + "/admin/document?id=" + string(docid.ID))
if err != nil {
log.Fatal().Msgf("error: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion server/test/admin/smc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const smcServer = "localhost:3002"

// SmcReencryptSecret re-encrypts the secret with the new public key
// and returns a xhatenc value that can be used to reveal the secret
func SmcReencryptSecret(pk kyber.Point, secret string) (kyber.Point, error) {
// first argument is supposed to be the proof
func SmcReencryptSecret(_ []byte, pk kyber.Point, secret string) (kyber.Point, error) {
resp, err := http.Post(smcServer+"/reencrypt", "application/json",
bytes.NewBuffer([]byte(`{"pubk": "`+encodePublickey(pk)+`", "encrypted": "`+secret+`"}`)))
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions server/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ func main() {
// ---------------------------------------------------------

// create a document and save it encrypted into the database
doc := createDocument("John Doe", "12AB456789", 0, "passport.jpg")
log.Info().Msg("CREATE document for test purpose")
doc := createDocument("John Doe", "12AB456789", 0, "./passport.jpg")
log.Info().Msg("SUCCESS! created new document")

// add the document to the registry
docid := user.RegistrationAdd(doc, symKey)
log.Info().Msg("ADD document to the registry")
docid, err := user.RegistrationAdd(doc, symKey)
if err != nil {
log.Fatal().Msgf("error: %v", err)
}
log.Info().Msgf("SUCCESS! added document ID=%v", docid)

// get the SMC pub key
log.Info().Msg("FETCH SMC key")
smcKey := user.SmcGetKey()
log.Info().Msgf("SUCCESS! got SMC key: %v", smcKey)

// add secret = symKey to the blockchain
log.Info().Msg("ADD secret to the blockchain")
secret := user.BlockchainEncryptAndAddSecret(smcKey, symKey, docid)
log.Info().Msgf("SUCCESS! added secret=%v with ID=%v to blockchain", secret, docid)

Expand All @@ -45,10 +52,10 @@ func main() {
docIDs := admin.BlockchainGetDocIDs(pk)

for _, id := range docIDs {
secret := admin.BlockchainGetSecret(id, pk)
secret, proof := admin.BlockchainGetSecret(id, pk)
log.Info().Msgf("secret: %v", secret)

xhatenc, err := admin.SmcReencryptSecret(pk, secret.Data)
xhatenc, err := admin.SmcReencryptSecret(proof, pk, secret.Data)
if err != nil {
log.Fatal().Msgf("error: %v", err)
}
Expand All @@ -65,6 +72,9 @@ func main() {
log.Fatal().Msg("symmetric key mismatch")
}

// encrypt binary array with symmetric key
// and save it to the registry

// TODO: get the encrypted document from the registry
// TODO: decrypt the document - optional
// TODO: update the document status to registered
Expand Down
87 changes: 69 additions & 18 deletions server/test/user/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,86 @@ import (
"context"
"encoding/binary"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"

"github.com/rs/zerolog/log"
"go.dedis.ch/hbt/server/registration/registry"
"go.dedis.ch/hbt/server/test/key"
)

const registrationServer = "localhost:3000"
const registrationServer = "http://localhost:3000"

// RegistrationAdd adds a new registration to the registry
func RegistrationAdd(data registry.RegistrationData, symKey []byte) registry.RegistrationID {
// Encrypt the data
encrypted, err := encryptRegistrationData(data, symKey)
func RegistrationAdd(data registry.RegistrationData, symKey []byte) (
registry.RegistrationID,
error,
) {
var b bytes.Buffer
w := multipart.NewWriter(&b)

fw, err := w.CreateFormField("pubkey")
if err != nil {
log.Fatal().Msgf("error: %v", err)
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw, bytes.NewReader(symKey)); err != nil {
return registry.RegistrationID{}, err
}

// Add the encrypted document to the registry
resp, err := http.PostForm(registrationServer+"/document",
url.Values{
"name": {string(encrypted.Name)},
"passport": {string(encrypted.Passport)},
"role": {string(encrypted.Role)},
"picture": {string(encrypted.Picture)},
"registered": {string(encrypted.Registered)},
})
fw, err = w.CreateFormField("name")
if err != nil {
log.Fatal().Msgf("error: %v", err)
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw, bytes.NewReader([]byte(data.Name))); err != nil {
return registry.RegistrationID{}, err
}

fw, err = w.CreateFormField("passport")
if err != nil {
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw, bytes.NewReader([]byte(data.Passport))); err != nil {
return registry.RegistrationID{}, err
}

fw, err = w.CreateFormField("role")
if err != nil {
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw,
bytes.NewReader([]byte(strconv.FormatUint(data.Role, 10)))); err != nil {
return registry.RegistrationID{}, err
}

fw, err = w.CreateFormField("registered")
if err != nil {
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw,
bytes.NewReader([]byte(strconv.FormatBool(data.Registered)))); err != nil {
return registry.RegistrationID{}, err
}

fw, err = w.CreateFormField("picture")
if err != nil {
return registry.RegistrationID{}, err
}
if _, err = io.Copy(fw, bytes.NewReader(data.Picture)); err != nil {
return registry.RegistrationID{}, err
}

defer w.Close()

req, err := http.NewRequest(http.MethodPost, registrationServer+"/document", &b)
if err != nil {
return registry.RegistrationID{}, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return registry.RegistrationID{}, err
}

defer resp.Body.Close()
Expand All @@ -42,10 +93,10 @@ func RegistrationAdd(data registry.RegistrationData, symKey []byte) registry.Reg
var docid registry.RegistrationID
err = json.NewDecoder(resp.Body).Decode(&docid)
if err != nil {
log.Error().Msgf("error decoding response: %v", err)
return registry.RegistrationID{}, err
}

return docid
return docid, err
}

// RegistrationGet polls the data to see if registered
Expand Down

0 comments on commit 417bbd5

Please sign in to comment.