11package ocmf_go
22
33import (
4+ "crypto/ecdsa"
5+ "crypto/elliptic"
6+ "crypto/rand"
47 "testing"
58
69 "github.com/stretchr/testify/suite"
@@ -32,7 +35,10 @@ func (s *builderTestSuite) TestNewBuilder() {
3235}
3336
3437func (s * builderTestSuite ) TestBuilder_Valid () {
35- privateKey := "" // todo
38+ curve := elliptic .P256 ()
39+ privateKey , err := ecdsa .GenerateKey (curve , rand .Reader )
40+ s .Require ().NoError (err )
41+
3642 builder := NewBuilder (privateKey ).
3743 WithPagination ("1" ).
3844 WithMeterSerial ("exampleSerial123" ).
@@ -61,7 +67,7 @@ func (s *builderTestSuite) TestBuilder_Valid() {
6167}
6268
6369func (s * builderTestSuite ) TestBuilder_MissingAttributes () {
64- builder := NewBuilder ("privateKey" ).
70+ builder := NewBuilder (nil ).
6571 // WithPagination("1").
6672 WithMeterSerial ("exampleSerial123" ).
6773 WithIdentificationStatus (true ).
@@ -79,7 +85,7 @@ func (s *builderTestSuite) TestBuilder_MissingAttributes() {
7985}
8086
8187func (s * builderTestSuite ) TestBuilder_CantSign () {
82- builder := NewBuilder ("privateKey" ).
88+ builder := NewBuilder (nil ).
8389 WithPagination ("1" ).
8490 WithMeterSerial ("exampleSerial123" ).
8591 WithIdentificationStatus (true ).
@@ -91,110 +97,11 @@ func (s *builderTestSuite) TestBuilder_CantSign() {
9197 Status : string (MeterOk ),
9298 })
9399
94- builder .privateKey = ""
95-
96100 payload , err := builder .Build ()
97101 s .Error (err )
98102 s .Nil (payload )
99103}
100104
101- func (s * builderTestSuite ) TestWithSignatureAlgorithm () {
102- tests := []struct {
103- name string
104- algorithm SignatureAlgorithm
105- want bool
106- }{
107- {
108- name : "ECDSA-secp192k1-SHA256" ,
109- algorithm : SignatureAlgorithmECDSAsecp192k1SHA256 ,
110- want : true ,
111- },
112- {
113- name : "ECDSA-secp256k1-SHA256" ,
114- algorithm : SignatureAlgorithmECDSAsecp256k1SHA256 ,
115- want : true ,
116- },
117- {
118- name : "ECDSA-secp384r1-SHA256" ,
119- algorithm : SignatureAlgorithmECDSAsecp384r1SHA256 ,
120- want : true ,
121- },
122- {
123- name : "ECDSA-brainpool256r1-SHA256" ,
124- algorithm : SignatureAlgorithmECDSAbrainpool256r11SHA256 ,
125- want : true ,
126- },
127- {
128- name : "ECDSA-secp256r1-SHA256" ,
129- algorithm : SignatureAlgorithmECDSAsecp256r1SHA256 ,
130- want : true ,
131- },
132- {
133- name : "ECDSA-secp192r1-SHA256" ,
134- algorithm : SignatureAlgorithmECDSAsecp192r1SHA256 ,
135- want : true ,
136- },
137- {
138- name : "Unknown algorithm" ,
139- algorithm : SignatureAlgorithm ("ABCD" ),
140- want : false ,
141- },
142- {
143- name : "Empty algorithm" ,
144- algorithm : SignatureAlgorithm ("" ),
145- want : false ,
146- },
147- }
148-
149- for _ , tt := range tests {
150- s .T ().Run (tt .name , func (t * testing.T ) {
151- builder := NewBuilder ("privateKey" , WithSignatureAlgorithm (tt .algorithm ))
152-
153- if tt .name == "Unknown algorithm" || tt .name == "Empty algorithm" {
154- s .NotEqual (tt .algorithm , builder .signature .Algorithm )
155- } else {
156- s .Equal (tt .algorithm , builder .signature .Algorithm )
157- }
158- })
159- }
160- }
161-
162- func (s * builderTestSuite ) TestWithWithSignatureEncoding () {
163- tests := []struct {
164- name string
165- encoding SignatureEncoding
166- }{
167- {
168- name : "Base64 encoding" ,
169- encoding : SignatureEncodingBase64 ,
170- },
171- {
172- name : "Hex encoding" ,
173- encoding : SignatureEncodingHex ,
174- },
175- {
176- name : "Empty encoding" ,
177- encoding : SignatureEncoding ("" ),
178- },
179- {
180- name : "Unknown encoding" ,
181- encoding : SignatureEncoding ("ABDD" ),
182- },
183- }
184-
185- for _ , tt := range tests {
186- s .T ().Run (tt .name , func (t * testing.T ) {
187- builder := NewBuilder ("privateKey" , WithSignatureEncoding (tt .encoding ))
188-
189- if tt .encoding == SignatureEncodingBase64 || tt .encoding == SignatureEncodingHex {
190- s .Equal (tt .encoding , builder .signature .Encoding )
191- } else {
192- s .NotEqual (tt .encoding , builder .signature .Encoding )
193- }
194- })
195- }
196- }
197-
198105func TestBuilder (t * testing.T ) {
199106 suite .Run (t , new (builderTestSuite ))
200107}
0 commit comments