-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcompliance_test.go
191 lines (150 loc) · 8.92 KB
/
compliance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2020 DSR Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rest_test
import (
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/common"
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/auth"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/compliance"
)
//nolint:funlen,lll
func TestComplianceDemo_KeepTrackCompliance(t *testing.T) {
// Register new Vendor account
vendor := utils.CreateNewAccount(auth.AccountRoles{auth.Vendor}, testconstants.VID)
// Register new TestHouse account
testHouse := utils.CreateNewAccount(auth.AccountRoles{auth.TestHouse}, 0)
// Register new CertificationCenter account
zigbee := utils.CreateNewAccount(auth.AccountRoles{auth.CertificationCenter}, 0)
// Get all compliance infos
inputComplianceInfos, _ := utils.GetComplianceInfos()
// Get all certified models
inputCertifiedModels, _ := utils.GetAllCertifiedModels()
// Get all revoked models
inputRevokedModels, _ := utils.GetAllRevokedModels()
// Publish model
model := utils.NewMsgAddModel(vendor.Address, testconstants.VID)
_, _ = utils.AddModel(model, vendor)
// Publish modelVersion
modelVersion := utils.NewMsgAddModelVersion(model.VID, model.PID,
testconstants.SoftwareVersion, testconstants.SoftwareVersionString, vendor.Address)
_, _ = utils.AddModelVersion(modelVersion, vendor)
// Check if model either certified or revoked before Compliance record was created
modelIsCertified, _ := utils.GetCertifiedModel(model.VID, model.PID, modelVersion.SoftwareVersion, compliance.ZigbeeCertificationType)
require.False(t, modelIsCertified.Value)
modelIsRevoked, _ := utils.GetRevokedModel(model.VID, model.PID, modelVersion.SoftwareVersion, compliance.ZigbeeCertificationType)
require.False(t, modelIsRevoked.Value)
// Publish testing result
testingResult := utils.NewMsgAddTestingResult(model.VID, model.PID, modelVersion.SoftwareVersion, modelVersion.SoftwareVersionString, testHouse.Address)
_, _ = utils.PublishTestingResult(testingResult, testHouse)
// Certify model
certifyModelMsg := compliance.NewMsgCertifyModel(model.VID, model.PID, modelVersion.SoftwareVersion, modelVersion.SoftwareVersionString, time.Now().UTC(),
compliance.CertificationType(testconstants.CertificationType), testconstants.EmptyString, false, zigbee.Address)
_, _ = utils.PublishCertifiedModel(certifyModelMsg, zigbee)
// Check model is certified
modelIsCertified, _ = utils.GetCertifiedModel(model.VID, model.PID, modelVersion.SoftwareVersion, certifyModelMsg.CertificationType)
require.True(t, modelIsCertified.Value)
// Register other CertificationCenter account
secondZb := utils.CreateNewAccount(auth.AccountRoles{auth.CertificationCenter}, 0)
// Certify model by other CertificationCenter account
secondCertifyModelMsg := compliance.NewMsgCertifyModel(model.VID, model.PID, modelVersion.SoftwareVersion, modelVersion.SoftwareVersionString, time.Now().UTC(),
compliance.CertificationType(testconstants.CertificationType), testconstants.EmptyString, false, secondZb.Address)
secondCertifyResult, _ := utils.PublishCertifiedModel(secondCertifyModelMsg, secondZb)
require.Equal(t, compliance.CodeAlreadyCertifyed, sdk.CodeType(secondCertifyResult.Code))
modelIsRevoked, _ = utils.GetRevokedModel(model.VID, model.PID, modelVersion.SoftwareVersion, certifyModelMsg.CertificationType)
require.False(t, modelIsRevoked.Value)
// Get all certified models
certifiedModels, _ := utils.GetAllCertifiedModels()
require.Equal(t, utils.ParseUint(inputCertifiedModels.Total)+1, utils.ParseUint(certifiedModels.Total))
// Revoke model certification
revocationTime := certifyModelMsg.CertificationDate.AddDate(0, 0, 1)
revokeModelMsg := compliance.NewMsgRevokeModel(model.VID, model.PID, modelVersion.SoftwareVersion, revocationTime,
compliance.CertificationType(testconstants.CertificationType), testconstants.RevocationReason, zigbee.Address)
_, _ = utils.PublishRevokedModel(revokeModelMsg, zigbee)
// Check model is revoked
modelIsCertified, _ = utils.GetCertifiedModel(model.VID, model.PID, modelVersion.SoftwareVersion, revokeModelMsg.CertificationType)
require.False(t, modelIsCertified.Value)
modelIsRevoked, _ = utils.GetRevokedModel(model.VID, model.PID, modelVersion.SoftwareVersion, revokeModelMsg.CertificationType)
require.True(t, modelIsRevoked.Value)
// Get all revoked models
revokedModels, _ := utils.GetAllRevokedModels()
require.Equal(t, utils.ParseUint(inputRevokedModels.Total)+1, utils.ParseUint(revokedModels.Total))
// Get all certified models
certifiedModels, _ = utils.GetAllCertifiedModels()
require.Equal(t, utils.ParseUint(inputCertifiedModels.Total), utils.ParseUint(certifiedModels.Total))
// Get all compliance infos
complianceInfos, _ := utils.GetComplianceInfos()
require.Equal(t, utils.ParseUint(inputComplianceInfos.Total)+1, utils.ParseUint(complianceInfos.Total))
// Get compliance info
complianceInfo, _ := utils.GetComplianceInfo(model.VID, model.PID, modelVersion.SoftwareVersion, certifyModelMsg.CertificationType)
require.Equal(t, complianceInfo.SoftwareVersionCertificationStatus, compliance.CodeRevoked)
require.Equal(t, 1, len(complianceInfo.History))
require.Equal(t, complianceInfo.History[0].SoftwareVersionCertificationStatus, compliance.CodeCertified)
}
//nolint:funlen
func TestComplianceDemo_KeepTrackRevocation(t *testing.T) {
// Register new account Vendor, CertificationCenter
// Publish model info
// Get all certified and revoked models
_, zigbee, model, modelVersion, inputCertifiedModels, inputRevokedModels := utils.InitStartData()
vid, pid := model.VID, model.PID
sv, svs := modelVersion.SoftwareVersion, modelVersion.SoftwareVersionString
// Revoke non-existent model
revocationTime := time.Now().UTC()
revokeModelMsg := compliance.NewMsgRevokeModel(common.RandUint16(),
common.RandUint16(), common.RandUint32(), revocationTime,
compliance.CertificationType(testconstants.CertificationType), testconstants.RevocationReason, zigbee.Address)
_, _ = utils.PublishRevokedModel(revokeModelMsg, zigbee)
// Check non-existent model is revoked
modelIsRevoked, _ := utils.GetRevokedModel(revokeModelMsg.VID,
revokeModelMsg.PID, revokeModelMsg.SoftwareVersion, revokeModelMsg.CertificationType)
require.False(t, modelIsRevoked.Value)
// Revoke model
revocationTime = time.Now().UTC()
revokeModelMsg = compliance.NewMsgRevokeModel(vid, pid, sv, revocationTime,
compliance.CertificationType(testconstants.CertificationType), testconstants.RevocationReason, zigbee.Address)
_, _ = utils.PublishRevokedModel(revokeModelMsg, zigbee)
// Check model is revoked
modelIsRevoked, _ = utils.GetRevokedModel(revokeModelMsg.VID,
revokeModelMsg.PID, revokeModelMsg.SoftwareVersion, revokeModelMsg.CertificationType)
require.True(t, modelIsRevoked.Value)
modelIsCertified, _ := utils.GetCertifiedModel(revokeModelMsg.VID,
revokeModelMsg.PID, revokeModelMsg.SoftwareVersion, revokeModelMsg.CertificationType)
require.False(t, modelIsCertified.Value)
// Get all revoked models
revokedModels, _ := utils.GetAllRevokedModels()
require.Equal(t, utils.ParseUint(inputRevokedModels.Total)+1, utils.ParseUint(revokedModels.Total))
// Certify model
certificationTime := revocationTime.AddDate(0, 0, 1)
certifyModelMsg := compliance.NewMsgCertifyModel(vid, pid, sv, svs, certificationTime,
compliance.CertificationType(testconstants.CertificationType), testconstants.EmptyString, false, zigbee.Address)
_, _ = utils.PublishCertifiedModel(certifyModelMsg, zigbee)
// Check model is certified
modelIsRevoked, _ = utils.GetRevokedModel(certifyModelMsg.VID,
certifyModelMsg.PID, certifyModelMsg.SoftwareVersion, certifyModelMsg.CertificationType)
require.False(t, modelIsRevoked.Value)
modelIsCertified, _ = utils.GetCertifiedModel(certifyModelMsg.VID,
certifyModelMsg.PID, certifyModelMsg.SoftwareVersion, certifyModelMsg.CertificationType)
require.True(t, modelIsCertified.Value)
// Get all certified models
certifiedModels, _ := utils.GetAllCertifiedModels()
require.Equal(t, utils.ParseUint(inputCertifiedModels.Total)+1, utils.ParseUint(certifiedModels.Total))
// Get all revoked models
revokedModels, _ = utils.GetAllRevokedModels()
require.Equal(t, utils.ParseUint(inputRevokedModels.Total), utils.ParseUint(revokedModels.Total))
}