From b53b7f49502b938e8cdc5d4da0e4ca3a2799cabd Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 14 Jan 2020 15:34:06 -0800 Subject: [PATCH] . --- Makefile | 2 +- go/Makefile | 2 -- go/cmd/zerotier/cli/selftest.go | 5 ++-- go/native/CoreTests.cpp | 37 ++++++++++++++------------ go/pkg/zerotier/node.go | 46 ++++++++++++++------------------- go/pkg/zerotier/selftest.go | 39 ++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 48 deletions(-) delete mode 100644 go/Makefile create mode 100644 go/pkg/zerotier/selftest.go diff --git a/Makefile b/Makefile index 9c82f78ef..58e5faba2 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ debug: mkdir -p ${BUILDDIR} && cd ${BUILDDIR} && cmake .. -DCMAKE_BUILD_TYPE=Debug && $(MAKE) clean: - rm -rf ${BUILDDIR} + rm -rf ${BUILDDIR} cmake-build-* distclean: rm -rf ${BUILDDIR} diff --git a/go/Makefile b/go/Makefile deleted file mode 100644 index e5b5ed79e..000000000 --- a/go/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -all: - go build -trimpath -ldflags -s -o ../build/zerotier cmd/zerotier/zerotier.go diff --git a/go/cmd/zerotier/cli/selftest.go b/go/cmd/zerotier/cli/selftest.go index 2f880d1d4..3cf236d3e 100644 --- a/go/cmd/zerotier/cli/selftest.go +++ b/go/cmd/zerotier/cli/selftest.go @@ -21,9 +21,8 @@ import ( ) func SelfTest() { - fmt.Print("Running ZeroTier core tests...\n\n") - if !zerotier.CSelfTest() { - fmt.Println("FAILED: at least one ZeroTier core test reported failure.") + if !zerotier.SelfTest() { + fmt.Println("FAILED: at least one ZeroTier self-test reported failure.") os.Exit(1) } os.Exit(0) diff --git a/go/native/CoreTests.cpp b/go/native/CoreTests.cpp index 98398856a..1acc448f2 100644 --- a/go/native/CoreTests.cpp +++ b/go/native/CoreTests.cpp @@ -230,23 +230,25 @@ extern "C" int ZT_TestCrypto() return -1; } } - Salsa20 s20(s20TV0Key,s20TV0Iv); - memset(buf1,0,sizeof(buf1)); - memset(buf2,0,sizeof(buf2)); - s20.crypt20(buf1,buf2,64); - if (memcmp(buf2,s20TV0Ks,64)) { - std::cout << "FAIL (test vector 0)" ZT_EOL_S; - return -1; + { + Salsa20 s20(s20TV0Key,s20TV0Iv); + memset(buf1,0,sizeof(buf1)); + memset(buf2,0,sizeof(buf2)); + s20.crypt20(buf1,buf2,64); + if (memcmp(buf2,s20TV0Ks,64)) { + std::cout << "FAIL (test vector 0)" ZT_EOL_S; + return -1; + } + s20.init(s2012TV0Key,s2012TV0Iv); + memset(buf1,0,sizeof(buf1)); + memset(buf2,0,sizeof(buf2)); + s20.crypt12(buf1,buf2,64); + if (memcmp(buf2,s2012TV0Ks,64)) { + std::cout << "FAIL (test vector 1)" ZT_EOL_S; + return -1; + } + std::cout << "PASS" ZT_EOL_S; } - s20.init(s2012TV0Key,s2012TV0Iv); - memset(buf1,0,sizeof(buf1)); - memset(buf2,0,sizeof(buf2)); - s20.crypt12(buf1,buf2,64); - if (memcmp(buf2,s2012TV0Ks,64)) { - std::cout << "FAIL (test vector 1)" ZT_EOL_S; - return -1; - } - std::cout << "PASS" ZT_EOL_S; #ifdef ZT_SALSA20_SSE std::cout << "[crypto] Salsa20 SSE: ENABLED" ZT_EOL_S; @@ -493,6 +495,7 @@ extern "C" int ZT_TestCrypto() std::cout << " ECC P-384 ECDH: " << (1000.0 / ((double)(end - start) / 1000.0)) << " agreements/second" ZT_EOL_S; } + std::cout.flush(); return 0; } @@ -598,6 +601,7 @@ extern "C" int ZT_TestIdentity() } } + std::cout.flush(); return 0; } @@ -743,5 +747,6 @@ extern "C" int ZT_TestOther() std::cout << "PASS (junk value to prevent optimization-out of test: " << foo << ")" ZT_EOL_S; } + std::cout.flush(); return 0; } diff --git a/go/pkg/zerotier/node.go b/go/pkg/zerotier/node.go index 84a66a0b7..392c321d4 100644 --- a/go/pkg/zerotier/node.go +++ b/go/pkg/zerotier/node.go @@ -86,28 +86,14 @@ var ( nodesByUserPtrLock sync.RWMutex ) -// CSelfTest runs self-test functions from the core, sending results to stdout and returning true on success. -func CSelfTest() bool { - if C.ZT_TestOther() != 0 { - return false - } - fmt.Println() - if C.ZT_TestCrypto() != 0 { - return false - } - fmt.Println() - if C.ZT_TestIdentity() != 0 { - return false - } - return true -} - // Node is an instance of the ZeroTier core node and related C++ I/O code type Node struct { networks map[NetworkID]*Network networksByMAC map[MAC]*Network // locked by networksLock interfaceAddresses map[string]net.IP // physical external IPs on the machine basePath string + peersPath string + networksPath string localConfigPath string localConfig LocalConfig localConfigLock sync.RWMutex @@ -126,22 +112,30 @@ type Node struct { } // NewNode creates and initializes a new instance of the ZeroTier node service -func NewNode(basePath string) (*Node, error) { - var err error - - _ = os.MkdirAll(basePath, 0755) - if _, err := os.Stat(basePath); err != nil { - return nil, err - } - - n := new(Node) - +func NewNode(basePath string) (n *Node, err error) { + n = new(Node) n.networks = make(map[NetworkID]*Network) n.networksByMAC = make(map[MAC]*Network) n.interfaceAddresses = make(map[string]net.IP) + _ = os.MkdirAll(basePath, 0755) + if _, err = os.Stat(basePath); err != nil { + return + } n.basePath = basePath + n.peersPath = path.Join(basePath, "peers.d") + _ = os.MkdirAll(n.peersPath, 0700) + _ = acl.Chmod(n.peersPath, 0700) + if _, err = os.Stat(n.peersPath); err != nil { + return + } + n.networksPath = path.Join(basePath, "networks.d") + _ = os.MkdirAll(n.networksPath, 0755) + if _, err = os.Stat(n.networksPath); err != nil { + return + } n.localConfigPath = path.Join(basePath, "local.conf") + err = n.localConfig.Read(n.localConfigPath, true) if err != nil { return nil, err diff --git a/go/pkg/zerotier/selftest.go b/go/pkg/zerotier/selftest.go new file mode 100644 index 000000000..9cab2b4d4 --- /dev/null +++ b/go/pkg/zerotier/selftest.go @@ -0,0 +1,39 @@ +/* + * Copyright (c)2019 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2023-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +package zerotier + +//#include "../../native/GoGlue.h" +import "C" + +import "fmt" + +// SelfTest runs a series of tests on the ZeroTier core and the Go service code, returning true on success. +// Results are sent to stdout. +func SelfTest() bool { + fmt.Print("Running ZeroTier core tests...\n\n") + + if C.ZT_TestOther() != 0 { + return false + } + fmt.Println() + if C.ZT_TestCrypto() != 0 { + return false + } + fmt.Println() + if C.ZT_TestIdentity() != 0 { + return false + } + + return true +}