Kaynağa Gözat

change secure test

master
kidjung 4 yıl önce
ebeveyn
işleme
399036454e

BIN
.DS_Store Dosyayı Görüntüle


+ 4
- 0
.idea/vcs.xml Dosyayı Görüntüle

@@ -2,5 +2,9 @@
2 2
 <project version="4">
3 3
   <component name="VcsDirectoryMappings">
4 4
     <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+    <mapping directory="$PROJECT_DIR$/src/github.com/davecgh/go-spew" vcs="Git" />
6
+    <mapping directory="$PROJECT_DIR$/src/github.com/pmezard/go-difflib" vcs="Git" />
7
+    <mapping directory="$PROJECT_DIR$/src/github.com/stretchr/testify" vcs="Git" />
8
+    <mapping directory="$PROJECT_DIR$/src/gopkg.in/yaml.v3" vcs="Git" />
5 9
   </component>
6 10
 </project>

+ 0
- 3
src/broker/modules/go.mod Dosyayı Görüntüle

@@ -1,3 +0,0 @@
1
-module modules
2
-
3
-go 1.16

+ 20
- 20
src/broker/modules/matching.go Dosyayı Görüntüle

@@ -1,21 +1,21 @@
1 1
 package modules
2
-
3
-import (
4
-	_ "errors"
5
-	_ "fmt"
6
-)
7
-
8
-type match_manager struct {
9
-}
10
-
11
-func (match_mng *match_manager) matching(queue *MsgQueue) error {
12
-	msg := queue.pop(true)
13
-
14
-	//Implement here ~~
15
-	subscription := msg.(*PublishMsg).subscription
16
-	content := msg.(*PublishMsg).content
17
-
18
-	println(subscription, content)
19
-
20
-	return nil
21
-}
2
+//
3
+//import (
4
+//	_ "errors"
5
+//	_ "fmt"
6
+//)
7
+//
8
+//type match_manager struct {
9
+//}
10
+//
11
+//func (match_mng *match_manager) matching(queue *MsgQueue) error {
12
+//	msg := queue.pop(true)
13
+//
14
+//	//Implement here ~~
15
+//	subscription := msg.(*PublishMsg).subscription
16
+//	content := msg.(*PublishMsg).content
17
+//
18
+//	println(subscription, content)
19
+//
20
+//	return nil
21
+//}

+ 36
- 36
src/broker/modules/matching_test.go Dosyayı Görüntüle

@@ -1,37 +1,37 @@
1 1
 package modules
2
-
3
-import (
4
-	"fmt"
5
-	"testing"
6
-)
7
-
8
-func Test_simpleLList_pbNtraverse(t *testing.T) {
9
-	mylist := new(LList)
10
-	mylist.PushBack(30)
11
-	mylist.PushBack(40)
12
-	mylist.PushBack(20)
13
-	mylist.PushBack(10)
14
-	ptr := mylist.head
15
-	for ptr != nil {
16
-		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
17
-		ptr = ptr.next
18
-	}
19
-}
20
-
21
-func Test_simpleLList_mergeSort(t *testing.T) {
22
-	mylist := new(LList)
23
-	mylist.PushBack(30)
24
-	mylist.PushBack(40)
25
-	mylist.PushBack(20)
26
-	mylist.PushBack(10)
27
-	mylist.head = MergeSort(mylist.head, mylist.size / 2)
28
-	ptr := mylist.head
29
-	for ptr != nil {
30
-		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
31
-		ptr = ptr.next
32
-	}
33
-}
34
-
35
-func Test_removeArray(t *testing.T){}
36
-func Test_deleteList(t *testing.T){}
37
-func Test_insertList(t *testing.T){}
2
+//
3
+//import (
4
+//	"fmt"
5
+//	"testing"
6
+//)
7
+//
8
+//func Test_simpleLList_pbNtraverse(t *testing.T) {
9
+//	mylist := new(LList)
10
+//	mylist.PushBack(30)
11
+//	mylist.PushBack(40)
12
+//	mylist.PushBack(20)
13
+//	mylist.PushBack(10)
14
+//	ptr := mylist.head
15
+//	for ptr != nil {
16
+//		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
17
+//		ptr = ptr.next
18
+//	}
19
+//}
20
+//
21
+//func Test_simpleLList_mergeSort(t *testing.T) {
22
+//	mylist := new(LList)
23
+//	mylist.PushBack(30)
24
+//	mylist.PushBack(40)
25
+//	mylist.PushBack(20)
26
+//	mylist.PushBack(10)
27
+//	mylist.head = MergeSort(mylist.head, mylist.size / 2)
28
+//	ptr := mylist.head
29
+//	for ptr != nil {
30
+//		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
31
+//		ptr = ptr.next
32
+//	}
33
+//}
34
+//
35
+//func Test_removeArray(t *testing.T){}
36
+//func Test_deleteList(t *testing.T){}
37
+//func Test_insertList(t *testing.T){}

