Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commons #20

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ Please refer to the [official installation guide](https://go.dev/doc/install).
Finally, enter the `go version` in the console to view the go version number.

### Step 3: Build a DeOSS

Clone deoss source code:
```
git clone https://github.com/CESSProject/DeOSS.git
```
Run unit test:
```
cd DeOSS/
go test -v ./...
```

Build a deoss:
```
go build -o deoss cmd/main.go
```

If all goes well, you will get a program called `deoss`.


Expand Down
83 changes: 83 additions & 0 deletions pkg/hashtree/hashtree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2022 CESS (Cumulus Encrypted Storage System) authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hashtree

import (
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewHashTree(t *testing.T) {

var err error
var content_one = "content_one"
var content_two = "content_two"
var content_three = "content_three"
var content_four = "content_four"
var content_one_hash = sha256.Sum256([]byte(content_one))
var content_two_hash = sha256.Sum256([]byte(content_two))
var content_three_hash = sha256.Sum256([]byte(content_three))
var content_four_hash = sha256.Sum256([]byte(content_four))

var content_five = make([]byte, 0)
var content_sex = make([]byte, 0)
content_five = append(content_five, content_one_hash[:]...)
content_five = append(content_five, content_two_hash[:]...)
content_sex = append(content_sex, content_three_hash[:]...)
content_sex = append(content_sex, content_four_hash[:]...)
hash_five := sha256.Sum256(content_five)
hash_sex := sha256.Sum256(content_sex)
var content_seven = make([]byte, 0)
content_seven = append(content_seven, hash_five[:]...)
content_seven = append(content_seven, hash_sex[:]...)
arrhashs := sha256.Sum256(content_seven)
var roothashs = make([]byte, 0)
for _, ele := range arrhashs {
roothashs = append(roothashs, ele)
}
var want_root_hash = hex.EncodeToString(roothashs)

basedir, err := os.Getwd()
assert.NoError(t, err)
file_content_one := filepath.Join(basedir, content_one)
file_content_two := filepath.Join(basedir, content_two)
file_content_three := filepath.Join(basedir, content_three)
file_content_four := filepath.Join(basedir, content_four)
err = os.WriteFile(file_content_one, []byte(content_one), os.ModePerm)
assert.NoError(t, err)
defer os.Remove(file_content_one)
err = os.WriteFile(file_content_two, []byte(content_two), os.ModePerm)
assert.NoError(t, err)
defer os.Remove(file_content_two)
err = os.WriteFile(file_content_three, []byte(content_three), os.ModePerm)
assert.NoError(t, err)
defer os.Remove(file_content_three)
err = os.WriteFile(file_content_four, []byte(content_four), os.ModePerm)
assert.NoError(t, err)
defer os.Remove(file_content_four)
var chunks = []string{file_content_one, file_content_two, file_content_three, file_content_four}

mtree, err := NewHashTree(chunks)
assert.NoError(t, err)
got_root_hash := hex.EncodeToString(mtree.MerkleRoot())
assert.Equal(t, want_root_hash, got_root_hash)
}