• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codenotary / immudb / 6783902372

07 Nov 2023 11:34AM UTC coverage: 89.548% (-0.02%) from 89.571%
6783902372

push

gh-ci

jeroiraz
test(pkg/pgsql): unit testing for deallocate stmt

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

33645 of 37572 relevant lines covered (89.55%)

146027.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.71
/pkg/pgsql/server/initialize_session.go
1
/*
2
Copyright 2022 Codenotary Inc. All rights reserved.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
        http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package server
18

19
import (
20
        "bufio"
21
        "bytes"
22
        "context"
23
        "crypto/tls"
24
        "encoding/binary"
25
        "errors"
26
        "fmt"
27

28
        "github.com/codenotary/immudb/pkg/client"
29
        "github.com/codenotary/immudb/pkg/database"
30
        pserr "github.com/codenotary/immudb/pkg/pgsql/errors"
31
        bm "github.com/codenotary/immudb/pkg/pgsql/server/bmessages"
32
        fm "github.com/codenotary/immudb/pkg/pgsql/server/fmessages"
33
        "github.com/codenotary/immudb/pkg/pgsql/server/pgmeta"
34
        "google.golang.org/grpc"
35
        "google.golang.org/grpc/credentials"
36
        "google.golang.org/grpc/credentials/insecure"
37
        "google.golang.org/grpc/metadata"
38
)
39

40
// InitializeSession
41
func (s *session) InitializeSession() (err error) {
26✔
42
        defer func() {
52✔
43
                if err != nil {
27✔
44
                        s.HandleError(err)
1✔
45
                        s.mr.CloseConnection()
1✔
46
                }
1✔
47
        }()
48

49
        lb := make([]byte, 4)
26✔
50
        if _, err := s.mr.Read(lb); err != nil {
26✔
51
                return err
×
52
        }
×
53
        pvb := make([]byte, 4)
26✔
54
        if _, err := s.mr.Read(pvb); err != nil {
26✔
55
                return err
×
56
        }
×
57

58
        s.protocolVersion = parseProtocolVersion(pvb)
26✔
59

26✔
60
        // SSL Request packet
26✔
61
        if s.protocolVersion == "1234.5679" {
28✔
62
                if s.tlsConfig == nil || len(s.tlsConfig.Certificates) == 0 {
3✔
63
                        if _, err = s.writeMessage([]byte(`N`)); err != nil {
1✔
64
                                return err
×
65
                        }
×
66
                        return pserr.ErrSSLNotSupported
1✔
67
                }
68

69
                if _, err = s.writeMessage([]byte(`S`)); err != nil {
1✔
70
                        return err
×
71
                }
×
72

73
                if err = s.handshake(); err != nil {
1✔
74
                        return err
×
75
                }
×
76

77
                lb = make([]byte, 4)
1✔
78
                if _, err := s.mr.Read(lb); err != nil {
1✔
79
                        return err
×
80
                }
×
81
                pvb = make([]byte, 4)
1✔
82
                if _, err := s.mr.Read(pvb); err != nil {
1✔
83
                        return err
×
84
                }
×
85

86
                s.protocolVersion = parseProtocolVersion(pvb)
1✔
87
        }
88

89
        // startup message
90
        connStringLenght := int(binary.BigEndian.Uint32(lb) - 4)
25✔
91
        connString := make([]byte, connStringLenght)
25✔
92

25✔
93
        if _, err := s.mr.Read(connString); err != nil {
25✔
94
                return err
×
95
        }
×
96

97
        pr := bufio.NewScanner(bytes.NewBuffer(connString))
25✔
98

25✔
99
        split := func(data []byte, atEOF bool) (int, []byte, error) {
418✔
100
                if atEOF && len(data) == 0 {
443✔
101
                        return 0, nil, nil
50✔
102
                }
50✔
103
                if i := bytes.IndexByte(data, 0); i >= 0 {
686✔
104
                        return i + 1, data[0:i], nil
343✔
105
                }
343✔
106
                if atEOF {
×
107
                        return len(data), data, nil
×
108
                }
×
109
                return 0, nil, nil
×
110
        }
111

112
        pr.Split(split)
25✔
113

25✔
114
        pmap := make(map[string]string)
25✔
115

25✔
116
        for pr.Scan() {
209✔
117
                key := pr.Text()
184✔
118
                for pr.Scan() {
343✔
119
                        value := pr.Text()
159✔
120
                        if value != "" {
268✔
121
                                pmap[key] = value
109✔
122
                        }
109✔
123
                        break
159✔
124
                }
125
        }
126

127
        s.connParams = pmap
25✔
128

25✔
129
        return nil
25✔
130
}
131

132
// HandleStartup errors are returned and handled in the caller
133
func (s *session) HandleStartup(ctx context.Context) (err error) {
25✔
134
        defer func() {
50✔
135
                if err != nil {
30✔
136
                        s.HandleError(err)
5✔
137
                        s.mr.CloseConnection()
5✔
138
                }
5✔
139
        }()
140

141
        user, ok := s.connParams["user"]
25✔
142
        if !ok || user == "" {
25✔
143
                return pserr.ErrUsernameNotprovided
×
144
        }
×
145

146
        db, ok := s.connParams["database"]
25✔
147
        if !ok {
26✔
148
                return pserr.ErrDBNotprovided
1✔
149
        }
1✔
150

151
        s.db, err = s.dbList.GetByName(db)
24✔
152
        if err != nil {
25✔
153
                if errors.Is(err, database.ErrDatabaseNotExists) {
2✔
154
                        return pserr.ErrDBNotExists
1✔
155
                }
1✔
156
                return err
×
157
        }
158

159
        if _, err = s.writeMessage(bm.AuthenticationCleartextPassword()); err != nil {
23✔
160
                return err
×
161
        }
×
162

163
        msg, _, err := s.nextMessage()
23✔
164
        if err != nil {
23✔
165
                return err
×
166
        }
×
167

168
        pw, ok := msg.(fm.PasswordMsg)
23✔
169
        if !ok || pw.GetSecret() == "" {
25✔
170
                return pserr.ErrPwNotprovided
2✔
171
        }
2✔
172

173
        var transportCredentials credentials.TransportCredentials
21✔
174

21✔
175
        if s.tlsConfig == nil || s.tlsConfig.RootCAs == nil {
41✔
176
                transportCredentials = insecure.NewCredentials()
20✔
177
        } else {
21✔
178
                config := &tls.Config{
1✔
179
                        RootCAs: s.tlsConfig.RootCAs,
1✔
180
                }
1✔
181

1✔
182
                transportCredentials = credentials.NewTLS(config)
1✔
183
        }
1✔
184

185
        opts := client.DefaultOptions().
21✔
186
                WithAddress(s.immudbHost).
21✔
187
                WithPort(s.immudbPort).
21✔
188
                WithDisableIdentityCheck(true).
21✔
189
                WithDialOptions([]grpc.DialOption{grpc.WithTransportCredentials(transportCredentials)})
21✔
190

21✔
191
        s.client = client.NewClient().WithOptions(opts)
21✔
192

21✔
193
        err = s.client.OpenSession(ctx, []byte(user), []byte(pw.GetSecret()), db)
21✔
194
        if err != nil {
22✔
195
                return err
1✔
196
        }
1✔
197

198
        s.client.CurrentState(context.Background())
20✔
199

20✔
200
        sessionID := s.client.GetSessionID()
20✔
201
        s.ctx = metadata.NewIncomingContext(context.Background(), metadata.Pairs("sessionid", sessionID))
20✔
202

20✔
203
        s.log.Debugf("authentication successful for %s", user)
20✔
204
        if _, err := s.writeMessage(bm.AuthenticationOk()); err != nil {
20✔
205
                return err
×
206
        }
×
207

208
        if _, err := s.writeMessage(bm.ParameterStatus([]byte("standard_conforming_strings"), []byte("on"))); err != nil {
20✔
209
                return err
×
210
        }
×
211

212
        if _, err := s.writeMessage(bm.ParameterStatus([]byte("client_encoding"), []byte("UTF8"))); err != nil {
20✔
213
                return err
×
214
        }
×
215

216
        // todo this is needed by jdbc driver. Here is added the minor supported version at the moment
217
        if _, err := s.writeMessage(bm.ParameterStatus([]byte("server_version"), []byte(pgmeta.PgsqlProtocolVersion))); err != nil {
20✔
218
                return err
×
219
        }
×
220

221
        return nil
20✔
222
}
223

224
func parseProtocolVersion(payload []byte) string {
27✔
225
        major := int(binary.BigEndian.Uint16(payload[0:2]))
27✔
226
        minor := int(binary.BigEndian.Uint16(payload[2:4]))
27✔
227
        return fmt.Sprintf("%d.%d", major, minor)
27✔
228
}
27✔
229

230
func (s *session) Close() error {
10✔
231
        s.mr.CloseConnection()
10✔
232

10✔
233
        if s.client != nil {
15✔
234
                return s.client.CloseSession(s.ctx)
5✔
235
        }
5✔
236

237
        return nil
5✔
238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc