1// Copyright 2016-2020 The Libsacloud Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package sacloud
16
17import (
18	"encoding/json"
19	"testing"
20	"time"
21
22	"github.com/stretchr/testify/assert"
23)
24
25var testProxyLBJSON = `
26
27{
28    "ID": 123456789012,
29    "Name": "example",
30    "Description": "example",
31    "Settings": {
32      "ProxyLB": {
33        "HealthCheck": {
34          "Protocol": "http",
35          "Path": "/",
36          "Host": "example.com",
37          "DelayLoop": 10
38        },
39        "SorryServer": {
40          "IPAddress": "192.2.0.1",
41          "Port": 80
42        },
43        "BindPorts": [
44          {
45            "ProxyMode": "https",
46            "Port": 443
47          }
48        ],
49        "Servers": [
50          {
51            "IPAddress": "192.2.0.11",
52            "Port": 443,
53            "Enabled": true
54          },
55          {
56            "IPAddress": "192.2.0.12",
57            "Port": 443,
58            "Enabled": true
59          }
60        ]
61      }
62    },
63    "Status": {
64      "FQDN": "site-xxxxxxx.proxylbN.sakura.ne.jp",
65      "ProxyNetworks": [
66        "192.51.0.0/28"
67      ],
68      "UseVIPFailover": true
69    },
70    "ServiceClass": "cloud/proxylb/plain/1000",
71    "Availability": "available",
72    "CreatedAt": "2019-02-28T11:15:59+09:00",
73    "ModifiedAt": "2019-02-28T11:15:59+09:00",
74    "Provider": {
75      "ID": 9100001,
76      "Class": "proxylb",
77      "Name": "proxylb1",
78      "ServiceClass": "cloud/proxylb"
79    },
80    "Tags": [
81      "tag1",
82      "tag2"
83    ]
84  }
85`
86
87var testProxyLBCertificatesJSON = `
88	{
89		"ServerCertificate": "dummy1",
90		"IntermediateCertificate": "dummy2",
91    	"PrivateKey": "dummy3",
92    	"CertificateEndDate": "May  4 01:37:47 2019 GMT",
93		"CertificateCommonName": ""
94	}
95`
96
97func TestMarshalProxyLBJSON(t *testing.T) {
98	var proxyLB ProxyLB
99	err := json.Unmarshal([]byte(testProxyLBJSON), &proxyLB)
100
101	assert.NoError(t, err)
102	assert.NotEmpty(t, proxyLB)
103
104	assert.NotEmpty(t, proxyLB.ID)
105	assert.NotEmpty(t, proxyLB.Status.FQDN)
106	assert.NotEmpty(t, proxyLB.Status.ProxyNetworks)
107	assert.True(t, proxyLB.Status.UseVIPFailover)
108	assert.NotEmpty(t, proxyLB.Provider.Class)
109}
110
111func TestMarshalProxyLBCertificate(t *testing.T) {
112	var certs ProxyLBCertificate
113	err := json.Unmarshal([]byte(testProxyLBCertificatesJSON), &certs)
114
115	assert.NoError(t, err)
116	assert.NotEmpty(t, certs)
117
118	assert.Equal(t, "dummy1", certs.ServerCertificate)
119	assert.Equal(t, "dummy2", certs.IntermediateCertificate)
120	assert.Equal(t, "dummy3", certs.PrivateKey)
121	loc, _ := time.LoadLocation("GMT")
122	assert.Equal(t, time.Date(2019, 5, 4, 1, 37, 47, 0, loc).Unix(), certs.CertificateEndDate.Unix())
123}
124
125func TestMarshalProxyLBCertificates(t *testing.T) {
126
127	t.Run("AdditionalCerts is empty", func(t *testing.T) {
128		data := `{
129			"AdditionalCerts": "",
130			"PrimaryCert": {
131				"CertificateCommonName": "",
132				"CertificateEndDate": "",
133				"IntermediateCertificate": "",
134				"PrivateKey": "",
135				"ServerCertificate": ""
136			}
137		}`
138
139		res := &ProxyLBCertificates{}
140		err := json.Unmarshal([]byte(data), res)
141		assert.NoError(t, err)
142	})
143
144	t.Run("AdditionalCerts is array", func(t *testing.T) {
145		data := `{
146			"AdditionalCerts": [
147				{
148					"CertificateCommonName": "bbb",
149					"CertificateEndDate": "",
150					"IntermediateCertificate": "",
151					"PrivateKey": "",
152					"ServerCertificate": ""
153				},
154				{
155					"CertificateCommonName": "ccc",
156					"CertificateEndDate": "",
157					"IntermediateCertificate": "",
158					"PrivateKey": "",
159					"ServerCertificate": ""
160				}
161			],
162			"PrimaryCert": {
163				"CertificateCommonName": "aaa",
164				"CertificateEndDate": "",
165				"IntermediateCertificate": "",
166				"PrivateKey": "",
167				"ServerCertificate": ""
168			}
169		}`
170
171		var res ProxyLBCertificates
172		err := json.Unmarshal([]byte(data), &res)
173		assert.NoError(t, err)
174		assert.NotNil(t, res)
175		assert.Equal(t, "aaa", res.PrimaryCert.CertificateCommonName)
176		assert.NotNil(t, res.AdditionalCerts)
177		assert.Len(t, res.AdditionalCerts, 2)
178	})
179}
180