+ 1
- 1
src/broker/modules/secure.go Dosyayı Görüntüle

@@ -40,7 +40,7 @@ func (sc Security) GetNodeKey(nodeName string) int64 {
40 40
 	messageStringKey := sc.KeyMap[nodeName]
41 41
 	mKey, err := strconv.ParseInt(messageStringKey, 10, 64)
42 42
 	if err != nil {
43
-		fmt.Println("reEncrypt Error: key string to int64 parsing error.")
43
+		fmt.Println("GetNodeKey Error: key string to int64 parsing error.")
44 44
 	}
45 45
 	return mKey
46 46
 }

+ 54
- 17
src/broker/modules/secure_test.go Dosyayı Görüntüle

@@ -26,13 +26,13 @@ func CreatePubMsg(msg Message, topic string, value string, content string) *Publ
26 26
 	toPubMsg.Message = msg
27 27
 
28 28
 	intArr := []rune(topic)
29
-	fmt.Print("topic length ")
30
-	fmt.Println(len(intArr))
31
-	fmt.Println(len(toPubMsg.topic))
29
+	//fmt.Print("topic length ")
30
+	//fmt.Println(len(intArr))
31
+	//fmt.Println(len(toPubMsg.topic))
32 32
 	for index := 0; index < len(intArr); index++ {
33 33
 		toPubMsg.topic = append(toPubMsg.topic, int64(intArr[index]))
34 34
 	}
35
-	fmt.Println(len(toPubMsg.topic))
35
+	//fmt.Println(len(toPubMsg.topic))
36 36
 	intArr = []rune(value)
37 37
 	for index := 0; index < len(intArr); index++ {
38 38
 		toPubMsg.value = append(toPubMsg.value, int64(intArr[index]))
@@ -45,7 +45,7 @@ func CreatePubMsg(msg Message, topic string, value string, content string) *Publ
45 45
 	return toPubMsg
46 46
 }
47 47
 
48
-func EncryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) PublishMsg {
48
+func EncryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) *PublishMsg {
49 49
 	for index := range msg.topic {
50 50
 		msg.topic[index] = msg.topic[index] + gyKey
51 51
 	}
@@ -56,7 +56,8 @@ func EncryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) PublishMsg {
56 56
 		msg.content[index] = msg.content[index] + gyKey + privateKey
57 57
 	}
58 58
 
59
-	return *msg
59
+
60
+	return msg
60 61
 }
61 62
 
