Пример #1
0
func TestGenerated(ot *testing.T) {
	psk := kmgCrypto.Get32PskFromString("abc")
	closer := ListenAndServe_Demo(":34895", &Demo{}, psk)
	defer closer()
	client := NewClient_Demo("http://127.0.0.1:34895/f", psk)
	info, err := client.PostScoreInt("LbId", 1)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(info, "LbId")

	info, err = client.PostScoreInt("LbId", 2)
	kmgTest.Equal(info, "")
	kmgTest.Ok(err != nil, err)
	kmgTest.Equal(err.Error(), "Score!=1")

	info, err = client.DemoFunc8(DemoRequest{}, &DemoRequest{}, 1)
	kmgTest.Equal(info, "info1")

	info, err = client.DemoFunc8(DemoRequest{}, &DemoRequest{}, 2)
	kmgTest.Equal(info, "info")

	inT := kmgTime.MustParseAutoInDefault("2001-01-01 01:01:01")
	outT, err := client.DemoTime(inT)
	kmgTest.Equal(err, nil)
	kmgTest.Ok(outT.Equal(kmgTime.MustParseAutoInDefault("2001-01-01T02:01:01.001000001+08:00")), outT)

	ip, err := client.DemoClientIp()
	kmgTest.Equal(err, nil)
	kmgTest.Equal(ip, "127.0.0.1")
}
Пример #2
0
func TestScanCallback(ot *testing.T) {
	MustFlushDbV2()
	MustInsert("test_1", "abc")
	MustInsert("test_2", "abc")
	MustInsert("testno_3", "abc")

	scanSize = 2
	outKey := []string{}
	err := ScanCallback("*", func(key string) error {
		outKey = append(outKey, key)
		return nil
	})
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(outKey), 3)
	kmgTest.Ok(kmgStrings.IsInSlice(outKey, "test_1"))
	kmgTest.Ok(kmgStrings.IsInSlice(outKey, "test_2"))
	kmgTest.Ok(kmgStrings.IsInSlice(outKey, "testno_3"))

	scanSize = 10000
	outKey = []string{}
	err = ScanCallback("test_*", func(key string) error {
		outKey = append(outKey, key)
		return nil
	})
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(outKey), 2)
	kmgTest.Ok(kmgStrings.IsInSlice(outKey, "test_1"))
	kmgTest.Ok(kmgStrings.IsInSlice(outKey, "test_2"))

	sList, err := ScanWithOutputLimit("test_*", 1)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(sList), 1)

	//benchmarkScanCallback()
}
Пример #3
0
func TestKmgCipherV2(ot *testing.T) {
	//加密数据,可以正确解密测试
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		ob := EncryptV2([]byte("1"), origin)
		ret, err := DecryptV2([]byte("1"), ob)
		kmgTest.Equal(err, nil)
		kmgTest.Equal(ret, origin)
	}
	//任意数据传入解密不会挂掉,并且会报错
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		_, err := Decrypt([]byte("1"), origin)
		kmgTest.Ok(err != nil)
	}
	origin := []byte("1234567890123456712345678901234567")
	//多次加密得到不同的结果 (随机化iv)
	kmgTest.Ok(!bytes.Equal(EncryptV2([]byte("1"), origin), EncryptV2([]byte("1"), origin)))
	//修改任何一个字节都会报错 (hash)

	ob := EncryptV2([]byte("1"), origin)
	for i := 0; i < len(ob); i++ {
		newOb := make([]byte, len(ob))
		newOb[i] = -newOb[i]
		_, err := DecryptV2([]byte("1"), newOb)
		kmgTest.Ok(err != nil)
	}
}
Пример #4
0
func TestPtrType(ot *testing.T) {
	var data **string
	data = new(*string)
	m, err := NewContext(data)
	kmgTest.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr", "ptr"}, "")
	kmgTest.Equal(err, nil)
	kmgTest.Ok(data != nil)
	kmgTest.Ok(*data != nil)
	kmgTest.Equal(**data, "")
}
Пример #5
0
func TestTun(ot *testing.T) {
	fmt.Println(1)
	tun, err := NewTun("")
	fmt.Println(2)
	if os.IsPermission(err) {
		ot.Skip("you need root permission to run this test.")
		return
	}
	kmgTest.Equal(err, nil)
	defer tun.Close()
	kmgTest.Equal(tun.GetDeviceType(), DeviceTypeTun)
	fmt.Println(3)

	err = SetP2PIpAndUp(tun, "10.209.34.1", "10.209.34.2")
	kmgTest.Equal(err, nil)

	err = SetMtu(tun, 1420)
	kmgTest.Equal(err, nil)
	fmt.Println(4)
	cmd := kmgCmd.CmdString("ping 10.209.34.2").GetExecCmd()
	err = cmd.Start()
	kmgTest.Equal(err, nil)
	defer cmd.Process.Kill()

	buf := make([]byte, 4096)
	n, err := tun.Read(buf)
	kmgTest.Equal(err, nil)
	kmgTest.Ok(n > 0)
	/*
		tun2, err := NewTun("")
		t.Equal(err, nil)
		defer tun2.Close()
	*/
}
Пример #6
0
func TestMustTlsTestListen(ot *testing.T) {
	listener := kmgCrypto.MustTlsTestListen(":0")
	defer listener.Close()
	listenAddr := kmgNet.MustGetLocalAddrFromListener(listener)
	waitAcceptChan := make(chan int)
	go func() {
		defer func() { waitAcceptChan <- 1 }()
		conn, err := listener.Accept()
		kmgTest.Equal(err, nil)
		defer conn.Close()
		//此处开始检测两条连接是否连在了一起
		result, err := ioutil.ReadAll(conn)
		kmgTest.Equal(err, nil)
		kmgTest.Ok(bytes.Equal(result, []byte(`hello world`)))
	}()
	conn, err := tls.Dial("tcp", listenAddr, &tls.Config{
		InsecureSkipVerify: true,
	})
	kmgTest.Equal(err, nil)
	defer conn.Close()
	_, err = conn.Write([]byte(`hello world`))
	kmgTest.Equal(err, nil)
	conn.Close()
	<-waitAcceptChan
}
Пример #7
0
func TestCreateCert(ot *testing.T) {
	config, err := CreateTlsConfig()
	kmgTest.Equal(err, nil)
	kmgTest.Ok(config != nil)

	_ = tls.NewListener(&mockListener{}, config)
}
Пример #8
0
func TestAuthPap(ot *testing.T) {
	//AccessRequest
	inBytes := []byte{0x1, 0xef, 0x0, 0x8e, 0x94, 0xb, 0x18, 0xaf, 0xa, 0xb6, 0x12, 0xf5, 0x24, 0x4, 0x94, 0xbe, 0x18, 0xbc, 0x7, 0x4d,
		0x1, 0x4, 0x72, 0x48, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0, 0x10, 0x57,
		0xf, 0x69, 0x6f, 0x73, 0x5f, 0x78, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x70, 0x73, 0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e,
		0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31,
		0x2e, 0x31, 0x2e, 0x37, 0x30, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77,
		0x61, 0x6e, 0x2, 0x12, 0x8d, 0x7, 0xc2, 0xc0, 0xa4, 0x2, 0x2c, 0xed, 0x8e, 0x69, 0x5b, 0x9e, 0x25, 0x77, 0xe5, 0xd, 0x50,
		0x12, 0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5, 0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}
	server := server{
		handler: Handler{
			Auth: func(username string) (password string, exist bool) {
				if username != "rH" {
					panic(`username!="rH"`)
				}
				return "O6", true
			},
		},
	}
	inPac, err := DecodeRequestPacket([]byte("sEcReT"), inBytes)
	kmgTest.Equal(inPac.GetPassword(), "O6")
	kmgTest.Ok(err == nil)
	outPac := server.PacketHandler(inPac)
	kmgTest.Equal(outPac.Code, CodeAccessAccept)
}
Пример #9
0
func TestUnmarshalTypeNotMatch(t *testing.T) {
	data := `t1:
     k1: v1`
	out := map[string][]map[string]string{}
	err := Unmarshal([]byte(data), &out)
	kmgTest.Ok(err != nil)
}
Пример #10
0
func TestAuthMschapV2Step1(ot *testing.T) {
	//step 1
	in1 := []byte{ /* Packet 302 */
		0x01, 0x8f, 0x00, 0xb2, 0x5b, 0xb5, 0xce, 0xbf,
		0x70, 0x72, 0xd9, 0xac, 0xd8, 0x4b, 0xe0, 0x66,
		0x5b, 0xa6, 0xc6, 0x74, 0x01, 0x12, 0x79, 0x64,
		0x76, 0x62, 0x49, 0x77, 0x30, 0x63, 0x41, 0x49,
		0x34, 0x37, 0x45, 0x4d, 0x51, 0x57, 0x3d, 0x06,
		0x00, 0x00, 0x00, 0x05, 0x06, 0x06, 0x00, 0x00,
		0x00, 0x02, 0x05, 0x06, 0x00, 0x00, 0x00, 0x07,
		0x57, 0x0f, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b,
		0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x04,
		0x06, 0x78, 0x19, 0xe5, 0xd6, 0x1e, 0x16, 0x31,
		0x32, 0x30, 0x2e, 0x32, 0x35, 0x2e, 0x32, 0x32,
		0x39, 0x2e, 0x32, 0x31, 0x34, 0x5b, 0x34, 0x35,
		0x30, 0x30, 0x5d, 0x1f, 0x17, 0x31, 0x37, 0x35,
		0x2e, 0x31, 0x35, 0x32, 0x2e, 0x31, 0x31, 0x37,
		0x2e, 0x32, 0x30, 0x34, 0x5b, 0x34, 0x35, 0x30,
		0x30, 0x5d, 0x4f, 0x08, 0x02, 0x01, 0x00, 0x06,
		0x03, 0x1a, 0x20, 0x0c, 0x73, 0x74, 0x72, 0x6f,
		0x6e, 0x67, 0x53, 0x77, 0x61, 0x6e, 0x18, 0x12,
		0xb6, 0x64, 0xc6, 0x5c, 0xb6, 0x65, 0xc2, 0x69,
		0x0a, 0x8b, 0x5c, 0xbf, 0xb8, 0xfe, 0x43, 0x79,
		0x50, 0x12, 0x0c, 0x69, 0xf1, 0xef, 0x1a, 0xdb,
		0x42, 0x9c, 0xcb, 0xa0, 0x30, 0xea, 0x0b, 0x31,
		0x92, 0xef}
	server := server{
		handler: Handler{
			Auth: func(username string) (password string, exist bool) {
				if username != "ydvbIw0cAI47EMQW" {
					panic(`username!="ydvbIw0cAI47EMQW"`)
				}
				return "FogWi6Iz8oOkF1If", true
			},
		},
		mschapMap: map[string]mschapStatus{},
	}
	inPac, err := DecodeRequestPacket([]byte("sEcReT"), in1)
	kmgTest.Ok(err == nil, err)
	outPac := server.PacketHandler(inPac)
	kmgTest.Ok(len(outPac.GetEAPMessage().(*eap.MSCHAPV2Packet).MSCHAPV2.(*MSCHAPV2.ChallengePacket).Challenge) > 0)
	kmgTest.Equal(outPac.Code, CodeAccessChallenge)
}
Пример #11
0
func TestUnmarshalWithFalseSetterIgnoresValue(ot *testing.T) {
	setterResult[2] = false
	setterResult[4] = false
	defer func() {
		delete(setterResult, 2)
		delete(setterResult, 4)
	}()

	m := map[string]*typeWithSetter{}
	data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
	err := Unmarshal([]byte(data), m)
	kmgTest.Equal(err, nil)
	kmgTest.Ok(m["abc"] != nil)
	kmgTest.Equal(m["def"], nil)
	kmgTest.Ok(m["ghi"] != nil)
	kmgTest.Equal(m["jkl"], nil)
	kmgTest.Equal(m["abc"].value, 1)
	kmgTest.Equal(m["ghi"].value, 3)
}
Пример #12
0
func TestPossibilityWeight(t *testing.T) {
	r := NewInt64SeedKmgRand(0)
	rander := NewPossibilityWeightRander([]float64{1e-20})
	for i := 0; i < 100; i++ {
		kmgTest.Equal(rander.ChoiceOne(r), 0)
	}
	rander = NewPossibilityWeightRander([]float64{1, 2, 3, 4})
	for i := 0; i < 100; i++ {
		ret := rander.ChoiceOne(r)
		kmgTest.Ok(ret >= 0)
		kmgTest.Ok(ret <= 3)
	}
	rander = NewPossibilityWeightRander([]float64{1, 0, 3, 0, 1})
	for i := 0; i < 100; i++ {
		ret := rander.ChoiceOne(r)
		kmgTest.Ok(ret >= 0)
		kmgTest.Ok(ret <= 4)
	}
}
Пример #13
0
// 过滤 HTML的内容
func TestHTMLPurifier(t *testing.T) {
	input := []string{
		`<a onblur="alert(secret)" href="http://www.google.com">Google</a>`,
		`<a href="http://www.google.com/" target="_blank"><img src="https://ssl.gstatic.com/accounts/ui/logo_2x.png"/></a>`,
	}
	for _, content := range input {
		output := kmgHTMLPurifier.HTMLPurifier(content)
		kmgTest.Ok(!strings.Contains(output, "alert"))
	}
}
Пример #14
0
func TestStructType(ot *testing.T) {
	data := &struct {
		A string
	}{}
	m, err := NewContext(data)
	kmgTest.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr", "A"}, "123")
	kmgTest.Equal(err, nil)
	kmgTest.Ok(data != nil)
	kmgTest.Equal(data.A, "123")
}
Пример #15
0
func Test1(ot *testing.T) {
	//加密数据,可以正确解密测试
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		ob, err := Encrypt([]byte("1"), origin)
		kmgTest.Equal(err, nil)
		ret, err := Decrypt([]byte("1"), ob)
		kmgTest.Equal(err, nil)
		kmgTest.Equal(ret, origin)

		sob, err := EncryptString("1", origin)
		kmgTest.Equal(err, nil)
		sret, err := DecryptString("1", sob)
		kmgTest.Equal(err, nil)
		kmgTest.Equal(sret, origin)
	}
	//任意数据传入解密不会挂掉,并且会报错
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		_, err := Decrypt([]byte("1"), origin)
		kmgTest.Ok(err != nil)
	}
}
Пример #16
0
func TestGetCurrentDeviceAddrFromIPAddr(ot *testing.T) {
	addrs, err := getCurrentDeviceAddrFromIPAddr([]byte(`1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:00:05:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.20.8.1/32 scope global eth0
       valid_lft forever preferred_lft forever
307: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1496 qdisc pfifo_fast state UNKNOWN group default qlen 3
    link/ppp
    inet 172.20.0.1 peer 172.20.0.2/32 scope global ppp0
       valid_lft forever preferred_lft forever
`))
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(addrs), 4)
	kmgTest.Ok(addrs[0].IP.Equal(net.IPv4(127, 0, 0, 1)))
	//fmt.Println(addrs[0].IPNet.Mask.Size())
	one, _ := addrs[0].IPNet.Mask.Size()
	kmgTest.Equal(one, 8)
	kmgTest.Equal(addrs[0].DevString, "lo")

	kmgTest.Ok(addrs[1].IP.Equal(net.IPv4(192, 168, 1, 2)))
	kmgTest.Ok(addrs[1].IPNet.IP.Equal(net.IPv4(192, 168, 1, 0)))
	one, _ = addrs[1].IPNet.Mask.Size()
	kmgTest.Equal(one, 24)
	kmgTest.Equal(addrs[1].DevString, "eth0")

	kmgTest.Ok(addrs[2].IP.Equal(net.IPv4(172, 20, 8, 1)))
	one, _ = addrs[2].IPNet.Mask.Size()
	kmgTest.Equal(one, 32)
	kmgTest.Equal(addrs[2].DevString, "eth0")

	kmgTest.Ok(addrs[3].IP.Equal(net.IPv4(172, 20, 0, 1)))
	kmgTest.Equal(addrs[3].DevString, "ppp0")
}
Пример #17
0
func TestOverseaPay(ot *testing.T) {
	trade := OverseaTrade{
		PartnerId:    "20881234567890123",
		SecurityCode: "56Tae2ZROl2DSw",
	}
	url := trade.Pay(&OverseaTradePayRequest{
		Subject:    "多项测试",
		Body:       "test哈哈only",
		OutTradeNo: "1433229365",
		Currency:   "JPY",
		TotalFee:   10,
	})
	kmgTest.Ok(strings.Contains(url, "8d927a97f15a2f40a31040267a1d9afe"))
	//	kmgTest.Equal(query["sign"],"b49d7f5e6341e66870473222edc5df0b")
}
Пример #18
0
func TestAuthMschapV2Step5(ot *testing.T) {
	//step 5
	in1 := []byte{
		0x01, 0x73, 0x00, 0xb2, 0x65, 0x4d, 0x3c, 0x73,
		0x87, 0x8c, 0xfa, 0x28, 0xb6, 0xfd, 0x87, 0x96,
		0xba, 0x96, 0xd2, 0xe7, 0x01, 0x12, 0x79, 0x64,
		0x76, 0x62, 0x49, 0x77, 0x30, 0x63, 0x41, 0x49,
		0x34, 0x37, 0x45, 0x4d, 0x51, 0x57, 0x3d, 0x06,
		0x00, 0x00, 0x00, 0x05, 0x06, 0x06, 0x00, 0x00,
		0x00, 0x02, 0x05, 0x06, 0x00, 0x00, 0x00, 0x07,
		0x57, 0x0f, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b,
		0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x04,
		0x06, 0x78, 0x19, 0xe5, 0xd6, 0x1e, 0x16, 0x31,
		0x32, 0x30, 0x2e, 0x32, 0x35, 0x2e, 0x32, 0x32,
		0x39, 0x2e, 0x32, 0x31, 0x34, 0x5b, 0x34, 0x35,
		0x30, 0x30, 0x5d, 0x1f, 0x17, 0x31, 0x37, 0x35,
		0x2e, 0x31, 0x35, 0x32, 0x2e, 0x31, 0x31, 0x37,
		0x2e, 0x32, 0x30, 0x34, 0x5b, 0x34, 0x35, 0x30,
		0x30, 0x5d, 0x4f, 0x08, 0x02, 0x03, 0x00, 0x06,
		0x1a, 0x03, 0x20, 0x0c, 0x73, 0x74, 0x72, 0x6f,
		0x6e, 0x67, 0x53, 0x77, 0x61, 0x6e, 0x18, 0x12,
		0xb6, 0x64, 0xc6, 0x5c, 0xb4, 0x67, 0xdc, 0x69,
		0x0a, 0x8b, 0x5c, 0xbf, 0xb8, 0xfe, 0x43, 0x79,
		0x50, 0x12, 0xce, 0xf2, 0xb2, 0x87, 0x4b, 0x88,
		0x2e, 0x0b, 0x44, 0xc9, 0x10, 0x6c, 0xca, 0xeb,
		0x74, 0xd3}
	server := server{
		handler: Handler{
			Auth: func(username string) (password string, exist bool) {
				if username != "ydvbIw0cAI47EMQW" {
					panic(`username!="ydvbIw0cAI47EMQW"`)
				}
				return "FogWi6Iz8oOkF1If", true
			},
		},
		mschapMap: map[string]mschapStatus{
			string([]byte{0xb6, 0x64, 0xc6, 0x5c, 0xb4, 0x67, 0xdc, 0x69, 0xa, 0x8b, 0x5c, 0xbf, 0xb8, 0xfe, 0x43, 0x79}): mschapStatus{
				Challenge: [16]byte{0x78, 0xbe, 0x5a, 0xd5, 0xbd, 0x02, 0xce, 0xe8,
					0x11, 0xc4, 0x7b, 0x9a, 0x93, 0x0a, 0x7b, 0x0f},
				NTResponse: [24]byte{0xd3, 0xf8, 0x5f, 0xc7, 0xd4, 0x4e, 0x9f, 0x1b,
					0x92, 0x23, 0x87, 0xd2, 0x4e, 0x21, 0xb3, 0xd0, 0xa7, 0x3b, 0x27,
					0xcc, 0x29, 0x14, 0xd4, 0x5a},
			},
		},
	}
	inPac, err := DecodeRequestPacket([]byte("sEcReT"), in1)
	kmgTest.Ok(err == nil, err)
	outPac := server.PacketHandler(inPac)
	kmgTest.Equal(outPac.Code, CodeAccessAccept)
	kmgTest.Equal(outPac.GetEAPMessage().Header().Code, eap.CodeSuccess)
	kmgTest.Equal(outPac.GetVsa(VendorTypeMSMPPESendKey).(*MSMPPESendOrRecvKeyVSA).Key, []byte{0x34, 0x29, 0xe7, 0x78, 0xe5, 0xad, 0x12, 0x14, 0xbf, 0x82, 0x6f, 0x2e, 0x3d, 0xe7, 0x6a, 0x77})
	kmgTest.Equal(outPac.GetVsa(VendorTypeMSMPPERecvKey).(*MSMPPESendOrRecvKeyVSA).Key, []byte{0x3e, 0x24, 0x79, 0x82, 0xcb, 0x8, 0x1, 0xc7, 0x59, 0x6d, 0x2, 0x94, 0x83, 0xf3, 0x39, 0x1a})
	outB, err := outPac.Encode()
	kmgTest.Equal(err, nil)
	outPac, err = DecodeResponsePacket([]byte("sEcReT"), outB, inPac.Authenticator)
	kmgTest.Equal(err, nil)

	out1 := []byte{
		0x02, 0x73, 0x00, 0xaa, 0xd9, 0x05, 0xde, 0x06,
		0x87, 0xae, 0xa9, 0x95, 0x2a, 0x5f, 0x0a, 0x2c,
		0x59, 0x0a, 0xbe, 0x0b, 0x1a, 0x0c, 0x00, 0x00,
		0x01, 0x37, 0x07, 0x06, 0x00, 0x00, 0x00, 0x01,
		0x1a, 0x0c, 0x00, 0x00, 0x01, 0x37, 0x08, 0x06,
		0x00, 0x00, 0x00, 0x06, 0x1a, 0x2a, 0x00, 0x00,
		0x01, 0x37, 0x10, 0x24, 0x92, 0xc3, 0xf4, 0x53,
		0x23, 0x8a, 0x1b, 0x31, 0x84, 0x16, 0xc0, 0x67,
		0xe2, 0x77, 0x29, 0x1b, 0x03, 0x00, 0xf6, 0x9f,
		0x36, 0x9d, 0x65, 0x6e, 0xdb, 0xd6, 0xfe, 0xe6,
		0x43, 0x9b, 0xe9, 0x2c, 0x29, 0x46, 0x1a, 0x2a,
		0x00, 0x00, 0x01, 0x37, 0x11, 0x24, 0x9e, 0x2b,
		0xf1, 0xf0, 0x6e, 0xf0, 0x20, 0x55, 0x5d, 0x5a,
		0xef, 0x36, 0x00, 0x08, 0x58, 0xce, 0x98, 0x9a,
		0x50, 0x80, 0x1b, 0x4d, 0xd5, 0xea, 0x17, 0xb2,
		0x08, 0xe6, 0xed, 0x0a, 0x21, 0xbb, 0x61, 0x0a,
		0x4f, 0x06, 0x03, 0x03, 0x00, 0x04, 0x50, 0x12,
		0x32, 0xaa, 0x90, 0x86, 0x7b, 0x31, 0xb9, 0xc0,
		0x55, 0x43, 0x64, 0x28, 0xef, 0xe7, 0x1c, 0x25,
		0x01, 0x12, 0x79, 0x64, 0x76, 0x62, 0x49, 0x77,
		0x30, 0x63, 0x41, 0x49, 0x34, 0x37, 0x45, 0x4d,
		0x51, 0x57}
	outPac, err = DecodeResponsePacket([]byte("sEcReT"), out1, [16]byte{0x65, 0x4d, 0x3c, 0x73,
		0x87, 0x8c, 0xfa, 0x28, 0xb6, 0xfd, 0x87, 0x96,
		0xba, 0x96, 0xd2, 0xe7})
	kmgTest.Equal(err, nil)
	kmgTest.Equal(outPac.Code, CodeAccessAccept)
	kmgTest.Equal(outPac.GetEAPMessage().Header().Code, eap.CodeSuccess)
	kmgTest.Equal(outPac.GetVsa(VendorTypeMSMPPESendKey).(*MSMPPESendOrRecvKeyVSA).Key, []byte{0x34, 0x29, 0xe7, 0x78, 0xe5, 0xad, 0x12, 0x14, 0xbf, 0x82, 0x6f, 0x2e, 0x3d, 0xe7, 0x6a, 0x77})
	kmgTest.Equal(outPac.GetVsa(VendorTypeMSMPPERecvKey).(*MSMPPESendOrRecvKeyVSA).Key, []byte{0x3e, 0x24, 0x79, 0x82, 0xcb, 0x8, 0x1, 0xc7, 0x59, 0x6d, 0x2, 0x94, 0x83, 0xf3, 0x39, 0x1a})
}
Пример #19
0
func TestRedisKvdb(ot *testing.T) {
	MustFlushDbV2()
	err := Set("test_1", "abc")
	kmgTest.Equal(err, nil)
	v, err := Get("test_1")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(v, "abc")

	_, err = Get("test_2")
	kmgTest.Equal(err, ErrKeyNotExist)

	err = Del("test_1")
	kmgTest.Equal(err, nil)

	_, err = Get("test_2")
	kmgTest.Equal(err, ErrKeyNotExist)

	err = Insert("test_3", "abcd")
	kmgTest.Equal(err, nil)

	err = Insert("test_3", "abcde")
	kmgTest.Equal(err, ErrKeyExist)

	kmgTest.Equal(MustGet("test_3"), "abcd")

	err = Set("test_3", "abcdef")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(MustGet("test_3"), "abcdef")

	err = Update("test_3", "abcdef")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(MustGet("test_3"), "abcdef")

	err = Update("test_4", "abcdefg")
	kmgTest.Equal(err, ErrKeyNotExist)
	kmgTest.Equal(MustGet("test_3"), "abcdef")

	simpleData := []string{"1", "2"}
	err = InsertGob("test_4", simpleData)
	kmgTest.Equal(err, nil)

	err = SetGob("test_4", simpleData)
	kmgTest.Equal(err, nil)

	getSimpleData := []string{}
	err = GetGob("test_4", &getSimpleData)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(simpleData, getSimpleData)

	simpleData = []string{"1", "2", "3"}
	err = UpdateGob("test_4", simpleData)

	getSimpleData = []string{}
	err = GetGob("test_4", &getSimpleData)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(simpleData, getSimpleData)

	var getSimpleData1 []string
	err = GetGob("test_4", &getSimpleData1)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(simpleData, getSimpleData1)

	var getSimpleData2 *[]string
	err = GetGob("test_4", getSimpleData2)
	kmgTest.Ok(err != nil)

	err = GetGob("test_4", &getSimpleData2)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(simpleData, *getSimpleData2)

	keyList, err := Keys("test_*")
	kmgTest.Equal(err, nil)
	sort.Strings(keyList)
	kmgTest.Equal(keyList, []string{"test_3", "test_4"})

	exist, err := Exists("test_4")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(exist, true)

	exist, err = Exists("test_5")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(exist, false)

	MustInsert("test_5", "")
	v, err = Get("test_5")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(v, "")

	err = SetEx("test_6", time.Second, "abc")
	kmgTest.Equal(err, nil)

	kmgTest.Equal(MustGet("test_6"), "abc")

	time.Sleep(time.Second * 2)
	exist, err = Exists("test_6")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(exist, false)
}
Пример #20
0
func TestMustGetExchangeRate(ot *testing.T) {
	rate := MustGetExchangeRate("JPY", "CNY")
	kmgTest.Ok(rate > 0)
}
Пример #21
0
func TestExist(t *testing.T) {
	kmgTest.Ok(!Exist("absadfklsjdfa"))
	kmgTest.Ok(Exist("ls"))
	kmgTest.Ok(Exist("top"))
}
Пример #22
0
func TestStartWith(t *testing.T) {
	kmgTest.Ok(IsStartWith("abc", "a"))
	kmgTest.Ok(!IsStartWith("abc", "d"))
}
Пример #23
0
func TestType(ot *testing.T) {
	data := &T{}
	m, err := NewContext(data)
	kmgTest.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr", "String1"}, "B")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.String1, "B")

	m.SaveByPath(Path{"ptr", "Map1", "A"}, "1123")
	_, ok := data.Map1["A"]
	kmgTest.Equal(ok, true)
	kmgTest.Equal(data.Map1["A"], "1123")

	err = m.SaveByPath(Path{"ptr", "Map1", "A"}, "1124")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Map1["A"], "1124")

	err = m.DeleteByPath(Path{"ptr", "Map1", "A"})
	kmgTest.Equal(err, nil)
	_, ok = data.Map1["A"]
	kmgTest.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map2", "B", "ptr"}, "1")
	kmgTest.Equal(err, nil)
	rpString, ok := data.Map2["B"]
	kmgTest.Equal(ok, true)
	kmgTest.Equal(*rpString, "1")

	err = m.SaveByPath(Path{"ptr", "Map2", "B", "ptr"}, "2")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(*rpString, "2")

	err = m.DeleteByPath(Path{"ptr", "Map2", "B", "ptr"})
	kmgTest.Equal(err, nil)
	_, ok = data.Map2["B"]
	kmgTest.Equal(ok, true)
	kmgTest.Equal(data.Map2["B"], nil)

	err = m.DeleteByPath(Path{"ptr", "Map2", "B"})
	kmgTest.Equal(err, nil)
	_, ok = data.Map2["B"]
	kmgTest.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map3", "C", "A"}, "1")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Map3["C"].A, "1")

	err = m.DeleteByPath(Path{"ptr", "Map3", "C"})
	kmgTest.Equal(err, nil)
	kmgTest.Ok(data.Map3 != nil)
	_, ok = data.Map3["C"]
	kmgTest.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "F"}, "1234")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Map4["D"]["F"], "1234")

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "H"}, "12345")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Map4["D"]["H"], "12345")

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "H"}, "12346")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Map4["D"]["H"], "12346")

	err = m.DeleteByPath(Path{"ptr", "Map4", "D", "F"})
	kmgTest.Equal(err, nil)
	kmgTest.Ok(data.Map4["D"] != nil)
	_, ok = data.Map4["D"]["F"]
	kmgTest.Equal(ok, false)

	_, ok = data.Map4["D"]["H"]
	kmgTest.Equal(ok, true)

	err = m.SaveByPath(Path{"ptr", "Map5", "D", ""}, "1234")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(data.Map5["D"]), 1)
	kmgTest.Equal(data.Map5["D"][0], "1234")

	err = m.DeleteByPath(Path{"ptr", "Map5", "D", "0"})
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(data.Map5["D"]), 0)

	err = m.SaveByPath(Path{"ptr", "Slice1", ""}, "1234")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(data.Slice1), 1)
	kmgTest.Equal(data.Slice1[0], "1234")

	err = m.SaveByPath(Path{"ptr", "Slice1", ""}, "12345")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Slice1[1], "12345")
	kmgTest.Equal(len(data.Slice1), 2)

	err = m.DeleteByPath(Path{"ptr", "Slice1", "0"})
	kmgTest.Equal(err, nil)
	kmgTest.Equal(len(data.Slice1), 1)
	kmgTest.Equal(data.Slice1[0], "12345")

	err = m.SaveByPath(Path{"ptr", "Ptr1", "ptr"}, "12345")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(*data.Ptr1, "12345")

	err = m.SaveByPath(Path{"ptr", "Ptr2", "ptr"}, "")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Ptr2.A, "")

	err = m.SaveByPath(Path{"ptr", "Array1", "1"}, "12345")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(data.Array1[1], "12345")
}
Пример #24
0
func TestAuthMschapV2Step3(ot *testing.T) {
	//step 3
	in1 := []byte{
		0x01, 0x81, 0x00, 0xf7, 0xf9, 0x19, 0xba, 0x5f,
		0xff, 0xcb, 0xfb, 0x8b, 0x71, 0x90, 0x46, 0x82,
		0x2e, 0xd0, 0x94, 0x76, 0x01, 0x12, 0x79, 0x64,
		0x76, 0x62, 0x49, 0x77, 0x30, 0x63, 0x41, 0x49,
		0x34, 0x37, 0x45, 0x4d, 0x51, 0x57, 0x3d, 0x06,
		0x00, 0x00, 0x00, 0x05, 0x06, 0x06, 0x00, 0x00,
		0x00, 0x02, 0x05, 0x06, 0x00, 0x00, 0x00, 0x07,
		0x57, 0x0f, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b,
		0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x04,
		0x06, 0x78, 0x19, 0xe5, 0xd6, 0x1e, 0x16, 0x31,
		0x32, 0x30, 0x2e, 0x32, 0x35, 0x2e, 0x32, 0x32,
		0x39, 0x2e, 0x32, 0x31, 0x34, 0x5b, 0x34, 0x35,
		0x30, 0x30, 0x5d, 0x1f, 0x17, 0x31, 0x37, 0x35,
		0x2e, 0x31, 0x35, 0x32, 0x2e, 0x31, 0x31, 0x37,
		0x2e, 0x32, 0x30, 0x34, 0x5b, 0x34, 0x35, 0x30,
		0x30, 0x5d, 0x4f, 0x4d, 0x02, 0x02, 0x00, 0x4b,
		0x1a, 0x02, 0x02, 0x00, 0x46, 0x31, 0xdd, 0x67,
		0x8b, 0x50, 0x83, 0x39, 0xee, 0x8c, 0x3c, 0x50,
		0xb8, 0x05, 0x82, 0x9e, 0xdd, 0x8b, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd3, 0xf8,
		0x5f, 0xc7, 0xd4, 0x4e, 0x9f, 0x1b, 0x92, 0x23,
		0x87, 0xd2, 0x4e, 0x21, 0xb3, 0xd0, 0xa7, 0x3b,
		0x27, 0xcc, 0x29, 0x14, 0xd4, 0x5a, 0x00, 0x79,
		0x64, 0x76, 0x62, 0x49, 0x77, 0x30, 0x63, 0x41,
		0x49, 0x34, 0x37, 0x45, 0x4d, 0x51, 0x57, 0x20,
		0x0c, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53,
		0x77, 0x61, 0x6e, 0x18, 0x12, 0xb6, 0x64, 0xc6,
		0x5c, 0xb7, 0x66, 0xdc, 0x69, 0x0a, 0x8b, 0x5c,
		0xbf, 0xb8, 0xfe, 0x43, 0x79, 0x50, 0x12, 0x6a,
		0x2b, 0x7e, 0xbb, 0xb8, 0xab, 0xcd, 0xe7, 0x23,
		0x02, 0x73, 0x45, 0xe6, 0xc2, 0xa2, 0xf7}
	server := server{
		handler: Handler{
			Auth: func(username string) (password string, exist bool) {
				if username != "ydvbIw0cAI47EMQW" {
					panic(`username!="ydvbIw0cAI47EMQW"`)
				}
				return "FogWi6Iz8oOkF1If", true
			},
		},
		mschapMap: map[string]mschapStatus{
			string([]byte{0xb6, 0x64, 0xc6,
				0x5c, 0xb7, 0x66, 0xdc, 0x69, 0x0a, 0x8b, 0x5c,
				0xbf, 0xb8, 0xfe, 0x43, 0x79}): mschapStatus{
				Challenge: [16]byte{0x78, 0xbe, 0x5a, 0xd5, 0xbd, 0x02, 0xce, 0xe8,
					0x11, 0xc4, 0x7b, 0x9a, 0x93, 0x0a, 0x7b, 0x0f},
			},
		},
	}
	inPac, err := DecodeRequestPacket([]byte("sEcReT"), in1)
	kmgTest.Equal(inPac.GetEAPMessage().(*eap.MSCHAPV2Packet).MSCHAPV2.(*MSCHAPV2.ResponsePacket).NTResponse,
		[24]byte{0xd3, 0xf8, 0x5f, 0xc7, 0xd4, 0x4e, 0x9f, 0x1b, 0x92, 0x23, 0x87, 0xd2, 0x4e, 0x21, 0xb3, 0xd0, 0xa7, 0x3b, 0x27, 0xcc, 0x29, 0x14, 0xd4, 0x5a})
	kmgTest.Ok(err == nil, err)
	outPac := server.PacketHandler(inPac)
	fmt.Println(outPac)
	kmgTest.Equal(outPac.GetEAPMessage().(*eap.MSCHAPV2Packet).MSCHAPV2.OpCode(), MSCHAPV2.OpCodeSuccess)
	kmgTest.Equal(kmgHex.UpperEncodeBytesToString(outPac.GetEAPMessage().(*eap.MSCHAPV2Packet).MSCHAPV2.(*MSCHAPV2.SuccessPacket).Auth[:]), "689DE06F16B7AFFB04E8EDCC8C2DE8CCBA78E0A9")
	kmgTest.Equal(outPac.Code, CodeAccessChallenge)
}
Пример #25
0
func EncryptTester(encrypt func(key *[32]byte, data []byte) (output []byte),
	decrypt func(key *[32]byte, data []byte) (output []byte, err error),
	maxOverhead int) {
	key := &[32]byte{0xd8, 0x51, 0xea, 0x81, 0xb9, 0xe, 0xf, 0x2f, 0x8c, 0x85, 0x5f, 0xb6, 0x14, 0xb2}
	//加密数据,可以正确解密测试
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
		bytes.Repeat([]byte("1234567890"), 100),
	} {
		ob := encrypt(key, origin)
		ret, err := decrypt(key, ob)
		kmgTest.Equal(err, nil, origin)
		kmgTest.Equal(ret, origin)
	}
	//任意数据传入解密不会挂掉,并且会报错
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		_, err := decrypt(key, origin)
		kmgTest.Ok(err != nil)
	}
	origin := []byte("1234567890123456712345678901234567")
	//多次加密得到不同的结果 (随机化iv)
	kmgTest.Ok(!bytes.Equal(encrypt(key, origin), encrypt(key, origin)))
	//修改任何一个字节都会报错 (hash)

	ob := encrypt(key, origin)
	for i := 0; i < len(ob); i++ {
		newOb := make([]byte, len(ob))
		newOb[i] = -newOb[i]
		_, err := decrypt(key, newOb)
		kmgTest.Ok(err != nil)
	}

	for _, i := range []int{1, 10, 100, 1000, 10000} {
		ob := encrypt(key, kmgRand.MustCryptoRandBytes(i))
		kmgTest.Ok(len(ob)-i <= maxOverhead, i)
	}
}