]> Cypherpunks.ru repositories - nncp.git/commitdiff
Use bytes.Equal() instead of bytes.Compare()==0
authorSergey Matveev <stargrave@stargrave.org>
Thu, 23 Mar 2023 09:59:40 +0000 (12:59 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 23 Mar 2023 10:00:43 +0000 (13:00 +0300)
src/cfg.go
src/check.go
src/cmd/nncp-cfgenc/main.go
src/cmd/nncp-reass/main.go
src/mth_test.go
src/pkt.go
src/pkt_test.go
src/sp.go
src/toss_test.go
src/tx_test.go

index 52bd3f3fd875c16b6ccefce7dd73cfa5e7298695..a57277e88be3462aee9c4dcc4ab1d1282d07cd62 100644 (file)
@@ -468,7 +468,7 @@ func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
 
 func CfgParse(data []byte) (*CfgJSON, error) {
        var err error
-       if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 {
+       if bytes.Equal(data[:8], MagicNNCPBv3.B[:]) {
                os.Stderr.WriteString("Passphrase:")
                password, err := term.ReadPassword(0)
                if err != nil {
@@ -479,9 +479,9 @@ func CfgParse(data []byte) (*CfgJSON, error) {
                if err != nil {
                        return nil, err
                }
-       } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 {
+       } else if bytes.Equal(data[:8], MagicNNCPBv2.B[:]) {
                log.Fatalln(MagicNNCPBv2.TooOld())
-       } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 {
+       } else if bytes.Equal(data[:8], MagicNNCPBv1.B[:]) {
                log.Fatalln(MagicNNCPBv1.TooOld())
        }
        var cfgGeneral map[string]interface{}
index 6a1b18ab846b19fe87cb5ec588fbf5aa96089b62..a4e0d06d31ea7cc51048acb4ec8b0d7a5a0dbd9f 100644 (file)
@@ -44,7 +44,7 @@ func Check(
        ); err != nil {
                return false, err
        }
-       return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
+       return bytes.Equal(hsh.Sum(nil), checksum), nil
 }
 
 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
@@ -113,7 +113,7 @@ func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth MTH) (int
                ); err != nil {
                        return 0, err
                }
-               if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 {
+               if bytes.Equal(mth.Sum(nil), hshValue[:]) {
                        gut = true
                }
        }
index ef4a4308954860908ed6493c940d98b3e6debce9..5fc83e2a519ee6ccece1e17dd861d1c061c895e2 100644 (file)
@@ -115,7 +115,7 @@ func main() {
                log.Fatalln(err)
        }
        os.Stderr.WriteString("\n")
-       if bytes.Compare(password1, password2) != 0 {
+       if !bytes.Equal(password1, password2) {
                log.Fatalln(errors.New("Passphrases do not match"))
        }
        eblob, err := nncp.NewEBlob(*sOpt, *tOpt, *pOpt, password1, data)
index fc54a5b27718ef0693835d2cdf5da4b366f50fae..3961449cfa78df112f815ef77f91c24fd189260d 100644 (file)
@@ -164,7 +164,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo
                        log.Fatalln(err)
                }
                fd.Close()
-               if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 {
+               if !bytes.Equal(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) {
                        ctx.LogE(
                                "reass-chunk",
                                nncp.LEs{{K: "Path", V: path}, {K: "Chunk", V: chunkNum}},
index 6d6b5c3bdfae407c364cb35aa0111a4f7e010a8b..fcdff1752c2476abb369d1ec9106f931469dd22d 100644 (file)
@@ -49,13 +49,13 @@ func TestMTHSeqSymmetric(t *testing.T) {
                if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
                        panic(err)
                }
-               if bytes.Compare(hsh0, mth.Sum(nil)) != 0 {
+               if !bytes.Equal(hsh0, mth.Sum(nil)) {
                        return false
                }
 
                mth = MTHSeqNew(0, 0)
                mth.Write(data)
-               if bytes.Compare(hsh0, mth.Sum(nil)) != 0 {
+               if !bytes.Equal(hsh0, mth.Sum(nil)) {
                        return false
                }
 
@@ -65,7 +65,7 @@ func TestMTHSeqSymmetric(t *testing.T) {
                        panic(err)
                }
                hsh00 := mth.Sum(nil)
-               if bytes.Compare(hsh0, hsh00) == 0 {
+               if bytes.Equal(hsh0, hsh00) {
                        return false
                }
 
@@ -76,17 +76,13 @@ func TestMTHSeqSymmetric(t *testing.T) {
                if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
                        panic(err)
                }
-               if bytes.Compare(hsh00, mth.Sum(nil)) != 0 {
+               if !bytes.Equal(hsh00, mth.Sum(nil)) {
                        return false
                }
 
                mth = MTHSeqNew(0, 0)
                mth.Write(data)
-               if bytes.Compare(hsh00, mth.Sum(nil)) != 0 {
-                       return false
-               }
-
-               return true
+               return bytes.Equal(hsh00, mth.Sum(nil))
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
@@ -110,7 +106,7 @@ func TestMTHSeqAndFatEqual(t *testing.T) {
                if _, err := io.Copy(seq, bytes.NewReader(data)); err != nil {
                        panic(err)
                }
-               return bytes.Compare(hshFat, seq.Sum(nil)) == 0
+               return bytes.Equal(hshFat, seq.Sum(nil))
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
@@ -128,7 +124,7 @@ func TestMTHNull(t *testing.T) {
        if _, err := seq.Write(nil); err != nil {
                t.Error(err)
        }
-       if bytes.Compare(hshFat, seq.Sum(nil)) != 0 {
+       if !bytes.Equal(hshFat, seq.Sum(nil)) {
                t.FailNow()
        }
 }
index f6ab749ac9e936c824dc157ecd7e6c2151cce350..38e4659a0030f0d153e889d822ea83745abc6bf7 100644 (file)
@@ -514,7 +514,7 @@ FullRead:
                if err != nil {
                        panic(err)
                }
-               if bytes.Compare(ct[:n], pt[:n]) != 0 {
+               if !bytes.Equal(ct[:n], pt[:n]) {
                        err = errors.New("wrong pad value")
                        return
                }
index 5077c57f21b147ff5b20d69e950b2959afe9e6a2..d90877dafac36ec4ad38d227e2b9ff13d2515947 100644 (file)
@@ -146,7 +146,7 @@ func TestPktEncRead(t *testing.T) {
                }
                var pktBuf bytes.Buffer
                xdr.Marshal(&pktBuf, &pkt)
-               return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data...)) == 0
+               return bytes.Equal(pt.Bytes(), append(pktBuf.Bytes(), data...))
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
index 1077563b33056fad9a7ef561d8c6d28e6ffa66e9..29fbf9b5f2945d70c725f888babbb4b023d2eb6f 100644 (file)
--- a/src/sp.go
+++ b/src/sp.go
@@ -1477,7 +1477,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                        if hasherAndOffset != nil {
                                delete(state.fileHashers, filePath)
                                if hasherAndOffset.mth.PreaddSize() == 0 {
-                                       if bytes.Compare(hasherAndOffset.mth.Sum(nil), file.Hash[:]) != 0 {
+                                       if !bytes.Equal(hasherAndOffset.mth.Sum(nil), file.Hash[:]) {
                                                state.Ctx.LogE(
                                                        "sp-file-bad-checksum", lesp,
                                                        errors.New("checksum mismatch"),
index e0f682b8b5fb5c3c68659ee63eea7501cb5a5c52..b17fc7ba6ec8e2f4acc2f81965dc6051472fc4bd 100644 (file)
@@ -150,7 +150,7 @@ func TestTossExec(t *testing.T) {
                        )
                        expected = append(expected, []byte("BODY\n")...)
                }
-               return bytes.Compare(mbox, expected) == 0
+               return bytes.Equal(mbox, expected)
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
@@ -240,7 +240,7 @@ func TestTossFile(t *testing.T) {
                        if err != nil {
                                panic(err)
                        }
-                       if bytes.Compare(data, fileData) != 0 {
+                       if !bytes.Equal(data, fileData) {
                                return false
                        }
                }
@@ -423,8 +423,7 @@ func TestTossFreq(t *testing.T) {
                        if pkt.Nice != replyNice {
                                return false
                        }
-                       dst := string(pkt.Path[:int(pkt.PathLen)])
-                       if bytes.Compare(buf.Bytes(), files[dst]) != 0 {
+                       if !bytes.Equal(buf.Bytes(), files[string(pkt.Path[:int(pkt.PathLen)])]) {
                                return false
                        }
                }
@@ -514,7 +513,7 @@ func TestTossTrns(t *testing.T) {
                                panic(err)
                        }
                        for k, data := range datum {
-                               if bytes.Compare(dataRead, data) == 0 {
+                               if bytes.Equal(dataRead, data) {
                                        delete(datum, k)
                                }
                        }
index 9c061ee0fe8c8628590b031736c2874a49427a46..6c0aa78569b8adb5f3471590bf0dadb203a73371 100644 (file)
@@ -148,14 +148,14 @@ func TestTx(t *testing.T) {
                                if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
                                        return false
                                }
-                               if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
+                               if !bytes.Equal(bufR.Bytes(), []byte(data)) {
                                        return false
                                }
                        } else {
                                if pkt.Type != PktTypeTrns {
                                        return false
                                }
-                               if bytes.Compare(pkt.Path[:MTHSize], vias[i+1][:]) != 0 {
+                               if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) {
                                        return false
                                }
                        }