62 63
 func DecryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) {
@@ -74,7 +75,7 @@ func DecryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) {
74 75
 	for index := range msg.topic {
75 76
 		runeArr = append(runeArr, rune(int(msg.topic[index])))
76 77
 	}
77
-	fmt.Println("\n\ntopic is: " + string(runeArr))
78
+	fmt.Println("topic is: " + string(runeArr))
78 79
 	runeArr = nil
79 80
 
80 81
 	for index := range msg.value {
@@ -91,22 +92,58 @@ func DecryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) {
91 92
 
92 93
 }
93 94
 
95
+func printMsg(msg *PublishMsg){
96
+	var runeArr []rune
97
+	for index := range msg.topic {
98
+		runeArr = append(runeArr, rune(int(msg.topic[index])))
99
+	}
100
+	fmt.Println("topic is: " + string(runeArr))
101
+	runeArr = nil
102
+
103
+	for index := range msg.value {
104
+		runeArr = append(runeArr, rune(int(msg.value[index])))
105
+	}
106
+	fmt.Println("value is: " + string(runeArr))
107
+	runeArr = nil
108
+
109
+	for index := range msg.content {
110
+		runeArr = append(runeArr, rune(int(msg.content[index])))
111
+	}
112
+	fmt.Println("content is: " + string(runeArr) + "\n\n")
113
+	runeArr = nil
114
+}
115
+
94 116
 // from "1.1.1.1" to "3.3.3.3" node
95 117
 func TestReEnc(t *testing.T) {
96 118
 	var security = NewSecurity()
97 119
 	var sm SecurityManager
98 120
 	sm = security
99
-	security.KeyMap["1.1.1.1"] = "5678"
100
-	security.KeyMap["3.3.3.3"] = "9999"
121
+	security.KeyMap["1.1.1.1"] = "56789"
122
+	security.KeyMap["3.3.3.3"] = "99999"
101 123
 
102
-	fmt.Println(sm.GetNodeKey("3.3.3.3"))
103
-	msg := Message{from: "1.1.1.1", version: "1", time: "123", kind: 1}
104
-	fmt.Println(msg)
105
-	publishMsg := CreatePubMsg(msg, "soccer123한글", "playerList", "son and 10 players")
124
+	//fmt.Println(sm.GetNodeKey("3.3.3.3"))
125
+	msg := Message{from: "1.1.1.1", version: "1", time: "123", kind: 3}
126
+	//fmt.Println(msg)
127
+	publishMsg := CreatePubMsg(msg, "soccer123한글", "playerList", "Son and 10 players")
106 128
 	fmt.Println(publishMsg)
107
-	encPublishMsg := EncryptionMsg(publishMsg, 1234, 5678)
129
+	fmt.Println("original publish message is...")
130
+	printMsg(publishMsg)
131
+	encPublishMsg := EncryptionMsg(publishMsg, 1234, 56789)
132
+	fmt.Println("encrypt publish message by publisher's private key")
133
+	printMsg(encPublishMsg)
108 134
 	fmt.Println(encPublishMsg)
109
-	reEncPublishMsg := sm.ReEncPubMsg(encPublishMsg, "3.3.3.3")
110
-	fmt.Println(reEncPublishMsg)
111
-	DecryptionMsg(reEncPublishMsg, 1234, 9999)
135
+	reEncPublishMsg := sm.ReEncPubMsg(*encPublishMsg, "3.3.3.3")
136
+	fmt.Println("re-encrypt publish message by subscriber's private key")
137
+	printMsg(reEncPublishMsg)
138
+	//fmt.Println(reEncPublishMsg)
139
+	fmt.Println("decrypted publish message is...")
140
+	DecryptionMsg(reEncPublishMsg, 1234, 99999)
112 141
 }
142
+
143
+//func TestReEncUsingSHA(r *testing.T){
144
+//	var security = NewSecurity()
145
+//	var sm SecurityManager
146
+//	sm = security
147
+//	security.KeyMap["1.1.1.1"] = "56789"
148
+//	security.KeyMap["3.3.3.3"] = "99999"
149
+//}

+ 144
- 144
src/broker/modules/subscription_test.go Dosyayı Görüntüle

@@ -1,145 +1,145 @@
1 1
 package modules
2
-
3
-import (
4
-	"fmt"
5
-	_ "fmt"
6
-	"math/rand"
7
-	"strconv"
8
-	"testing"
9
-	"time"
10
-	"github.com/stretchr/testify/assert"
11
-)
12
-
13
-func dataMake(isalpha bool) MsgUnit{
14
-	rand.Seed(time.Now().UnixNano())
15
-
16
-	// Set Ipaddr
17
-	msg := Message{"", "1.0", "", SM}
18
-	for i := 0; i < 4; i++{
19
-		itoa := strconv.Itoa(rand.Int() % 256)
20
-
21
-		msg.from += itoa
22
-		if i != 3{
23
-			msg.from += "."
24
-		}
25
-	}
26
-
27
-	// Set Time
28
-	msg.time += strconv.Itoa(rand.Int()%24) + ":"
29
-	msg.time += strconv.Itoa(rand.Int()%60)
30
-
31
-	// Set Topic, Value, operator
32
-	topic := []int64{}
33
-	value := []int64{}
34
-	operator := []string{}
35
-	candOp := []string{">", ">=", "<=" ,"<", "=="}
36
-	logicalOp := []string{"&&", "||"}
37
-
38
-	topic = append(topic, rand.Int63())
39
-
40
-	if isalpha{
41
-		value = append(value, rand.Int63())
42
-		operator = append(operator, "==")
43
-	} else{
44
-		len := rand.Int() % 2 + 1
45
-		value = append(value, rand.Int63())
46
-
47
-		if len == 1{
48
-			operator = append(operator, candOp[rand.Int()%5])
49
-		} else{
50
-			value = append(value, rand.Int63())
51
-			op := rand.Int()%2
52
-			operator = append(operator, candOp[rand.Int()%2])
53
-			if op == 0 {
54
-				operator = append(operator, logicalOp[0])
55
-			} else{
56
-				operator = append(operator, logicalOp[1])
57
-			}
58
-			operator = append(operator, candOp[rand.Int()%2 + 2])
59
-		}
60
-	}
61
-
62
-	return &SubscriptionMsg{msg, topic, value, operator, isalpha}
63
-}
64
-
65
-func Test_addSubscription(t *testing.T) {
66
-	rand.Seed(time.Now().UnixNano())
67
-
68
-	// To Init sub_mng
69
-	mos := Moscato{sub_mng : sub_manager{}}
70
-
71
-	// Make Data set(Subscription)
72
-	msgList := []MsgUnit{}
73
-	dataLen:= 3
74
-	for i := 0 ; i < dataLen ; i++{
75
-		seed := rand.Int()%2
76
-		if seed == 0{
77
-			msgList = append(msgList, dataMake(false))
78
-		} else{
79
-			msgList = append(msgList, dataMake(true))
80
-		}
81
-	}
82
-
83
-	// Watch Data set
84
-	for i := 0; i < dataLen ; i++{
85
-		msg := msgList[i]
86
-		fmt.Println(
87
-			" from = ", msg.(*SubscriptionMsg).Message.from,
88
-			" time = ", msg.(*SubscriptionMsg).Message.time,
89
-			" topic = ", msg.(*SubscriptionMsg).topic,
90
-			" value = ", msg.(*SubscriptionMsg).value,
91
-			" operator = ", msg.(*SubscriptionMsg).operator,
92
-			" isAlpha ?= ", msg.(*SubscriptionMsg).isAlpha)
93
-	}
94
-
95
-	// Test addSubScription
96
-	for i := 0; i < dataLen ; i++{
97
-
98
-		msg := msgList[i]
99
-		ip := msg.(*SubscriptionMsg).Message.from
100
-		topic := msg.(*SubscriptionMsg).topic
101
-		value := msg.(*SubscriptionMsg).value
102
-		//operator := msg.(*SubscriptionMsg).operator
103
-		//isAlpha := msg.(*SubscriptionMsg).isAlpha
104
-		subnumber := mos.sub_mng.count_sub
105
-
106
-		mos.sub_mng.addSubscription(msg)
107
-
108
-
109
-		// 1. Check if ip mapping is correct
110
-		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip]) - 1], "Ip mapping is failed")
111
-
112
-		// 2. Check topicNode
113
-		nameptr := mos.sub_mng.list.head
114
-		for nameptr != nil{
115
-			if nameptr.topic[0] == topic[0]{
116
-				break
117
-			}
118
-			nameptr = nameptr.next
119
-		}
120
-
121
-		for i := 0 ; i < len(topic); i++{
122
-			assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
123
-		}
124
-
125
-		// 3. Check Value in ValueNode
126
-		valptr := nameptr.list.head
127
-		for valptr != nil{
128
-			for i:=0 ; i < len(value); i++{
129
-				find := true
130
-				if value[i] != valptr.val[i]{
131
-					find = false
132
-				}
133
-				if find{
134
-					break
135
-				}
136
-			}
137
-			valptr = valptr.next
138
-		}
139
-		fmt.Println("my value = ", valptr.val, " real value = ", value)
140
-
141
-		for i := 0; i < len(value); i++{
142
-			assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
143
-		}
144
-	}
145
-}
2
+//
3
+//import (
4
+//	"fmt"
5
+//	_ "fmt"
6
+//	"math/rand"
7
+//	"strconv"
8
+//	"testing"
9
+//	"time"
10
+//	"github.com/stretchr/testify/assert"
11
+//)
12
+//
13
+//func dataMake(isalpha bool) MsgUnit{
14
+//	rand.Seed(time.Now().UnixNano())
15
+//
16
+//	// Set Ipaddr
17
+//	msg := Message{"", "1.0", "", SM}
18
+//	for i := 0; i < 4; i++{
19
+//		itoa := strconv.Itoa(rand.Int() % 256)
20
+//
21
+//		msg.from += itoa
22
+//		if i != 3{
23
+//			msg.from += "."
24
+//		}
25
+//	}
26
+//
27
+//	// Set Time
28
+//	msg.time += strconv.Itoa(rand.Int()%24) + ":"
29
+//	msg.time += strconv.Itoa(rand.Int()%60)
30
+//
31
+//	// Set Topic, Value, operator
32
+//	topic := []int64{}
33
+//	value := []int64{}
34
+//	operator := []string{}
35
+//	candOp := []string{">", ">=", "<=" ,"<", "=="}
36
+//	logicalOp := []string{"&&", "||"}
37
+//
38
+//	topic = append(topic, rand.Int63())
39
+//
40
+//	if isalpha{
41
+//		value = append(value, rand.Int63())
42
+//		operator = append(operator, "==")
43
+//	} else{
44
+//		len := rand.Int() % 2 + 1
45
+//		value = append(value, rand.Int63())
46
+//
47
+//		if len == 1{
48
+//			operator = append(operator, candOp[rand.Int()%5])
49
+//		} else{
50
+//			value = append(value, rand.Int63())
51
+//			op := rand.Int()%2
52
+//			operator = append(operator, candOp[rand.Int()%2])
53
+//			if op == 0 {
54
+//				operator = append(operator, logicalOp[0])
55
+//			} else{
56
+//				operator = append(operator, logicalOp[1])
57
+//			}
58
+//			operator = append(operator, candOp[rand.Int()%2 + 2])
59
+//		}
60
+//	}
61
+//
62
+//	return &SubscriptionMsg{msg, topic, value, operator, isalpha}
63
+//}
64
+//
65
+//func Test_addSubscription(t *testing.T) {
66
+//	rand.Seed(time.Now().UnixNano())
67
+//
68
+//	// To Init sub_mng
69
+//	mos := Moscato{sub_mng : sub_manager{}}
70
+//
71
+//	// Make Data set(Subscription)
72
+//	msgList := []MsgUnit{}
73
+//	dataLen:= 3
74
+//	for i := 0 ; i < dataLen ; i++{
75
+//		seed := rand.Int()%2
76
+//		if seed == 0{
77
+//			msgList = append(msgList, dataMake(false))
78
+//		} else{
79
+//			msgList = append(msgList, dataMake(true))
80
+//		}
81
+//	}
82
+//
83
+//	// Watch Data set
84
+//	for i := 0; i < dataLen ; i++{
85
+//		msg := msgList[i]
86
+//		fmt.Println(
87
+//			" from = ", msg.(*SubscriptionMsg).Message.from,
88
+//			" time = ", msg.(*SubscriptionMsg).Message.time,
89
+//			" topic = ", msg.(*SubscriptionMsg).topic,
90
+//			" value = ", msg.(*SubscriptionMsg).value,
91
+//			" operator = ", msg.(*SubscriptionMsg).operator,
92
+//			" isAlpha ?= ", msg.(*SubscriptionMsg).isAlpha)
93
+//	}
94
+//
95
+//	// Test addSubScription
96
+//	for i := 0; i < dataLen ; i++{
97
+//
98
+//		msg := msgList[i]
99
+//		ip := msg.(*SubscriptionMsg).Message.from
100
+//		topic := msg.(*SubscriptionMsg).topic
101
+//		value := msg.(*SubscriptionMsg).value
102
+//		//operator := msg.(*SubscriptionMsg).operator
103
+//		//isAlpha := msg.(*SubscriptionMsg).isAlpha
104
+//		subnumber := mos.sub_mng.count_sub
105
+//
106
+//		mos.sub_mng.addSubscription(msg)
107
+//
108
+//
109
+//		// 1. Check if ip mapping is correct
110
+//		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip]) - 1], "Ip mapping is failed")
111
+//
112
+//		// 2. Check topicNode
113
+//		nameptr := mos.sub_mng.list.head
114
+//		for nameptr != nil{
115
+//			if nameptr.topic[0] == topic[0]{
116
+//				break
117
+//			}
118
+//			nameptr = nameptr.next
119
+//		}
120
+//
121
+//		for i := 0 ; i < len(topic); i++{
122
+//			assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
123
+//		}
124
+//
125
+//		// 3. Check Value in ValueNode
126
+//		valptr := nameptr.list.head
127
+//		for valptr != nil{
128
+//			for i:=0 ; i < len(value); i++{
129
+//				find := true
130
+//				if value[i] != valptr.val[i]{
131
+//					find = false
132
+//				}
133
+//				if find{
134
+//					break
135
+//				}
136
+//			}
137
+//			valptr = valptr.next
138
+//		}
139
+//		fmt.Println("my value = ", valptr.val, " real value = ", value)
140
+//
141
+//		for i := 0; i < len(value); i++{
142
+//			assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
143
+//		}
144
+//	}
145
+//}

Loading…
İptal
Kaydet