|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package java |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "encoding/hex" |
| 23 | + |
| 24 | + "github.com/hyperledger/fabric/core/util" |
| 25 | + |
| 26 | + "bytes" |
| 27 | + "os" |
| 28 | +) |
| 29 | + |
| 30 | +func TestHashDiffRemoteRepo(t *testing.T) { |
| 31 | + b := []byte("firstcontent") |
| 32 | + hash := util.ComputeCryptoHash(b) |
| 33 | + |
| 34 | + srcPath1, err := getCodeFromHTTP("https://github.com/hyperledger/fabric-test-resources/") |
| 35 | + if err != nil { |
| 36 | + t.Logf("Error getting code from remote repo %s", err) |
| 37 | + t.Fail() |
| 38 | + } |
| 39 | + srcPath2, err := getCodeFromHTTP("https://github.com/hyperledger/fabric-sdk-java") |
| 40 | + if err != nil { |
| 41 | + t.Logf("Error getting code from remote repo %s", err) |
| 42 | + t.Fail() |
| 43 | + } |
| 44 | + |
| 45 | + defer func() { |
| 46 | + os.RemoveAll(srcPath1) |
| 47 | + }() |
| 48 | + defer func() { |
| 49 | + os.RemoveAll(srcPath2) |
| 50 | + }() |
| 51 | + hash1, err := hashFilesInDir(srcPath1, srcPath1, hash, nil) |
| 52 | + if err != nil { |
| 53 | + t.Logf("Error getting code %s", err) |
| 54 | + t.Fail() |
| 55 | + } |
| 56 | + hash2, err := hashFilesInDir(srcPath2, srcPath2, hash, nil) |
| 57 | + if err != nil { |
| 58 | + t.Logf("Error getting code %s", err) |
| 59 | + t.Fail() |
| 60 | + } |
| 61 | + if bytes.Compare(hash1, hash2) == 0 { |
| 62 | + t.Logf("Hash should be different for 2 different remote repos") |
| 63 | + t.Fail() |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | +func TestHashSameRemoteRepo(t *testing.T) { |
| 68 | + b := []byte("firstcontent") |
| 69 | + hash := util.ComputeCryptoHash(b) |
| 70 | + |
| 71 | + srcPath1, err := getCodeFromHTTP("https://github.com/hyperledger/fabric-test-resources") |
| 72 | + if err != nil { |
| 73 | + t.Logf("Error getting code from remote repo %s", err) |
| 74 | + t.Fail() |
| 75 | + } |
| 76 | + srcPath2, err := getCodeFromHTTP("https://github.com/hyperledger/fabric-test-resources") |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + t.Logf("Error getting code from remote repo %s", err) |
| 80 | + t.Fail() |
| 81 | + } |
| 82 | + |
| 83 | + defer func() { |
| 84 | + os.RemoveAll(srcPath1) |
| 85 | + }() |
| 86 | + defer func() { |
| 87 | + os.RemoveAll(srcPath2) |
| 88 | + }() |
| 89 | + hash1, err := hashFilesInDir(srcPath1, srcPath1, hash, nil) |
| 90 | + if err != nil { |
| 91 | + t.Logf("Error getting code %s", err) |
| 92 | + t.Fail() |
| 93 | + } |
| 94 | + hash2, err := hashFilesInDir(srcPath2, srcPath2, hash, nil) |
| 95 | + if err != nil { |
| 96 | + t.Logf("Error getting code %s", err) |
| 97 | + t.Fail() |
| 98 | + } |
| 99 | + if bytes.Compare(hash1, hash2) != 0 { |
| 100 | + t.Logf("Hash should be same across multiple downloads") |
| 101 | + t.Fail() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestHashOverLocalDir(t *testing.T) { |
| 106 | + b := []byte("firstcontent") |
| 107 | + hash := util.ComputeCryptoHash(b) |
| 108 | + |
| 109 | + hash, err := hashFilesInDir(".", "../golang/hashtestfiles", hash, nil) |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + t.Fail() |
| 113 | + t.Logf("error : %s", err) |
| 114 | + } |
| 115 | + |
| 116 | + expectedHash := "7b3b2193bed2bd7c19300aa5d6d7f6bb4d61602e4978a78bc08028379cb5cf0ed877bd9db3e990230e8bf6c974edd765f3027f061fd8657d30fc858a676a6f4a" |
| 117 | + |
| 118 | + computedHash := hex.EncodeToString(hash[:]) |
| 119 | + |
| 120 | + if expectedHash != computedHash { |
| 121 | + t.Fail() |
| 122 | + t.Logf("Hash expected to be unchanged") |
| 123 | + } |
| 124 | +} |
0 commit comments