Go语言的安全与测试

安全

一、安全相关的基础概念

1、数据加密

2、数字签名

3、数字证书

4、PKI体系

二、通信安全

1、哈希函数

代码如下:

package main

import(
	"crypto/md5"
	"crypto/sha1"
	"fmt"
)

func main(){
	TestString:="Hi, Golang!"
	Md5Inst:=md5.New()
	Md5Inst.Write([]byte(TestString))
	Result:=Md5Inst.Sum([]byte(""))
	fmt.Printf("%x",Result)
	fmt.Printf("\n")
	sha1Inst:=sha1.New()
	sha1Inst.Write([]byte(TestString))
	Result=sha1Inst.Sum([]byte(""))
	fmt.Printf("%x\n",Result)
}

运行结果:

e80c196f0227f671c4804af43bd535f7
83d59fbccf927fbe331312b70ebe2933f63a397a

代码如下:

package main

import(
	"crypto/md5"
	"crypto/sha1"
	"fmt"
	"io"
	"os"
)

func main(){
	fileName:="test.go"
	infile,inerr:=os.Open(fileName)
	if inerr==nil{
		md5h:=md5.New()
		io.Copy(md5h,infile)
		fmt.Printf("文件“%s”的md5值为:%x",fileName, md5h.Sum([]byte("")))
		fmt.Printf("\n")
		sha1h:=sha1.New()
		io.Copy(sha1h,infile)
		fmt.Printf("文件“%s”的sha1值为:%x",fileName, sha1h.Sum([]byte("")))
		fmt.Printf("\n")
	}else{
		fmt.Println(inerr)
		os.Exit(1)
	}
}

运行结果:

文件“test.go”的md5值为:4b3f99748df3c8a83fef3fb30b30232d
文件“test.go”的sha1值为:da39a3ee5e6b4b0d3255bfef95601890afd80709

2、加密通信流程

3、支持HTTPS的Web服务器

代码如下:

package main

import(
	"fmt"
	"net/http"
)

const SERVER_PORT=8080
const SERVER_DOMAIN="localhost"
const RESPONSE_TEMPLATE="Hello World"

func rootHandler(w http.ResponseWriter, req *http.Request){
	w.Header().Set("Content-Type","text/html")
	w.Header().Set("Content-Length",fmt.Sprint(len(RESPONSE_TEMPLATE)))
	w.Write([]byte(RESPONSE_TEMPLATE))
}

func main(){
	http.HandleFunc(fmt.Sprintf("%s:%d/",SERVER_DOMAIN,SERVER_PORT),rootHandler)
	http.ListenAndServeTLS(fmt.Sprintf(":%d",SERVER_PORT),"some.crt","some.key",nil)
}

运行结果如下:空白

书上主:如果一切准备好,运行该服务器程序后,在浏览器中访问localhost:8000可以看到一句“Hello World”。

4、支持HTTPS的文件服务器

代码如下:

package main

import(
	"net/http"
)


func main(){
	h:=http.FileServer(http.Dir("."))
	http.ListenAndServeTLS(":8001","rui.crt","rui.key",h)
}

运行结果如下:空白

测试

单元测试(Unit Testing)又称为模块测试。

一、单元测试

testing是Go语言的一个package,它提供自动化测试功能。通过go test命令能够自动执行如下形式的函数:

func TestXxx(*testing.T)

要编写一个新的测试模块,需要创建一个名称以_test.go结尾的文件,该文件包含TestXxx函数,最后将该文件放在与被测试的包相同的包目录中。

运行go help test和go help testflag可以了解更加详细的信息。

1、第一个单元测试

代码如下:basic.go

package main

import "fmt"

func main() {
	fmt.Println(Age(-7))
}

//Age测试
func Age(n int) int {
	if n > 0 {
		return n
	}
	n = 0
	return n
}

代码如下:basic_test.go

package main

import "testing"

//TestFib测试
func TestFib(t *testing.T) {
	var (
		input    = -100
		expected = 0
	)
	actual := Age(input)
	if actual != expected {
		t.Errorf("Fib(%d)=%d,预期为 %d", input, actual, expected)
	}
}

执行测试命令:

go test .

运行结果:

ok      test/test       0.693s

2、表组测试

代码如下:

package main

import "fmt"

func main() {
	fmt.Println(isPrime(25))
}

//大于1的自然数中,除了1和它本身以外不再有其它因数的数称为质数
func isPrime(value int) bool {
	if value<=3{
		return value >=2
	}
	if value%2==0||value%3==0{
		return false
	}
	for i:=5;i*i<=value;i+=6{
		if value%i==0||value%(i+2)==0{
			return false
		}
	}
	return true
}

代码如下:

package main

import "testing"

//TestPrime测试
func TestPrime(t *testing.T) {
	var primeTests = []struct {
		input    int
		expected bool
	}{
		{1, false},
		{2, true},
		{3, true},
		{4, false},
		{5, true},
		{6, false},
		{7, false},
	}
	for _, tt := range primeTests {
		actual := isPrime(tt.input)
		if actual != tt.expected {
			t.Errorf("%d %v %v", tt.input, actual, tt.expected)
		}
	}
}

执行测试命令:

go test .

运行结果:

--- FAIL: TestPrime (0.00s)
    basic_test.go:22: 7 true false
FAIL
FAIL    test/test       1.375s
FAIL

3、模拟测试

4、测试覆盖率

go test -v -coverprofile=c.out

go tool cover -html=c.out -o=tag.html

二、基准测试

基准测试(benchmarking)

go test -bench=. -run=none

go test -bench=. -benchtime=3s -run=none

1、性能对比

go test -bench=. -run=none

go test -bench=. -benchmem -run=none

2、pprof

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值