Commit e1f57f2d authored by Zhu Yong's avatar Zhu Yong
Browse files

Add SSL connection support, added test cases for InstanceErase,

SecureErase, SetErasePin, SetLockPin
parent 6cc6a0f2
Loading
Loading
Loading
Loading
+37 −1
Original line number Diff line number Diff line
@@ -13,10 +13,14 @@ var (
var option = ClientOptions{
	Host: "10.29.24.55",
	Port: 8123,
	//Port:   8443, // For SSL connection
	User: 1,
	Hmac: []byte("asdfasdf")}
	Hmac: []byte("asdfasdf"),
	//UseSSL: true,
}

func TestMain(m *testing.M) {
	SetLogLevel(LogLevelDebug)
	blockConn, _ = NewBlockConnection(option)
	if blockConn != nil {
		code := m.Run()
@@ -210,3 +214,35 @@ func TestBlockSetClusterVersion(t *testing.T) {
	}
	t.Log(status.String())
}

func TestBlockInstantErase(t *testing.T) {
	t.Skip("Danger: Skip InstanceErase Test")
	status, err := blockConn.InstantErase([]byte("PIN"))
	if err != nil || status.Code != OK {
		t.Fatal("Blocking InstantErase Failure: ", err, status.String())
	}
}

func TestBlockSecureErase(t *testing.T) {
	t.Skip("Danger: Skip SecureErase Test")
	status, err := blockConn.SecureErase([]byte(""))
	if err != nil || status.Code != OK {
		t.Fatal("Blocking SecureErase Failure: ", err, status.String())
	}
}

func TestBlockSetErasePin(t *testing.T) {
	t.Skip("Danger: Skip SetErasePin Test")
	status, err := blockConn.SetErasePin([]byte(""), []byte("PIN"))
	if err != nil || status.Code != OK {
		t.Fatal("Blocking SetErasePin Failure: ", err, status.String())
	}
}

func TestBlockSetLockPin(t *testing.T) {
	t.Skip("Danger: Skip SetLockPin Test")
	status, err := blockConn.SetLockPin([]byte(""), []byte("PIN"))
	if err != nil || status.Code != OK {
		t.Fatal("Blocking SetLockPin Failure: ", err, status.String())
	}
}
+5 −4
Original line number Diff line number Diff line
@@ -52,10 +52,11 @@ func SetLogOutput(out io.Writer) {

// ClientOptions
type ClientOptions struct {
	Host string
	Port int
	User int64
	Host   string // Kinetic device IP address
	Port   int    // Network port to connect, if UseSSL is true, this port should be the TlsPort
	User   int64  // User Id
	Hmac   []byte
	UseSSL bool // Use SSL connection, or plain connection
}

// message type
+15 −3
Original line number Diff line number Diff line
package kinetic

import (
	"crypto/tls"
	"encoding/binary"
	"errors"
	"fmt"
@@ -55,10 +56,19 @@ type networkService struct {
}

func newNetworkService(op ClientOptions) (*networkService, error) {
	var conn net.Conn
	var err error
	target := fmt.Sprintf("%s:%d", op.Host, op.Port)
	conn, err := net.DialTimeout("tcp", target, networkTimeout)
	if op.UseSSL {
		// TODO: Need to enable verify certification later
		config := tls.Config{InsecureSkipVerify: true}
		conn, err = tls.Dial("tcp", target, &config)
	} else {
		conn, err = net.DialTimeout("tcp", target, networkTimeout)
	}

	if err != nil {
		klog.Panic("Can't establish connection to ", op.Host)
		klog.Panic("Can't establish connection to ", op.Host, err)
		return nil, err
	}

@@ -83,13 +93,15 @@ func newNetworkService(op ClientOptions) (*networkService, error) {
		return nil, err
	}

	klog.Debugf("Connected to %s", op.Host)
	klog.Debugf("Connected to %s:%d", op.Host, op.Port)
	klog.Debugf("\tVendor: %s", ns.device.Configuration.Vendor)
	klog.Debugf("\tModel: %s", ns.device.Configuration.Model)
	klog.Debugf("\tWorldWideName: %s", ns.device.Configuration.WorldWideName)
	klog.Debugf("\tSerial Number: %s", ns.device.Configuration.SerialNumber)
	klog.Debugf("\tFirmware Version: %s", ns.device.Configuration.Version)
	klog.Debugf("\tKinetic Protocol Version: %s", ns.device.Configuration.ProtocolVersion)
	klog.Debugf("\tPort: %d", ns.device.Configuration.Port)
	klog.Debugf("\tTlsPort: %d", ns.device.Configuration.TlsPort)

	return ns, nil
}
+2 −2
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ import (
)

func TestUploadAppletFile(t *testing.T) {
	// file not exist, expected to fail
	t.Skip("Skip UploadAppletFile Test")
	file := "not/exist/applet/javapplet.jar"
	keys, err := UploadAppletFile(blockConn, file, "test-applet")
	if err != nil || len(keys) <= 0 {
@@ -14,7 +14,7 @@ func TestUploadAppletFile(t *testing.T) {
}

func TestUpdateFirmware(t *testing.T) {
	// file not exist, expected to fail
	t.Skip("Danger: Skip UpdateFirmware Test")
	file := "not/exist/firmare/unknown-version.slod"
	err := UpdateFirmware(blockConn, file)
	if err != nil {